blob: ab7385359749a653f1b58ad72cecf72799f7eeca [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 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -07005 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010012 *
Bryce Harringtona0bbfea2015-06-11 15:35:43 -070013 * The above copyright notice and this permission notice (including the
14 * next paragraph) shall be included in all copies or substantial
15 * portions of the Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 * SOFTWARE.
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010025 */
26
Daniel Stonec228e232013-05-22 18:03:19 +030027#include "config.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010028
29#include <stddef.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010030#include <stdio.h>
31#include <stdlib.h>
32#include <string.h>
33#include <fcntl.h>
34#include <unistd.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010035#include <sys/mman.h>
Jason Ekstrand5ea04802013-11-07 20:13:33 -060036#include <linux/input.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010037
Benjamin Franzkebe014562011-02-18 17:04:24 +010038#include <wayland-client.h>
39#include <wayland-egl.h>
Jason Ekstrand7744f712013-10-27 22:24:55 -050040#include <wayland-cursor.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010041
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010042#include "compositor.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010043#include "gl-renderer.h"
Jason Ekstrandff2fd462013-10-27 22:24:58 -050044#include "pixman-renderer.h"
Jon Cruz35b2eaa2015-06-15 15:37:08 -070045#include "shared/helpers.h"
Jon Cruz4678bab2015-06-15 15:37:07 -070046#include "shared/image-loader.h"
47#include "shared/os-compatibility.h"
48#include "shared/cairo-util.h"
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050049#include "fullscreen-shell-client-protocol.h"
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020050#include "presentation_timing-server-protocol.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010051
Jason Ekstrand48ce4212013-10-27 22:25:02 -050052#define WINDOW_TITLE "Weston Compositor"
53
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010054struct wayland_compositor {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050055 struct weston_compositor base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010056
57 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040058 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040059 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010060 struct wl_compositor *compositor;
61 struct wl_shell *shell;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050062 struct _wl_fullscreen_shell *fshell;
Jonas Ådahle5a12252013-04-05 23:07:11 +020063 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010064
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050065 struct wl_list output_list;
66
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010067 struct wl_event_source *wl_source;
68 uint32_t event_mask;
69 } parent;
70
Jason Ekstrandff2fd462013-10-27 22:24:58 -050071 int use_pixman;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -050072 int sprawl_across_outputs;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050073
Jason Ekstrand7744f712013-10-27 22:24:55 -050074 struct theme *theme;
75 cairo_device_t *frame_device;
76 struct wl_cursor_theme *cursor_theme;
77 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050078
Jason Ekstrand06ced802013-11-07 20:13:29 -060079 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010080};
81
82struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050083 struct weston_output base;
84
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010085 struct {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050086 int draw_initial_frame;
87 struct wl_surface *surface;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050088
89 struct wl_output *output;
90 uint32_t global_id;
91
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050092 struct wl_shell_surface *shell_surface;
Jason Ekstrand5ea04802013-11-07 20:13:33 -060093 int configure_width, configure_height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010094 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050095
Jason Ekstrand7744f712013-10-27 22:24:55 -050096 int keyboard_count;
97
Jason Ekstrand5ea04802013-11-07 20:13:33 -060098 char *name;
Jason Ekstrand7744f712013-10-27 22:24:55 -050099 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500100
Jason Ekstrand7744f712013-10-27 22:24:55 -0500101 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500102 struct wl_egl_window *egl_window;
103 struct {
104 cairo_surface_t *top;
105 cairo_surface_t *left;
106 cairo_surface_t *right;
107 cairo_surface_t *bottom;
108 } border;
109 } gl;
110
111 struct {
112 struct wl_list buffers;
113 struct wl_list free_buffers;
114 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500115
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500116 struct weston_mode mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500117 uint32_t scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100118};
119
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500120struct wayland_parent_output {
121 struct wayland_output *output;
122 struct wl_list link;
123
124 struct wl_output *global;
125 uint32_t id;
126
127 struct {
128 char *make;
129 char *model;
130 int32_t width, height;
131 uint32_t subpixel;
132 } physical;
133
134 int32_t x, y;
135 uint32_t transform;
136 uint32_t scale;
137
138 struct wl_list mode_list;
139 struct weston_mode *preferred_mode;
140 struct weston_mode *current_mode;
141};
142
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500143struct wayland_shm_buffer {
144 struct wayland_output *output;
145 struct wl_list link;
146 struct wl_list free_link;
147
148 struct wl_buffer *buffer;
149 void *data;
150 size_t size;
151 pixman_region32_t damage;
152 int frame_damaged;
153
154 pixman_image_t *pm_image;
155 cairo_surface_t *c_surface;
156};
157
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100158struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400159 struct weston_seat base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100160 struct wayland_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100161 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500162
163 struct {
164 struct wl_seat *seat;
165 struct wl_pointer *pointer;
166 struct wl_keyboard *keyboard;
167 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500168
169 struct {
170 struct wl_surface *surface;
171 int32_t hx, hy;
172 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500173 } parent;
174
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -0500175 enum weston_key_state_update keyboard_state_update;
Daniel Stone50692802012-06-22 13:21:41 +0100176 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400177 uint32_t enter_serial;
178 int focus;
179 struct wayland_output *output;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500180 struct wayland_output *keyboard_focus;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100181};
182
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300183struct gl_renderer_interface *gl_renderer;
184
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500185static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500186wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
187{
188 cairo_surface_destroy(buffer->c_surface);
189 pixman_image_unref(buffer->pm_image);
190
191 wl_buffer_destroy(buffer->buffer);
192 munmap(buffer->data, buffer->size);
193
194 pixman_region32_fini(&buffer->damage);
195
196 wl_list_remove(&buffer->link);
197 wl_list_remove(&buffer->free_link);
198 free(buffer);
199}
200
201static void
202buffer_release(void *data, struct wl_buffer *buffer)
203{
204 struct wayland_shm_buffer *sb = data;
205
206 if (sb->output) {
207 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
208 } else {
209 wayland_shm_buffer_destroy(sb);
210 }
211}
212
213static const struct wl_buffer_listener buffer_listener = {
214 buffer_release
215};
216
217static struct wayland_shm_buffer *
218wayland_output_get_shm_buffer(struct wayland_output *output)
219{
220 struct wayland_compositor *c =
221 (struct wayland_compositor *) output->base.compositor;
222 struct wl_shm *shm = c->parent.shm;
223 struct wayland_shm_buffer *sb;
224
225 struct wl_shm_pool *pool;
226 int width, height, stride;
227 int32_t fx, fy;
228 int fd;
229 unsigned char *data;
230
231 if (!wl_list_empty(&output->shm.free_buffers)) {
232 sb = container_of(output->shm.free_buffers.next,
233 struct wayland_shm_buffer, free_link);
234 wl_list_remove(&sb->free_link);
235 wl_list_init(&sb->free_link);
236
237 return sb;
238 }
239
240 if (output->frame) {
241 width = frame_width(output->frame);
242 height = frame_height(output->frame);
243 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500244 width = output->base.current_mode->width;
245 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500246 }
247
248 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
249
250 fd = os_create_anonymous_file(height * stride);
251 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000252 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500253 return NULL;
254 }
255
256 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
257 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000258 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500259 close(fd);
260 return NULL;
261 }
262
263 sb = zalloc(sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000264 if (sb == NULL) {
Thierry Reding6ac60c12014-05-27 09:08:29 +0200265 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000266 close(fd);
267 free(data);
268 return NULL;
269 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500270
271 sb->output = output;
272 wl_list_init(&sb->free_link);
273 wl_list_insert(&output->shm.buffers, &sb->link);
274
275 pixman_region32_init_rect(&sb->damage, 0, 0,
276 output->base.width, output->base.height);
277 sb->frame_damaged = 1;
278
279 sb->data = data;
280 sb->size = height * stride;
281
282 pool = wl_shm_create_pool(shm, fd, sb->size);
283
284 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
285 width, height,
286 stride,
287 WL_SHM_FORMAT_ARGB8888);
288 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
289 wl_shm_pool_destroy(pool);
290 close(fd);
291
292 memset(data, 0, sb->size);
293
294 sb->c_surface =
295 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
296 width, height, stride);
297
298 fx = 0;
299 fy = 0;
300 if (output->frame)
301 frame_interior(output->frame, &fx, &fy, 0, 0);
302 sb->pm_image =
303 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
304 (uint32_t *)(data + fy * stride) + fx,
305 stride);
306
307 return sb;
308}
309
310static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500311frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400312{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500313 struct weston_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400314 struct timespec ts;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400315
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500316 wl_callback_destroy(callback);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400317
318 /* XXX: use the presentation extension for proper timings */
Pekka Paalanen04f8a9b2015-04-02 16:26:06 +0300319
320 /*
321 * This is the fallback case, where Presentation extension is not
322 * available from the parent compositor. We do not know the base for
323 * 'time', so we cannot feed it to finish_frame(). Do the only thing
324 * we can, and pretend finish_frame time is when we process this
325 * event.
326 */
327 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200328 weston_output_finish_frame(output, &ts, 0);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400329}
330
331static const struct wl_callback_listener frame_listener = {
332 frame_done
333};
334
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500335static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200336draw_initial_frame(struct wayland_output *output)
337{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500338 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200339
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500340 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200341
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500342 /* If we are rendering with GL, then orphan it so that it gets
343 * destroyed immediately */
344 if (output->gl.egl_window)
345 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500346
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500347 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500348 wl_surface_damage(output->parent.surface, 0, 0,
349 output->base.current_mode->width,
350 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200351}
352
353static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500354wayland_output_update_gl_border(struct wayland_output *output)
355{
356 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
357 cairo_t *cr;
358
359 if (!output->frame)
360 return;
361 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
362 return;
363
364 fwidth = frame_width(output->frame);
365 fheight = frame_height(output->frame);
366 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
367
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500368 if (!output->gl.border.top)
369 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500370 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
371 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500372 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500373 frame_repaint(output->frame, cr);
374 cairo_destroy(cr);
375 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
376 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500377 cairo_image_surface_get_stride(output->gl.border.top) / 4,
378 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500379
380
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500381 if (!output->gl.border.left)
382 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500383 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
384 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500385 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500386 cairo_translate(cr, 0, -iy);
387 frame_repaint(output->frame, cr);
388 cairo_destroy(cr);
389 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
390 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500391 cairo_image_surface_get_stride(output->gl.border.left) / 4,
392 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500393
394
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500395 if (!output->gl.border.right)
396 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500397 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
398 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500399 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500400 cairo_translate(cr, -(iwidth + ix), -iy);
401 frame_repaint(output->frame, cr);
402 cairo_destroy(cr);
403 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
404 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500405 cairo_image_surface_get_stride(output->gl.border.right) / 4,
406 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500407
408
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500409 if (!output->gl.border.bottom)
410 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500411 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
412 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500413 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500414 cairo_translate(cr, 0, -(iy + iheight));
415 frame_repaint(output->frame, cr);
416 cairo_destroy(cr);
417 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
418 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500419 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
420 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500421}
422
423static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200424wayland_output_start_repaint_loop(struct weston_output *output_base)
425{
426 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrand286ff682013-10-27 22:24:57 -0500427 struct wayland_compositor *wc =
428 (struct wayland_compositor *)output->base.compositor;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200429 struct wl_callback *callback;
430
431 /* If this is the initial frame, we need to attach a buffer so that
432 * the compositor can map the surface and include it in its render
433 * loop. If the surface doesn't end up in the render loop, the frame
434 * callback won't be invoked. The buffer is transparent and of the
435 * same size as the future real output buffer. */
436 if (output->parent.draw_initial_frame) {
437 output->parent.draw_initial_frame = 0;
438
439 draw_initial_frame(output);
440 }
441
442 callback = wl_surface_frame(output->parent.surface);
443 wl_callback_add_listener(callback, &frame_listener, output);
444 wl_surface_commit(output->parent.surface);
Jason Ekstrand286ff682013-10-27 22:24:57 -0500445 wl_display_flush(wc->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200446}
447
David Herrmann1edf44c2013-10-22 17:11:26 +0200448static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500449wayland_output_repaint_gl(struct weston_output *output_base,
450 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400451{
452 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400453 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400454 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600455
Kristian Høgsberg33418202011-08-16 23:01:28 -0400456 callback = wl_surface_frame(output->parent.surface);
457 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100458
Jason Ekstrand7744f712013-10-27 22:24:55 -0500459 wayland_output_update_gl_border(output);
460
Pekka Paalanenbc106382012-10-10 12:49:31 +0300461 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200462
463 pixman_region32_subtract(&ec->primary_plane.damage,
464 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200465 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100466}
467
Matt Roper361d2ad2011-08-29 13:52:23 -0700468static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500469wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
470{
471 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
472 cairo_t *cr;
473
474 if (!buffer->output->frame || !buffer->frame_damaged)
475 return;
476
477 cr = cairo_create(buffer->c_surface);
478
479 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
480 fwidth = frame_width(buffer->output->frame);
481 fheight = frame_height(buffer->output->frame);
482
483 /* Set the clip so we don't unnecisaraly damage the surface */
484 cairo_move_to(cr, ix, iy);
485 cairo_rel_line_to(cr, iwidth, 0);
486 cairo_rel_line_to(cr, 0, iheight);
487 cairo_rel_line_to(cr, -iwidth, 0);
488 cairo_line_to(cr, ix, iy);
489 cairo_line_to(cr, 0, iy);
490 cairo_line_to(cr, 0, fheight);
491 cairo_line_to(cr, fwidth, fheight);
492 cairo_line_to(cr, fwidth, 0);
493 cairo_line_to(cr, 0, 0);
494 cairo_line_to(cr, 0, iy);
495 cairo_close_path(cr);
496 cairo_clip(cr);
497
498 /* Draw using a pattern so that the final result gets clipped */
499 cairo_push_group(cr);
500 frame_repaint(buffer->output->frame, cr);
501 cairo_pop_group_to_source(cr);
502 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
503 cairo_paint(cr);
504
505 cairo_destroy(cr);
506}
507
508static void
509wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
510{
511 pixman_region32_t damage;
512 pixman_box32_t *rects;
513 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
514 int i, n;
515
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500516 pixman_region32_init(&damage);
517 weston_transformed_region(sb->output->base.width,
518 sb->output->base.height,
519 sb->output->base.transform,
520 sb->output->base.current_scale,
521 &sb->damage, &damage);
522
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500523 if (sb->output->frame) {
524 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
525 fwidth = frame_width(sb->output->frame);
526 fheight = frame_height(sb->output->frame);
527
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500528 pixman_region32_translate(&damage, ix, iy);
529
530 if (sb->frame_damaged) {
531 pixman_region32_union_rect(&damage, &damage,
532 0, 0, fwidth, iy);
533 pixman_region32_union_rect(&damage, &damage,
534 0, iy, ix, iheight);
535 pixman_region32_union_rect(&damage, &damage,
536 ix + iwidth, iy,
537 fwidth - (ix + iwidth), iheight);
538 pixman_region32_union_rect(&damage, &damage,
539 0, iy + iheight,
540 fwidth, fheight - (iy + iheight));
541 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500542 }
543
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500544 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500545 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
546 for (i = 0; i < n; ++i)
547 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
548 rects[i].y1, rects[i].x2 - rects[i].x1,
549 rects[i].y2 - rects[i].y1);
550
551 if (sb->output->frame)
552 pixman_region32_fini(&damage);
553}
554
555static int
556wayland_output_repaint_pixman(struct weston_output *output_base,
557 pixman_region32_t *damage)
558{
559 struct wayland_output *output = (struct wayland_output *) output_base;
560 struct wayland_compositor *c =
561 (struct wayland_compositor *)output->base.compositor;
562 struct wl_callback *callback;
563 struct wayland_shm_buffer *sb;
564
565 if (output->frame) {
566 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
567 wl_list_for_each(sb, &output->shm.buffers, link)
568 sb->frame_damaged = 1;
569 }
570
571 wl_list_for_each(sb, &output->shm.buffers, link)
572 pixman_region32_union(&sb->damage, &sb->damage, damage);
573
574 sb = wayland_output_get_shm_buffer(output);
575
576 wayland_output_update_shm_border(sb);
577 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
578 c->base.renderer->repaint_output(output_base, &sb->damage);
579
580 wayland_shm_buffer_attach(sb);
581
582 callback = wl_surface_frame(output->parent.surface);
583 wl_callback_add_listener(callback, &frame_listener, output);
584 wl_surface_commit(output->parent.surface);
585 wl_display_flush(c->parent.wl_display);
586
587 pixman_region32_fini(&sb->damage);
588 pixman_region32_init(&sb->damage);
589 sb->frame_damaged = 0;
590
591 pixman_region32_subtract(&c->base.primary_plane.damage,
592 &c->base.primary_plane.damage, damage);
593 return 0;
594}
595
596static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500597wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700598{
599 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500600 struct wayland_compositor *c =
601 (struct wayland_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700602
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500603 if (c->use_pixman) {
604 pixman_renderer_output_destroy(output_base);
605 } else {
606 gl_renderer->output_destroy(output_base);
607 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100608
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500609 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500610 wl_surface_destroy(output->parent.surface);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500611 if (output->parent.shell_surface)
612 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500613
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600614 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500615 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600616
617 cairo_surface_destroy(output->gl.border.top);
618 cairo_surface_destroy(output->gl.border.left);
619 cairo_surface_destroy(output->gl.border.right);
620 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500621
622 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700623 free(output);
624
625 return;
626}
627
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300628static const struct wl_shell_surface_listener shell_surface_listener;
629
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200630static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500631wayland_output_init_gl_renderer(struct wayland_output *output)
632{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500633 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500634
635 if (output->frame) {
636 fwidth = frame_width(output->frame);
637 fheight = frame_height(output->frame);
638 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500639 fwidth = output->base.current_mode->width;
640 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500641 }
642
643 output->gl.egl_window =
644 wl_egl_window_create(output->parent.surface,
645 fwidth, fheight);
646 if (!output->gl.egl_window) {
647 weston_log("failure to create wl_egl_window\n");
648 return -1;
649 }
650
651 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000652 output->gl.egl_window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100653 output->gl.egl_window,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000654 gl_renderer->alpha_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500655 NULL,
656 0) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500657 goto cleanup_window;
658
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500659 return 0;
660
661cleanup_window:
662 wl_egl_window_destroy(output->gl.egl_window);
663 return -1;
664}
665
666static int
667wayland_output_init_pixman_renderer(struct wayland_output *output)
668{
669 return pixman_renderer_output_create(&output->base);
670}
671
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600672static void
673wayland_output_resize_surface(struct wayland_output *output)
674{
675 struct wayland_compositor *c =
676 (struct wayland_compositor *)output->base.compositor;
677 struct wayland_shm_buffer *buffer, *next;
678 int32_t ix, iy, iwidth, iheight;
679 int32_t width, height;
680 struct wl_region *region;
681
682 width = output->base.current_mode->width;
683 height = output->base.current_mode->height;
684
685 if (output->frame) {
686 frame_resize_inside(output->frame, width, height);
687
688 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
689 region = wl_compositor_create_region(c->parent.compositor);
690 wl_region_add(region, ix, iy, iwidth, iheight);
691 wl_surface_set_input_region(output->parent.surface, region);
692 wl_region_destroy(region);
693
694 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
695 region = wl_compositor_create_region(c->parent.compositor);
696 wl_region_add(region, ix, iy, iwidth, iheight);
697 wl_surface_set_opaque_region(output->parent.surface, region);
698 wl_region_destroy(region);
699
700 width = frame_width(output->frame);
701 height = frame_height(output->frame);
702 } else {
703 region = wl_compositor_create_region(c->parent.compositor);
704 wl_region_add(region, 0, 0, width, height);
705 wl_surface_set_input_region(output->parent.surface, region);
706 wl_region_destroy(region);
707
708 region = wl_compositor_create_region(c->parent.compositor);
709 wl_region_add(region, 0, 0, width, height);
710 wl_surface_set_opaque_region(output->parent.surface, region);
711 wl_region_destroy(region);
712 }
713
714 if (output->gl.egl_window) {
715 wl_egl_window_resize(output->gl.egl_window,
716 width, height, 0, 0);
717
718 /* These will need to be re-created due to the resize */
719 gl_renderer->output_set_border(&output->base,
720 GL_RENDERER_BORDER_TOP,
721 0, 0, 0, NULL);
722 cairo_surface_destroy(output->gl.border.top);
723 output->gl.border.top = NULL;
724 gl_renderer->output_set_border(&output->base,
725 GL_RENDERER_BORDER_LEFT,
726 0, 0, 0, NULL);
727 cairo_surface_destroy(output->gl.border.left);
728 output->gl.border.left = NULL;
729 gl_renderer->output_set_border(&output->base,
730 GL_RENDERER_BORDER_RIGHT,
731 0, 0, 0, NULL);
732 cairo_surface_destroy(output->gl.border.right);
733 output->gl.border.right = NULL;
734 gl_renderer->output_set_border(&output->base,
735 GL_RENDERER_BORDER_BOTTOM,
736 0, 0, 0, NULL);
737 cairo_surface_destroy(output->gl.border.bottom);
738 output->gl.border.bottom = NULL;
739 }
740
741 /* Throw away any remaining SHM buffers */
742 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, link)
743 wayland_shm_buffer_destroy(buffer);
744 /* These will get thrown away when they get released */
745 wl_list_for_each(buffer, &output->shm.buffers, link)
746 buffer->output = NULL;
747}
748
749static int
750wayland_output_set_windowed(struct wayland_output *output)
751{
752 struct wayland_compositor *c =
753 (struct wayland_compositor *)output->base.compositor;
754 int tlen;
755 char *title;
756
757 if (output->frame)
758 return 0;
759
760 if (output->name) {
761 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
762 title = malloc(tlen + 1);
763 if (!title)
764 return -1;
765
766 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
767 } else {
768 title = strdup(WINDOW_TITLE);
769 }
770
771 if (!c->theme) {
772 c->theme = theme_create();
773 if (!c->theme) {
774 free(title);
775 return -1;
776 }
777 }
778 output->frame = frame_create(c->theme, 100, 100,
779 FRAME_BUTTON_CLOSE, title);
780 free(title);
781 if (!output->frame)
782 return -1;
783
784 if (output->keyboard_count)
785 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
786
787 wayland_output_resize_surface(output);
788
789 wl_shell_surface_set_toplevel(output->parent.shell_surface);
790
791 return 0;
792}
793
794static void
795wayland_output_set_fullscreen(struct wayland_output *output,
796 enum wl_shell_surface_fullscreen_method method,
797 uint32_t framerate, struct wl_output *target)
798{
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500799 struct wayland_compositor *c =
800 (struct wayland_compositor *)output->base.compositor;
801
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600802 if (output->frame) {
803 frame_destroy(output->frame);
804 output->frame = NULL;
805 }
806
807 wayland_output_resize_surface(output);
808
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500809 if (output->parent.shell_surface) {
810 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
811 method, framerate, target);
812 } else if (c->parent.fshell) {
813 _wl_fullscreen_shell_present_surface(c->parent.fshell,
814 output->parent.surface,
815 method, target);
816 }
817}
818
819static struct weston_mode *
820wayland_output_choose_mode(struct wayland_output *output,
821 struct weston_mode *ref_mode)
822{
823 struct weston_mode *mode;
824
825 /* First look for an exact match */
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 mode->refresh == ref_mode->refresh)
830 return mode;
831
832 /* If we can't find an exact match, ignore refresh and try again */
833 wl_list_for_each(mode, &output->base.mode_list, link)
834 if (mode->width == ref_mode->width &&
835 mode->height == ref_mode->height)
836 return mode;
837
838 /* Yeah, we failed */
839 return NULL;
840}
841
842enum mode_status {
843 MODE_STATUS_UNKNOWN,
844 MODE_STATUS_SUCCESS,
845 MODE_STATUS_FAIL,
846 MODE_STATUS_CANCEL,
847};
848
849static void
850mode_feedback_successful(void *data,
851 struct _wl_fullscreen_shell_mode_feedback *fb)
852{
853 enum mode_status *value = data;
854
855 printf("Mode switch successful\n");
856
857 *value = MODE_STATUS_SUCCESS;
858}
859
860static void
861mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
862{
863 enum mode_status *value = data;
864
865 printf("Mode switch failed\n");
866
867 *value = MODE_STATUS_FAIL;
868}
869
870static void
871mode_feedback_cancelled(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
872{
873 enum mode_status *value = data;
874
875 printf("Mode switch cancelled\n");
876
877 *value = MODE_STATUS_CANCEL;
878}
879
880struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
881 mode_feedback_successful,
882 mode_feedback_failed,
883 mode_feedback_cancelled,
884};
885
886static int
887wayland_output_switch_mode(struct weston_output *output_base,
888 struct weston_mode *mode)
889{
890 struct wayland_output *output = (struct wayland_output *) output_base;
891 struct wayland_compositor *c;
892 struct wl_surface *old_surface;
893 struct weston_mode *old_mode;
894 struct _wl_fullscreen_shell_mode_feedback *mode_feedback;
895 enum mode_status mode_status;
896 int ret = 0;
897
898 if (output_base == NULL) {
899 weston_log("output is NULL.\n");
900 return -1;
901 }
902
903 if (mode == NULL) {
904 weston_log("mode is NULL.\n");
905 return -1;
906 }
907
908 c = (struct wayland_compositor *)output_base->compositor;
909
910 if (output->parent.shell_surface || !c->parent.fshell)
911 return -1;
912
913 mode = wayland_output_choose_mode(output, mode);
914 if (mode == NULL)
915 return -1;
916
917 if (output->base.current_mode == mode)
918 return 0;
919
920 old_mode = output->base.current_mode;
921 old_surface = output->parent.surface;
922 output->base.current_mode = mode;
923 output->parent.surface =
924 wl_compositor_create_surface(c->parent.compositor);
925 wl_surface_set_user_data(output->parent.surface, output);
926
927 /* Blow the old buffers because we changed size/surfaces */
928 wayland_output_resize_surface(output);
929
930 mode_feedback =
931 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
932 output->parent.surface,
933 output->parent.output,
934 mode->refresh);
935 _wl_fullscreen_shell_mode_feedback_add_listener(mode_feedback,
936 &mode_feedback_listener,
937 &mode_status);
938
939 /* This should kick-start things again */
940 output->parent.draw_initial_frame = 1;
941 wayland_output_start_repaint_loop(&output->base);
942
943 mode_status = MODE_STATUS_UNKNOWN;
944 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
945 ret = wl_display_dispatch(c->parent.wl_display);
946
947 _wl_fullscreen_shell_mode_feedback_destroy(mode_feedback);
948
949 if (mode_status == MODE_STATUS_FAIL) {
950 output->base.current_mode = old_mode;
951 wl_surface_destroy(output->parent.surface);
952 output->parent.surface = old_surface;
953 wayland_output_resize_surface(output);
954
955 return -1;
956 }
957
958 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
959 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
960
961 if (c->use_pixman) {
962 pixman_renderer_output_destroy(output_base);
963 if (wayland_output_init_pixman_renderer(output) < 0)
964 goto err_output;
965 } else {
966 gl_renderer->output_destroy(output_base);
967 wl_egl_window_destroy(output->gl.egl_window);
968 if (wayland_output_init_gl_renderer(output) < 0)
969 goto err_output;
970 }
971 wl_surface_destroy(old_surface);
972
973 weston_output_schedule_repaint(&output->base);
974
975 return 0;
976
977err_output:
978 /* XXX */
979 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600980}
981
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500982static struct wayland_output *
983wayland_output_create(struct wayland_compositor *c, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600984 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500985 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100986{
987 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500988 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600989
990 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
991 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100992
Peter Huttererf3d62272013-08-08 11:57:05 +1000993 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100994 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500995 return NULL;
996
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600997 output->name = name ? strdup(name) : NULL;
998 output->base.make = "waywayland";
999 output->base.model = "none";
1000
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001001 output_width = width * scale;
1002 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001003
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001004 output->parent.surface =
1005 wl_compositor_create_surface(c->parent.compositor);
1006 if (!output->parent.surface)
1007 goto err_name;
1008 wl_surface_set_user_data(output->parent.surface, output);
1009
1010 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001011
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001012 if (c->parent.shell) {
1013 output->parent.shell_surface =
1014 wl_shell_get_shell_surface(c->parent.shell,
1015 output->parent.surface);
1016 if (!output->parent.shell_surface)
1017 goto err_surface;
1018 wl_shell_surface_add_listener(output->parent.shell_surface,
1019 &shell_surface_listener, output);
1020 }
1021
1022 if (fullscreen && c->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001023 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1024 0, 0, NULL);
1025 wl_display_roundtrip(c->parent.wl_display);
1026 if (!width)
1027 output_width = output->parent.configure_width;
1028 if (!height)
1029 output_height = output->parent.configure_height;
1030 }
1031
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001032 output->mode.flags =
1033 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001034 output->mode.width = output_width;
1035 output->mode.height = output_height;
1036 output->mode.refresh = 60000;
1037 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001038 wl_list_init(&output->base.mode_list);
1039 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001040 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001041
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001042 wl_list_init(&output->shm.buffers);
1043 wl_list_init(&output->shm.free_buffers);
1044
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001045 weston_output_init(&output->base, &c->base, x, y, width, height,
1046 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001047
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001048 if (c->use_pixman) {
1049 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001050 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001051 output->base.repaint = wayland_output_repaint_pixman;
1052 } else {
1053 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001054 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001055 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001056 }
1057
Jonas Ådahle5a12252013-04-05 23:07:11 +02001058 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001059 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001060 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001061 output->base.set_backlight = NULL;
1062 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001063 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001064
Giulio Camuffob1147152015-05-06 21:41:57 +03001065 weston_compositor_add_output(&c->base, &output->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001066
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001067 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001068
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001069err_output:
1070 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001071 if (output->parent.shell_surface)
1072 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001073err_surface:
1074 wl_surface_destroy(output->parent.surface);
1075err_name:
1076 free(output->name);
1077
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001078 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001079 free(output);
1080
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001081 return NULL;
1082}
1083
1084static struct wayland_output *
1085wayland_output_create_for_config(struct wayland_compositor *c,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001086 struct weston_config_section *config_section,
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001087 int option_width, int option_height,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001088 int option_scale, int32_t x, int32_t y)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001089{
1090 struct wayland_output *output;
1091 char *mode, *t, *name, *str;
1092 int width, height, scale;
1093 uint32_t transform;
Derek Foreman64a3df02014-10-23 12:24:18 -05001094 unsigned int slen;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001095
1096 weston_config_section_get_string(config_section, "name", &name, NULL);
1097 if (name) {
1098 slen = strlen(name);
1099 slen += strlen(WINDOW_TITLE " - ");
1100 str = malloc(slen + 1);
1101 if (str)
1102 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
1103 free(name);
1104 name = str;
1105 }
1106 if (!name)
U. Artie Eoff1a08d112014-01-17 12:22:50 -08001107 name = strdup(WINDOW_TITLE);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001108
1109 weston_config_section_get_string(config_section,
1110 "mode", &mode, "1024x600");
1111 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1112 weston_log("Invalid mode \"%s\" for output %s\n",
1113 mode, name);
1114 width = 1024;
1115 height = 640;
1116 }
1117 free(mode);
1118
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001119 if (option_width)
1120 width = option_width;
1121 if (option_height)
1122 height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001123
1124 weston_config_section_get_int(config_section, "scale", &scale, 1);
1125
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001126 if (option_scale)
1127 scale = option_scale;
1128
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001129 weston_config_section_get_string(config_section,
1130 "transform", &t, "normal");
Derek Foreman64a3df02014-10-23 12:24:18 -05001131 if (weston_parse_transform(t, &transform) < 0)
1132 weston_log("Invalid transform \"%s\" for output %s\n",
1133 t, name);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001134 free(t);
1135
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001136 output = wayland_output_create(c, x, y, width, height, name, 0,
1137 transform, scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001138 free(name);
1139
1140 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001141}
1142
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001143static struct wayland_output *
1144wayland_output_create_for_parent_output(struct wayland_compositor *c,
1145 struct wayland_parent_output *poutput)
1146{
1147 struct wayland_output *output;
1148 struct weston_mode *mode;
1149 int32_t x;
1150
1151 if (poutput->current_mode) {
1152 mode = poutput->current_mode;
1153 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001154 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001155 } else if (!wl_list_empty(&poutput->mode_list)) {
1156 mode = container_of(poutput->mode_list.next,
1157 struct weston_mode, link);
1158 } else {
1159 weston_log("No valid modes found. Skipping output");
1160 return NULL;
1161 }
1162
1163 if (!wl_list_empty(&c->base.output_list)) {
1164 output = container_of(c->base.output_list.prev,
1165 struct wayland_output, base.link);
1166 x = output->base.x + output->base.current_mode->width;
1167 } else {
1168 x = 0;
1169 }
1170
1171 output = wayland_output_create(c, x, 0, mode->width, mode->height,
1172 NULL, 0,
1173 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1174 if (!output)
1175 return NULL;
1176
1177 output->parent.output = poutput->global;
1178
1179 output->base.make = poutput->physical.make;
1180 output->base.model = poutput->physical.model;
1181 wl_list_init(&output->base.mode_list);
1182 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1183 wl_list_init(&poutput->mode_list);
1184
1185 wayland_output_set_fullscreen(output,
1186 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1187 mode->refresh, poutput->global);
1188
1189 if (output->parent.shell_surface) {
1190 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1191 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1192 mode->refresh, poutput->global);
1193 } else if (c->parent.fshell) {
1194 _wl_fullscreen_shell_present_surface(c->parent.fshell,
1195 output->parent.surface,
1196 _WL_FULLSCREEN_SHELL_PRESENT_METHOD_CENTER,
1197 poutput->global);
1198 _wl_fullscreen_shell_mode_feedback_destroy(
1199 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
1200 output->parent.surface,
1201 poutput->global,
1202 mode->refresh));
1203 }
1204
1205 return output;
1206}
1207
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001208static void
1209shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1210 uint32_t serial)
1211{
1212 wl_shell_surface_pong(shell_surface, serial);
1213}
1214
1215static void
1216shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1217 uint32_t edges, int32_t width, int32_t height)
1218{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001219 struct wayland_output *output = data;
1220
1221 output->parent.configure_width = width;
1222 output->parent.configure_height = height;
1223
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001224 /* FIXME: implement resizing */
1225}
1226
1227static void
1228shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1229{
1230}
1231
1232static const struct wl_shell_surface_listener shell_surface_listener = {
1233 shell_surface_ping,
1234 shell_surface_configure,
1235 shell_surface_popup_done
1236};
1237
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001238/* Events received from the wayland-server this compositor is client of: */
1239
Jason Ekstrand7744f712013-10-27 22:24:55 -05001240/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001241static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001242input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001243{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001244
Jason Ekstrand7744f712013-10-27 22:24:55 -05001245 struct wl_buffer *buffer;
1246 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001247
Jason Ekstrand7744f712013-10-27 22:24:55 -05001248 if (!input->compositor->cursor)
1249 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001250
Jason Ekstrand7744f712013-10-27 22:24:55 -05001251 image = input->compositor->cursor->images[0];
1252 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001253 if (!buffer)
1254 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001255
1256 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1257 input->parent.cursor.surface,
1258 image->hotspot_x, image->hotspot_y);
1259
1260 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1261 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1262 image->width, image->height);
1263 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001264}
1265
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001266static void
Daniel Stone37816df2012-05-16 18:45:18 +01001267input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1268 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001269 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001270{
1271 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001272 int32_t fx, fy;
1273 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001274
Daniel Stone50692802012-06-22 13:21:41 +01001275 /* XXX: If we get a modifier event immediately before the focus,
1276 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001277 input->enter_serial = serial;
1278 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001279
1280 if (input->output->frame) {
1281 location = frame_pointer_enter(input->output->frame, input,
1282 wl_fixed_to_int(x),
1283 wl_fixed_to_int(y));
1284 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1285 x -= wl_fixed_from_int(fx);
1286 y -= wl_fixed_from_int(fy);
1287
1288 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1289 weston_output_schedule_repaint(&input->output->base);
1290 } else {
1291 location = THEME_LOCATION_CLIENT_AREA;
1292 }
1293
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001294 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001295
1296 if (location == THEME_LOCATION_CLIENT_AREA) {
1297 input->focus = 1;
1298 notify_pointer_focus(&input->base, &input->output->base, x, y);
1299 wl_pointer_set_cursor(input->parent.pointer,
1300 input->enter_serial, NULL, 0, 0);
1301 } else {
1302 input->focus = 0;
1303 notify_pointer_focus(&input->base, NULL, 0, 0);
1304 input_set_cursor(input);
1305 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001306}
1307
1308static void
Daniel Stone37816df2012-05-16 18:45:18 +01001309input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1310 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001311{
1312 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001313
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001314 if (!input->output)
1315 return;
1316
Jason Ekstrand7744f712013-10-27 22:24:55 -05001317 if (input->output->frame) {
1318 frame_pointer_leave(input->output->frame, input);
1319
1320 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1321 weston_output_schedule_repaint(&input->output->base);
1322 }
1323
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001324 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001325 input->output = NULL;
1326 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001327}
1328
1329static void
Daniel Stone37816df2012-05-16 18:45:18 +01001330input_handle_motion(void *data, struct wl_pointer *pointer,
1331 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1332{
1333 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001334 int32_t fx, fy;
1335 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001336
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001337 if (!input->output)
1338 return;
1339
Jason Ekstrand7744f712013-10-27 22:24:55 -05001340 if (input->output->frame) {
1341 location = frame_pointer_motion(input->output->frame, input,
1342 wl_fixed_to_int(x),
1343 wl_fixed_to_int(y));
1344 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1345 x -= wl_fixed_from_int(fx);
1346 y -= wl_fixed_from_int(fy);
1347
1348 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1349 weston_output_schedule_repaint(&input->output->base);
1350 } else {
1351 location = THEME_LOCATION_CLIENT_AREA;
1352 }
1353
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001354 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001355
1356 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
1357 input_set_cursor(input);
1358 notify_pointer_focus(&input->base, NULL, 0, 0);
1359 input->focus = 0;
1360 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
1361 wl_pointer_set_cursor(input->parent.pointer,
1362 input->enter_serial, NULL, 0, 0);
1363 notify_pointer_focus(&input->base, &input->output->base, x, y);
1364 input->focus = 1;
1365 }
1366
1367 if (location == THEME_LOCATION_CLIENT_AREA)
1368 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +01001369}
1370
1371static void
1372input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001373 uint32_t serial, uint32_t time, uint32_t button,
1374 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001375{
1376 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001377 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001378 enum frame_button_state fstate;
1379 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001380
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001381 if (!input->output)
1382 return;
1383
Jason Ekstrand7744f712013-10-27 22:24:55 -05001384 if (input->output->frame) {
1385 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1386 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1387
1388 location = frame_pointer_button(input->output->frame, input,
1389 button, fstate);
1390
1391 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1392
1393 wl_shell_surface_move(input->output->parent.shell_surface,
1394 input->parent.seat, serial);
1395 frame_status_clear(input->output->frame,
1396 FRAME_STATUS_MOVE);
1397 return;
1398 }
1399
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001400 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1401 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001402 input->output = NULL;
1403 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001404
1405 if (wl_list_empty(&input->compositor->base.output_list))
1406 wl_display_terminate(input->compositor->base.wl_display);
1407
1408 return;
1409 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001410
1411 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1412 weston_output_schedule_repaint(&input->output->base);
1413 } else {
1414 location = THEME_LOCATION_CLIENT_AREA;
1415 }
1416
1417 if (location == THEME_LOCATION_CLIENT_AREA)
1418 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +01001419}
1420
1421static void
1422input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001423 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001424{
1425 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001426
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001427 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +01001428}
1429
1430static const struct wl_pointer_listener pointer_listener = {
1431 input_handle_pointer_enter,
1432 input_handle_pointer_leave,
1433 input_handle_motion,
1434 input_handle_button,
1435 input_handle_axis,
1436};
1437
1438static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001439input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1440 int fd, uint32_t size)
1441{
1442 struct wayland_input *input = data;
1443 struct xkb_keymap *keymap;
1444 char *map_str;
1445
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001446 if (!data) {
1447 close(fd);
1448 return;
1449 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001450
1451 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1452 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1453 if (map_str == MAP_FAILED) {
1454 weston_log("mmap failed: %m\n");
1455 goto error;
1456 }
1457
Ran Benita2e1968f2014-08-19 23:59:51 +03001458 keymap = xkb_keymap_new_from_string(input->compositor->base.xkb_context,
1459 map_str,
1460 XKB_KEYMAP_FORMAT_TEXT_V1,
1461 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001462 munmap(map_str, size);
1463
1464 if (!keymap) {
1465 weston_log("failed to compile keymap\n");
1466 goto error;
1467 }
1468
1469 input->keyboard_state_update = STATE_UPDATE_NONE;
1470 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1471 weston_log("No keymap provided; falling back to defalt\n");
1472 keymap = NULL;
1473 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1474 } else {
1475 weston_log("Invalid keymap\n");
1476 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001477 }
1478
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001479 close(fd);
1480
Rui Matos0c194ce2013-10-10 19:44:21 +02001481 if (input->base.keyboard)
1482 weston_seat_update_keymap(&input->base, keymap);
1483 else
1484 weston_seat_init_keyboard(&input->base, keymap);
1485
Ran Benitac9c74152014-08-19 23:59:52 +03001486 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001487
1488 return;
1489
1490error:
1491 wl_keyboard_release(input->parent.keyboard);
1492 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001493}
1494
1495static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001496input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001497 struct wl_keyboard *keyboard,
1498 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001499 struct wl_surface *surface,
1500 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001501{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001502 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001503 struct wayland_output *focus;
1504
1505 focus = input->keyboard_focus;
1506 if (focus) {
1507 /* This shouldn't happen */
1508 focus->keyboard_count--;
1509 if (!focus->keyboard_count && focus->frame)
1510 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1511 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1512 weston_output_schedule_repaint(&focus->base);
1513 }
1514
1515 input->keyboard_focus = wl_surface_get_user_data(surface);
1516 input->keyboard_focus->keyboard_count++;
1517
1518 focus = input->keyboard_focus;
1519 if (focus->frame) {
1520 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1521 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1522 weston_output_schedule_repaint(&focus->base);
1523 }
1524
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001525
Daniel Stone50692802012-06-22 13:21:41 +01001526 /* XXX: If we get a modifier event immediately before the focus,
1527 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001528 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001529 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001530}
1531
1532static void
1533input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001534 struct wl_keyboard *keyboard,
1535 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001536 struct wl_surface *surface)
1537{
1538 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001539 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001540
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001541 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001542
1543 focus = input->keyboard_focus;
1544 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001545 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001546
1547 focus->keyboard_count--;
1548 if (!focus->keyboard_count && focus->frame) {
1549 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1550 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1551 weston_output_schedule_repaint(&focus->base);
1552 }
1553
1554 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001555}
1556
Daniel Stone37816df2012-05-16 18:45:18 +01001557static void
1558input_handle_key(void *data, struct wl_keyboard *keyboard,
1559 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1560{
1561 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001562
Daniel Stone50692802012-06-22 13:21:41 +01001563 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001564 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001565 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001566 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001567 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001568}
1569
Daniel Stone351eb612012-05-31 15:27:47 -04001570static void
1571input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001572 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001573 uint32_t mods_latched, uint32_t mods_locked,
1574 uint32_t group)
1575{
Daniel Stone50692802012-06-22 13:21:41 +01001576 struct wayland_input *input = data;
1577 struct wayland_compositor *c = input->compositor;
1578 uint32_t serial_out;
1579
1580 /* If we get a key event followed by a modifier event with the
1581 * same serial number, then we try to preserve those semantics by
1582 * reusing the same serial number on the way out too. */
1583 if (serial_in == input->key_serial)
1584 serial_out = wl_display_get_serial(c->base.wl_display);
1585 else
1586 serial_out = wl_display_next_serial(c->base.wl_display);
1587
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001588 xkb_state_update_mask(input->base.keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001589 mods_depressed, mods_latched,
1590 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001591 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001592}
1593
Jonny Lamb497994a2014-08-12 14:58:26 +02001594static void
1595input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1596 int32_t rate, int32_t delay)
1597{
1598 struct wayland_input *input = data;
1599 struct wayland_compositor *c = input->compositor;
1600
1601 c->base.kb_repeat_rate = rate;
1602 c->base.kb_repeat_delay = delay;
1603}
1604
Daniel Stone37816df2012-05-16 18:45:18 +01001605static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001606 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001607 input_handle_keyboard_enter,
1608 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001609 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001610 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001611 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001612};
1613
1614static void
Daniel Stone37816df2012-05-16 18:45:18 +01001615input_handle_capabilities(void *data, struct wl_seat *seat,
1616 enum wl_seat_capability caps)
1617{
1618 struct wayland_input *input = data;
1619
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001620 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1621 input->parent.pointer = wl_seat_get_pointer(seat);
1622 wl_pointer_set_user_data(input->parent.pointer, input);
1623 wl_pointer_add_listener(input->parent.pointer,
1624 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001625 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001626 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1627 wl_pointer_destroy(input->parent.pointer);
1628 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001629 }
1630
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001631 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1632 input->parent.keyboard = wl_seat_get_keyboard(seat);
1633 wl_keyboard_set_user_data(input->parent.keyboard, input);
1634 wl_keyboard_add_listener(input->parent.keyboard,
1635 &keyboard_listener, input);
1636 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1637 wl_keyboard_destroy(input->parent.keyboard);
1638 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001639 }
1640}
1641
Jonny Lamb497994a2014-08-12 14:58:26 +02001642static void
1643input_handle_name(void *data, struct wl_seat *seat,
1644 const char *name)
1645{
1646}
1647
Daniel Stone37816df2012-05-16 18:45:18 +01001648static const struct wl_seat_listener seat_listener = {
1649 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001650 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001651};
1652
1653static void
Jonny Lamb497994a2014-08-12 14:58:26 +02001654display_add_seat(struct wayland_compositor *c, uint32_t id, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001655{
1656 struct wayland_input *input;
1657
Peter Huttererf3d62272013-08-08 11:57:05 +10001658 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001659 if (input == NULL)
1660 return;
1661
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001662 weston_seat_init(&input->base, &c->base, "default");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001663 input->compositor = c;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001664 input->parent.seat = wl_registry_bind(c->parent.registry, id,
Jonny Lamb497994a2014-08-12 14:58:26 +02001665 &wl_seat_interface, MIN(version, 4));
Jason Ekstrand06ced802013-11-07 20:13:29 -06001666 wl_list_insert(c->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001667
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001668 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1669 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001670
1671 input->parent.cursor.surface =
1672 wl_compositor_create_surface(c->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001673}
1674
1675static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001676wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1677 int32_t x, int32_t y,
1678 int32_t physical_width, int32_t physical_height,
1679 int32_t subpixel, const char *make,
1680 const char *model, int32_t transform)
1681{
1682 struct wayland_parent_output *output = data;
1683
1684 output->x = x;
1685 output->y = y;
1686 output->physical.width = physical_width;
1687 output->physical.height = physical_height;
1688 output->physical.subpixel = subpixel;
1689
1690 free(output->physical.make);
1691 output->physical.make = strdup(make);
1692 free(output->physical.model);
1693 output->physical.model = strdup(model);
1694
1695 output->transform = transform;
1696}
1697
1698static struct weston_mode *
1699find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1700{
1701 struct weston_mode *mode;
1702
1703 wl_list_for_each(mode, list, link) {
1704 if (mode->width == width && mode->height == height &&
1705 mode->refresh == refresh)
1706 return mode;
1707 }
1708
1709 mode = zalloc(sizeof *mode);
1710 if (!mode)
1711 return NULL;
1712
1713 mode->width = width;
1714 mode->height = height;
1715 mode->refresh = refresh;
1716 wl_list_insert(list, &mode->link);
1717
1718 return mode;
1719}
1720
1721static void
1722wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1723 uint32_t flags, int32_t width, int32_t height,
1724 int32_t refresh)
1725{
1726 struct wayland_parent_output *output = data;
1727 struct weston_mode *mode;
1728
1729 if (output->output) {
1730 mode = find_mode(&output->output->base.mode_list,
1731 width, height, refresh);
1732 if (!mode)
1733 return;
1734 mode->flags = flags;
1735 /* Do a mode-switch on current mode change? */
1736 } else {
1737 mode = find_mode(&output->mode_list, width, height, refresh);
1738 if (!mode)
1739 return;
1740 mode->flags = flags;
1741 if (flags & WL_OUTPUT_MODE_CURRENT)
1742 output->current_mode = mode;
1743 if (flags & WL_OUTPUT_MODE_PREFERRED)
1744 output->preferred_mode = mode;
1745 }
1746}
1747
1748static const struct wl_output_listener output_listener = {
1749 wayland_parent_output_geometry,
1750 wayland_parent_output_mode
1751};
1752
1753static void
1754wayland_compositor_register_output(struct wayland_compositor *c, uint32_t id)
1755{
1756 struct wayland_parent_output *output;
1757
1758 output = zalloc(sizeof *output);
1759 if (!output)
1760 return;
1761
1762 output->id = id;
1763 output->global = wl_registry_bind(c->parent.registry, id,
1764 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001765 if (!output->global) {
1766 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001767 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001768 }
1769
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001770 wl_output_add_listener(output->global, &output_listener, output);
1771
1772 output->scale = 0;
1773 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1774 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
1775 wl_list_init(&output->mode_list);
1776 wl_list_insert(&c->parent.output_list, &output->link);
1777
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05001778 if (c->sprawl_across_outputs) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001779 wl_display_roundtrip(c->parent.wl_display);
1780 wayland_output_create_for_parent_output(c, output);
1781 }
1782}
1783
1784static void
1785wayland_parent_output_destroy(struct wayland_parent_output *output)
1786{
1787 struct weston_mode *mode, *next;
1788
1789 if (output->output)
1790 wayland_output_destroy(&output->output->base);
1791
1792 wl_output_destroy(output->global);
1793 free(output->physical.make);
1794 free(output->physical.model);
1795
1796 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
1797 wl_list_remove(&mode->link);
1798 free(mode);
1799 }
1800}
1801
1802static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001803registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1804 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001805{
1806 struct wayland_compositor *c = data;
1807
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001808 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001809 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001810 wl_registry_bind(registry, name,
1811 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001812 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001813 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001814 wl_registry_bind(registry, name,
1815 &wl_shell_interface, 1);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001816 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
1817 c->parent.fshell =
1818 wl_registry_bind(registry, name,
1819 &_wl_fullscreen_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01001820 } else if (strcmp(interface, "wl_seat") == 0) {
Jonny Lamb497994a2014-08-12 14:58:26 +02001821 display_add_seat(c, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001822 } else if (strcmp(interface, "wl_output") == 0) {
1823 wayland_compositor_register_output(c, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001824 } else if (strcmp(interface, "wl_shm") == 0) {
1825 c->parent.shm =
1826 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001827 }
1828}
1829
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001830static void
1831registry_handle_global_remove(void *data, struct wl_registry *registry,
1832 uint32_t name)
1833{
1834 struct wayland_compositor *c = data;
1835 struct wayland_parent_output *output;
1836
1837 wl_list_for_each(output, &c->parent.output_list, link)
1838 if (output->id == name)
1839 wayland_parent_output_destroy(output);
1840}
1841
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001842static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001843 registry_handle_global,
1844 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001845};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001846
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001847static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001848wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1849{
1850 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001851 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001852
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001853 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1854 wl_display_terminate(c->base.wl_display);
1855 return 0;
1856 }
1857
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001858 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001859 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05001860 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001861 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001862
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001863 if (mask == 0) {
1864 count = wl_display_dispatch_pending(c->parent.wl_display);
1865 wl_display_flush(c->parent.wl_display);
1866 }
1867
1868 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001869}
1870
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001871static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001872wayland_restore(struct weston_compositor *ec)
1873{
1874}
1875
1876static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001877wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001878{
Jonas Ådahle5a12252013-04-05 23:07:11 +02001879 struct wayland_compositor *c = (struct wayland_compositor *) ec;
1880
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001881 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001882
Jonas Ådahle5a12252013-04-05 23:07:11 +02001883 if (c->parent.shm)
1884 wl_shm_destroy(c->parent.shm);
1885
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001886 free(ec);
1887}
1888
Jason Ekstrand7744f712013-10-27 22:24:55 -05001889static const char *left_ptrs[] = {
1890 "left_ptr",
1891 "default",
1892 "top_left_arrow",
1893 "left-arrow"
1894};
1895
1896static void
1897create_cursor(struct wayland_compositor *c, struct weston_config *config)
1898{
1899 struct weston_config_section *s;
1900 int size;
1901 char *theme = NULL;
1902 unsigned int i;
1903
1904 s = weston_config_get_section(config, "shell", NULL, NULL);
1905 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1906 weston_config_section_get_int(s, "cursor-size", &size, 32);
1907
1908 c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
Hardening842a36a2014-03-18 14:12:50 +01001909 if (!c->cursor_theme) {
1910 fprintf(stderr, "could not load cursor theme\n");
1911 return;
1912 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001913
U. Artie Eoffff755002014-01-17 12:36:58 -08001914 free(theme);
1915
Jason Ekstrand7744f712013-10-27 22:24:55 -05001916 c->cursor = NULL;
1917 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1918 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
1919 left_ptrs[i]);
1920 if (!c->cursor) {
1921 fprintf(stderr, "could not load left cursor\n");
1922 return;
1923 }
1924}
1925
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001926static void
1927fullscreen_binding(struct weston_seat *seat_base, uint32_t time, uint32_t key,
1928 void *data)
1929{
1930 struct wayland_compositor *c = data;
1931 struct wayland_input *input = NULL;
1932
1933 wl_list_for_each(input, &c->input_list, link)
1934 if (&input->base == seat_base)
1935 break;
1936
1937 if (!input || !input->output)
1938 return;
1939
1940 if (input->output->frame)
1941 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
1942 else
1943 wayland_output_set_windowed(input->output);
1944
1945 weston_output_schedule_repaint(&input->output->base);
1946}
1947
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001948static struct wayland_compositor *
1949wayland_compositor_create(struct wl_display *display, int use_pixman,
1950 const char *display_name, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001951 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001952{
1953 struct wayland_compositor *c;
1954 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001955 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001956
Peter Huttererf3d62272013-08-08 11:57:05 +10001957 c = zalloc(sizeof *c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001958 if (c == NULL)
1959 return NULL;
1960
Daniel Stone725c2c32012-06-22 14:04:36 +01001961 if (weston_compositor_init(&c->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001962 config) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +02001963 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001964
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001965 if (weston_compositor_set_presentation_clock_software(&c->base) < 0)
1966 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001967
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001968 c->parent.wl_display = wl_display_connect(display_name);
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001969 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001970 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001971 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001972 }
1973
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001974 wl_list_init(&c->parent.output_list);
Jason Ekstrand06ced802013-11-07 20:13:29 -06001975 wl_list_init(&c->input_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001976 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1977 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001978 wl_display_roundtrip(c->parent.wl_display);
1979
1980 create_cursor(c, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001981
1982 c->base.wl_display = display;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001983
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001984 c->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001985
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001986 if (!c->use_pixman) {
1987 gl_renderer = weston_load_module("gl-renderer.so",
1988 "gl_renderer_interface");
1989 if (!gl_renderer)
1990 c->use_pixman = 1;
1991 }
1992
1993 if (!c->use_pixman) {
Jonny Lamb74eed312015-03-24 13:12:04 +01001994 if (gl_renderer->create(&c->base,
Derek Foremane76f1852015-05-15 12:12:39 -05001995 EGL_PLATFORM_WAYLAND_KHR,
1996 c->parent.wl_display,
1997 gl_renderer->alpha_attribs,
1998 NULL,
1999 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05002000 weston_log("Failed to initialize the GL renderer; "
2001 "falling back to pixman.\n");
2002 c->use_pixman = 1;
2003 }
2004 }
2005
2006 if (c->use_pixman) {
2007 if (pixman_renderer_init(&c->base) < 0) {
2008 weston_log("Failed to initialize pixman renderer\n");
2009 goto err_display;
2010 }
2011 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002012
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002013 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002014 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002015
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002016 loop = wl_display_get_event_loop(c->base.wl_display);
2017
2018 fd = wl_display_get_fd(c->parent.wl_display);
2019 c->parent.wl_source =
2020 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2021 wayland_compositor_handle_event, c);
2022 if (c->parent.wl_source == NULL)
2023 goto err_renderer;
2024
2025 wl_event_source_check(c->parent.wl_source);
2026
2027 return c;
2028err_renderer:
2029 c->base.renderer->destroy(&c->base);
2030err_display:
2031 wl_display_disconnect(c->parent.wl_display);
2032err_compositor:
2033 weston_compositor_shutdown(&c->base);
2034err_free:
2035 free(c);
2036 return NULL;
2037}
2038
2039static void
2040wayland_compositor_destroy(struct wayland_compositor *c)
2041{
U. Artie Eoffa1e887b2014-05-21 09:20:02 -07002042 struct weston_output *output, *next;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002043
U. Artie Eoffa1e887b2014-05-21 09:20:02 -07002044 wl_list_for_each_safe(output, next, &c->base.output_list, link)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002045 wayland_output_destroy(output);
2046
2047 c->base.renderer->destroy(&c->base);
2048 wl_display_disconnect(c->parent.wl_display);
2049
2050 if (c->theme)
2051 theme_destroy(c->theme);
2052 if (c->frame_device)
2053 cairo_device_destroy(c->frame_device);
2054 wl_cursor_theme_destroy(c->cursor_theme);
2055
2056 weston_compositor_shutdown(&c->base);
2057 free(c);
2058}
2059
2060WL_EXPORT struct weston_compositor *
2061backend_init(struct wl_display *display, int *argc, char *argv[],
2062 struct weston_config *config)
2063{
2064 struct wayland_compositor *c;
2065 struct wayland_output *output;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002066 struct wayland_parent_output *poutput;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002067 struct weston_config_section *section;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002068 int x, count, width, height, scale, use_pixman, fullscreen, sprawl;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002069 const char *section_name, *display_name;
2070 char *name;
2071
2072 const struct weston_option wayland_options[] = {
2073 { WESTON_OPTION_INTEGER, "width", 0, &width },
2074 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002075 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002076 { WESTON_OPTION_STRING, "display", 0, &display_name },
2077 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2078 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002079 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002080 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002081 };
2082
2083 width = 0;
2084 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002085 scale = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002086 display_name = NULL;
2087 use_pixman = 0;
2088 count = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002089 fullscreen = 0;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002090 sprawl = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002091 parse_options(wayland_options,
2092 ARRAY_LENGTH(wayland_options), argc, argv);
2093
2094 c = wayland_compositor_create(display, use_pixman, display_name,
2095 argc, argv, config);
2096 if (!c)
2097 return NULL;
2098
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002099 if (sprawl || c->parent.fshell) {
2100 c->sprawl_across_outputs = 1;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002101 wl_display_roundtrip(c->parent.wl_display);
2102
2103 wl_list_for_each(poutput, &c->parent.output_list, link)
2104 wayland_output_create_for_parent_output(c, poutput);
2105
2106 return &c->base;
2107 }
2108
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002109 if (fullscreen) {
2110 output = wayland_output_create(c, 0, 0, width, height,
2111 NULL, 1, 0, 1);
2112 if (!output)
2113 goto err_outputs;
2114
Axel Davydd8b88d2013-11-17 21:34:16 +01002115 wayland_output_set_fullscreen(output, 0, 0, NULL);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002116 return &c->base;
2117 }
2118
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002119 section = NULL;
2120 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002121 while (weston_config_next_section(config, &section, &section_name)) {
2122 if (!section_name || strcmp(section_name, "output") != 0)
2123 continue;
2124 weston_config_section_get_string(section, "name", &name, NULL);
2125 if (name == NULL)
2126 continue;
2127
2128 if (name[0] != 'W' || name[1] != 'L') {
2129 free(name);
2130 continue;
2131 }
2132 free(name);
2133
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002134 output = wayland_output_create_for_config(c, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002135 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002136 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002137 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002138 if (wayland_output_set_windowed(output))
2139 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002140
2141 x += output->base.width;
2142 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002143 }
2144
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002145 if (!width)
2146 width = 1024;
2147 if (!height)
2148 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002149 if (!scale)
2150 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002151 while (count > 0) {
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002152 output = wayland_output_create(c, x, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002153 NULL, 0, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002154 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002155 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002156 if (wayland_output_set_windowed(output))
2157 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002158
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002159 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002160 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002161 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002162
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002163 weston_compositor_add_key_binding(&c->base, KEY_F,
2164 MODIFIER_CTRL | MODIFIER_ALT,
2165 fullscreen_binding, c);
2166
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002167 return &c->base;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002168
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002169err_outputs:
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002170 wayland_compositor_destroy(c);
Martin Olssonc5db50f2012-07-08 03:03:43 +02002171 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002172}