blob: b6fd37d188e46916b884d1d9cb22862a35993931 [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,
648 gl_renderer->alpha_attribs,
649 NULL) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500650 goto cleanup_window;
651
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500652 return 0;
653
654cleanup_window:
655 wl_egl_window_destroy(output->gl.egl_window);
656 return -1;
657}
658
659static int
660wayland_output_init_pixman_renderer(struct wayland_output *output)
661{
662 return pixman_renderer_output_create(&output->base);
663}
664
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600665static void
666wayland_output_resize_surface(struct wayland_output *output)
667{
668 struct wayland_compositor *c =
669 (struct wayland_compositor *)output->base.compositor;
670 struct wayland_shm_buffer *buffer, *next;
671 int32_t ix, iy, iwidth, iheight;
672 int32_t width, height;
673 struct wl_region *region;
674
675 width = output->base.current_mode->width;
676 height = output->base.current_mode->height;
677
678 if (output->frame) {
679 frame_resize_inside(output->frame, width, height);
680
681 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
682 region = wl_compositor_create_region(c->parent.compositor);
683 wl_region_add(region, ix, iy, iwidth, iheight);
684 wl_surface_set_input_region(output->parent.surface, region);
685 wl_region_destroy(region);
686
687 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
688 region = wl_compositor_create_region(c->parent.compositor);
689 wl_region_add(region, ix, iy, iwidth, iheight);
690 wl_surface_set_opaque_region(output->parent.surface, region);
691 wl_region_destroy(region);
692
693 width = frame_width(output->frame);
694 height = frame_height(output->frame);
695 } else {
696 region = wl_compositor_create_region(c->parent.compositor);
697 wl_region_add(region, 0, 0, width, height);
698 wl_surface_set_input_region(output->parent.surface, region);
699 wl_region_destroy(region);
700
701 region = wl_compositor_create_region(c->parent.compositor);
702 wl_region_add(region, 0, 0, width, height);
703 wl_surface_set_opaque_region(output->parent.surface, region);
704 wl_region_destroy(region);
705 }
706
707 if (output->gl.egl_window) {
708 wl_egl_window_resize(output->gl.egl_window,
709 width, height, 0, 0);
710
711 /* These will need to be re-created due to the resize */
712 gl_renderer->output_set_border(&output->base,
713 GL_RENDERER_BORDER_TOP,
714 0, 0, 0, NULL);
715 cairo_surface_destroy(output->gl.border.top);
716 output->gl.border.top = NULL;
717 gl_renderer->output_set_border(&output->base,
718 GL_RENDERER_BORDER_LEFT,
719 0, 0, 0, NULL);
720 cairo_surface_destroy(output->gl.border.left);
721 output->gl.border.left = NULL;
722 gl_renderer->output_set_border(&output->base,
723 GL_RENDERER_BORDER_RIGHT,
724 0, 0, 0, NULL);
725 cairo_surface_destroy(output->gl.border.right);
726 output->gl.border.right = NULL;
727 gl_renderer->output_set_border(&output->base,
728 GL_RENDERER_BORDER_BOTTOM,
729 0, 0, 0, NULL);
730 cairo_surface_destroy(output->gl.border.bottom);
731 output->gl.border.bottom = NULL;
732 }
733
734 /* Throw away any remaining SHM buffers */
735 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, link)
736 wayland_shm_buffer_destroy(buffer);
737 /* These will get thrown away when they get released */
738 wl_list_for_each(buffer, &output->shm.buffers, link)
739 buffer->output = NULL;
740}
741
742static int
743wayland_output_set_windowed(struct wayland_output *output)
744{
745 struct wayland_compositor *c =
746 (struct wayland_compositor *)output->base.compositor;
747 int tlen;
748 char *title;
749
750 if (output->frame)
751 return 0;
752
753 if (output->name) {
754 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
755 title = malloc(tlen + 1);
756 if (!title)
757 return -1;
758
759 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
760 } else {
761 title = strdup(WINDOW_TITLE);
762 }
763
764 if (!c->theme) {
765 c->theme = theme_create();
766 if (!c->theme) {
767 free(title);
768 return -1;
769 }
770 }
771 output->frame = frame_create(c->theme, 100, 100,
772 FRAME_BUTTON_CLOSE, title);
773 free(title);
774 if (!output->frame)
775 return -1;
776
777 if (output->keyboard_count)
778 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
779
780 wayland_output_resize_surface(output);
781
782 wl_shell_surface_set_toplevel(output->parent.shell_surface);
783
784 return 0;
785}
786
787static void
788wayland_output_set_fullscreen(struct wayland_output *output,
789 enum wl_shell_surface_fullscreen_method method,
790 uint32_t framerate, struct wl_output *target)
791{
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500792 struct wayland_compositor *c =
793 (struct wayland_compositor *)output->base.compositor;
794
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600795 if (output->frame) {
796 frame_destroy(output->frame);
797 output->frame = NULL;
798 }
799
800 wayland_output_resize_surface(output);
801
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500802 if (output->parent.shell_surface) {
803 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
804 method, framerate, target);
805 } else if (c->parent.fshell) {
806 _wl_fullscreen_shell_present_surface(c->parent.fshell,
807 output->parent.surface,
808 method, target);
809 }
810}
811
812static struct weston_mode *
813wayland_output_choose_mode(struct wayland_output *output,
814 struct weston_mode *ref_mode)
815{
816 struct weston_mode *mode;
817
818 /* First look for an exact match */
819 wl_list_for_each(mode, &output->base.mode_list, link)
820 if (mode->width == ref_mode->width &&
821 mode->height == ref_mode->height &&
822 mode->refresh == ref_mode->refresh)
823 return mode;
824
825 /* If we can't find an exact match, ignore refresh and try again */
826 wl_list_for_each(mode, &output->base.mode_list, link)
827 if (mode->width == ref_mode->width &&
828 mode->height == ref_mode->height)
829 return mode;
830
831 /* Yeah, we failed */
832 return NULL;
833}
834
835enum mode_status {
836 MODE_STATUS_UNKNOWN,
837 MODE_STATUS_SUCCESS,
838 MODE_STATUS_FAIL,
839 MODE_STATUS_CANCEL,
840};
841
842static void
843mode_feedback_successful(void *data,
844 struct _wl_fullscreen_shell_mode_feedback *fb)
845{
846 enum mode_status *value = data;
847
848 printf("Mode switch successful\n");
849
850 *value = MODE_STATUS_SUCCESS;
851}
852
853static void
854mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
855{
856 enum mode_status *value = data;
857
858 printf("Mode switch failed\n");
859
860 *value = MODE_STATUS_FAIL;
861}
862
863static void
864mode_feedback_cancelled(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
865{
866 enum mode_status *value = data;
867
868 printf("Mode switch cancelled\n");
869
870 *value = MODE_STATUS_CANCEL;
871}
872
873struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
874 mode_feedback_successful,
875 mode_feedback_failed,
876 mode_feedback_cancelled,
877};
878
879static int
880wayland_output_switch_mode(struct weston_output *output_base,
881 struct weston_mode *mode)
882{
883 struct wayland_output *output = (struct wayland_output *) output_base;
884 struct wayland_compositor *c;
885 struct wl_surface *old_surface;
886 struct weston_mode *old_mode;
887 struct _wl_fullscreen_shell_mode_feedback *mode_feedback;
888 enum mode_status mode_status;
889 int ret = 0;
890
891 if (output_base == NULL) {
892 weston_log("output is NULL.\n");
893 return -1;
894 }
895
896 if (mode == NULL) {
897 weston_log("mode is NULL.\n");
898 return -1;
899 }
900
901 c = (struct wayland_compositor *)output_base->compositor;
902
903 if (output->parent.shell_surface || !c->parent.fshell)
904 return -1;
905
906 mode = wayland_output_choose_mode(output, mode);
907 if (mode == NULL)
908 return -1;
909
910 if (output->base.current_mode == mode)
911 return 0;
912
913 old_mode = output->base.current_mode;
914 old_surface = output->parent.surface;
915 output->base.current_mode = mode;
916 output->parent.surface =
917 wl_compositor_create_surface(c->parent.compositor);
918 wl_surface_set_user_data(output->parent.surface, output);
919
920 /* Blow the old buffers because we changed size/surfaces */
921 wayland_output_resize_surface(output);
922
923 mode_feedback =
924 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
925 output->parent.surface,
926 output->parent.output,
927 mode->refresh);
928 _wl_fullscreen_shell_mode_feedback_add_listener(mode_feedback,
929 &mode_feedback_listener,
930 &mode_status);
931
932 /* This should kick-start things again */
933 output->parent.draw_initial_frame = 1;
934 wayland_output_start_repaint_loop(&output->base);
935
936 mode_status = MODE_STATUS_UNKNOWN;
937 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
938 ret = wl_display_dispatch(c->parent.wl_display);
939
940 _wl_fullscreen_shell_mode_feedback_destroy(mode_feedback);
941
942 if (mode_status == MODE_STATUS_FAIL) {
943 output->base.current_mode = old_mode;
944 wl_surface_destroy(output->parent.surface);
945 output->parent.surface = old_surface;
946 wayland_output_resize_surface(output);
947
948 return -1;
949 }
950
951 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
952 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
953
954 if (c->use_pixman) {
955 pixman_renderer_output_destroy(output_base);
956 if (wayland_output_init_pixman_renderer(output) < 0)
957 goto err_output;
958 } else {
959 gl_renderer->output_destroy(output_base);
960 wl_egl_window_destroy(output->gl.egl_window);
961 if (wayland_output_init_gl_renderer(output) < 0)
962 goto err_output;
963 }
964 wl_surface_destroy(old_surface);
965
966 weston_output_schedule_repaint(&output->base);
967
968 return 0;
969
970err_output:
971 /* XXX */
972 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600973}
974
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500975static struct wayland_output *
976wayland_output_create(struct wayland_compositor *c, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600977 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500978 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100979{
980 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500981 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600982
983 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
984 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100985
Peter Huttererf3d62272013-08-08 11:57:05 +1000986 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100987 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500988 return NULL;
989
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600990 output->name = name ? strdup(name) : NULL;
991 output->base.make = "waywayland";
992 output->base.model = "none";
993
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500994 output_width = width * scale;
995 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100996
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600997 output->parent.surface =
998 wl_compositor_create_surface(c->parent.compositor);
999 if (!output->parent.surface)
1000 goto err_name;
1001 wl_surface_set_user_data(output->parent.surface, output);
1002
1003 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001004
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001005 if (c->parent.shell) {
1006 output->parent.shell_surface =
1007 wl_shell_get_shell_surface(c->parent.shell,
1008 output->parent.surface);
1009 if (!output->parent.shell_surface)
1010 goto err_surface;
1011 wl_shell_surface_add_listener(output->parent.shell_surface,
1012 &shell_surface_listener, output);
1013 }
1014
1015 if (fullscreen && c->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001016 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1017 0, 0, NULL);
1018 wl_display_roundtrip(c->parent.wl_display);
1019 if (!width)
1020 output_width = output->parent.configure_width;
1021 if (!height)
1022 output_height = output->parent.configure_height;
1023 }
1024
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001025 output->mode.flags =
1026 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001027 output->mode.width = output_width;
1028 output->mode.height = output_height;
1029 output->mode.refresh = 60000;
1030 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001031 wl_list_init(&output->base.mode_list);
1032 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001033 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001034
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001035 wl_list_init(&output->shm.buffers);
1036 wl_list_init(&output->shm.free_buffers);
1037
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001038 weston_output_init(&output->base, &c->base, x, y, width, height,
1039 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001040
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001041 if (c->use_pixman) {
1042 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001043 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001044 output->base.repaint = wayland_output_repaint_pixman;
1045 } else {
1046 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001047 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001048 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001049 }
1050
Jonas Ådahle5a12252013-04-05 23:07:11 +02001051 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001052 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001053 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001054 output->base.set_backlight = NULL;
1055 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001056 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001057
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001058 wl_list_insert(c->base.output_list.prev, &output->base.link);
1059
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001060 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001061
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001062err_output:
1063 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001064 if (output->parent.shell_surface)
1065 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001066err_surface:
1067 wl_surface_destroy(output->parent.surface);
1068err_name:
1069 free(output->name);
1070
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001071 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001072 free(output);
1073
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001074 return NULL;
1075}
1076
1077static struct wayland_output *
1078wayland_output_create_for_config(struct wayland_compositor *c,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001079 struct weston_config_section *config_section,
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001080 int option_width, int option_height,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001081 int option_scale, int32_t x, int32_t y)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001082{
1083 struct wayland_output *output;
1084 char *mode, *t, *name, *str;
1085 int width, height, scale;
1086 uint32_t transform;
Derek Foreman64a3df02014-10-23 12:24:18 -05001087 unsigned int slen;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001088
1089 weston_config_section_get_string(config_section, "name", &name, NULL);
1090 if (name) {
1091 slen = strlen(name);
1092 slen += strlen(WINDOW_TITLE " - ");
1093 str = malloc(slen + 1);
1094 if (str)
1095 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
1096 free(name);
1097 name = str;
1098 }
1099 if (!name)
U. Artie Eoff1a08d112014-01-17 12:22:50 -08001100 name = strdup(WINDOW_TITLE);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001101
1102 weston_config_section_get_string(config_section,
1103 "mode", &mode, "1024x600");
1104 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1105 weston_log("Invalid mode \"%s\" for output %s\n",
1106 mode, name);
1107 width = 1024;
1108 height = 640;
1109 }
1110 free(mode);
1111
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001112 if (option_width)
1113 width = option_width;
1114 if (option_height)
1115 height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001116
1117 weston_config_section_get_int(config_section, "scale", &scale, 1);
1118
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001119 if (option_scale)
1120 scale = option_scale;
1121
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001122 weston_config_section_get_string(config_section,
1123 "transform", &t, "normal");
Derek Foreman64a3df02014-10-23 12:24:18 -05001124 if (weston_parse_transform(t, &transform) < 0)
1125 weston_log("Invalid transform \"%s\" for output %s\n",
1126 t, name);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001127 free(t);
1128
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001129 output = wayland_output_create(c, x, y, width, height, name, 0,
1130 transform, scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001131 free(name);
1132
1133 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001134}
1135
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001136static struct wayland_output *
1137wayland_output_create_for_parent_output(struct wayland_compositor *c,
1138 struct wayland_parent_output *poutput)
1139{
1140 struct wayland_output *output;
1141 struct weston_mode *mode;
1142 int32_t x;
1143
1144 if (poutput->current_mode) {
1145 mode = poutput->current_mode;
1146 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001147 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001148 } else if (!wl_list_empty(&poutput->mode_list)) {
1149 mode = container_of(poutput->mode_list.next,
1150 struct weston_mode, link);
1151 } else {
1152 weston_log("No valid modes found. Skipping output");
1153 return NULL;
1154 }
1155
1156 if (!wl_list_empty(&c->base.output_list)) {
1157 output = container_of(c->base.output_list.prev,
1158 struct wayland_output, base.link);
1159 x = output->base.x + output->base.current_mode->width;
1160 } else {
1161 x = 0;
1162 }
1163
1164 output = wayland_output_create(c, x, 0, mode->width, mode->height,
1165 NULL, 0,
1166 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1167 if (!output)
1168 return NULL;
1169
1170 output->parent.output = poutput->global;
1171
1172 output->base.make = poutput->physical.make;
1173 output->base.model = poutput->physical.model;
1174 wl_list_init(&output->base.mode_list);
1175 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1176 wl_list_init(&poutput->mode_list);
1177
1178 wayland_output_set_fullscreen(output,
1179 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1180 mode->refresh, poutput->global);
1181
1182 if (output->parent.shell_surface) {
1183 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1184 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1185 mode->refresh, poutput->global);
1186 } else if (c->parent.fshell) {
1187 _wl_fullscreen_shell_present_surface(c->parent.fshell,
1188 output->parent.surface,
1189 _WL_FULLSCREEN_SHELL_PRESENT_METHOD_CENTER,
1190 poutput->global);
1191 _wl_fullscreen_shell_mode_feedback_destroy(
1192 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
1193 output->parent.surface,
1194 poutput->global,
1195 mode->refresh));
1196 }
1197
1198 return output;
1199}
1200
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001201static void
1202shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1203 uint32_t serial)
1204{
1205 wl_shell_surface_pong(shell_surface, serial);
1206}
1207
1208static void
1209shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1210 uint32_t edges, int32_t width, int32_t height)
1211{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001212 struct wayland_output *output = data;
1213
1214 output->parent.configure_width = width;
1215 output->parent.configure_height = height;
1216
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001217 /* FIXME: implement resizing */
1218}
1219
1220static void
1221shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1222{
1223}
1224
1225static const struct wl_shell_surface_listener shell_surface_listener = {
1226 shell_surface_ping,
1227 shell_surface_configure,
1228 shell_surface_popup_done
1229};
1230
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001231/* Events received from the wayland-server this compositor is client of: */
1232
Jason Ekstrand7744f712013-10-27 22:24:55 -05001233/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001234static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001235input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001236{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001237
Jason Ekstrand7744f712013-10-27 22:24:55 -05001238 struct wl_buffer *buffer;
1239 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001240
Jason Ekstrand7744f712013-10-27 22:24:55 -05001241 if (!input->compositor->cursor)
1242 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001243
Jason Ekstrand7744f712013-10-27 22:24:55 -05001244 image = input->compositor->cursor->images[0];
1245 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001246 if (!buffer)
1247 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001248
1249 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1250 input->parent.cursor.surface,
1251 image->hotspot_x, image->hotspot_y);
1252
1253 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1254 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1255 image->width, image->height);
1256 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001257}
1258
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001259static void
Daniel Stone37816df2012-05-16 18:45:18 +01001260input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1261 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001262 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001263{
1264 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001265 int32_t fx, fy;
1266 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001267
Daniel Stone50692802012-06-22 13:21:41 +01001268 /* XXX: If we get a modifier event immediately before the focus,
1269 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001270 input->enter_serial = serial;
1271 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001272
1273 if (input->output->frame) {
1274 location = frame_pointer_enter(input->output->frame, input,
1275 wl_fixed_to_int(x),
1276 wl_fixed_to_int(y));
1277 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1278 x -= wl_fixed_from_int(fx);
1279 y -= wl_fixed_from_int(fy);
1280
1281 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1282 weston_output_schedule_repaint(&input->output->base);
1283 } else {
1284 location = THEME_LOCATION_CLIENT_AREA;
1285 }
1286
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001287 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001288
1289 if (location == THEME_LOCATION_CLIENT_AREA) {
1290 input->focus = 1;
1291 notify_pointer_focus(&input->base, &input->output->base, x, y);
1292 wl_pointer_set_cursor(input->parent.pointer,
1293 input->enter_serial, NULL, 0, 0);
1294 } else {
1295 input->focus = 0;
1296 notify_pointer_focus(&input->base, NULL, 0, 0);
1297 input_set_cursor(input);
1298 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001299}
1300
1301static void
Daniel Stone37816df2012-05-16 18:45:18 +01001302input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1303 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001304{
1305 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001306
Jason Ekstrand7744f712013-10-27 22:24:55 -05001307 if (input->output->frame) {
1308 frame_pointer_leave(input->output->frame, input);
1309
1310 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1311 weston_output_schedule_repaint(&input->output->base);
1312 }
1313
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001314 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001315 input->output = NULL;
1316 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001317}
1318
1319static void
Daniel Stone37816df2012-05-16 18:45:18 +01001320input_handle_motion(void *data, struct wl_pointer *pointer,
1321 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1322{
1323 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001324 int32_t fx, fy;
1325 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001326
Jason Ekstrand7744f712013-10-27 22:24:55 -05001327 if (input->output->frame) {
1328 location = frame_pointer_motion(input->output->frame, input,
1329 wl_fixed_to_int(x),
1330 wl_fixed_to_int(y));
1331 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1332 x -= wl_fixed_from_int(fx);
1333 y -= wl_fixed_from_int(fy);
1334
1335 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1336 weston_output_schedule_repaint(&input->output->base);
1337 } else {
1338 location = THEME_LOCATION_CLIENT_AREA;
1339 }
1340
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001341 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001342
1343 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
1344 input_set_cursor(input);
1345 notify_pointer_focus(&input->base, NULL, 0, 0);
1346 input->focus = 0;
1347 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
1348 wl_pointer_set_cursor(input->parent.pointer,
1349 input->enter_serial, NULL, 0, 0);
1350 notify_pointer_focus(&input->base, &input->output->base, x, y);
1351 input->focus = 1;
1352 }
1353
1354 if (location == THEME_LOCATION_CLIENT_AREA)
1355 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +01001356}
1357
1358static void
1359input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001360 uint32_t serial, uint32_t time, uint32_t button,
1361 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001362{
1363 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001364 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001365 enum frame_button_state fstate;
1366 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001367
Jason Ekstrand7744f712013-10-27 22:24:55 -05001368 if (input->output->frame) {
1369 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1370 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1371
1372 location = frame_pointer_button(input->output->frame, input,
1373 button, fstate);
1374
1375 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1376
1377 wl_shell_surface_move(input->output->parent.shell_surface,
1378 input->parent.seat, serial);
1379 frame_status_clear(input->output->frame,
1380 FRAME_STATUS_MOVE);
1381 return;
1382 }
1383
1384 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE)
1385 wl_display_terminate(input->compositor->base.wl_display);
1386
1387 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1388 weston_output_schedule_repaint(&input->output->base);
1389 } else {
1390 location = THEME_LOCATION_CLIENT_AREA;
1391 }
1392
1393 if (location == THEME_LOCATION_CLIENT_AREA)
1394 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +01001395}
1396
1397static void
1398input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001399 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001400{
1401 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001402
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001403 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +01001404}
1405
1406static const struct wl_pointer_listener pointer_listener = {
1407 input_handle_pointer_enter,
1408 input_handle_pointer_leave,
1409 input_handle_motion,
1410 input_handle_button,
1411 input_handle_axis,
1412};
1413
1414static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001415input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1416 int fd, uint32_t size)
1417{
1418 struct wayland_input *input = data;
1419 struct xkb_keymap *keymap;
1420 char *map_str;
1421
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001422 if (!data) {
1423 close(fd);
1424 return;
1425 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001426
1427 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1428 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1429 if (map_str == MAP_FAILED) {
1430 weston_log("mmap failed: %m\n");
1431 goto error;
1432 }
1433
Ran Benita2e1968f2014-08-19 23:59:51 +03001434 keymap = xkb_keymap_new_from_string(input->compositor->base.xkb_context,
1435 map_str,
1436 XKB_KEYMAP_FORMAT_TEXT_V1,
1437 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001438 munmap(map_str, size);
1439
1440 if (!keymap) {
1441 weston_log("failed to compile keymap\n");
1442 goto error;
1443 }
1444
1445 input->keyboard_state_update = STATE_UPDATE_NONE;
1446 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1447 weston_log("No keymap provided; falling back to defalt\n");
1448 keymap = NULL;
1449 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1450 } else {
1451 weston_log("Invalid keymap\n");
1452 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001453 }
1454
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001455 close(fd);
1456
Rui Matos0c194ce2013-10-10 19:44:21 +02001457 if (input->base.keyboard)
1458 weston_seat_update_keymap(&input->base, keymap);
1459 else
1460 weston_seat_init_keyboard(&input->base, keymap);
1461
Ran Benitac9c74152014-08-19 23:59:52 +03001462 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001463
1464 return;
1465
1466error:
1467 wl_keyboard_release(input->parent.keyboard);
1468 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001469}
1470
1471static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001472input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001473 struct wl_keyboard *keyboard,
1474 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001475 struct wl_surface *surface,
1476 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001477{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001478 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001479 struct wayland_output *focus;
1480
1481 focus = input->keyboard_focus;
1482 if (focus) {
1483 /* This shouldn't happen */
1484 focus->keyboard_count--;
1485 if (!focus->keyboard_count && focus->frame)
1486 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1487 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1488 weston_output_schedule_repaint(&focus->base);
1489 }
1490
1491 input->keyboard_focus = wl_surface_get_user_data(surface);
1492 input->keyboard_focus->keyboard_count++;
1493
1494 focus = input->keyboard_focus;
1495 if (focus->frame) {
1496 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1497 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1498 weston_output_schedule_repaint(&focus->base);
1499 }
1500
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001501
Daniel Stone50692802012-06-22 13:21:41 +01001502 /* XXX: If we get a modifier event immediately before the focus,
1503 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001504 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001505 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001506}
1507
1508static void
1509input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001510 struct wl_keyboard *keyboard,
1511 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001512 struct wl_surface *surface)
1513{
1514 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001515 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001516
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001517 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001518
1519 focus = input->keyboard_focus;
1520 if (!focus)
1521 return; /* This shouldn't happen */
1522
1523 focus->keyboard_count--;
1524 if (!focus->keyboard_count && focus->frame) {
1525 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1526 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1527 weston_output_schedule_repaint(&focus->base);
1528 }
1529
1530 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001531}
1532
Daniel Stone37816df2012-05-16 18:45:18 +01001533static void
1534input_handle_key(void *data, struct wl_keyboard *keyboard,
1535 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1536{
1537 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001538
Daniel Stone50692802012-06-22 13:21:41 +01001539 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001540 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001541 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001542 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001543 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001544}
1545
Daniel Stone351eb612012-05-31 15:27:47 -04001546static void
1547input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001548 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001549 uint32_t mods_latched, uint32_t mods_locked,
1550 uint32_t group)
1551{
Daniel Stone50692802012-06-22 13:21:41 +01001552 struct wayland_input *input = data;
1553 struct wayland_compositor *c = input->compositor;
1554 uint32_t serial_out;
1555
1556 /* If we get a key event followed by a modifier event with the
1557 * same serial number, then we try to preserve those semantics by
1558 * reusing the same serial number on the way out too. */
1559 if (serial_in == input->key_serial)
1560 serial_out = wl_display_get_serial(c->base.wl_display);
1561 else
1562 serial_out = wl_display_next_serial(c->base.wl_display);
1563
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001564 xkb_state_update_mask(input->base.keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001565 mods_depressed, mods_latched,
1566 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001567 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001568}
1569
Jonny Lamb497994a2014-08-12 14:58:26 +02001570static void
1571input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1572 int32_t rate, int32_t delay)
1573{
1574 struct wayland_input *input = data;
1575 struct wayland_compositor *c = input->compositor;
1576
1577 c->base.kb_repeat_rate = rate;
1578 c->base.kb_repeat_delay = delay;
1579}
1580
Daniel Stone37816df2012-05-16 18:45:18 +01001581static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001582 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001583 input_handle_keyboard_enter,
1584 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001585 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001586 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001587 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001588};
1589
1590static void
Daniel Stone37816df2012-05-16 18:45:18 +01001591input_handle_capabilities(void *data, struct wl_seat *seat,
1592 enum wl_seat_capability caps)
1593{
1594 struct wayland_input *input = data;
1595
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001596 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1597 input->parent.pointer = wl_seat_get_pointer(seat);
1598 wl_pointer_set_user_data(input->parent.pointer, input);
1599 wl_pointer_add_listener(input->parent.pointer,
1600 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001601 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001602 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1603 wl_pointer_destroy(input->parent.pointer);
1604 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001605 }
1606
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001607 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1608 input->parent.keyboard = wl_seat_get_keyboard(seat);
1609 wl_keyboard_set_user_data(input->parent.keyboard, input);
1610 wl_keyboard_add_listener(input->parent.keyboard,
1611 &keyboard_listener, input);
1612 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1613 wl_keyboard_destroy(input->parent.keyboard);
1614 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001615 }
1616}
1617
Jonny Lamb497994a2014-08-12 14:58:26 +02001618static void
1619input_handle_name(void *data, struct wl_seat *seat,
1620 const char *name)
1621{
1622}
1623
Daniel Stone37816df2012-05-16 18:45:18 +01001624static const struct wl_seat_listener seat_listener = {
1625 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001626 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001627};
1628
1629static void
Jonny Lamb497994a2014-08-12 14:58:26 +02001630display_add_seat(struct wayland_compositor *c, uint32_t id, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001631{
1632 struct wayland_input *input;
1633
Peter Huttererf3d62272013-08-08 11:57:05 +10001634 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001635 if (input == NULL)
1636 return;
1637
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001638 weston_seat_init(&input->base, &c->base, "default");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001639 input->compositor = c;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001640 input->parent.seat = wl_registry_bind(c->parent.registry, id,
Jonny Lamb497994a2014-08-12 14:58:26 +02001641 &wl_seat_interface, MIN(version, 4));
Jason Ekstrand06ced802013-11-07 20:13:29 -06001642 wl_list_insert(c->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001643
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001644 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1645 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001646
1647 input->parent.cursor.surface =
1648 wl_compositor_create_surface(c->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001649}
1650
1651static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001652wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1653 int32_t x, int32_t y,
1654 int32_t physical_width, int32_t physical_height,
1655 int32_t subpixel, const char *make,
1656 const char *model, int32_t transform)
1657{
1658 struct wayland_parent_output *output = data;
1659
1660 output->x = x;
1661 output->y = y;
1662 output->physical.width = physical_width;
1663 output->physical.height = physical_height;
1664 output->physical.subpixel = subpixel;
1665
1666 free(output->physical.make);
1667 output->physical.make = strdup(make);
1668 free(output->physical.model);
1669 output->physical.model = strdup(model);
1670
1671 output->transform = transform;
1672}
1673
1674static struct weston_mode *
1675find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1676{
1677 struct weston_mode *mode;
1678
1679 wl_list_for_each(mode, list, link) {
1680 if (mode->width == width && mode->height == height &&
1681 mode->refresh == refresh)
1682 return mode;
1683 }
1684
1685 mode = zalloc(sizeof *mode);
1686 if (!mode)
1687 return NULL;
1688
1689 mode->width = width;
1690 mode->height = height;
1691 mode->refresh = refresh;
1692 wl_list_insert(list, &mode->link);
1693
1694 return mode;
1695}
1696
1697static void
1698wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1699 uint32_t flags, int32_t width, int32_t height,
1700 int32_t refresh)
1701{
1702 struct wayland_parent_output *output = data;
1703 struct weston_mode *mode;
1704
1705 if (output->output) {
1706 mode = find_mode(&output->output->base.mode_list,
1707 width, height, refresh);
1708 if (!mode)
1709 return;
1710 mode->flags = flags;
1711 /* Do a mode-switch on current mode change? */
1712 } else {
1713 mode = find_mode(&output->mode_list, width, height, refresh);
1714 if (!mode)
1715 return;
1716 mode->flags = flags;
1717 if (flags & WL_OUTPUT_MODE_CURRENT)
1718 output->current_mode = mode;
1719 if (flags & WL_OUTPUT_MODE_PREFERRED)
1720 output->preferred_mode = mode;
1721 }
1722}
1723
1724static const struct wl_output_listener output_listener = {
1725 wayland_parent_output_geometry,
1726 wayland_parent_output_mode
1727};
1728
1729static void
1730wayland_compositor_register_output(struct wayland_compositor *c, uint32_t id)
1731{
1732 struct wayland_parent_output *output;
1733
1734 output = zalloc(sizeof *output);
1735 if (!output)
1736 return;
1737
1738 output->id = id;
1739 output->global = wl_registry_bind(c->parent.registry, id,
1740 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001741 if (!output->global) {
1742 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001743 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001744 }
1745
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001746 wl_output_add_listener(output->global, &output_listener, output);
1747
1748 output->scale = 0;
1749 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1750 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
1751 wl_list_init(&output->mode_list);
1752 wl_list_insert(&c->parent.output_list, &output->link);
1753
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05001754 if (c->sprawl_across_outputs) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001755 wl_display_roundtrip(c->parent.wl_display);
1756 wayland_output_create_for_parent_output(c, output);
1757 }
1758}
1759
1760static void
1761wayland_parent_output_destroy(struct wayland_parent_output *output)
1762{
1763 struct weston_mode *mode, *next;
1764
1765 if (output->output)
1766 wayland_output_destroy(&output->output->base);
1767
1768 wl_output_destroy(output->global);
1769 free(output->physical.make);
1770 free(output->physical.model);
1771
1772 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
1773 wl_list_remove(&mode->link);
1774 free(mode);
1775 }
1776}
1777
1778static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001779registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1780 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001781{
1782 struct wayland_compositor *c = data;
1783
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001784 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001785 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001786 wl_registry_bind(registry, name,
1787 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001788 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001789 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001790 wl_registry_bind(registry, name,
1791 &wl_shell_interface, 1);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001792 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
1793 c->parent.fshell =
1794 wl_registry_bind(registry, name,
1795 &_wl_fullscreen_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01001796 } else if (strcmp(interface, "wl_seat") == 0) {
Jonny Lamb497994a2014-08-12 14:58:26 +02001797 display_add_seat(c, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001798 } else if (strcmp(interface, "wl_output") == 0) {
1799 wayland_compositor_register_output(c, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001800 } else if (strcmp(interface, "wl_shm") == 0) {
1801 c->parent.shm =
1802 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001803 }
1804}
1805
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001806static void
1807registry_handle_global_remove(void *data, struct wl_registry *registry,
1808 uint32_t name)
1809{
1810 struct wayland_compositor *c = data;
1811 struct wayland_parent_output *output;
1812
1813 wl_list_for_each(output, &c->parent.output_list, link)
1814 if (output->id == name)
1815 wayland_parent_output_destroy(output);
1816}
1817
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001818static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001819 registry_handle_global,
1820 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001821};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001822
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001823static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001824wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1825{
1826 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001827 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001828
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001829 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1830 wl_display_terminate(c->base.wl_display);
1831 return 0;
1832 }
1833
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001834 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001835 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05001836 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001837 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001838
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001839 if (mask == 0) {
1840 count = wl_display_dispatch_pending(c->parent.wl_display);
1841 wl_display_flush(c->parent.wl_display);
1842 }
1843
1844 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001845}
1846
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001847static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001848wayland_restore(struct weston_compositor *ec)
1849{
1850}
1851
1852static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001853wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001854{
Jonas Ådahle5a12252013-04-05 23:07:11 +02001855 struct wayland_compositor *c = (struct wayland_compositor *) ec;
1856
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001857 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001858
Jonas Ådahle5a12252013-04-05 23:07:11 +02001859 if (c->parent.shm)
1860 wl_shm_destroy(c->parent.shm);
1861
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001862 free(ec);
1863}
1864
Jason Ekstrand7744f712013-10-27 22:24:55 -05001865static const char *left_ptrs[] = {
1866 "left_ptr",
1867 "default",
1868 "top_left_arrow",
1869 "left-arrow"
1870};
1871
1872static void
1873create_cursor(struct wayland_compositor *c, struct weston_config *config)
1874{
1875 struct weston_config_section *s;
1876 int size;
1877 char *theme = NULL;
1878 unsigned int i;
1879
1880 s = weston_config_get_section(config, "shell", NULL, NULL);
1881 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1882 weston_config_section_get_int(s, "cursor-size", &size, 32);
1883
1884 c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
Hardening842a36a2014-03-18 14:12:50 +01001885 if (!c->cursor_theme) {
1886 fprintf(stderr, "could not load cursor theme\n");
1887 return;
1888 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001889
U. Artie Eoffff755002014-01-17 12:36:58 -08001890 free(theme);
1891
Jason Ekstrand7744f712013-10-27 22:24:55 -05001892 c->cursor = NULL;
1893 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1894 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
1895 left_ptrs[i]);
1896 if (!c->cursor) {
1897 fprintf(stderr, "could not load left cursor\n");
1898 return;
1899 }
1900}
1901
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001902static void
1903fullscreen_binding(struct weston_seat *seat_base, uint32_t time, uint32_t key,
1904 void *data)
1905{
1906 struct wayland_compositor *c = data;
1907 struct wayland_input *input = NULL;
1908
1909 wl_list_for_each(input, &c->input_list, link)
1910 if (&input->base == seat_base)
1911 break;
1912
1913 if (!input || !input->output)
1914 return;
1915
1916 if (input->output->frame)
1917 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
1918 else
1919 wayland_output_set_windowed(input->output);
1920
1921 weston_output_schedule_repaint(&input->output->base);
1922}
1923
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001924static struct wayland_compositor *
1925wayland_compositor_create(struct wl_display *display, int use_pixman,
1926 const char *display_name, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001927 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001928{
1929 struct wayland_compositor *c;
1930 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001931 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001932
Peter Huttererf3d62272013-08-08 11:57:05 +10001933 c = zalloc(sizeof *c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001934 if (c == NULL)
1935 return NULL;
1936
Daniel Stone725c2c32012-06-22 14:04:36 +01001937 if (weston_compositor_init(&c->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001938 config) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +02001939 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001940
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001941 if (weston_compositor_set_presentation_clock_software(&c->base) < 0)
1942 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001943
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001944 c->parent.wl_display = wl_display_connect(display_name);
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001945 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001946 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001947 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001948 }
1949
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001950 wl_list_init(&c->parent.output_list);
Jason Ekstrand06ced802013-11-07 20:13:29 -06001951 wl_list_init(&c->input_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001952 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1953 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001954 wl_display_roundtrip(c->parent.wl_display);
1955
1956 create_cursor(c, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001957
1958 c->base.wl_display = display;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001959
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001960 c->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001961
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001962 if (!c->use_pixman) {
1963 gl_renderer = weston_load_module("gl-renderer.so",
1964 "gl_renderer_interface");
1965 if (!gl_renderer)
1966 c->use_pixman = 1;
1967 }
1968
1969 if (!c->use_pixman) {
Jonny Lamb70eba3f2015-03-20 15:26:50 +01001970 if (!gl_renderer->supports ||
1971 gl_renderer->supports(&c->base, "wayland") < 0) {
1972 weston_log("No support for "
1973 "EGL_{KHR,EXT,MESA}_platform_wayland; "
1974 "falling back to pixman.\n");
1975 c->use_pixman = 1;
1976 } else if (gl_renderer->create(&c->base,
1977 EGL_PLATFORM_WAYLAND_KHR,
1978 c->parent.wl_display,
1979 gl_renderer->alpha_attribs,
1980 NULL) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001981 weston_log("Failed to initialize the GL renderer; "
1982 "falling back to pixman.\n");
1983 c->use_pixman = 1;
1984 }
1985 }
1986
1987 if (c->use_pixman) {
1988 if (pixman_renderer_init(&c->base) < 0) {
1989 weston_log("Failed to initialize pixman renderer\n");
1990 goto err_display;
1991 }
1992 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001993
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001994 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001995 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001996
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001997 loop = wl_display_get_event_loop(c->base.wl_display);
1998
1999 fd = wl_display_get_fd(c->parent.wl_display);
2000 c->parent.wl_source =
2001 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2002 wayland_compositor_handle_event, c);
2003 if (c->parent.wl_source == NULL)
2004 goto err_renderer;
2005
2006 wl_event_source_check(c->parent.wl_source);
2007
2008 return c;
2009err_renderer:
2010 c->base.renderer->destroy(&c->base);
2011err_display:
2012 wl_display_disconnect(c->parent.wl_display);
2013err_compositor:
2014 weston_compositor_shutdown(&c->base);
2015err_free:
2016 free(c);
2017 return NULL;
2018}
2019
2020static void
2021wayland_compositor_destroy(struct wayland_compositor *c)
2022{
U. Artie Eoffa1e887b2014-05-21 09:20:02 -07002023 struct weston_output *output, *next;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002024
U. Artie Eoffa1e887b2014-05-21 09:20:02 -07002025 wl_list_for_each_safe(output, next, &c->base.output_list, link)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002026 wayland_output_destroy(output);
2027
2028 c->base.renderer->destroy(&c->base);
2029 wl_display_disconnect(c->parent.wl_display);
2030
2031 if (c->theme)
2032 theme_destroy(c->theme);
2033 if (c->frame_device)
2034 cairo_device_destroy(c->frame_device);
2035 wl_cursor_theme_destroy(c->cursor_theme);
2036
2037 weston_compositor_shutdown(&c->base);
2038 free(c);
2039}
2040
2041WL_EXPORT struct weston_compositor *
2042backend_init(struct wl_display *display, int *argc, char *argv[],
2043 struct weston_config *config)
2044{
2045 struct wayland_compositor *c;
2046 struct wayland_output *output;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002047 struct wayland_parent_output *poutput;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002048 struct weston_config_section *section;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002049 int x, count, width, height, scale, use_pixman, fullscreen, sprawl;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002050 const char *section_name, *display_name;
2051 char *name;
2052
2053 const struct weston_option wayland_options[] = {
2054 { WESTON_OPTION_INTEGER, "width", 0, &width },
2055 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002056 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002057 { WESTON_OPTION_STRING, "display", 0, &display_name },
2058 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2059 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002060 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002061 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002062 };
2063
2064 width = 0;
2065 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002066 scale = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002067 display_name = NULL;
2068 use_pixman = 0;
2069 count = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002070 fullscreen = 0;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002071 sprawl = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002072 parse_options(wayland_options,
2073 ARRAY_LENGTH(wayland_options), argc, argv);
2074
2075 c = wayland_compositor_create(display, use_pixman, display_name,
2076 argc, argv, config);
2077 if (!c)
2078 return NULL;
2079
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002080 if (sprawl || c->parent.fshell) {
2081 c->sprawl_across_outputs = 1;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002082 wl_display_roundtrip(c->parent.wl_display);
2083
2084 wl_list_for_each(poutput, &c->parent.output_list, link)
2085 wayland_output_create_for_parent_output(c, poutput);
2086
2087 return &c->base;
2088 }
2089
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002090 if (fullscreen) {
2091 output = wayland_output_create(c, 0, 0, width, height,
2092 NULL, 1, 0, 1);
2093 if (!output)
2094 goto err_outputs;
2095
Axel Davydd8b88d2013-11-17 21:34:16 +01002096 wayland_output_set_fullscreen(output, 0, 0, NULL);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002097 return &c->base;
2098 }
2099
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002100 section = NULL;
2101 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002102 while (weston_config_next_section(config, &section, &section_name)) {
2103 if (!section_name || strcmp(section_name, "output") != 0)
2104 continue;
2105 weston_config_section_get_string(section, "name", &name, NULL);
2106 if (name == NULL)
2107 continue;
2108
2109 if (name[0] != 'W' || name[1] != 'L') {
2110 free(name);
2111 continue;
2112 }
2113 free(name);
2114
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002115 output = wayland_output_create_for_config(c, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002116 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002117 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002118 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002119 if (wayland_output_set_windowed(output))
2120 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002121
2122 x += output->base.width;
2123 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002124 }
2125
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002126 if (!width)
2127 width = 1024;
2128 if (!height)
2129 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002130 if (!scale)
2131 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002132 while (count > 0) {
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002133 output = wayland_output_create(c, x, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002134 NULL, 0, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002135 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002136 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002137 if (wayland_output_set_windowed(output))
2138 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002139
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002140 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002141 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002142 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002143
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002144 weston_compositor_add_key_binding(&c->base, KEY_F,
2145 MODIFIER_CTRL | MODIFIER_ALT,
2146 fullscreen_binding, c);
2147
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002148 return &c->base;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002149
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002150err_outputs:
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002151 wayland_compositor_destroy(c);
Martin Olssonc5db50f2012-07-08 03:03:43 +02002152 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002153}