blob: 65bce39bf846d7a21a3aedd29d00ac1242723813 [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"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010046
Jason Ekstrand48ce4212013-10-27 22:25:02 -050047#define WINDOW_TITLE "Weston Compositor"
48
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010049struct wayland_compositor {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050050 struct weston_compositor base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010051
52 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040053 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040054 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010055 struct wl_compositor *compositor;
56 struct wl_shell *shell;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050057 struct _wl_fullscreen_shell *fshell;
Jonas Ådahle5a12252013-04-05 23:07:11 +020058 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010059
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050060 struct wl_list output_list;
61
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010062 struct wl_event_source *wl_source;
63 uint32_t event_mask;
64 } parent;
65
Jason Ekstrandff2fd462013-10-27 22:24:58 -050066 int use_pixman;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -050067 int sprawl_across_outputs;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050068
Jason Ekstrand7744f712013-10-27 22:24:55 -050069 struct theme *theme;
70 cairo_device_t *frame_device;
71 struct wl_cursor_theme *cursor_theme;
72 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050073
Jason Ekstrand06ced802013-11-07 20:13:29 -060074 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010075};
76
77struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050078 struct weston_output base;
79
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010080 struct {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050081 int draw_initial_frame;
82 struct wl_surface *surface;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050083
84 struct wl_output *output;
85 uint32_t global_id;
86
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050087 struct wl_shell_surface *shell_surface;
Jason Ekstrand5ea04802013-11-07 20:13:33 -060088 int configure_width, configure_height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010089 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050090
Jason Ekstrand7744f712013-10-27 22:24:55 -050091 int keyboard_count;
92
Jason Ekstrand5ea04802013-11-07 20:13:33 -060093 char *name;
Jason Ekstrand7744f712013-10-27 22:24:55 -050094 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050095
Jason Ekstrand7744f712013-10-27 22:24:55 -050096 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -050097 struct wl_egl_window *egl_window;
98 struct {
99 cairo_surface_t *top;
100 cairo_surface_t *left;
101 cairo_surface_t *right;
102 cairo_surface_t *bottom;
103 } border;
104 } gl;
105
106 struct {
107 struct wl_list buffers;
108 struct wl_list free_buffers;
109 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500110
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500111 struct weston_mode mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500112 uint32_t scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100113};
114
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500115struct wayland_parent_output {
116 struct wayland_output *output;
117 struct wl_list link;
118
119 struct wl_output *global;
120 uint32_t id;
121
122 struct {
123 char *make;
124 char *model;
125 int32_t width, height;
126 uint32_t subpixel;
127 } physical;
128
129 int32_t x, y;
130 uint32_t transform;
131 uint32_t scale;
132
133 struct wl_list mode_list;
134 struct weston_mode *preferred_mode;
135 struct weston_mode *current_mode;
136};
137
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500138struct wayland_shm_buffer {
139 struct wayland_output *output;
140 struct wl_list link;
141 struct wl_list free_link;
142
143 struct wl_buffer *buffer;
144 void *data;
145 size_t size;
146 pixman_region32_t damage;
147 int frame_damaged;
148
149 pixman_image_t *pm_image;
150 cairo_surface_t *c_surface;
151};
152
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100153struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400154 struct weston_seat base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100155 struct wayland_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100156 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500157
158 struct {
159 struct wl_seat *seat;
160 struct wl_pointer *pointer;
161 struct wl_keyboard *keyboard;
162 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500163
164 struct {
165 struct wl_surface *surface;
166 int32_t hx, hy;
167 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500168 } parent;
169
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -0500170 enum weston_key_state_update keyboard_state_update;
Daniel Stone50692802012-06-22 13:21:41 +0100171 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400172 uint32_t enter_serial;
173 int focus;
174 struct wayland_output *output;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500175 struct wayland_output *keyboard_focus;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100176};
177
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300178struct gl_renderer_interface *gl_renderer;
179
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500180static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500181wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
182{
183 cairo_surface_destroy(buffer->c_surface);
184 pixman_image_unref(buffer->pm_image);
185
186 wl_buffer_destroy(buffer->buffer);
187 munmap(buffer->data, buffer->size);
188
189 pixman_region32_fini(&buffer->damage);
190
191 wl_list_remove(&buffer->link);
192 wl_list_remove(&buffer->free_link);
193 free(buffer);
194}
195
196static void
197buffer_release(void *data, struct wl_buffer *buffer)
198{
199 struct wayland_shm_buffer *sb = data;
200
201 if (sb->output) {
202 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
203 } else {
204 wayland_shm_buffer_destroy(sb);
205 }
206}
207
208static const struct wl_buffer_listener buffer_listener = {
209 buffer_release
210};
211
212static struct wayland_shm_buffer *
213wayland_output_get_shm_buffer(struct wayland_output *output)
214{
215 struct wayland_compositor *c =
216 (struct wayland_compositor *) output->base.compositor;
217 struct wl_shm *shm = c->parent.shm;
218 struct wayland_shm_buffer *sb;
219
220 struct wl_shm_pool *pool;
221 int width, height, stride;
222 int32_t fx, fy;
223 int fd;
224 unsigned char *data;
225
226 if (!wl_list_empty(&output->shm.free_buffers)) {
227 sb = container_of(output->shm.free_buffers.next,
228 struct wayland_shm_buffer, free_link);
229 wl_list_remove(&sb->free_link);
230 wl_list_init(&sb->free_link);
231
232 return sb;
233 }
234
235 if (output->frame) {
236 width = frame_width(output->frame);
237 height = frame_height(output->frame);
238 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500239 width = output->base.current_mode->width;
240 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500241 }
242
243 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
244
245 fd = os_create_anonymous_file(height * stride);
246 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000247 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500248 return NULL;
249 }
250
251 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
252 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000253 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500254 close(fd);
255 return NULL;
256 }
257
258 sb = zalloc(sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000259 if (sb == NULL) {
Thierry Reding6ac60c12014-05-27 09:08:29 +0200260 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000261 close(fd);
262 free(data);
263 return NULL;
264 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500265
266 sb->output = output;
267 wl_list_init(&sb->free_link);
268 wl_list_insert(&output->shm.buffers, &sb->link);
269
270 pixman_region32_init_rect(&sb->damage, 0, 0,
271 output->base.width, output->base.height);
272 sb->frame_damaged = 1;
273
274 sb->data = data;
275 sb->size = height * stride;
276
277 pool = wl_shm_create_pool(shm, fd, sb->size);
278
279 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
280 width, height,
281 stride,
282 WL_SHM_FORMAT_ARGB8888);
283 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
284 wl_shm_pool_destroy(pool);
285 close(fd);
286
287 memset(data, 0, sb->size);
288
289 sb->c_surface =
290 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
291 width, height, stride);
292
293 fx = 0;
294 fy = 0;
295 if (output->frame)
296 frame_interior(output->frame, &fx, &fy, 0, 0);
297 sb->pm_image =
298 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
299 (uint32_t *)(data + fy * stride) + fx,
300 stride);
301
302 return sb;
303}
304
305static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500306frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400307{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500308 struct weston_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400309 struct timespec ts;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400310
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500311 wl_callback_destroy(callback);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400312
313 /* XXX: use the presentation extension for proper timings */
314 ts.tv_sec = time / 1000;
315 ts.tv_nsec = (time % 1000) * 1000000;
316 weston_output_finish_frame(output, &ts);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400317}
318
319static const struct wl_callback_listener frame_listener = {
320 frame_done
321};
322
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500323static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200324draw_initial_frame(struct wayland_output *output)
325{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500326 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200327
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500328 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200329
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500330 /* If we are rendering with GL, then orphan it so that it gets
331 * destroyed immediately */
332 if (output->gl.egl_window)
333 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500334
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500335 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500336 wl_surface_damage(output->parent.surface, 0, 0,
337 output->base.current_mode->width,
338 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200339}
340
341static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500342wayland_output_update_gl_border(struct wayland_output *output)
343{
344 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
345 cairo_t *cr;
346
347 if (!output->frame)
348 return;
349 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
350 return;
351
352 fwidth = frame_width(output->frame);
353 fheight = frame_height(output->frame);
354 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
355
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500356 if (!output->gl.border.top)
357 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500358 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
359 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500360 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500361 frame_repaint(output->frame, cr);
362 cairo_destroy(cr);
363 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
364 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500365 cairo_image_surface_get_stride(output->gl.border.top) / 4,
366 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500367
368
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500369 if (!output->gl.border.left)
370 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500371 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
372 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500373 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500374 cairo_translate(cr, 0, -iy);
375 frame_repaint(output->frame, cr);
376 cairo_destroy(cr);
377 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
378 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500379 cairo_image_surface_get_stride(output->gl.border.left) / 4,
380 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500381
382
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500383 if (!output->gl.border.right)
384 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500385 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
386 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500387 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500388 cairo_translate(cr, -(iwidth + ix), -iy);
389 frame_repaint(output->frame, cr);
390 cairo_destroy(cr);
391 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
392 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500393 cairo_image_surface_get_stride(output->gl.border.right) / 4,
394 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500395
396
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500397 if (!output->gl.border.bottom)
398 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500399 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
400 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500401 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500402 cairo_translate(cr, 0, -(iy + iheight));
403 frame_repaint(output->frame, cr);
404 cairo_destroy(cr);
405 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
406 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500407 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
408 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500409}
410
411static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200412wayland_output_start_repaint_loop(struct weston_output *output_base)
413{
414 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrand286ff682013-10-27 22:24:57 -0500415 struct wayland_compositor *wc =
416 (struct wayland_compositor *)output->base.compositor;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200417 struct wl_callback *callback;
418
419 /* If this is the initial frame, we need to attach a buffer so that
420 * the compositor can map the surface and include it in its render
421 * loop. If the surface doesn't end up in the render loop, the frame
422 * callback won't be invoked. The buffer is transparent and of the
423 * same size as the future real output buffer. */
424 if (output->parent.draw_initial_frame) {
425 output->parent.draw_initial_frame = 0;
426
427 draw_initial_frame(output);
428 }
429
430 callback = wl_surface_frame(output->parent.surface);
431 wl_callback_add_listener(callback, &frame_listener, output);
432 wl_surface_commit(output->parent.surface);
Jason Ekstrand286ff682013-10-27 22:24:57 -0500433 wl_display_flush(wc->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200434}
435
David Herrmann1edf44c2013-10-22 17:11:26 +0200436static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500437wayland_output_repaint_gl(struct weston_output *output_base,
438 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400439{
440 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400441 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400442 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600443
Kristian Høgsberg33418202011-08-16 23:01:28 -0400444 callback = wl_surface_frame(output->parent.surface);
445 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100446
Jason Ekstrand7744f712013-10-27 22:24:55 -0500447 wayland_output_update_gl_border(output);
448
Pekka Paalanenbc106382012-10-10 12:49:31 +0300449 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200450
451 pixman_region32_subtract(&ec->primary_plane.damage,
452 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200453 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100454}
455
Matt Roper361d2ad2011-08-29 13:52:23 -0700456static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500457wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
458{
459 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
460 cairo_t *cr;
461
462 if (!buffer->output->frame || !buffer->frame_damaged)
463 return;
464
465 cr = cairo_create(buffer->c_surface);
466
467 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
468 fwidth = frame_width(buffer->output->frame);
469 fheight = frame_height(buffer->output->frame);
470
471 /* Set the clip so we don't unnecisaraly damage the surface */
472 cairo_move_to(cr, ix, iy);
473 cairo_rel_line_to(cr, iwidth, 0);
474 cairo_rel_line_to(cr, 0, iheight);
475 cairo_rel_line_to(cr, -iwidth, 0);
476 cairo_line_to(cr, ix, iy);
477 cairo_line_to(cr, 0, iy);
478 cairo_line_to(cr, 0, fheight);
479 cairo_line_to(cr, fwidth, fheight);
480 cairo_line_to(cr, fwidth, 0);
481 cairo_line_to(cr, 0, 0);
482 cairo_line_to(cr, 0, iy);
483 cairo_close_path(cr);
484 cairo_clip(cr);
485
486 /* Draw using a pattern so that the final result gets clipped */
487 cairo_push_group(cr);
488 frame_repaint(buffer->output->frame, cr);
489 cairo_pop_group_to_source(cr);
490 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
491 cairo_paint(cr);
492
493 cairo_destroy(cr);
494}
495
496static void
497wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
498{
499 pixman_region32_t damage;
500 pixman_box32_t *rects;
501 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
502 int i, n;
503
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500504 pixman_region32_init(&damage);
505 weston_transformed_region(sb->output->base.width,
506 sb->output->base.height,
507 sb->output->base.transform,
508 sb->output->base.current_scale,
509 &sb->damage, &damage);
510
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500511 if (sb->output->frame) {
512 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
513 fwidth = frame_width(sb->output->frame);
514 fheight = frame_height(sb->output->frame);
515
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500516 pixman_region32_translate(&damage, ix, iy);
517
518 if (sb->frame_damaged) {
519 pixman_region32_union_rect(&damage, &damage,
520 0, 0, fwidth, iy);
521 pixman_region32_union_rect(&damage, &damage,
522 0, iy, ix, iheight);
523 pixman_region32_union_rect(&damage, &damage,
524 ix + iwidth, iy,
525 fwidth - (ix + iwidth), iheight);
526 pixman_region32_union_rect(&damage, &damage,
527 0, iy + iheight,
528 fwidth, fheight - (iy + iheight));
529 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500530 }
531
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500532 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500533 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
534 for (i = 0; i < n; ++i)
535 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
536 rects[i].y1, rects[i].x2 - rects[i].x1,
537 rects[i].y2 - rects[i].y1);
538
539 if (sb->output->frame)
540 pixman_region32_fini(&damage);
541}
542
543static int
544wayland_output_repaint_pixman(struct weston_output *output_base,
545 pixman_region32_t *damage)
546{
547 struct wayland_output *output = (struct wayland_output *) output_base;
548 struct wayland_compositor *c =
549 (struct wayland_compositor *)output->base.compositor;
550 struct wl_callback *callback;
551 struct wayland_shm_buffer *sb;
552
553 if (output->frame) {
554 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
555 wl_list_for_each(sb, &output->shm.buffers, link)
556 sb->frame_damaged = 1;
557 }
558
559 wl_list_for_each(sb, &output->shm.buffers, link)
560 pixman_region32_union(&sb->damage, &sb->damage, damage);
561
562 sb = wayland_output_get_shm_buffer(output);
563
564 wayland_output_update_shm_border(sb);
565 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
566 c->base.renderer->repaint_output(output_base, &sb->damage);
567
568 wayland_shm_buffer_attach(sb);
569
570 callback = wl_surface_frame(output->parent.surface);
571 wl_callback_add_listener(callback, &frame_listener, output);
572 wl_surface_commit(output->parent.surface);
573 wl_display_flush(c->parent.wl_display);
574
575 pixman_region32_fini(&sb->damage);
576 pixman_region32_init(&sb->damage);
577 sb->frame_damaged = 0;
578
579 pixman_region32_subtract(&c->base.primary_plane.damage,
580 &c->base.primary_plane.damage, damage);
581 return 0;
582}
583
584static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500585wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700586{
587 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500588 struct wayland_compositor *c =
589 (struct wayland_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700590
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500591 if (c->use_pixman) {
592 pixman_renderer_output_destroy(output_base);
593 } else {
594 gl_renderer->output_destroy(output_base);
595 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100596
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500597 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500598 wl_surface_destroy(output->parent.surface);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500599 if (output->parent.shell_surface)
600 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500601
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600602 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500603 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600604
605 cairo_surface_destroy(output->gl.border.top);
606 cairo_surface_destroy(output->gl.border.left);
607 cairo_surface_destroy(output->gl.border.right);
608 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500609
610 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700611 free(output);
612
613 return;
614}
615
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300616static const struct wl_shell_surface_listener shell_surface_listener;
617
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200618static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500619wayland_output_init_gl_renderer(struct wayland_output *output)
620{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500621 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500622
623 if (output->frame) {
624 fwidth = frame_width(output->frame);
625 fheight = frame_height(output->frame);
626 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500627 fwidth = output->base.current_mode->width;
628 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500629 }
630
631 output->gl.egl_window =
632 wl_egl_window_create(output->parent.surface,
633 fwidth, fheight);
634 if (!output->gl.egl_window) {
635 weston_log("failure to create wl_egl_window\n");
636 return -1;
637 }
638
639 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000640 output->gl.egl_window,
641 gl_renderer->alpha_attribs,
642 NULL) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500643 goto cleanup_window;
644
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500645 return 0;
646
647cleanup_window:
648 wl_egl_window_destroy(output->gl.egl_window);
649 return -1;
650}
651
652static int
653wayland_output_init_pixman_renderer(struct wayland_output *output)
654{
655 return pixman_renderer_output_create(&output->base);
656}
657
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600658static void
659wayland_output_resize_surface(struct wayland_output *output)
660{
661 struct wayland_compositor *c =
662 (struct wayland_compositor *)output->base.compositor;
663 struct wayland_shm_buffer *buffer, *next;
664 int32_t ix, iy, iwidth, iheight;
665 int32_t width, height;
666 struct wl_region *region;
667
668 width = output->base.current_mode->width;
669 height = output->base.current_mode->height;
670
671 if (output->frame) {
672 frame_resize_inside(output->frame, width, height);
673
674 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
675 region = wl_compositor_create_region(c->parent.compositor);
676 wl_region_add(region, ix, iy, iwidth, iheight);
677 wl_surface_set_input_region(output->parent.surface, region);
678 wl_region_destroy(region);
679
680 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
681 region = wl_compositor_create_region(c->parent.compositor);
682 wl_region_add(region, ix, iy, iwidth, iheight);
683 wl_surface_set_opaque_region(output->parent.surface, region);
684 wl_region_destroy(region);
685
686 width = frame_width(output->frame);
687 height = frame_height(output->frame);
688 } else {
689 region = wl_compositor_create_region(c->parent.compositor);
690 wl_region_add(region, 0, 0, width, height);
691 wl_surface_set_input_region(output->parent.surface, region);
692 wl_region_destroy(region);
693
694 region = wl_compositor_create_region(c->parent.compositor);
695 wl_region_add(region, 0, 0, width, height);
696 wl_surface_set_opaque_region(output->parent.surface, region);
697 wl_region_destroy(region);
698 }
699
700 if (output->gl.egl_window) {
701 wl_egl_window_resize(output->gl.egl_window,
702 width, height, 0, 0);
703
704 /* These will need to be re-created due to the resize */
705 gl_renderer->output_set_border(&output->base,
706 GL_RENDERER_BORDER_TOP,
707 0, 0, 0, NULL);
708 cairo_surface_destroy(output->gl.border.top);
709 output->gl.border.top = NULL;
710 gl_renderer->output_set_border(&output->base,
711 GL_RENDERER_BORDER_LEFT,
712 0, 0, 0, NULL);
713 cairo_surface_destroy(output->gl.border.left);
714 output->gl.border.left = NULL;
715 gl_renderer->output_set_border(&output->base,
716 GL_RENDERER_BORDER_RIGHT,
717 0, 0, 0, NULL);
718 cairo_surface_destroy(output->gl.border.right);
719 output->gl.border.right = NULL;
720 gl_renderer->output_set_border(&output->base,
721 GL_RENDERER_BORDER_BOTTOM,
722 0, 0, 0, NULL);
723 cairo_surface_destroy(output->gl.border.bottom);
724 output->gl.border.bottom = NULL;
725 }
726
727 /* Throw away any remaining SHM buffers */
728 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, link)
729 wayland_shm_buffer_destroy(buffer);
730 /* These will get thrown away when they get released */
731 wl_list_for_each(buffer, &output->shm.buffers, link)
732 buffer->output = NULL;
733}
734
735static int
736wayland_output_set_windowed(struct wayland_output *output)
737{
738 struct wayland_compositor *c =
739 (struct wayland_compositor *)output->base.compositor;
740 int tlen;
741 char *title;
742
743 if (output->frame)
744 return 0;
745
746 if (output->name) {
747 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
748 title = malloc(tlen + 1);
749 if (!title)
750 return -1;
751
752 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
753 } else {
754 title = strdup(WINDOW_TITLE);
755 }
756
757 if (!c->theme) {
758 c->theme = theme_create();
759 if (!c->theme) {
760 free(title);
761 return -1;
762 }
763 }
764 output->frame = frame_create(c->theme, 100, 100,
765 FRAME_BUTTON_CLOSE, title);
766 free(title);
767 if (!output->frame)
768 return -1;
769
770 if (output->keyboard_count)
771 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
772
773 wayland_output_resize_surface(output);
774
775 wl_shell_surface_set_toplevel(output->parent.shell_surface);
776
777 return 0;
778}
779
780static void
781wayland_output_set_fullscreen(struct wayland_output *output,
782 enum wl_shell_surface_fullscreen_method method,
783 uint32_t framerate, struct wl_output *target)
784{
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500785 struct wayland_compositor *c =
786 (struct wayland_compositor *)output->base.compositor;
787
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600788 if (output->frame) {
789 frame_destroy(output->frame);
790 output->frame = NULL;
791 }
792
793 wayland_output_resize_surface(output);
794
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500795 if (output->parent.shell_surface) {
796 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
797 method, framerate, target);
798 } else if (c->parent.fshell) {
799 _wl_fullscreen_shell_present_surface(c->parent.fshell,
800 output->parent.surface,
801 method, target);
802 }
803}
804
805static struct weston_mode *
806wayland_output_choose_mode(struct wayland_output *output,
807 struct weston_mode *ref_mode)
808{
809 struct weston_mode *mode;
810
811 /* First look for an exact match */
812 wl_list_for_each(mode, &output->base.mode_list, link)
813 if (mode->width == ref_mode->width &&
814 mode->height == ref_mode->height &&
815 mode->refresh == ref_mode->refresh)
816 return mode;
817
818 /* If we can't find an exact match, ignore refresh and try again */
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 return mode;
823
824 /* Yeah, we failed */
825 return NULL;
826}
827
828enum mode_status {
829 MODE_STATUS_UNKNOWN,
830 MODE_STATUS_SUCCESS,
831 MODE_STATUS_FAIL,
832 MODE_STATUS_CANCEL,
833};
834
835static void
836mode_feedback_successful(void *data,
837 struct _wl_fullscreen_shell_mode_feedback *fb)
838{
839 enum mode_status *value = data;
840
841 printf("Mode switch successful\n");
842
843 *value = MODE_STATUS_SUCCESS;
844}
845
846static void
847mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
848{
849 enum mode_status *value = data;
850
851 printf("Mode switch failed\n");
852
853 *value = MODE_STATUS_FAIL;
854}
855
856static void
857mode_feedback_cancelled(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
858{
859 enum mode_status *value = data;
860
861 printf("Mode switch cancelled\n");
862
863 *value = MODE_STATUS_CANCEL;
864}
865
866struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
867 mode_feedback_successful,
868 mode_feedback_failed,
869 mode_feedback_cancelled,
870};
871
872static int
873wayland_output_switch_mode(struct weston_output *output_base,
874 struct weston_mode *mode)
875{
876 struct wayland_output *output = (struct wayland_output *) output_base;
877 struct wayland_compositor *c;
878 struct wl_surface *old_surface;
879 struct weston_mode *old_mode;
880 struct _wl_fullscreen_shell_mode_feedback *mode_feedback;
881 enum mode_status mode_status;
882 int ret = 0;
883
884 if (output_base == NULL) {
885 weston_log("output is NULL.\n");
886 return -1;
887 }
888
889 if (mode == NULL) {
890 weston_log("mode is NULL.\n");
891 return -1;
892 }
893
894 c = (struct wayland_compositor *)output_base->compositor;
895
896 if (output->parent.shell_surface || !c->parent.fshell)
897 return -1;
898
899 mode = wayland_output_choose_mode(output, mode);
900 if (mode == NULL)
901 return -1;
902
903 if (output->base.current_mode == mode)
904 return 0;
905
906 old_mode = output->base.current_mode;
907 old_surface = output->parent.surface;
908 output->base.current_mode = mode;
909 output->parent.surface =
910 wl_compositor_create_surface(c->parent.compositor);
911 wl_surface_set_user_data(output->parent.surface, output);
912
913 /* Blow the old buffers because we changed size/surfaces */
914 wayland_output_resize_surface(output);
915
916 mode_feedback =
917 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
918 output->parent.surface,
919 output->parent.output,
920 mode->refresh);
921 _wl_fullscreen_shell_mode_feedback_add_listener(mode_feedback,
922 &mode_feedback_listener,
923 &mode_status);
924
925 /* This should kick-start things again */
926 output->parent.draw_initial_frame = 1;
927 wayland_output_start_repaint_loop(&output->base);
928
929 mode_status = MODE_STATUS_UNKNOWN;
930 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
931 ret = wl_display_dispatch(c->parent.wl_display);
932
933 _wl_fullscreen_shell_mode_feedback_destroy(mode_feedback);
934
935 if (mode_status == MODE_STATUS_FAIL) {
936 output->base.current_mode = old_mode;
937 wl_surface_destroy(output->parent.surface);
938 output->parent.surface = old_surface;
939 wayland_output_resize_surface(output);
940
941 return -1;
942 }
943
944 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
945 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
946
947 if (c->use_pixman) {
948 pixman_renderer_output_destroy(output_base);
949 if (wayland_output_init_pixman_renderer(output) < 0)
950 goto err_output;
951 } else {
952 gl_renderer->output_destroy(output_base);
953 wl_egl_window_destroy(output->gl.egl_window);
954 if (wayland_output_init_gl_renderer(output) < 0)
955 goto err_output;
956 }
957 wl_surface_destroy(old_surface);
958
959 weston_output_schedule_repaint(&output->base);
960
961 return 0;
962
963err_output:
964 /* XXX */
965 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600966}
967
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500968static struct wayland_output *
969wayland_output_create(struct wayland_compositor *c, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600970 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500971 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100972{
973 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500974 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600975
976 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
977 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100978
Peter Huttererf3d62272013-08-08 11:57:05 +1000979 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100980 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500981 return NULL;
982
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600983 output->name = name ? strdup(name) : NULL;
984 output->base.make = "waywayland";
985 output->base.model = "none";
986
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500987 output_width = width * scale;
988 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100989
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600990 output->parent.surface =
991 wl_compositor_create_surface(c->parent.compositor);
992 if (!output->parent.surface)
993 goto err_name;
994 wl_surface_set_user_data(output->parent.surface, output);
995
996 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600997
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500998 if (c->parent.shell) {
999 output->parent.shell_surface =
1000 wl_shell_get_shell_surface(c->parent.shell,
1001 output->parent.surface);
1002 if (!output->parent.shell_surface)
1003 goto err_surface;
1004 wl_shell_surface_add_listener(output->parent.shell_surface,
1005 &shell_surface_listener, output);
1006 }
1007
1008 if (fullscreen && c->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001009 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1010 0, 0, NULL);
1011 wl_display_roundtrip(c->parent.wl_display);
1012 if (!width)
1013 output_width = output->parent.configure_width;
1014 if (!height)
1015 output_height = output->parent.configure_height;
1016 }
1017
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001018 output->mode.flags =
1019 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001020 output->mode.width = output_width;
1021 output->mode.height = output_height;
1022 output->mode.refresh = 60000;
1023 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001024 wl_list_init(&output->base.mode_list);
1025 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001026 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001027
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001028 wl_list_init(&output->shm.buffers);
1029 wl_list_init(&output->shm.free_buffers);
1030
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001031 weston_output_init(&output->base, &c->base, x, y, width, height,
1032 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001033
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001034 if (c->use_pixman) {
1035 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001036 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001037 output->base.repaint = wayland_output_repaint_pixman;
1038 } else {
1039 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001040 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001041 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001042 }
1043
Jonas Ådahle5a12252013-04-05 23:07:11 +02001044 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001045 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001046 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001047 output->base.set_backlight = NULL;
1048 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001049 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001050
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001051 wl_list_insert(c->base.output_list.prev, &output->base.link);
1052
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001053 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001054
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001055err_output:
1056 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001057 if (output->parent.shell_surface)
1058 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001059err_surface:
1060 wl_surface_destroy(output->parent.surface);
1061err_name:
1062 free(output->name);
1063
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001064 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001065 free(output);
1066
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001067 return NULL;
1068}
1069
1070static struct wayland_output *
1071wayland_output_create_for_config(struct wayland_compositor *c,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001072 struct weston_config_section *config_section,
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001073 int option_width, int option_height,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001074 int option_scale, int32_t x, int32_t y)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001075{
1076 struct wayland_output *output;
1077 char *mode, *t, *name, *str;
1078 int width, height, scale;
1079 uint32_t transform;
Derek Foreman64a3df02014-10-23 12:24:18 -05001080 unsigned int slen;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001081
1082 weston_config_section_get_string(config_section, "name", &name, NULL);
1083 if (name) {
1084 slen = strlen(name);
1085 slen += strlen(WINDOW_TITLE " - ");
1086 str = malloc(slen + 1);
1087 if (str)
1088 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
1089 free(name);
1090 name = str;
1091 }
1092 if (!name)
U. Artie Eoff1a08d112014-01-17 12:22:50 -08001093 name = strdup(WINDOW_TITLE);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001094
1095 weston_config_section_get_string(config_section,
1096 "mode", &mode, "1024x600");
1097 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1098 weston_log("Invalid mode \"%s\" for output %s\n",
1099 mode, name);
1100 width = 1024;
1101 height = 640;
1102 }
1103 free(mode);
1104
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001105 if (option_width)
1106 width = option_width;
1107 if (option_height)
1108 height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001109
1110 weston_config_section_get_int(config_section, "scale", &scale, 1);
1111
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001112 if (option_scale)
1113 scale = option_scale;
1114
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001115 weston_config_section_get_string(config_section,
1116 "transform", &t, "normal");
Derek Foreman64a3df02014-10-23 12:24:18 -05001117 if (weston_parse_transform(t, &transform) < 0)
1118 weston_log("Invalid transform \"%s\" for output %s\n",
1119 t, name);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001120 free(t);
1121
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001122 output = wayland_output_create(c, x, y, width, height, name, 0,
1123 transform, scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001124 free(name);
1125
1126 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001127}
1128
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001129static struct wayland_output *
1130wayland_output_create_for_parent_output(struct wayland_compositor *c,
1131 struct wayland_parent_output *poutput)
1132{
1133 struct wayland_output *output;
1134 struct weston_mode *mode;
1135 int32_t x;
1136
1137 if (poutput->current_mode) {
1138 mode = poutput->current_mode;
1139 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001140 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001141 } else if (!wl_list_empty(&poutput->mode_list)) {
1142 mode = container_of(poutput->mode_list.next,
1143 struct weston_mode, link);
1144 } else {
1145 weston_log("No valid modes found. Skipping output");
1146 return NULL;
1147 }
1148
1149 if (!wl_list_empty(&c->base.output_list)) {
1150 output = container_of(c->base.output_list.prev,
1151 struct wayland_output, base.link);
1152 x = output->base.x + output->base.current_mode->width;
1153 } else {
1154 x = 0;
1155 }
1156
1157 output = wayland_output_create(c, x, 0, mode->width, mode->height,
1158 NULL, 0,
1159 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1160 if (!output)
1161 return NULL;
1162
1163 output->parent.output = poutput->global;
1164
1165 output->base.make = poutput->physical.make;
1166 output->base.model = poutput->physical.model;
1167 wl_list_init(&output->base.mode_list);
1168 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1169 wl_list_init(&poutput->mode_list);
1170
1171 wayland_output_set_fullscreen(output,
1172 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1173 mode->refresh, poutput->global);
1174
1175 if (output->parent.shell_surface) {
1176 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1177 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1178 mode->refresh, poutput->global);
1179 } else if (c->parent.fshell) {
1180 _wl_fullscreen_shell_present_surface(c->parent.fshell,
1181 output->parent.surface,
1182 _WL_FULLSCREEN_SHELL_PRESENT_METHOD_CENTER,
1183 poutput->global);
1184 _wl_fullscreen_shell_mode_feedback_destroy(
1185 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
1186 output->parent.surface,
1187 poutput->global,
1188 mode->refresh));
1189 }
1190
1191 return output;
1192}
1193
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001194static void
1195shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1196 uint32_t serial)
1197{
1198 wl_shell_surface_pong(shell_surface, serial);
1199}
1200
1201static void
1202shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1203 uint32_t edges, int32_t width, int32_t height)
1204{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001205 struct wayland_output *output = data;
1206
1207 output->parent.configure_width = width;
1208 output->parent.configure_height = height;
1209
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001210 /* FIXME: implement resizing */
1211}
1212
1213static void
1214shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1215{
1216}
1217
1218static const struct wl_shell_surface_listener shell_surface_listener = {
1219 shell_surface_ping,
1220 shell_surface_configure,
1221 shell_surface_popup_done
1222};
1223
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001224/* Events received from the wayland-server this compositor is client of: */
1225
Jason Ekstrand7744f712013-10-27 22:24:55 -05001226/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001227static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001228input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001229{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001230
Jason Ekstrand7744f712013-10-27 22:24:55 -05001231 struct wl_buffer *buffer;
1232 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001233
Jason Ekstrand7744f712013-10-27 22:24:55 -05001234 if (!input->compositor->cursor)
1235 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001236
Jason Ekstrand7744f712013-10-27 22:24:55 -05001237 image = input->compositor->cursor->images[0];
1238 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001239 if (!buffer)
1240 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001241
1242 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1243 input->parent.cursor.surface,
1244 image->hotspot_x, image->hotspot_y);
1245
1246 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1247 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1248 image->width, image->height);
1249 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001250}
1251
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001252static void
Daniel Stone37816df2012-05-16 18:45:18 +01001253input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1254 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001255 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001256{
1257 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001258 int32_t fx, fy;
1259 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001260
Daniel Stone50692802012-06-22 13:21:41 +01001261 /* XXX: If we get a modifier event immediately before the focus,
1262 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001263 input->enter_serial = serial;
1264 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001265
1266 if (input->output->frame) {
1267 location = frame_pointer_enter(input->output->frame, input,
1268 wl_fixed_to_int(x),
1269 wl_fixed_to_int(y));
1270 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1271 x -= wl_fixed_from_int(fx);
1272 y -= wl_fixed_from_int(fy);
1273
1274 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1275 weston_output_schedule_repaint(&input->output->base);
1276 } else {
1277 location = THEME_LOCATION_CLIENT_AREA;
1278 }
1279
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001280 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001281
1282 if (location == THEME_LOCATION_CLIENT_AREA) {
1283 input->focus = 1;
1284 notify_pointer_focus(&input->base, &input->output->base, x, y);
1285 wl_pointer_set_cursor(input->parent.pointer,
1286 input->enter_serial, NULL, 0, 0);
1287 } else {
1288 input->focus = 0;
1289 notify_pointer_focus(&input->base, NULL, 0, 0);
1290 input_set_cursor(input);
1291 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001292}
1293
1294static void
Daniel Stone37816df2012-05-16 18:45:18 +01001295input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1296 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001297{
1298 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001299
Jason Ekstrand7744f712013-10-27 22:24:55 -05001300 if (input->output->frame) {
1301 frame_pointer_leave(input->output->frame, input);
1302
1303 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1304 weston_output_schedule_repaint(&input->output->base);
1305 }
1306
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001307 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001308 input->output = NULL;
1309 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001310}
1311
1312static void
Daniel Stone37816df2012-05-16 18:45:18 +01001313input_handle_motion(void *data, struct wl_pointer *pointer,
1314 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1315{
1316 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001317 int32_t fx, fy;
1318 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001319
Jason Ekstrand7744f712013-10-27 22:24:55 -05001320 if (input->output->frame) {
1321 location = frame_pointer_motion(input->output->frame, input,
1322 wl_fixed_to_int(x),
1323 wl_fixed_to_int(y));
1324 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1325 x -= wl_fixed_from_int(fx);
1326 y -= wl_fixed_from_int(fy);
1327
1328 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1329 weston_output_schedule_repaint(&input->output->base);
1330 } else {
1331 location = THEME_LOCATION_CLIENT_AREA;
1332 }
1333
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001334 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001335
1336 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
1337 input_set_cursor(input);
1338 notify_pointer_focus(&input->base, NULL, 0, 0);
1339 input->focus = 0;
1340 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
1341 wl_pointer_set_cursor(input->parent.pointer,
1342 input->enter_serial, NULL, 0, 0);
1343 notify_pointer_focus(&input->base, &input->output->base, x, y);
1344 input->focus = 1;
1345 }
1346
1347 if (location == THEME_LOCATION_CLIENT_AREA)
1348 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +01001349}
1350
1351static void
1352input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001353 uint32_t serial, uint32_t time, uint32_t button,
1354 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001355{
1356 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001357 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001358 enum frame_button_state fstate;
1359 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001360
Jason Ekstrand7744f712013-10-27 22:24:55 -05001361 if (input->output->frame) {
1362 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1363 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1364
1365 location = frame_pointer_button(input->output->frame, input,
1366 button, fstate);
1367
1368 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1369
1370 wl_shell_surface_move(input->output->parent.shell_surface,
1371 input->parent.seat, serial);
1372 frame_status_clear(input->output->frame,
1373 FRAME_STATUS_MOVE);
1374 return;
1375 }
1376
1377 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE)
1378 wl_display_terminate(input->compositor->base.wl_display);
1379
1380 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1381 weston_output_schedule_repaint(&input->output->base);
1382 } else {
1383 location = THEME_LOCATION_CLIENT_AREA;
1384 }
1385
1386 if (location == THEME_LOCATION_CLIENT_AREA)
1387 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +01001388}
1389
1390static void
1391input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001392 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001393{
1394 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001395
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001396 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +01001397}
1398
1399static const struct wl_pointer_listener pointer_listener = {
1400 input_handle_pointer_enter,
1401 input_handle_pointer_leave,
1402 input_handle_motion,
1403 input_handle_button,
1404 input_handle_axis,
1405};
1406
1407static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001408input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1409 int fd, uint32_t size)
1410{
1411 struct wayland_input *input = data;
1412 struct xkb_keymap *keymap;
1413 char *map_str;
1414
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001415 if (!data) {
1416 close(fd);
1417 return;
1418 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001419
1420 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1421 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1422 if (map_str == MAP_FAILED) {
1423 weston_log("mmap failed: %m\n");
1424 goto error;
1425 }
1426
Ran Benita2e1968f2014-08-19 23:59:51 +03001427 keymap = xkb_keymap_new_from_string(input->compositor->base.xkb_context,
1428 map_str,
1429 XKB_KEYMAP_FORMAT_TEXT_V1,
1430 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001431 munmap(map_str, size);
1432
1433 if (!keymap) {
1434 weston_log("failed to compile keymap\n");
1435 goto error;
1436 }
1437
1438 input->keyboard_state_update = STATE_UPDATE_NONE;
1439 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1440 weston_log("No keymap provided; falling back to defalt\n");
1441 keymap = NULL;
1442 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1443 } else {
1444 weston_log("Invalid keymap\n");
1445 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001446 }
1447
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001448 close(fd);
1449
Rui Matos0c194ce2013-10-10 19:44:21 +02001450 if (input->base.keyboard)
1451 weston_seat_update_keymap(&input->base, keymap);
1452 else
1453 weston_seat_init_keyboard(&input->base, keymap);
1454
Ran Benitac9c74152014-08-19 23:59:52 +03001455 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001456
1457 return;
1458
1459error:
1460 wl_keyboard_release(input->parent.keyboard);
1461 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001462}
1463
1464static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001465input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001466 struct wl_keyboard *keyboard,
1467 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001468 struct wl_surface *surface,
1469 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001470{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001471 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001472 struct wayland_output *focus;
1473
1474 focus = input->keyboard_focus;
1475 if (focus) {
1476 /* This shouldn't happen */
1477 focus->keyboard_count--;
1478 if (!focus->keyboard_count && focus->frame)
1479 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1480 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1481 weston_output_schedule_repaint(&focus->base);
1482 }
1483
1484 input->keyboard_focus = wl_surface_get_user_data(surface);
1485 input->keyboard_focus->keyboard_count++;
1486
1487 focus = input->keyboard_focus;
1488 if (focus->frame) {
1489 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1490 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1491 weston_output_schedule_repaint(&focus->base);
1492 }
1493
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001494
Daniel Stone50692802012-06-22 13:21:41 +01001495 /* XXX: If we get a modifier event immediately before the focus,
1496 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001497 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001498 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001499}
1500
1501static void
1502input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001503 struct wl_keyboard *keyboard,
1504 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001505 struct wl_surface *surface)
1506{
1507 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001508 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001509
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001510 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001511
1512 focus = input->keyboard_focus;
1513 if (!focus)
1514 return; /* This shouldn't happen */
1515
1516 focus->keyboard_count--;
1517 if (!focus->keyboard_count && focus->frame) {
1518 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1519 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1520 weston_output_schedule_repaint(&focus->base);
1521 }
1522
1523 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001524}
1525
Daniel Stone37816df2012-05-16 18:45:18 +01001526static void
1527input_handle_key(void *data, struct wl_keyboard *keyboard,
1528 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1529{
1530 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001531
Daniel Stone50692802012-06-22 13:21:41 +01001532 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001533 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001534 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001535 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001536 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001537}
1538
Daniel Stone351eb612012-05-31 15:27:47 -04001539static void
1540input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001541 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001542 uint32_t mods_latched, uint32_t mods_locked,
1543 uint32_t group)
1544{
Daniel Stone50692802012-06-22 13:21:41 +01001545 struct wayland_input *input = data;
1546 struct wayland_compositor *c = input->compositor;
1547 uint32_t serial_out;
1548
1549 /* If we get a key event followed by a modifier event with the
1550 * same serial number, then we try to preserve those semantics by
1551 * reusing the same serial number on the way out too. */
1552 if (serial_in == input->key_serial)
1553 serial_out = wl_display_get_serial(c->base.wl_display);
1554 else
1555 serial_out = wl_display_next_serial(c->base.wl_display);
1556
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001557 xkb_state_update_mask(input->base.keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001558 mods_depressed, mods_latched,
1559 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001560 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001561}
1562
Jonny Lamb497994a2014-08-12 14:58:26 +02001563static void
1564input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1565 int32_t rate, int32_t delay)
1566{
1567 struct wayland_input *input = data;
1568 struct wayland_compositor *c = input->compositor;
1569
1570 c->base.kb_repeat_rate = rate;
1571 c->base.kb_repeat_delay = delay;
1572}
1573
Daniel Stone37816df2012-05-16 18:45:18 +01001574static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001575 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001576 input_handle_keyboard_enter,
1577 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001578 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001579 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001580 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001581};
1582
1583static void
Daniel Stone37816df2012-05-16 18:45:18 +01001584input_handle_capabilities(void *data, struct wl_seat *seat,
1585 enum wl_seat_capability caps)
1586{
1587 struct wayland_input *input = data;
1588
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001589 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1590 input->parent.pointer = wl_seat_get_pointer(seat);
1591 wl_pointer_set_user_data(input->parent.pointer, input);
1592 wl_pointer_add_listener(input->parent.pointer,
1593 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001594 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001595 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1596 wl_pointer_destroy(input->parent.pointer);
1597 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001598 }
1599
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001600 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1601 input->parent.keyboard = wl_seat_get_keyboard(seat);
1602 wl_keyboard_set_user_data(input->parent.keyboard, input);
1603 wl_keyboard_add_listener(input->parent.keyboard,
1604 &keyboard_listener, input);
1605 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1606 wl_keyboard_destroy(input->parent.keyboard);
1607 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001608 }
1609}
1610
Jonny Lamb497994a2014-08-12 14:58:26 +02001611static void
1612input_handle_name(void *data, struct wl_seat *seat,
1613 const char *name)
1614{
1615}
1616
Daniel Stone37816df2012-05-16 18:45:18 +01001617static const struct wl_seat_listener seat_listener = {
1618 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001619 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001620};
1621
1622static void
Jonny Lamb497994a2014-08-12 14:58:26 +02001623display_add_seat(struct wayland_compositor *c, uint32_t id, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001624{
1625 struct wayland_input *input;
1626
Peter Huttererf3d62272013-08-08 11:57:05 +10001627 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001628 if (input == NULL)
1629 return;
1630
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001631 weston_seat_init(&input->base, &c->base, "default");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001632 input->compositor = c;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001633 input->parent.seat = wl_registry_bind(c->parent.registry, id,
Jonny Lamb497994a2014-08-12 14:58:26 +02001634 &wl_seat_interface, MIN(version, 4));
Jason Ekstrand06ced802013-11-07 20:13:29 -06001635 wl_list_insert(c->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001636
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001637 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1638 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001639
1640 input->parent.cursor.surface =
1641 wl_compositor_create_surface(c->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001642}
1643
1644static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001645wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1646 int32_t x, int32_t y,
1647 int32_t physical_width, int32_t physical_height,
1648 int32_t subpixel, const char *make,
1649 const char *model, int32_t transform)
1650{
1651 struct wayland_parent_output *output = data;
1652
1653 output->x = x;
1654 output->y = y;
1655 output->physical.width = physical_width;
1656 output->physical.height = physical_height;
1657 output->physical.subpixel = subpixel;
1658
1659 free(output->physical.make);
1660 output->physical.make = strdup(make);
1661 free(output->physical.model);
1662 output->physical.model = strdup(model);
1663
1664 output->transform = transform;
1665}
1666
1667static struct weston_mode *
1668find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1669{
1670 struct weston_mode *mode;
1671
1672 wl_list_for_each(mode, list, link) {
1673 if (mode->width == width && mode->height == height &&
1674 mode->refresh == refresh)
1675 return mode;
1676 }
1677
1678 mode = zalloc(sizeof *mode);
1679 if (!mode)
1680 return NULL;
1681
1682 mode->width = width;
1683 mode->height = height;
1684 mode->refresh = refresh;
1685 wl_list_insert(list, &mode->link);
1686
1687 return mode;
1688}
1689
1690static void
1691wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1692 uint32_t flags, int32_t width, int32_t height,
1693 int32_t refresh)
1694{
1695 struct wayland_parent_output *output = data;
1696 struct weston_mode *mode;
1697
1698 if (output->output) {
1699 mode = find_mode(&output->output->base.mode_list,
1700 width, height, refresh);
1701 if (!mode)
1702 return;
1703 mode->flags = flags;
1704 /* Do a mode-switch on current mode change? */
1705 } else {
1706 mode = find_mode(&output->mode_list, width, height, refresh);
1707 if (!mode)
1708 return;
1709 mode->flags = flags;
1710 if (flags & WL_OUTPUT_MODE_CURRENT)
1711 output->current_mode = mode;
1712 if (flags & WL_OUTPUT_MODE_PREFERRED)
1713 output->preferred_mode = mode;
1714 }
1715}
1716
1717static const struct wl_output_listener output_listener = {
1718 wayland_parent_output_geometry,
1719 wayland_parent_output_mode
1720};
1721
1722static void
1723wayland_compositor_register_output(struct wayland_compositor *c, uint32_t id)
1724{
1725 struct wayland_parent_output *output;
1726
1727 output = zalloc(sizeof *output);
1728 if (!output)
1729 return;
1730
1731 output->id = id;
1732 output->global = wl_registry_bind(c->parent.registry, id,
1733 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001734 if (!output->global) {
1735 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001736 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001737 }
1738
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001739 wl_output_add_listener(output->global, &output_listener, output);
1740
1741 output->scale = 0;
1742 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1743 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
1744 wl_list_init(&output->mode_list);
1745 wl_list_insert(&c->parent.output_list, &output->link);
1746
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05001747 if (c->sprawl_across_outputs) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001748 wl_display_roundtrip(c->parent.wl_display);
1749 wayland_output_create_for_parent_output(c, output);
1750 }
1751}
1752
1753static void
1754wayland_parent_output_destroy(struct wayland_parent_output *output)
1755{
1756 struct weston_mode *mode, *next;
1757
1758 if (output->output)
1759 wayland_output_destroy(&output->output->base);
1760
1761 wl_output_destroy(output->global);
1762 free(output->physical.make);
1763 free(output->physical.model);
1764
1765 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
1766 wl_list_remove(&mode->link);
1767 free(mode);
1768 }
1769}
1770
1771static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001772registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1773 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001774{
1775 struct wayland_compositor *c = data;
1776
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001777 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001778 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001779 wl_registry_bind(registry, name,
1780 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001781 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001782 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001783 wl_registry_bind(registry, name,
1784 &wl_shell_interface, 1);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001785 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
1786 c->parent.fshell =
1787 wl_registry_bind(registry, name,
1788 &_wl_fullscreen_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01001789 } else if (strcmp(interface, "wl_seat") == 0) {
Jonny Lamb497994a2014-08-12 14:58:26 +02001790 display_add_seat(c, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001791 } else if (strcmp(interface, "wl_output") == 0) {
1792 wayland_compositor_register_output(c, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001793 } else if (strcmp(interface, "wl_shm") == 0) {
1794 c->parent.shm =
1795 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001796 }
1797}
1798
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001799static void
1800registry_handle_global_remove(void *data, struct wl_registry *registry,
1801 uint32_t name)
1802{
1803 struct wayland_compositor *c = data;
1804 struct wayland_parent_output *output;
1805
1806 wl_list_for_each(output, &c->parent.output_list, link)
1807 if (output->id == name)
1808 wayland_parent_output_destroy(output);
1809}
1810
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001811static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001812 registry_handle_global,
1813 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001814};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001815
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001816static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001817wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1818{
1819 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001820 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001821
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001822 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1823 wl_display_terminate(c->base.wl_display);
1824 return 0;
1825 }
1826
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001827 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001828 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05001829 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001830 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001831
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001832 if (mask == 0) {
1833 count = wl_display_dispatch_pending(c->parent.wl_display);
1834 wl_display_flush(c->parent.wl_display);
1835 }
1836
1837 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001838}
1839
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001840static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001841wayland_restore(struct weston_compositor *ec)
1842{
1843}
1844
1845static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001846wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001847{
Jonas Ådahle5a12252013-04-05 23:07:11 +02001848 struct wayland_compositor *c = (struct wayland_compositor *) ec;
1849
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001850 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001851
Jonas Ådahle5a12252013-04-05 23:07:11 +02001852 if (c->parent.shm)
1853 wl_shm_destroy(c->parent.shm);
1854
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001855 free(ec);
1856}
1857
Jason Ekstrand7744f712013-10-27 22:24:55 -05001858static const char *left_ptrs[] = {
1859 "left_ptr",
1860 "default",
1861 "top_left_arrow",
1862 "left-arrow"
1863};
1864
1865static void
1866create_cursor(struct wayland_compositor *c, struct weston_config *config)
1867{
1868 struct weston_config_section *s;
1869 int size;
1870 char *theme = NULL;
1871 unsigned int i;
1872
1873 s = weston_config_get_section(config, "shell", NULL, NULL);
1874 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1875 weston_config_section_get_int(s, "cursor-size", &size, 32);
1876
1877 c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
Hardening842a36a2014-03-18 14:12:50 +01001878 if (!c->cursor_theme) {
1879 fprintf(stderr, "could not load cursor theme\n");
1880 return;
1881 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001882
U. Artie Eoffff755002014-01-17 12:36:58 -08001883 free(theme);
1884
Jason Ekstrand7744f712013-10-27 22:24:55 -05001885 c->cursor = NULL;
1886 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1887 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
1888 left_ptrs[i]);
1889 if (!c->cursor) {
1890 fprintf(stderr, "could not load left cursor\n");
1891 return;
1892 }
1893}
1894
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001895static void
1896fullscreen_binding(struct weston_seat *seat_base, uint32_t time, uint32_t key,
1897 void *data)
1898{
1899 struct wayland_compositor *c = data;
1900 struct wayland_input *input = NULL;
1901
1902 wl_list_for_each(input, &c->input_list, link)
1903 if (&input->base == seat_base)
1904 break;
1905
1906 if (!input || !input->output)
1907 return;
1908
1909 if (input->output->frame)
1910 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
1911 else
1912 wayland_output_set_windowed(input->output);
1913
1914 weston_output_schedule_repaint(&input->output->base);
1915}
1916
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001917static struct wayland_compositor *
1918wayland_compositor_create(struct wl_display *display, int use_pixman,
1919 const char *display_name, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001920 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001921{
1922 struct wayland_compositor *c;
1923 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001924 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001925
Peter Huttererf3d62272013-08-08 11:57:05 +10001926 c = zalloc(sizeof *c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001927 if (c == NULL)
1928 return NULL;
1929
Daniel Stone725c2c32012-06-22 14:04:36 +01001930 if (weston_compositor_init(&c->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001931 config) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +02001932 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001933
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001934 if (weston_compositor_set_presentation_clock_software(&c->base) < 0)
1935 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001936
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001937 c->parent.wl_display = wl_display_connect(display_name);
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001938 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001939 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001940 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001941 }
1942
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001943 wl_list_init(&c->parent.output_list);
Jason Ekstrand06ced802013-11-07 20:13:29 -06001944 wl_list_init(&c->input_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001945 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1946 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001947 wl_display_roundtrip(c->parent.wl_display);
1948
1949 create_cursor(c, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001950
1951 c->base.wl_display = display;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001952
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001953 c->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001954
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001955 if (!c->use_pixman) {
1956 gl_renderer = weston_load_module("gl-renderer.so",
1957 "gl_renderer_interface");
1958 if (!gl_renderer)
1959 c->use_pixman = 1;
1960 }
1961
1962 if (!c->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001963 if (gl_renderer->create(&c->base, c->parent.wl_display,
1964 gl_renderer->alpha_attribs,
1965 NULL) < 0) {
1966 weston_log("Failed to initialize the GL renderer; "
1967 "falling back to pixman.\n");
1968 c->use_pixman = 1;
1969 }
1970 }
1971
1972 if (c->use_pixman) {
1973 if (pixman_renderer_init(&c->base) < 0) {
1974 weston_log("Failed to initialize pixman renderer\n");
1975 goto err_display;
1976 }
1977 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001978
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001979 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001980 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001981
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001982 loop = wl_display_get_event_loop(c->base.wl_display);
1983
1984 fd = wl_display_get_fd(c->parent.wl_display);
1985 c->parent.wl_source =
1986 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1987 wayland_compositor_handle_event, c);
1988 if (c->parent.wl_source == NULL)
1989 goto err_renderer;
1990
1991 wl_event_source_check(c->parent.wl_source);
1992
1993 return c;
1994err_renderer:
1995 c->base.renderer->destroy(&c->base);
1996err_display:
1997 wl_display_disconnect(c->parent.wl_display);
1998err_compositor:
1999 weston_compositor_shutdown(&c->base);
2000err_free:
2001 free(c);
2002 return NULL;
2003}
2004
2005static void
2006wayland_compositor_destroy(struct wayland_compositor *c)
2007{
U. Artie Eoffa1e887b2014-05-21 09:20:02 -07002008 struct weston_output *output, *next;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002009
U. Artie Eoffa1e887b2014-05-21 09:20:02 -07002010 wl_list_for_each_safe(output, next, &c->base.output_list, link)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002011 wayland_output_destroy(output);
2012
2013 c->base.renderer->destroy(&c->base);
2014 wl_display_disconnect(c->parent.wl_display);
2015
2016 if (c->theme)
2017 theme_destroy(c->theme);
2018 if (c->frame_device)
2019 cairo_device_destroy(c->frame_device);
2020 wl_cursor_theme_destroy(c->cursor_theme);
2021
2022 weston_compositor_shutdown(&c->base);
2023 free(c);
2024}
2025
2026WL_EXPORT struct weston_compositor *
2027backend_init(struct wl_display *display, int *argc, char *argv[],
2028 struct weston_config *config)
2029{
2030 struct wayland_compositor *c;
2031 struct wayland_output *output;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002032 struct wayland_parent_output *poutput;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002033 struct weston_config_section *section;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002034 int x, count, width, height, scale, use_pixman, fullscreen, sprawl;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002035 const char *section_name, *display_name;
2036 char *name;
2037
2038 const struct weston_option wayland_options[] = {
2039 { WESTON_OPTION_INTEGER, "width", 0, &width },
2040 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002041 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002042 { WESTON_OPTION_STRING, "display", 0, &display_name },
2043 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2044 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002045 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002046 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002047 };
2048
2049 width = 0;
2050 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002051 scale = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002052 display_name = NULL;
2053 use_pixman = 0;
2054 count = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002055 fullscreen = 0;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002056 sprawl = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002057 parse_options(wayland_options,
2058 ARRAY_LENGTH(wayland_options), argc, argv);
2059
2060 c = wayland_compositor_create(display, use_pixman, display_name,
2061 argc, argv, config);
2062 if (!c)
2063 return NULL;
2064
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002065 if (sprawl || c->parent.fshell) {
2066 c->sprawl_across_outputs = 1;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002067 wl_display_roundtrip(c->parent.wl_display);
2068
2069 wl_list_for_each(poutput, &c->parent.output_list, link)
2070 wayland_output_create_for_parent_output(c, poutput);
2071
2072 return &c->base;
2073 }
2074
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002075 if (fullscreen) {
2076 output = wayland_output_create(c, 0, 0, width, height,
2077 NULL, 1, 0, 1);
2078 if (!output)
2079 goto err_outputs;
2080
Axel Davydd8b88d2013-11-17 21:34:16 +01002081 wayland_output_set_fullscreen(output, 0, 0, NULL);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002082 return &c->base;
2083 }
2084
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002085 section = NULL;
2086 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002087 while (weston_config_next_section(config, &section, &section_name)) {
2088 if (!section_name || strcmp(section_name, "output") != 0)
2089 continue;
2090 weston_config_section_get_string(section, "name", &name, NULL);
2091 if (name == NULL)
2092 continue;
2093
2094 if (name[0] != 'W' || name[1] != 'L') {
2095 free(name);
2096 continue;
2097 }
2098 free(name);
2099
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002100 output = wayland_output_create_for_config(c, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002101 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002102 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002103 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002104 if (wayland_output_set_windowed(output))
2105 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002106
2107 x += output->base.width;
2108 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002109 }
2110
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002111 if (!width)
2112 width = 1024;
2113 if (!height)
2114 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002115 if (!scale)
2116 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002117 while (count > 0) {
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002118 output = wayland_output_create(c, x, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002119 NULL, 0, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002120 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002121 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002122 if (wayland_output_set_windowed(output))
2123 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002124
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002125 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002126 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002127 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002128
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002129 weston_compositor_add_key_binding(&c->base, KEY_F,
2130 MODIFIER_CTRL | MODIFIER_ALT,
2131 fullscreen_binding, c);
2132
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002133 return &c->base;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002134
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002135err_outputs:
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002136 wayland_compositor_destroy(c);
Martin Olssonc5db50f2012-07-08 03:03:43 +02002137 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002138}