blob: 7cf61c9f2a4e4df817c6d184bdf9569d965c5a21 [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"
Kristian Høgsberg3c2360f2013-01-28 16:01:22 -050045#include "../shared/image-loader.h"
Jonas Ådahle5a12252013-04-05 23:07:11 +020046#include "../shared/os-compatibility.h"
Jason Ekstrand7744f712013-10-27 22:24:55 -050047#include "../shared/cairo-util.h"
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050048#include "fullscreen-shell-client-protocol.h"
Pekka Paalanen363aa7b2014-12-17 16:20:40 +020049#include "presentation_timing-server-protocol.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010050
Jason Ekstrand48ce4212013-10-27 22:25:02 -050051#define WINDOW_TITLE "Weston Compositor"
52
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010053struct wayland_compositor {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050054 struct weston_compositor base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010055
56 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040057 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040058 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010059 struct wl_compositor *compositor;
60 struct wl_shell *shell;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050061 struct _wl_fullscreen_shell *fshell;
Jonas Ådahle5a12252013-04-05 23:07:11 +020062 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010063
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050064 struct wl_list output_list;
65
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010066 struct wl_event_source *wl_source;
67 uint32_t event_mask;
68 } parent;
69
Jason Ekstrandff2fd462013-10-27 22:24:58 -050070 int use_pixman;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -050071 int sprawl_across_outputs;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050072
Jason Ekstrand7744f712013-10-27 22:24:55 -050073 struct theme *theme;
74 cairo_device_t *frame_device;
75 struct wl_cursor_theme *cursor_theme;
76 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050077
Jason Ekstrand06ced802013-11-07 20:13:29 -060078 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010079};
80
81struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050082 struct weston_output base;
83
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010084 struct {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050085 int draw_initial_frame;
86 struct wl_surface *surface;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -050087
88 struct wl_output *output;
89 uint32_t global_id;
90
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050091 struct wl_shell_surface *shell_surface;
Jason Ekstrand5ea04802013-11-07 20:13:33 -060092 int configure_width, configure_height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010093 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050094
Jason Ekstrand7744f712013-10-27 22:24:55 -050095 int keyboard_count;
96
Jason Ekstrand5ea04802013-11-07 20:13:33 -060097 char *name;
Jason Ekstrand7744f712013-10-27 22:24:55 -050098 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050099
Jason Ekstrand7744f712013-10-27 22:24:55 -0500100 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500101 struct wl_egl_window *egl_window;
102 struct {
103 cairo_surface_t *top;
104 cairo_surface_t *left;
105 cairo_surface_t *right;
106 cairo_surface_t *bottom;
107 } border;
108 } gl;
109
110 struct {
111 struct wl_list buffers;
112 struct wl_list free_buffers;
113 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500114
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500115 struct weston_mode mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500116 uint32_t scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100117};
118
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500119struct wayland_parent_output {
120 struct wayland_output *output;
121 struct wl_list link;
122
123 struct wl_output *global;
124 uint32_t id;
125
126 struct {
127 char *make;
128 char *model;
129 int32_t width, height;
130 uint32_t subpixel;
131 } physical;
132
133 int32_t x, y;
134 uint32_t transform;
135 uint32_t scale;
136
137 struct wl_list mode_list;
138 struct weston_mode *preferred_mode;
139 struct weston_mode *current_mode;
140};
141
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500142struct wayland_shm_buffer {
143 struct wayland_output *output;
144 struct wl_list link;
145 struct wl_list free_link;
146
147 struct wl_buffer *buffer;
148 void *data;
149 size_t size;
150 pixman_region32_t damage;
151 int frame_damaged;
152
153 pixman_image_t *pm_image;
154 cairo_surface_t *c_surface;
155};
156
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100157struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400158 struct weston_seat base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100159 struct wayland_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100160 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500161
162 struct {
163 struct wl_seat *seat;
164 struct wl_pointer *pointer;
165 struct wl_keyboard *keyboard;
166 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500167
168 struct {
169 struct wl_surface *surface;
170 int32_t hx, hy;
171 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500172 } parent;
173
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -0500174 enum weston_key_state_update keyboard_state_update;
Daniel Stone50692802012-06-22 13:21:41 +0100175 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400176 uint32_t enter_serial;
177 int focus;
178 struct wayland_output *output;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500179 struct wayland_output *keyboard_focus;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100180};
181
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300182struct gl_renderer_interface *gl_renderer;
183
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500184static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500185wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
186{
187 cairo_surface_destroy(buffer->c_surface);
188 pixman_image_unref(buffer->pm_image);
189
190 wl_buffer_destroy(buffer->buffer);
191 munmap(buffer->data, buffer->size);
192
193 pixman_region32_fini(&buffer->damage);
194
195 wl_list_remove(&buffer->link);
196 wl_list_remove(&buffer->free_link);
197 free(buffer);
198}
199
200static void
201buffer_release(void *data, struct wl_buffer *buffer)
202{
203 struct wayland_shm_buffer *sb = data;
204
205 if (sb->output) {
206 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
207 } else {
208 wayland_shm_buffer_destroy(sb);
209 }
210}
211
212static const struct wl_buffer_listener buffer_listener = {
213 buffer_release
214};
215
216static struct wayland_shm_buffer *
217wayland_output_get_shm_buffer(struct wayland_output *output)
218{
219 struct wayland_compositor *c =
220 (struct wayland_compositor *) output->base.compositor;
221 struct wl_shm *shm = c->parent.shm;
222 struct wayland_shm_buffer *sb;
223
224 struct wl_shm_pool *pool;
225 int width, height, stride;
226 int32_t fx, fy;
227 int fd;
228 unsigned char *data;
229
230 if (!wl_list_empty(&output->shm.free_buffers)) {
231 sb = container_of(output->shm.free_buffers.next,
232 struct wayland_shm_buffer, free_link);
233 wl_list_remove(&sb->free_link);
234 wl_list_init(&sb->free_link);
235
236 return sb;
237 }
238
239 if (output->frame) {
240 width = frame_width(output->frame);
241 height = frame_height(output->frame);
242 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500243 width = output->base.current_mode->width;
244 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500245 }
246
247 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
248
249 fd = os_create_anonymous_file(height * stride);
250 if (fd < 0) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000251 weston_log("could not create an anonymous file buffer: %m\n");
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500252 return NULL;
253 }
254
255 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
256 if (data == MAP_FAILED) {
Bryce W. Harringtona0935022014-03-21 05:54:02 +0000257 weston_log("could not mmap %d memory for data: %m\n", height * stride);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500258 close(fd);
259 return NULL;
260 }
261
262 sb = zalloc(sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000263 if (sb == NULL) {
Thierry Reding6ac60c12014-05-27 09:08:29 +0200264 weston_log("could not zalloc %zu memory for sb: %m\n", sizeof *sb);
Bryce W. Harringtonbfd74f42014-04-21 23:51:02 +0000265 close(fd);
266 free(data);
267 return NULL;
268 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500269
270 sb->output = output;
271 wl_list_init(&sb->free_link);
272 wl_list_insert(&output->shm.buffers, &sb->link);
273
274 pixman_region32_init_rect(&sb->damage, 0, 0,
275 output->base.width, output->base.height);
276 sb->frame_damaged = 1;
277
278 sb->data = data;
279 sb->size = height * stride;
280
281 pool = wl_shm_create_pool(shm, fd, sb->size);
282
283 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
284 width, height,
285 stride,
286 WL_SHM_FORMAT_ARGB8888);
287 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
288 wl_shm_pool_destroy(pool);
289 close(fd);
290
291 memset(data, 0, sb->size);
292
293 sb->c_surface =
294 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
295 width, height, stride);
296
297 fx = 0;
298 fy = 0;
299 if (output->frame)
300 frame_interior(output->frame, &fx, &fy, 0, 0);
301 sb->pm_image =
302 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
303 (uint32_t *)(data + fy * stride) + fx,
304 stride);
305
306 return sb;
307}
308
309static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500310frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400311{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500312 struct weston_output *output = data;
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400313 struct timespec ts;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400314
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500315 wl_callback_destroy(callback);
Pekka Paalanenb5eedad2014-09-23 22:08:45 -0400316
317 /* XXX: use the presentation extension for proper timings */
Pekka Paalanen04f8a9b2015-04-02 16:26:06 +0300318
319 /*
320 * This is the fallback case, where Presentation extension is not
321 * available from the parent compositor. We do not know the base for
322 * 'time', so we cannot feed it to finish_frame(). Do the only thing
323 * we can, and pretend finish_frame time is when we process this
324 * event.
325 */
326 weston_compositor_read_presentation_clock(output->compositor, &ts);
Pekka Paalanen363aa7b2014-12-17 16:20:40 +0200327 weston_output_finish_frame(output, &ts, 0);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400328}
329
330static const struct wl_callback_listener frame_listener = {
331 frame_done
332};
333
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500334static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200335draw_initial_frame(struct wayland_output *output)
336{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500337 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200338
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500339 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200340
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500341 /* If we are rendering with GL, then orphan it so that it gets
342 * destroyed immediately */
343 if (output->gl.egl_window)
344 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500345
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500346 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500347 wl_surface_damage(output->parent.surface, 0, 0,
348 output->base.current_mode->width,
349 output->base.current_mode->height);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200350}
351
352static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500353wayland_output_update_gl_border(struct wayland_output *output)
354{
355 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
356 cairo_t *cr;
357
358 if (!output->frame)
359 return;
360 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
361 return;
362
363 fwidth = frame_width(output->frame);
364 fheight = frame_height(output->frame);
365 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
366
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500367 if (!output->gl.border.top)
368 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500369 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
370 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500371 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500372 frame_repaint(output->frame, cr);
373 cairo_destroy(cr);
374 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
375 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500376 cairo_image_surface_get_stride(output->gl.border.top) / 4,
377 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500378
379
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500380 if (!output->gl.border.left)
381 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500382 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
383 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500384 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500385 cairo_translate(cr, 0, -iy);
386 frame_repaint(output->frame, cr);
387 cairo_destroy(cr);
388 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
389 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500390 cairo_image_surface_get_stride(output->gl.border.left) / 4,
391 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500392
393
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500394 if (!output->gl.border.right)
395 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500396 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
397 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500398 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500399 cairo_translate(cr, -(iwidth + ix), -iy);
400 frame_repaint(output->frame, cr);
401 cairo_destroy(cr);
402 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
403 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500404 cairo_image_surface_get_stride(output->gl.border.right) / 4,
405 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500406
407
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500408 if (!output->gl.border.bottom)
409 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500410 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
411 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500412 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500413 cairo_translate(cr, 0, -(iy + iheight));
414 frame_repaint(output->frame, cr);
415 cairo_destroy(cr);
416 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
417 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500418 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
419 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500420}
421
422static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200423wayland_output_start_repaint_loop(struct weston_output *output_base)
424{
425 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrand286ff682013-10-27 22:24:57 -0500426 struct wayland_compositor *wc =
427 (struct wayland_compositor *)output->base.compositor;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200428 struct wl_callback *callback;
429
430 /* If this is the initial frame, we need to attach a buffer so that
431 * the compositor can map the surface and include it in its render
432 * loop. If the surface doesn't end up in the render loop, the frame
433 * callback won't be invoked. The buffer is transparent and of the
434 * same size as the future real output buffer. */
435 if (output->parent.draw_initial_frame) {
436 output->parent.draw_initial_frame = 0;
437
438 draw_initial_frame(output);
439 }
440
441 callback = wl_surface_frame(output->parent.surface);
442 wl_callback_add_listener(callback, &frame_listener, output);
443 wl_surface_commit(output->parent.surface);
Jason Ekstrand286ff682013-10-27 22:24:57 -0500444 wl_display_flush(wc->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200445}
446
David Herrmann1edf44c2013-10-22 17:11:26 +0200447static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500448wayland_output_repaint_gl(struct weston_output *output_base,
449 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400450{
451 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400452 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400453 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600454
Kristian Høgsberg33418202011-08-16 23:01:28 -0400455 callback = wl_surface_frame(output->parent.surface);
456 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100457
Jason Ekstrand7744f712013-10-27 22:24:55 -0500458 wayland_output_update_gl_border(output);
459
Pekka Paalanenbc106382012-10-10 12:49:31 +0300460 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200461
462 pixman_region32_subtract(&ec->primary_plane.damage,
463 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200464 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100465}
466
Matt Roper361d2ad2011-08-29 13:52:23 -0700467static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500468wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
469{
470 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
471 cairo_t *cr;
472
473 if (!buffer->output->frame || !buffer->frame_damaged)
474 return;
475
476 cr = cairo_create(buffer->c_surface);
477
478 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
479 fwidth = frame_width(buffer->output->frame);
480 fheight = frame_height(buffer->output->frame);
481
482 /* Set the clip so we don't unnecisaraly damage the surface */
483 cairo_move_to(cr, ix, iy);
484 cairo_rel_line_to(cr, iwidth, 0);
485 cairo_rel_line_to(cr, 0, iheight);
486 cairo_rel_line_to(cr, -iwidth, 0);
487 cairo_line_to(cr, ix, iy);
488 cairo_line_to(cr, 0, iy);
489 cairo_line_to(cr, 0, fheight);
490 cairo_line_to(cr, fwidth, fheight);
491 cairo_line_to(cr, fwidth, 0);
492 cairo_line_to(cr, 0, 0);
493 cairo_line_to(cr, 0, iy);
494 cairo_close_path(cr);
495 cairo_clip(cr);
496
497 /* Draw using a pattern so that the final result gets clipped */
498 cairo_push_group(cr);
499 frame_repaint(buffer->output->frame, cr);
500 cairo_pop_group_to_source(cr);
501 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
502 cairo_paint(cr);
503
504 cairo_destroy(cr);
505}
506
507static void
508wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
509{
510 pixman_region32_t damage;
511 pixman_box32_t *rects;
512 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
513 int i, n;
514
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500515 pixman_region32_init(&damage);
516 weston_transformed_region(sb->output->base.width,
517 sb->output->base.height,
518 sb->output->base.transform,
519 sb->output->base.current_scale,
520 &sb->damage, &damage);
521
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500522 if (sb->output->frame) {
523 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
524 fwidth = frame_width(sb->output->frame);
525 fheight = frame_height(sb->output->frame);
526
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500527 pixman_region32_translate(&damage, ix, iy);
528
529 if (sb->frame_damaged) {
530 pixman_region32_union_rect(&damage, &damage,
531 0, 0, fwidth, iy);
532 pixman_region32_union_rect(&damage, &damage,
533 0, iy, ix, iheight);
534 pixman_region32_union_rect(&damage, &damage,
535 ix + iwidth, iy,
536 fwidth - (ix + iwidth), iheight);
537 pixman_region32_union_rect(&damage, &damage,
538 0, iy + iheight,
539 fwidth, fheight - (iy + iheight));
540 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500541 }
542
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500543 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500544 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
545 for (i = 0; i < n; ++i)
546 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
547 rects[i].y1, rects[i].x2 - rects[i].x1,
548 rects[i].y2 - rects[i].y1);
549
550 if (sb->output->frame)
551 pixman_region32_fini(&damage);
552}
553
554static int
555wayland_output_repaint_pixman(struct weston_output *output_base,
556 pixman_region32_t *damage)
557{
558 struct wayland_output *output = (struct wayland_output *) output_base;
559 struct wayland_compositor *c =
560 (struct wayland_compositor *)output->base.compositor;
561 struct wl_callback *callback;
562 struct wayland_shm_buffer *sb;
563
564 if (output->frame) {
565 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
566 wl_list_for_each(sb, &output->shm.buffers, link)
567 sb->frame_damaged = 1;
568 }
569
570 wl_list_for_each(sb, &output->shm.buffers, link)
571 pixman_region32_union(&sb->damage, &sb->damage, damage);
572
573 sb = wayland_output_get_shm_buffer(output);
574
575 wayland_output_update_shm_border(sb);
576 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
577 c->base.renderer->repaint_output(output_base, &sb->damage);
578
579 wayland_shm_buffer_attach(sb);
580
581 callback = wl_surface_frame(output->parent.surface);
582 wl_callback_add_listener(callback, &frame_listener, output);
583 wl_surface_commit(output->parent.surface);
584 wl_display_flush(c->parent.wl_display);
585
586 pixman_region32_fini(&sb->damage);
587 pixman_region32_init(&sb->damage);
588 sb->frame_damaged = 0;
589
590 pixman_region32_subtract(&c->base.primary_plane.damage,
591 &c->base.primary_plane.damage, damage);
592 return 0;
593}
594
595static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500596wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700597{
598 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500599 struct wayland_compositor *c =
600 (struct wayland_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700601
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500602 if (c->use_pixman) {
603 pixman_renderer_output_destroy(output_base);
604 } else {
605 gl_renderer->output_destroy(output_base);
606 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100607
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500608 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500609 wl_surface_destroy(output->parent.surface);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500610 if (output->parent.shell_surface)
611 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500612
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600613 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500614 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600615
616 cairo_surface_destroy(output->gl.border.top);
617 cairo_surface_destroy(output->gl.border.left);
618 cairo_surface_destroy(output->gl.border.right);
619 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500620
621 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700622 free(output);
623
624 return;
625}
626
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300627static const struct wl_shell_surface_listener shell_surface_listener;
628
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200629static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500630wayland_output_init_gl_renderer(struct wayland_output *output)
631{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500632 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500633
634 if (output->frame) {
635 fwidth = frame_width(output->frame);
636 fheight = frame_height(output->frame);
637 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500638 fwidth = output->base.current_mode->width;
639 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500640 }
641
642 output->gl.egl_window =
643 wl_egl_window_create(output->parent.surface,
644 fwidth, fheight);
645 if (!output->gl.egl_window) {
646 weston_log("failure to create wl_egl_window\n");
647 return -1;
648 }
649
650 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000651 output->gl.egl_window,
Jonny Lamb671148f2015-03-20 15:26:52 +0100652 output->gl.egl_window,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000653 gl_renderer->alpha_attribs,
Derek Foremane76f1852015-05-15 12:12:39 -0500654 NULL,
655 0) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500656 goto cleanup_window;
657
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500658 return 0;
659
660cleanup_window:
661 wl_egl_window_destroy(output->gl.egl_window);
662 return -1;
663}
664
665static int
666wayland_output_init_pixman_renderer(struct wayland_output *output)
667{
668 return pixman_renderer_output_create(&output->base);
669}
670
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600671static void
672wayland_output_resize_surface(struct wayland_output *output)
673{
674 struct wayland_compositor *c =
675 (struct wayland_compositor *)output->base.compositor;
676 struct wayland_shm_buffer *buffer, *next;
677 int32_t ix, iy, iwidth, iheight;
678 int32_t width, height;
679 struct wl_region *region;
680
681 width = output->base.current_mode->width;
682 height = output->base.current_mode->height;
683
684 if (output->frame) {
685 frame_resize_inside(output->frame, width, height);
686
687 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
688 region = wl_compositor_create_region(c->parent.compositor);
689 wl_region_add(region, ix, iy, iwidth, iheight);
690 wl_surface_set_input_region(output->parent.surface, region);
691 wl_region_destroy(region);
692
693 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
694 region = wl_compositor_create_region(c->parent.compositor);
695 wl_region_add(region, ix, iy, iwidth, iheight);
696 wl_surface_set_opaque_region(output->parent.surface, region);
697 wl_region_destroy(region);
698
699 width = frame_width(output->frame);
700 height = frame_height(output->frame);
701 } else {
702 region = wl_compositor_create_region(c->parent.compositor);
703 wl_region_add(region, 0, 0, width, height);
704 wl_surface_set_input_region(output->parent.surface, region);
705 wl_region_destroy(region);
706
707 region = wl_compositor_create_region(c->parent.compositor);
708 wl_region_add(region, 0, 0, width, height);
709 wl_surface_set_opaque_region(output->parent.surface, region);
710 wl_region_destroy(region);
711 }
712
713 if (output->gl.egl_window) {
714 wl_egl_window_resize(output->gl.egl_window,
715 width, height, 0, 0);
716
717 /* These will need to be re-created due to the resize */
718 gl_renderer->output_set_border(&output->base,
719 GL_RENDERER_BORDER_TOP,
720 0, 0, 0, NULL);
721 cairo_surface_destroy(output->gl.border.top);
722 output->gl.border.top = NULL;
723 gl_renderer->output_set_border(&output->base,
724 GL_RENDERER_BORDER_LEFT,
725 0, 0, 0, NULL);
726 cairo_surface_destroy(output->gl.border.left);
727 output->gl.border.left = NULL;
728 gl_renderer->output_set_border(&output->base,
729 GL_RENDERER_BORDER_RIGHT,
730 0, 0, 0, NULL);
731 cairo_surface_destroy(output->gl.border.right);
732 output->gl.border.right = NULL;
733 gl_renderer->output_set_border(&output->base,
734 GL_RENDERER_BORDER_BOTTOM,
735 0, 0, 0, NULL);
736 cairo_surface_destroy(output->gl.border.bottom);
737 output->gl.border.bottom = NULL;
738 }
739
740 /* Throw away any remaining SHM buffers */
741 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, link)
742 wayland_shm_buffer_destroy(buffer);
743 /* These will get thrown away when they get released */
744 wl_list_for_each(buffer, &output->shm.buffers, link)
745 buffer->output = NULL;
746}
747
748static int
749wayland_output_set_windowed(struct wayland_output *output)
750{
751 struct wayland_compositor *c =
752 (struct wayland_compositor *)output->base.compositor;
753 int tlen;
754 char *title;
755
756 if (output->frame)
757 return 0;
758
759 if (output->name) {
760 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
761 title = malloc(tlen + 1);
762 if (!title)
763 return -1;
764
765 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
766 } else {
767 title = strdup(WINDOW_TITLE);
768 }
769
770 if (!c->theme) {
771 c->theme = theme_create();
772 if (!c->theme) {
773 free(title);
774 return -1;
775 }
776 }
777 output->frame = frame_create(c->theme, 100, 100,
778 FRAME_BUTTON_CLOSE, title);
779 free(title);
780 if (!output->frame)
781 return -1;
782
783 if (output->keyboard_count)
784 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
785
786 wayland_output_resize_surface(output);
787
788 wl_shell_surface_set_toplevel(output->parent.shell_surface);
789
790 return 0;
791}
792
793static void
794wayland_output_set_fullscreen(struct wayland_output *output,
795 enum wl_shell_surface_fullscreen_method method,
796 uint32_t framerate, struct wl_output *target)
797{
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500798 struct wayland_compositor *c =
799 (struct wayland_compositor *)output->base.compositor;
800
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600801 if (output->frame) {
802 frame_destroy(output->frame);
803 output->frame = NULL;
804 }
805
806 wayland_output_resize_surface(output);
807
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -0500808 if (output->parent.shell_surface) {
809 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
810 method, framerate, target);
811 } else if (c->parent.fshell) {
812 _wl_fullscreen_shell_present_surface(c->parent.fshell,
813 output->parent.surface,
814 method, target);
815 }
816}
817
818static struct weston_mode *
819wayland_output_choose_mode(struct wayland_output *output,
820 struct weston_mode *ref_mode)
821{
822 struct weston_mode *mode;
823
824 /* First look for an exact match */
825 wl_list_for_each(mode, &output->base.mode_list, link)
826 if (mode->width == ref_mode->width &&
827 mode->height == ref_mode->height &&
828 mode->refresh == ref_mode->refresh)
829 return mode;
830
831 /* If we can't find an exact match, ignore refresh and try again */
832 wl_list_for_each(mode, &output->base.mode_list, link)
833 if (mode->width == ref_mode->width &&
834 mode->height == ref_mode->height)
835 return mode;
836
837 /* Yeah, we failed */
838 return NULL;
839}
840
841enum mode_status {
842 MODE_STATUS_UNKNOWN,
843 MODE_STATUS_SUCCESS,
844 MODE_STATUS_FAIL,
845 MODE_STATUS_CANCEL,
846};
847
848static void
849mode_feedback_successful(void *data,
850 struct _wl_fullscreen_shell_mode_feedback *fb)
851{
852 enum mode_status *value = data;
853
854 printf("Mode switch successful\n");
855
856 *value = MODE_STATUS_SUCCESS;
857}
858
859static void
860mode_feedback_failed(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
861{
862 enum mode_status *value = data;
863
864 printf("Mode switch failed\n");
865
866 *value = MODE_STATUS_FAIL;
867}
868
869static void
870mode_feedback_cancelled(void *data, struct _wl_fullscreen_shell_mode_feedback *fb)
871{
872 enum mode_status *value = data;
873
874 printf("Mode switch cancelled\n");
875
876 *value = MODE_STATUS_CANCEL;
877}
878
879struct _wl_fullscreen_shell_mode_feedback_listener mode_feedback_listener = {
880 mode_feedback_successful,
881 mode_feedback_failed,
882 mode_feedback_cancelled,
883};
884
885static int
886wayland_output_switch_mode(struct weston_output *output_base,
887 struct weston_mode *mode)
888{
889 struct wayland_output *output = (struct wayland_output *) output_base;
890 struct wayland_compositor *c;
891 struct wl_surface *old_surface;
892 struct weston_mode *old_mode;
893 struct _wl_fullscreen_shell_mode_feedback *mode_feedback;
894 enum mode_status mode_status;
895 int ret = 0;
896
897 if (output_base == NULL) {
898 weston_log("output is NULL.\n");
899 return -1;
900 }
901
902 if (mode == NULL) {
903 weston_log("mode is NULL.\n");
904 return -1;
905 }
906
907 c = (struct wayland_compositor *)output_base->compositor;
908
909 if (output->parent.shell_surface || !c->parent.fshell)
910 return -1;
911
912 mode = wayland_output_choose_mode(output, mode);
913 if (mode == NULL)
914 return -1;
915
916 if (output->base.current_mode == mode)
917 return 0;
918
919 old_mode = output->base.current_mode;
920 old_surface = output->parent.surface;
921 output->base.current_mode = mode;
922 output->parent.surface =
923 wl_compositor_create_surface(c->parent.compositor);
924 wl_surface_set_user_data(output->parent.surface, output);
925
926 /* Blow the old buffers because we changed size/surfaces */
927 wayland_output_resize_surface(output);
928
929 mode_feedback =
930 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
931 output->parent.surface,
932 output->parent.output,
933 mode->refresh);
934 _wl_fullscreen_shell_mode_feedback_add_listener(mode_feedback,
935 &mode_feedback_listener,
936 &mode_status);
937
938 /* This should kick-start things again */
939 output->parent.draw_initial_frame = 1;
940 wayland_output_start_repaint_loop(&output->base);
941
942 mode_status = MODE_STATUS_UNKNOWN;
943 while (mode_status == MODE_STATUS_UNKNOWN && ret >= 0)
944 ret = wl_display_dispatch(c->parent.wl_display);
945
946 _wl_fullscreen_shell_mode_feedback_destroy(mode_feedback);
947
948 if (mode_status == MODE_STATUS_FAIL) {
949 output->base.current_mode = old_mode;
950 wl_surface_destroy(output->parent.surface);
951 output->parent.surface = old_surface;
952 wayland_output_resize_surface(output);
953
954 return -1;
955 }
956
957 old_mode->flags &= ~WL_OUTPUT_MODE_CURRENT;
958 output->base.current_mode->flags |= WL_OUTPUT_MODE_CURRENT;
959
960 if (c->use_pixman) {
961 pixman_renderer_output_destroy(output_base);
962 if (wayland_output_init_pixman_renderer(output) < 0)
963 goto err_output;
964 } else {
965 gl_renderer->output_destroy(output_base);
966 wl_egl_window_destroy(output->gl.egl_window);
967 if (wayland_output_init_gl_renderer(output) < 0)
968 goto err_output;
969 }
970 wl_surface_destroy(old_surface);
971
972 weston_output_schedule_repaint(&output->base);
973
974 return 0;
975
976err_output:
977 /* XXX */
978 return -1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600979}
980
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500981static struct wayland_output *
982wayland_output_create(struct wayland_compositor *c, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600983 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500984 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100985{
986 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500987 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600988
989 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
990 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100991
Peter Huttererf3d62272013-08-08 11:57:05 +1000992 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100993 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500994 return NULL;
995
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600996 output->name = name ? strdup(name) : NULL;
997 output->base.make = "waywayland";
998 output->base.model = "none";
999
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001000 output_width = width * scale;
1001 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001002
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001003 output->parent.surface =
1004 wl_compositor_create_surface(c->parent.compositor);
1005 if (!output->parent.surface)
1006 goto err_name;
1007 wl_surface_set_user_data(output->parent.surface, output);
1008
1009 output->parent.draw_initial_frame = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001010
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001011 if (c->parent.shell) {
1012 output->parent.shell_surface =
1013 wl_shell_get_shell_surface(c->parent.shell,
1014 output->parent.surface);
1015 if (!output->parent.shell_surface)
1016 goto err_surface;
1017 wl_shell_surface_add_listener(output->parent.shell_surface,
1018 &shell_surface_listener, output);
1019 }
1020
1021 if (fullscreen && c->parent.shell) {
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001022 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1023 0, 0, NULL);
1024 wl_display_roundtrip(c->parent.wl_display);
1025 if (!width)
1026 output_width = output->parent.configure_width;
1027 if (!height)
1028 output_height = output->parent.configure_height;
1029 }
1030
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001031 output->mode.flags =
1032 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001033 output->mode.width = output_width;
1034 output->mode.height = output_height;
1035 output->mode.refresh = 60000;
1036 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -04001037 wl_list_init(&output->base.mode_list);
1038 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +02001039 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -05001040
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001041 wl_list_init(&output->shm.buffers);
1042 wl_list_init(&output->shm.free_buffers);
1043
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001044 weston_output_init(&output->base, &c->base, x, y, width, height,
1045 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001046
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001047 if (c->use_pixman) {
1048 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001049 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001050 output->base.repaint = wayland_output_repaint_pixman;
1051 } else {
1052 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001053 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001054 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001055 }
1056
Jonas Ådahle5a12252013-04-05 23:07:11 +02001057 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -07001058 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -08001059 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +02001060 output->base.set_backlight = NULL;
1061 output->base.set_dpms = NULL;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001062 output->base.switch_mode = wayland_output_switch_mode;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +01001063
Giulio Camuffob1147152015-05-06 21:41:57 +03001064 weston_compositor_add_output(&c->base, &output->base);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001065
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001066 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +01001067
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001068err_output:
1069 weston_output_destroy(&output->base);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001070 if (output->parent.shell_surface)
1071 wl_shell_surface_destroy(output->parent.shell_surface);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001072err_surface:
1073 wl_surface_destroy(output->parent.surface);
1074err_name:
1075 free(output->name);
1076
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001077 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +01001078 free(output);
1079
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001080 return NULL;
1081}
1082
1083static struct wayland_output *
1084wayland_output_create_for_config(struct wayland_compositor *c,
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001085 struct weston_config_section *config_section,
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001086 int option_width, int option_height,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001087 int option_scale, int32_t x, int32_t y)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001088{
1089 struct wayland_output *output;
1090 char *mode, *t, *name, *str;
1091 int width, height, scale;
1092 uint32_t transform;
Derek Foreman64a3df02014-10-23 12:24:18 -05001093 unsigned int slen;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001094
1095 weston_config_section_get_string(config_section, "name", &name, NULL);
1096 if (name) {
1097 slen = strlen(name);
1098 slen += strlen(WINDOW_TITLE " - ");
1099 str = malloc(slen + 1);
1100 if (str)
1101 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
1102 free(name);
1103 name = str;
1104 }
1105 if (!name)
U. Artie Eoff1a08d112014-01-17 12:22:50 -08001106 name = strdup(WINDOW_TITLE);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001107
1108 weston_config_section_get_string(config_section,
1109 "mode", &mode, "1024x600");
1110 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
1111 weston_log("Invalid mode \"%s\" for output %s\n",
1112 mode, name);
1113 width = 1024;
1114 height = 640;
1115 }
1116 free(mode);
1117
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001118 if (option_width)
1119 width = option_width;
1120 if (option_height)
1121 height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001122
1123 weston_config_section_get_int(config_section, "scale", &scale, 1);
1124
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001125 if (option_scale)
1126 scale = option_scale;
1127
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001128 weston_config_section_get_string(config_section,
1129 "transform", &t, "normal");
Derek Foreman64a3df02014-10-23 12:24:18 -05001130 if (weston_parse_transform(t, &transform) < 0)
1131 weston_log("Invalid transform \"%s\" for output %s\n",
1132 t, name);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001133 free(t);
1134
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001135 output = wayland_output_create(c, x, y, width, height, name, 0,
1136 transform, scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001137 free(name);
1138
1139 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001140}
1141
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001142static struct wayland_output *
1143wayland_output_create_for_parent_output(struct wayland_compositor *c,
1144 struct wayland_parent_output *poutput)
1145{
1146 struct wayland_output *output;
1147 struct weston_mode *mode;
1148 int32_t x;
1149
1150 if (poutput->current_mode) {
1151 mode = poutput->current_mode;
1152 } else if (poutput->preferred_mode) {
U. Artie Eoff67072d02014-05-06 14:50:02 -07001153 mode = poutput->preferred_mode;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001154 } else if (!wl_list_empty(&poutput->mode_list)) {
1155 mode = container_of(poutput->mode_list.next,
1156 struct weston_mode, link);
1157 } else {
1158 weston_log("No valid modes found. Skipping output");
1159 return NULL;
1160 }
1161
1162 if (!wl_list_empty(&c->base.output_list)) {
1163 output = container_of(c->base.output_list.prev,
1164 struct wayland_output, base.link);
1165 x = output->base.x + output->base.current_mode->width;
1166 } else {
1167 x = 0;
1168 }
1169
1170 output = wayland_output_create(c, x, 0, mode->width, mode->height,
1171 NULL, 0,
1172 WL_OUTPUT_TRANSFORM_NORMAL, 1);
1173 if (!output)
1174 return NULL;
1175
1176 output->parent.output = poutput->global;
1177
1178 output->base.make = poutput->physical.make;
1179 output->base.model = poutput->physical.model;
1180 wl_list_init(&output->base.mode_list);
1181 wl_list_insert_list(&output->base.mode_list, &poutput->mode_list);
1182 wl_list_init(&poutput->mode_list);
1183
1184 wayland_output_set_fullscreen(output,
1185 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1186 mode->refresh, poutput->global);
1187
1188 if (output->parent.shell_surface) {
1189 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
1190 WL_SHELL_SURFACE_FULLSCREEN_METHOD_DRIVER,
1191 mode->refresh, poutput->global);
1192 } else if (c->parent.fshell) {
1193 _wl_fullscreen_shell_present_surface(c->parent.fshell,
1194 output->parent.surface,
1195 _WL_FULLSCREEN_SHELL_PRESENT_METHOD_CENTER,
1196 poutput->global);
1197 _wl_fullscreen_shell_mode_feedback_destroy(
1198 _wl_fullscreen_shell_present_surface_for_mode(c->parent.fshell,
1199 output->parent.surface,
1200 poutput->global,
1201 mode->refresh));
1202 }
1203
1204 return output;
1205}
1206
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001207static void
1208shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
1209 uint32_t serial)
1210{
1211 wl_shell_surface_pong(shell_surface, serial);
1212}
1213
1214static void
1215shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
1216 uint32_t edges, int32_t width, int32_t height)
1217{
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001218 struct wayland_output *output = data;
1219
1220 output->parent.configure_width = width;
1221 output->parent.configure_height = height;
1222
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +03001223 /* FIXME: implement resizing */
1224}
1225
1226static void
1227shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
1228{
1229}
1230
1231static const struct wl_shell_surface_listener shell_surface_listener = {
1232 shell_surface_ping,
1233 shell_surface_configure,
1234 shell_surface_popup_done
1235};
1236
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001237/* Events received from the wayland-server this compositor is client of: */
1238
Jason Ekstrand7744f712013-10-27 22:24:55 -05001239/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001240static void
Jason Ekstrand7744f712013-10-27 22:24:55 -05001241input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001242{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001243
Jason Ekstrand7744f712013-10-27 22:24:55 -05001244 struct wl_buffer *buffer;
1245 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001246
Jason Ekstrand7744f712013-10-27 22:24:55 -05001247 if (!input->compositor->cursor)
1248 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001249
Jason Ekstrand7744f712013-10-27 22:24:55 -05001250 image = input->compositor->cursor->images[0];
1251 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +01001252 if (!buffer)
1253 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001254
1255 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
1256 input->parent.cursor.surface,
1257 image->hotspot_x, image->hotspot_y);
1258
1259 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
1260 wl_surface_damage(input->parent.cursor.surface, 0, 0,
1261 image->width, image->height);
1262 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001263}
1264
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001265static void
Daniel Stone37816df2012-05-16 18:45:18 +01001266input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
1267 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -04001268 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001269{
1270 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001271 int32_t fx, fy;
1272 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001273
Daniel Stone50692802012-06-22 13:21:41 +01001274 /* XXX: If we get a modifier event immediately before the focus,
1275 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001276 input->enter_serial = serial;
1277 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001278
1279 if (input->output->frame) {
1280 location = frame_pointer_enter(input->output->frame, input,
1281 wl_fixed_to_int(x),
1282 wl_fixed_to_int(y));
1283 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1284 x -= wl_fixed_from_int(fx);
1285 y -= wl_fixed_from_int(fy);
1286
1287 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1288 weston_output_schedule_repaint(&input->output->base);
1289 } else {
1290 location = THEME_LOCATION_CLIENT_AREA;
1291 }
1292
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001293 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001294
1295 if (location == THEME_LOCATION_CLIENT_AREA) {
1296 input->focus = 1;
1297 notify_pointer_focus(&input->base, &input->output->base, x, y);
1298 wl_pointer_set_cursor(input->parent.pointer,
1299 input->enter_serial, NULL, 0, 0);
1300 } else {
1301 input->focus = 0;
1302 notify_pointer_focus(&input->base, NULL, 0, 0);
1303 input_set_cursor(input);
1304 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001305}
1306
1307static void
Daniel Stone37816df2012-05-16 18:45:18 +01001308input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1309 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001310{
1311 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001312
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001313 if (!input->output)
1314 return;
1315
Jason Ekstrand7744f712013-10-27 22:24:55 -05001316 if (input->output->frame) {
1317 frame_pointer_leave(input->output->frame, input);
1318
1319 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1320 weston_output_schedule_repaint(&input->output->base);
1321 }
1322
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001323 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001324 input->output = NULL;
1325 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001326}
1327
1328static void
Daniel Stone37816df2012-05-16 18:45:18 +01001329input_handle_motion(void *data, struct wl_pointer *pointer,
1330 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1331{
1332 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001333 int32_t fx, fy;
1334 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001335
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001336 if (!input->output)
1337 return;
1338
Jason Ekstrand7744f712013-10-27 22:24:55 -05001339 if (input->output->frame) {
1340 location = frame_pointer_motion(input->output->frame, input,
1341 wl_fixed_to_int(x),
1342 wl_fixed_to_int(y));
1343 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1344 x -= wl_fixed_from_int(fx);
1345 y -= wl_fixed_from_int(fy);
1346
1347 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1348 weston_output_schedule_repaint(&input->output->base);
1349 } else {
1350 location = THEME_LOCATION_CLIENT_AREA;
1351 }
1352
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001353 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001354
1355 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
1356 input_set_cursor(input);
1357 notify_pointer_focus(&input->base, NULL, 0, 0);
1358 input->focus = 0;
1359 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
1360 wl_pointer_set_cursor(input->parent.pointer,
1361 input->enter_serial, NULL, 0, 0);
1362 notify_pointer_focus(&input->base, &input->output->base, x, y);
1363 input->focus = 1;
1364 }
1365
1366 if (location == THEME_LOCATION_CLIENT_AREA)
1367 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +01001368}
1369
1370static void
1371input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001372 uint32_t serial, uint32_t time, uint32_t button,
1373 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001374{
1375 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001376 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001377 enum frame_button_state fstate;
1378 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001379
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001380 if (!input->output)
1381 return;
1382
Jason Ekstrand7744f712013-10-27 22:24:55 -05001383 if (input->output->frame) {
1384 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1385 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1386
1387 location = frame_pointer_button(input->output->frame, input,
1388 button, fstate);
1389
1390 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1391
1392 wl_shell_surface_move(input->output->parent.shell_surface,
1393 input->parent.seat, serial);
1394 frame_status_clear(input->output->frame,
1395 FRAME_STATUS_MOVE);
1396 return;
1397 }
1398
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001399 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE) {
1400 wayland_output_destroy(&input->output->base);
Dima Ryazanovb7e70af2015-05-20 01:03:53 -07001401 input->output = NULL;
1402 input->keyboard_focus = NULL;
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001403
1404 if (wl_list_empty(&input->compositor->base.output_list))
1405 wl_display_terminate(input->compositor->base.wl_display);
1406
1407 return;
1408 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001409
1410 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1411 weston_output_schedule_repaint(&input->output->base);
1412 } else {
1413 location = THEME_LOCATION_CLIENT_AREA;
1414 }
1415
1416 if (location == THEME_LOCATION_CLIENT_AREA)
1417 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +01001418}
1419
1420static void
1421input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001422 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001423{
1424 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001425
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001426 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +01001427}
1428
1429static const struct wl_pointer_listener pointer_listener = {
1430 input_handle_pointer_enter,
1431 input_handle_pointer_leave,
1432 input_handle_motion,
1433 input_handle_button,
1434 input_handle_axis,
1435};
1436
1437static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001438input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1439 int fd, uint32_t size)
1440{
1441 struct wayland_input *input = data;
1442 struct xkb_keymap *keymap;
1443 char *map_str;
1444
U. Artie Eoffd8d47012014-05-06 14:50:03 -07001445 if (!data) {
1446 close(fd);
1447 return;
1448 }
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001449
1450 if (format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1451 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1452 if (map_str == MAP_FAILED) {
1453 weston_log("mmap failed: %m\n");
1454 goto error;
1455 }
1456
Ran Benita2e1968f2014-08-19 23:59:51 +03001457 keymap = xkb_keymap_new_from_string(input->compositor->base.xkb_context,
1458 map_str,
1459 XKB_KEYMAP_FORMAT_TEXT_V1,
1460 0);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001461 munmap(map_str, size);
1462
1463 if (!keymap) {
1464 weston_log("failed to compile keymap\n");
1465 goto error;
1466 }
1467
1468 input->keyboard_state_update = STATE_UPDATE_NONE;
1469 } else if (format == WL_KEYBOARD_KEYMAP_FORMAT_NO_KEYMAP) {
1470 weston_log("No keymap provided; falling back to defalt\n");
1471 keymap = NULL;
1472 input->keyboard_state_update = STATE_UPDATE_AUTOMATIC;
1473 } else {
1474 weston_log("Invalid keymap\n");
1475 goto error;
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001476 }
1477
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001478 close(fd);
1479
Rui Matos0c194ce2013-10-10 19:44:21 +02001480 if (input->base.keyboard)
1481 weston_seat_update_keymap(&input->base, keymap);
1482 else
1483 weston_seat_init_keyboard(&input->base, keymap);
1484
Ran Benitac9c74152014-08-19 23:59:52 +03001485 xkb_keymap_unref(keymap);
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001486
1487 return;
1488
1489error:
1490 wl_keyboard_release(input->parent.keyboard);
1491 close(fd);
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001492}
1493
1494static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001495input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001496 struct wl_keyboard *keyboard,
1497 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001498 struct wl_surface *surface,
1499 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001500{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001501 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001502 struct wayland_output *focus;
1503
1504 focus = input->keyboard_focus;
1505 if (focus) {
1506 /* This shouldn't happen */
1507 focus->keyboard_count--;
1508 if (!focus->keyboard_count && focus->frame)
1509 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1510 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1511 weston_output_schedule_repaint(&focus->base);
1512 }
1513
1514 input->keyboard_focus = wl_surface_get_user_data(surface);
1515 input->keyboard_focus->keyboard_count++;
1516
1517 focus = input->keyboard_focus;
1518 if (focus->frame) {
1519 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1520 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1521 weston_output_schedule_repaint(&focus->base);
1522 }
1523
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001524
Daniel Stone50692802012-06-22 13:21:41 +01001525 /* XXX: If we get a modifier event immediately before the focus,
1526 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001527 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001528 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001529}
1530
1531static void
1532input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001533 struct wl_keyboard *keyboard,
1534 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001535 struct wl_surface *surface)
1536{
1537 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001538 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001539
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001540 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001541
1542 focus = input->keyboard_focus;
1543 if (!focus)
Dima Ryazanov01d5c022015-05-18 23:14:16 -07001544 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001545
1546 focus->keyboard_count--;
1547 if (!focus->keyboard_count && focus->frame) {
1548 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1549 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1550 weston_output_schedule_repaint(&focus->base);
1551 }
1552
1553 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001554}
1555
Daniel Stone37816df2012-05-16 18:45:18 +01001556static void
1557input_handle_key(void *data, struct wl_keyboard *keyboard,
1558 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1559{
1560 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001561
Daniel Stone50692802012-06-22 13:21:41 +01001562 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001563 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001564 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001565 WL_KEYBOARD_KEY_STATE_RELEASED,
Jason Ekstrandb7d9f2e2014-04-02 19:53:57 -05001566 input->keyboard_state_update);
Daniel Stone37816df2012-05-16 18:45:18 +01001567}
1568
Daniel Stone351eb612012-05-31 15:27:47 -04001569static void
1570input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001571 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001572 uint32_t mods_latched, uint32_t mods_locked,
1573 uint32_t group)
1574{
Daniel Stone50692802012-06-22 13:21:41 +01001575 struct wayland_input *input = data;
1576 struct wayland_compositor *c = input->compositor;
1577 uint32_t serial_out;
1578
1579 /* If we get a key event followed by a modifier event with the
1580 * same serial number, then we try to preserve those semantics by
1581 * reusing the same serial number on the way out too. */
1582 if (serial_in == input->key_serial)
1583 serial_out = wl_display_get_serial(c->base.wl_display);
1584 else
1585 serial_out = wl_display_next_serial(c->base.wl_display);
1586
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001587 xkb_state_update_mask(input->base.keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001588 mods_depressed, mods_latched,
1589 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001590 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001591}
1592
Jonny Lamb497994a2014-08-12 14:58:26 +02001593static void
1594input_handle_repeat_info(void *data, struct wl_keyboard *keyboard,
1595 int32_t rate, int32_t delay)
1596{
1597 struct wayland_input *input = data;
1598 struct wayland_compositor *c = input->compositor;
1599
1600 c->base.kb_repeat_rate = rate;
1601 c->base.kb_repeat_delay = delay;
1602}
1603
Daniel Stone37816df2012-05-16 18:45:18 +01001604static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001605 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001606 input_handle_keyboard_enter,
1607 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001608 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001609 input_handle_modifiers,
Jonny Lamb497994a2014-08-12 14:58:26 +02001610 input_handle_repeat_info,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001611};
1612
1613static void
Daniel Stone37816df2012-05-16 18:45:18 +01001614input_handle_capabilities(void *data, struct wl_seat *seat,
1615 enum wl_seat_capability caps)
1616{
1617 struct wayland_input *input = data;
1618
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001619 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1620 input->parent.pointer = wl_seat_get_pointer(seat);
1621 wl_pointer_set_user_data(input->parent.pointer, input);
1622 wl_pointer_add_listener(input->parent.pointer,
1623 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001624 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001625 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1626 wl_pointer_destroy(input->parent.pointer);
1627 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001628 }
1629
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001630 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1631 input->parent.keyboard = wl_seat_get_keyboard(seat);
1632 wl_keyboard_set_user_data(input->parent.keyboard, input);
1633 wl_keyboard_add_listener(input->parent.keyboard,
1634 &keyboard_listener, input);
1635 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1636 wl_keyboard_destroy(input->parent.keyboard);
1637 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001638 }
1639}
1640
Jonny Lamb497994a2014-08-12 14:58:26 +02001641static void
1642input_handle_name(void *data, struct wl_seat *seat,
1643 const char *name)
1644{
1645}
1646
Daniel Stone37816df2012-05-16 18:45:18 +01001647static const struct wl_seat_listener seat_listener = {
1648 input_handle_capabilities,
Jonny Lamb497994a2014-08-12 14:58:26 +02001649 input_handle_name,
Daniel Stone37816df2012-05-16 18:45:18 +01001650};
1651
1652static void
Jonny Lamb497994a2014-08-12 14:58:26 +02001653display_add_seat(struct wayland_compositor *c, uint32_t id, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001654{
1655 struct wayland_input *input;
1656
Peter Huttererf3d62272013-08-08 11:57:05 +10001657 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001658 if (input == NULL)
1659 return;
1660
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001661 weston_seat_init(&input->base, &c->base, "default");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001662 input->compositor = c;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001663 input->parent.seat = wl_registry_bind(c->parent.registry, id,
Jonny Lamb497994a2014-08-12 14:58:26 +02001664 &wl_seat_interface, MIN(version, 4));
Jason Ekstrand06ced802013-11-07 20:13:29 -06001665 wl_list_insert(c->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001666
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001667 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1668 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001669
1670 input->parent.cursor.surface =
1671 wl_compositor_create_surface(c->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001672}
1673
1674static void
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001675wayland_parent_output_geometry(void *data, struct wl_output *output_proxy,
1676 int32_t x, int32_t y,
1677 int32_t physical_width, int32_t physical_height,
1678 int32_t subpixel, const char *make,
1679 const char *model, int32_t transform)
1680{
1681 struct wayland_parent_output *output = data;
1682
1683 output->x = x;
1684 output->y = y;
1685 output->physical.width = physical_width;
1686 output->physical.height = physical_height;
1687 output->physical.subpixel = subpixel;
1688
1689 free(output->physical.make);
1690 output->physical.make = strdup(make);
1691 free(output->physical.model);
1692 output->physical.model = strdup(model);
1693
1694 output->transform = transform;
1695}
1696
1697static struct weston_mode *
1698find_mode(struct wl_list *list, int32_t width, int32_t height, uint32_t refresh)
1699{
1700 struct weston_mode *mode;
1701
1702 wl_list_for_each(mode, list, link) {
1703 if (mode->width == width && mode->height == height &&
1704 mode->refresh == refresh)
1705 return mode;
1706 }
1707
1708 mode = zalloc(sizeof *mode);
1709 if (!mode)
1710 return NULL;
1711
1712 mode->width = width;
1713 mode->height = height;
1714 mode->refresh = refresh;
1715 wl_list_insert(list, &mode->link);
1716
1717 return mode;
1718}
1719
1720static void
1721wayland_parent_output_mode(void *data, struct wl_output *wl_output_proxy,
1722 uint32_t flags, int32_t width, int32_t height,
1723 int32_t refresh)
1724{
1725 struct wayland_parent_output *output = data;
1726 struct weston_mode *mode;
1727
1728 if (output->output) {
1729 mode = find_mode(&output->output->base.mode_list,
1730 width, height, refresh);
1731 if (!mode)
1732 return;
1733 mode->flags = flags;
1734 /* Do a mode-switch on current mode change? */
1735 } else {
1736 mode = find_mode(&output->mode_list, width, height, refresh);
1737 if (!mode)
1738 return;
1739 mode->flags = flags;
1740 if (flags & WL_OUTPUT_MODE_CURRENT)
1741 output->current_mode = mode;
1742 if (flags & WL_OUTPUT_MODE_PREFERRED)
1743 output->preferred_mode = mode;
1744 }
1745}
1746
1747static const struct wl_output_listener output_listener = {
1748 wayland_parent_output_geometry,
1749 wayland_parent_output_mode
1750};
1751
1752static void
1753wayland_compositor_register_output(struct wayland_compositor *c, uint32_t id)
1754{
1755 struct wayland_parent_output *output;
1756
1757 output = zalloc(sizeof *output);
1758 if (!output)
1759 return;
1760
1761 output->id = id;
1762 output->global = wl_registry_bind(c->parent.registry, id,
1763 &wl_output_interface, 1);
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001764 if (!output->global) {
1765 free(output);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001766 return;
U. Artie Eoff8cbd8f32014-05-06 14:50:01 -07001767 }
1768
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001769 wl_output_add_listener(output->global, &output_listener, output);
1770
1771 output->scale = 0;
1772 output->transform = WL_OUTPUT_TRANSFORM_NORMAL;
1773 output->physical.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
1774 wl_list_init(&output->mode_list);
1775 wl_list_insert(&c->parent.output_list, &output->link);
1776
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05001777 if (c->sprawl_across_outputs) {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001778 wl_display_roundtrip(c->parent.wl_display);
1779 wayland_output_create_for_parent_output(c, output);
1780 }
1781}
1782
1783static void
1784wayland_parent_output_destroy(struct wayland_parent_output *output)
1785{
1786 struct weston_mode *mode, *next;
1787
1788 if (output->output)
1789 wayland_output_destroy(&output->output->base);
1790
1791 wl_output_destroy(output->global);
1792 free(output->physical.make);
1793 free(output->physical.model);
1794
1795 wl_list_for_each_safe(mode, next, &output->mode_list, link) {
1796 wl_list_remove(&mode->link);
1797 free(mode);
1798 }
1799}
1800
1801static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001802registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1803 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001804{
1805 struct wayland_compositor *c = data;
1806
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001807 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001808 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001809 wl_registry_bind(registry, name,
1810 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001811 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001812 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001813 wl_registry_bind(registry, name,
1814 &wl_shell_interface, 1);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001815 } else if (strcmp(interface, "_wl_fullscreen_shell") == 0) {
1816 c->parent.fshell =
1817 wl_registry_bind(registry, name,
1818 &_wl_fullscreen_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01001819 } else if (strcmp(interface, "wl_seat") == 0) {
Jonny Lamb497994a2014-08-12 14:58:26 +02001820 display_add_seat(c, name, version);
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001821 } else if (strcmp(interface, "wl_output") == 0) {
1822 wayland_compositor_register_output(c, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001823 } else if (strcmp(interface, "wl_shm") == 0) {
1824 c->parent.shm =
1825 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001826 }
1827}
1828
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001829static void
1830registry_handle_global_remove(void *data, struct wl_registry *registry,
1831 uint32_t name)
1832{
1833 struct wayland_compositor *c = data;
1834 struct wayland_parent_output *output;
1835
1836 wl_list_for_each(output, &c->parent.output_list, link)
1837 if (output->id == name)
1838 wayland_parent_output_destroy(output);
1839}
1840
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001841static const struct wl_registry_listener registry_listener = {
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001842 registry_handle_global,
1843 registry_handle_global_remove
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001844};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001845
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001846static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001847wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1848{
1849 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001850 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001851
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001852 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1853 wl_display_terminate(c->base.wl_display);
1854 return 0;
1855 }
1856
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001857 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001858 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05001859 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001860 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001861
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001862 if (mask == 0) {
1863 count = wl_display_dispatch_pending(c->parent.wl_display);
1864 wl_display_flush(c->parent.wl_display);
1865 }
1866
1867 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001868}
1869
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001870static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001871wayland_restore(struct weston_compositor *ec)
1872{
1873}
1874
1875static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001876wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001877{
Jonas Ådahle5a12252013-04-05 23:07:11 +02001878 struct wayland_compositor *c = (struct wayland_compositor *) ec;
1879
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001880 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001881
Jonas Ådahle5a12252013-04-05 23:07:11 +02001882 if (c->parent.shm)
1883 wl_shm_destroy(c->parent.shm);
1884
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001885 free(ec);
1886}
1887
Jason Ekstrand7744f712013-10-27 22:24:55 -05001888static const char *left_ptrs[] = {
1889 "left_ptr",
1890 "default",
1891 "top_left_arrow",
1892 "left-arrow"
1893};
1894
1895static void
1896create_cursor(struct wayland_compositor *c, struct weston_config *config)
1897{
1898 struct weston_config_section *s;
1899 int size;
1900 char *theme = NULL;
1901 unsigned int i;
1902
1903 s = weston_config_get_section(config, "shell", NULL, NULL);
1904 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1905 weston_config_section_get_int(s, "cursor-size", &size, 32);
1906
1907 c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
Hardening842a36a2014-03-18 14:12:50 +01001908 if (!c->cursor_theme) {
1909 fprintf(stderr, "could not load cursor theme\n");
1910 return;
1911 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001912
U. Artie Eoffff755002014-01-17 12:36:58 -08001913 free(theme);
1914
Jason Ekstrand7744f712013-10-27 22:24:55 -05001915 c->cursor = NULL;
1916 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1917 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
1918 left_ptrs[i]);
1919 if (!c->cursor) {
1920 fprintf(stderr, "could not load left cursor\n");
1921 return;
1922 }
1923}
1924
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001925static void
1926fullscreen_binding(struct weston_seat *seat_base, uint32_t time, uint32_t key,
1927 void *data)
1928{
1929 struct wayland_compositor *c = data;
1930 struct wayland_input *input = NULL;
1931
1932 wl_list_for_each(input, &c->input_list, link)
1933 if (&input->base == seat_base)
1934 break;
1935
1936 if (!input || !input->output)
1937 return;
1938
1939 if (input->output->frame)
1940 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
1941 else
1942 wayland_output_set_windowed(input->output);
1943
1944 weston_output_schedule_repaint(&input->output->base);
1945}
1946
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001947static struct wayland_compositor *
1948wayland_compositor_create(struct wl_display *display, int use_pixman,
1949 const char *display_name, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001950 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001951{
1952 struct wayland_compositor *c;
1953 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001954 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001955
Peter Huttererf3d62272013-08-08 11:57:05 +10001956 c = zalloc(sizeof *c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001957 if (c == NULL)
1958 return NULL;
1959
Daniel Stone725c2c32012-06-22 14:04:36 +01001960 if (weston_compositor_init(&c->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001961 config) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +02001962 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001963
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001964 if (weston_compositor_set_presentation_clock_software(&c->base) < 0)
1965 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001966
Pekka Paalanenb5eedad2014-09-23 22:08:45 -04001967 c->parent.wl_display = wl_display_connect(display_name);
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001968 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001969 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001970 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001971 }
1972
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05001973 wl_list_init(&c->parent.output_list);
Jason Ekstrand06ced802013-11-07 20:13:29 -06001974 wl_list_init(&c->input_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001975 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1976 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001977 wl_display_roundtrip(c->parent.wl_display);
1978
1979 create_cursor(c, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001980
1981 c->base.wl_display = display;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001982
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001983 c->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001984
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001985 if (!c->use_pixman) {
1986 gl_renderer = weston_load_module("gl-renderer.so",
1987 "gl_renderer_interface");
1988 if (!gl_renderer)
1989 c->use_pixman = 1;
1990 }
1991
1992 if (!c->use_pixman) {
Jonny Lamb74eed312015-03-24 13:12:04 +01001993 if (gl_renderer->create(&c->base,
Derek Foremane76f1852015-05-15 12:12:39 -05001994 EGL_PLATFORM_WAYLAND_KHR,
1995 c->parent.wl_display,
1996 gl_renderer->alpha_attribs,
1997 NULL,
1998 0) < 0) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001999 weston_log("Failed to initialize the GL renderer; "
2000 "falling back to pixman.\n");
2001 c->use_pixman = 1;
2002 }
2003 }
2004
2005 if (c->use_pixman) {
2006 if (pixman_renderer_init(&c->base) < 0) {
2007 weston_log("Failed to initialize pixman renderer\n");
2008 goto err_display;
2009 }
2010 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002011
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002012 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04002013 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01002014
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002015 loop = wl_display_get_event_loop(c->base.wl_display);
2016
2017 fd = wl_display_get_fd(c->parent.wl_display);
2018 c->parent.wl_source =
2019 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
2020 wayland_compositor_handle_event, c);
2021 if (c->parent.wl_source == NULL)
2022 goto err_renderer;
2023
2024 wl_event_source_check(c->parent.wl_source);
2025
2026 return c;
2027err_renderer:
2028 c->base.renderer->destroy(&c->base);
2029err_display:
2030 wl_display_disconnect(c->parent.wl_display);
2031err_compositor:
2032 weston_compositor_shutdown(&c->base);
2033err_free:
2034 free(c);
2035 return NULL;
2036}
2037
2038static void
2039wayland_compositor_destroy(struct wayland_compositor *c)
2040{
U. Artie Eoffa1e887b2014-05-21 09:20:02 -07002041 struct weston_output *output, *next;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002042
U. Artie Eoffa1e887b2014-05-21 09:20:02 -07002043 wl_list_for_each_safe(output, next, &c->base.output_list, link)
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002044 wayland_output_destroy(output);
2045
2046 c->base.renderer->destroy(&c->base);
2047 wl_display_disconnect(c->parent.wl_display);
2048
2049 if (c->theme)
2050 theme_destroy(c->theme);
2051 if (c->frame_device)
2052 cairo_device_destroy(c->frame_device);
2053 wl_cursor_theme_destroy(c->cursor_theme);
2054
2055 weston_compositor_shutdown(&c->base);
2056 free(c);
2057}
2058
2059WL_EXPORT struct weston_compositor *
2060backend_init(struct wl_display *display, int *argc, char *argv[],
2061 struct weston_config *config)
2062{
2063 struct wayland_compositor *c;
2064 struct wayland_output *output;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002065 struct wayland_parent_output *poutput;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002066 struct weston_config_section *section;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002067 int x, count, width, height, scale, use_pixman, fullscreen, sprawl;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002068 const char *section_name, *display_name;
2069 char *name;
2070
2071 const struct weston_option wayland_options[] = {
2072 { WESTON_OPTION_INTEGER, "width", 0, &width },
2073 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002074 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002075 { WESTON_OPTION_STRING, "display", 0, &display_name },
2076 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
2077 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002078 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002079 { WESTON_OPTION_BOOLEAN, "sprawl", 0, &sprawl },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002080 };
2081
2082 width = 0;
2083 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002084 scale = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002085 display_name = NULL;
2086 use_pixman = 0;
2087 count = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002088 fullscreen = 0;
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002089 sprawl = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002090 parse_options(wayland_options,
2091 ARRAY_LENGTH(wayland_options), argc, argv);
2092
2093 c = wayland_compositor_create(display, use_pixman, display_name,
2094 argc, argv, config);
2095 if (!c)
2096 return NULL;
2097
Jason Ekstrande4ca8b02014-04-02 19:53:55 -05002098 if (sprawl || c->parent.fshell) {
2099 c->sprawl_across_outputs = 1;
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002100 wl_display_roundtrip(c->parent.wl_display);
2101
2102 wl_list_for_each(poutput, &c->parent.output_list, link)
2103 wayland_output_create_for_parent_output(c, poutput);
2104
2105 return &c->base;
2106 }
2107
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002108 if (fullscreen) {
2109 output = wayland_output_create(c, 0, 0, width, height,
2110 NULL, 1, 0, 1);
2111 if (!output)
2112 goto err_outputs;
2113
Axel Davydd8b88d2013-11-17 21:34:16 +01002114 wayland_output_set_fullscreen(output, 0, 0, NULL);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002115 return &c->base;
2116 }
2117
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002118 section = NULL;
2119 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002120 while (weston_config_next_section(config, &section, &section_name)) {
2121 if (!section_name || strcmp(section_name, "output") != 0)
2122 continue;
2123 weston_config_section_get_string(section, "name", &name, NULL);
2124 if (name == NULL)
2125 continue;
2126
2127 if (name[0] != 'W' || name[1] != 'L') {
2128 free(name);
2129 continue;
2130 }
2131 free(name);
2132
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002133 output = wayland_output_create_for_config(c, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002134 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002135 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002136 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002137 if (wayland_output_set_windowed(output))
2138 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002139
2140 x += output->base.width;
2141 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002142 }
2143
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002144 if (!width)
2145 width = 1024;
2146 if (!height)
2147 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002148 if (!scale)
2149 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002150 while (count > 0) {
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06002151 output = wayland_output_create(c, x, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002152 NULL, 0, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002153 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002154 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06002155 if (wayland_output_set_windowed(output))
2156 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002157
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002158 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002159 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002160 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002161
Jason Ekstrand53ee0dc2014-04-02 19:53:54 -05002162 weston_compositor_add_key_binding(&c->base, KEY_F,
2163 MODIFIER_CTRL | MODIFIER_ALT,
2164 fullscreen_binding, c);
2165
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002166 return &c->base;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002167
Jason Ekstrand48ce4212013-10-27 22:25:02 -05002168err_outputs:
Jason Ekstrand0cf39352013-11-07 20:13:31 -06002169 wayland_compositor_destroy(c);
Martin Olssonc5db50f2012-07-08 03:03:43 +02002170 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01002171}