blob: 7d9e01b003d35eede8db87c17ce8f4cd0a3d1e0f [file] [log] [blame]
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001/*
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04002 * Copyright © 2010-2011 Benjamin Franzke
Jason Ekstrandff2fd462013-10-27 22:24:58 -05003 * Copyright © 2013 Jason Ekstrand
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01004 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -04005 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the copyright holders not be used in
10 * advertising or publicity pertaining to distribution of the software
11 * without specific, written prior permission. The copyright holders make
12 * no representations about the suitability of this software for any
13 * purpose. It is provided "as is" without express or implied warranty.
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010014 *
Kristian Høgsberg96aa7da2011-09-15 15:43:14 -040015 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
18 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
20 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010022 */
23
Daniel Stonec228e232013-05-22 18:03:19 +030024#include "config.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010025
26#include <stddef.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010027#include <stdio.h>
28#include <stdlib.h>
29#include <string.h>
30#include <fcntl.h>
31#include <unistd.h>
Daniel Stoneb7452fe2012-06-01 12:14:06 +010032#include <sys/mman.h>
Jason Ekstrand5ea04802013-11-07 20:13:33 -060033#include <linux/input.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010034
Benjamin Franzkebe014562011-02-18 17:04:24 +010035#include <wayland-client.h>
36#include <wayland-egl.h>
Jason Ekstrand7744f712013-10-27 22:24:55 -050037#include <wayland-cursor.h>
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010038
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010039#include "compositor.h"
John Kåre Alsaker30d2b1f2012-11-13 19:10:28 +010040#include "gl-renderer.h"
Jason Ekstrandff2fd462013-10-27 22:24:58 -050041#include "pixman-renderer.h"
Kristian Høgsberg3c2360f2013-01-28 16:01:22 -050042#include "../shared/image-loader.h"
Jonas Ådahle5a12252013-04-05 23:07:11 +020043#include "../shared/os-compatibility.h"
Jason Ekstrand7744f712013-10-27 22:24:55 -050044#include "../shared/cairo-util.h"
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010045
Jason Ekstrand48ce4212013-10-27 22:25:02 -050046#define WINDOW_TITLE "Weston Compositor"
47
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010048struct wayland_compositor {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050049 struct weston_compositor base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010050
51 struct {
Kristian Høgsberg362b6722012-06-18 15:13:51 -040052 struct wl_display *wl_display;
Kristian Høgsbergfa80e112012-10-10 21:34:26 -040053 struct wl_registry *registry;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010054 struct wl_compositor *compositor;
55 struct wl_shell *shell;
Jonas Ådahle5a12252013-04-05 23:07:11 +020056 struct wl_shm *shm;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010057
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010058 struct wl_event_source *wl_source;
59 uint32_t event_mask;
60 } parent;
61
Jason Ekstrandff2fd462013-10-27 22:24:58 -050062 int use_pixman;
63
Jason Ekstrand7744f712013-10-27 22:24:55 -050064 struct theme *theme;
65 cairo_device_t *frame_device;
66 struct wl_cursor_theme *cursor_theme;
67 struct wl_cursor *cursor;
Kristian Høgsberg546a8122012-02-01 07:45:51 -050068
Jason Ekstrand06ced802013-11-07 20:13:29 -060069 struct wl_list input_list;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010070};
71
72struct wayland_output {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050073 struct weston_output base;
74
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010075 struct {
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050076 int draw_initial_frame;
77 struct wl_surface *surface;
78 struct wl_shell_surface *shell_surface;
Jason Ekstrand5ea04802013-11-07 20:13:33 -060079 int configure_width, configure_height;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +010080 } parent;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -050081
Jason Ekstrand7744f712013-10-27 22:24:55 -050082 int keyboard_count;
83
Jason Ekstrand5ea04802013-11-07 20:13:33 -060084 char *name;
Jason Ekstrand7744f712013-10-27 22:24:55 -050085 struct frame *frame;
Jason Ekstrandff2fd462013-10-27 22:24:58 -050086
Jason Ekstrand7744f712013-10-27 22:24:55 -050087 struct {
Jason Ekstrandff2fd462013-10-27 22:24:58 -050088 struct wl_egl_window *egl_window;
89 struct {
90 cairo_surface_t *top;
91 cairo_surface_t *left;
92 cairo_surface_t *right;
93 cairo_surface_t *bottom;
94 } border;
95 } gl;
96
97 struct {
98 struct wl_list buffers;
99 struct wl_list free_buffers;
100 } shm;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500101
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500102 struct weston_mode mode;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500103 uint32_t scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100104};
105
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500106struct wayland_shm_buffer {
107 struct wayland_output *output;
108 struct wl_list link;
109 struct wl_list free_link;
110
111 struct wl_buffer *buffer;
112 void *data;
113 size_t size;
114 pixman_region32_t damage;
115 int frame_damaged;
116
117 pixman_image_t *pm_image;
118 cairo_surface_t *c_surface;
119};
120
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100121struct wayland_input {
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -0400122 struct weston_seat base;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100123 struct wayland_compositor *compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100124 struct wl_list link;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500125
126 struct {
127 struct wl_seat *seat;
128 struct wl_pointer *pointer;
129 struct wl_keyboard *keyboard;
130 struct wl_touch *touch;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500131
132 struct {
133 struct wl_surface *surface;
134 int32_t hx, hy;
135 } cursor;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -0500136 } parent;
137
Daniel Stone50692802012-06-22 13:21:41 +0100138 uint32_t key_serial;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400139 uint32_t enter_serial;
140 int focus;
141 struct wayland_output *output;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500142 struct wayland_output *keyboard_focus;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100143};
144
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +0300145struct gl_renderer_interface *gl_renderer;
146
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500147static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500148wayland_shm_buffer_destroy(struct wayland_shm_buffer *buffer)
149{
150 cairo_surface_destroy(buffer->c_surface);
151 pixman_image_unref(buffer->pm_image);
152
153 wl_buffer_destroy(buffer->buffer);
154 munmap(buffer->data, buffer->size);
155
156 pixman_region32_fini(&buffer->damage);
157
158 wl_list_remove(&buffer->link);
159 wl_list_remove(&buffer->free_link);
160 free(buffer);
161}
162
163static void
164buffer_release(void *data, struct wl_buffer *buffer)
165{
166 struct wayland_shm_buffer *sb = data;
167
168 if (sb->output) {
169 wl_list_insert(&sb->output->shm.free_buffers, &sb->free_link);
170 } else {
171 wayland_shm_buffer_destroy(sb);
172 }
173}
174
175static const struct wl_buffer_listener buffer_listener = {
176 buffer_release
177};
178
179static struct wayland_shm_buffer *
180wayland_output_get_shm_buffer(struct wayland_output *output)
181{
182 struct wayland_compositor *c =
183 (struct wayland_compositor *) output->base.compositor;
184 struct wl_shm *shm = c->parent.shm;
185 struct wayland_shm_buffer *sb;
186
187 struct wl_shm_pool *pool;
188 int width, height, stride;
189 int32_t fx, fy;
190 int fd;
191 unsigned char *data;
192
193 if (!wl_list_empty(&output->shm.free_buffers)) {
194 sb = container_of(output->shm.free_buffers.next,
195 struct wayland_shm_buffer, free_link);
196 wl_list_remove(&sb->free_link);
197 wl_list_init(&sb->free_link);
198
199 return sb;
200 }
201
202 if (output->frame) {
203 width = frame_width(output->frame);
204 height = frame_height(output->frame);
205 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500206 width = output->base.current_mode->width;
207 height = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500208 }
209
210 stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
211
212 fd = os_create_anonymous_file(height * stride);
213 if (fd < 0) {
214 perror("os_create_anonymous_file");
215 return NULL;
216 }
217
218 data = mmap(NULL, height * stride, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
219 if (data == MAP_FAILED) {
220 perror("mmap");
221 close(fd);
222 return NULL;
223 }
224
225 sb = zalloc(sizeof *sb);
226
227 sb->output = output;
228 wl_list_init(&sb->free_link);
229 wl_list_insert(&output->shm.buffers, &sb->link);
230
231 pixman_region32_init_rect(&sb->damage, 0, 0,
232 output->base.width, output->base.height);
233 sb->frame_damaged = 1;
234
235 sb->data = data;
236 sb->size = height * stride;
237
238 pool = wl_shm_create_pool(shm, fd, sb->size);
239
240 sb->buffer = wl_shm_pool_create_buffer(pool, 0,
241 width, height,
242 stride,
243 WL_SHM_FORMAT_ARGB8888);
244 wl_buffer_add_listener(sb->buffer, &buffer_listener, sb);
245 wl_shm_pool_destroy(pool);
246 close(fd);
247
248 memset(data, 0, sb->size);
249
250 sb->c_surface =
251 cairo_image_surface_create_for_data(data, CAIRO_FORMAT_ARGB32,
252 width, height, stride);
253
254 fx = 0;
255 fy = 0;
256 if (output->frame)
257 frame_interior(output->frame, &fx, &fy, 0, 0);
258 sb->pm_image =
259 pixman_image_create_bits(PIXMAN_a8r8g8b8, width, height,
260 (uint32_t *)(data + fy * stride) + fx,
261 stride);
262
263 return sb;
264}
265
266static void
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500267frame_done(void *data, struct wl_callback *callback, uint32_t time)
Kristian Høgsberg33418202011-08-16 23:01:28 -0400268{
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500269 struct weston_output *output = data;
Kristian Høgsberg33418202011-08-16 23:01:28 -0400270
Kristian Høgsbergcdd61d02012-02-07 09:56:15 -0500271 wl_callback_destroy(callback);
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500272 weston_output_finish_frame(output, time);
Kristian Høgsberg33418202011-08-16 23:01:28 -0400273}
274
275static const struct wl_callback_listener frame_listener = {
276 frame_done
277};
278
Kristian Høgsberg06cf6b02012-01-25 23:47:45 -0500279static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200280draw_initial_frame(struct wayland_output *output)
281{
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500282 struct wayland_shm_buffer *sb;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200283
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500284 sb = wayland_output_get_shm_buffer(output);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200285
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500286 /* If we are rendering with GL, then orphan it so that it gets
287 * destroyed immediately */
288 if (output->gl.egl_window)
289 sb->output = NULL;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500290
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500291 wl_surface_attach(output->parent.surface, sb->buffer, 0, 0);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200292
293 /* We only need to damage some part, as its only transparant
294 * pixels anyway. */
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500295 wl_surface_damage(output->parent.surface, 0, 0, 1, 1);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200296}
297
298static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500299wayland_output_update_gl_border(struct wayland_output *output)
300{
301 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
302 cairo_t *cr;
303
304 if (!output->frame)
305 return;
306 if (!(frame_status(output->frame) & FRAME_STATUS_REPAINT))
307 return;
308
309 fwidth = frame_width(output->frame);
310 fheight = frame_height(output->frame);
311 frame_interior(output->frame, &ix, &iy, &iwidth, &iheight);
312
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500313 if (!output->gl.border.top)
314 output->gl.border.top =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500315 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
316 fwidth, iy);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500317 cr = cairo_create(output->gl.border.top);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500318 frame_repaint(output->frame, cr);
319 cairo_destroy(cr);
320 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_TOP,
321 fwidth, iy,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500322 cairo_image_surface_get_stride(output->gl.border.top) / 4,
323 cairo_image_surface_get_data(output->gl.border.top));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500324
325
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500326 if (!output->gl.border.left)
327 output->gl.border.left =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500328 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
329 ix, 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500330 cr = cairo_create(output->gl.border.left);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500331 cairo_translate(cr, 0, -iy);
332 frame_repaint(output->frame, cr);
333 cairo_destroy(cr);
334 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_LEFT,
335 ix, 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500336 cairo_image_surface_get_stride(output->gl.border.left) / 4,
337 cairo_image_surface_get_data(output->gl.border.left));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500338
339
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500340 if (!output->gl.border.right)
341 output->gl.border.right =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500342 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
343 fwidth - (ix + iwidth), 1);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500344 cr = cairo_create(output->gl.border.right);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500345 cairo_translate(cr, -(iwidth + ix), -iy);
346 frame_repaint(output->frame, cr);
347 cairo_destroy(cr);
348 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_RIGHT,
349 fwidth - (ix + iwidth), 1,
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500350 cairo_image_surface_get_stride(output->gl.border.right) / 4,
351 cairo_image_surface_get_data(output->gl.border.right));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500352
353
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500354 if (!output->gl.border.bottom)
355 output->gl.border.bottom =
Jason Ekstrand7744f712013-10-27 22:24:55 -0500356 cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
357 fwidth, fheight - (iy + iheight));
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500358 cr = cairo_create(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500359 cairo_translate(cr, 0, -(iy + iheight));
360 frame_repaint(output->frame, cr);
361 cairo_destroy(cr);
362 gl_renderer->output_set_border(&output->base, GL_RENDERER_BORDER_BOTTOM,
363 fwidth, fheight - (iy + iheight),
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500364 cairo_image_surface_get_stride(output->gl.border.bottom) / 4,
365 cairo_image_surface_get_data(output->gl.border.bottom));
Jason Ekstrand7744f712013-10-27 22:24:55 -0500366}
367
368static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200369wayland_output_start_repaint_loop(struct weston_output *output_base)
370{
371 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrand286ff682013-10-27 22:24:57 -0500372 struct wayland_compositor *wc =
373 (struct wayland_compositor *)output->base.compositor;
Jonas Ådahle5a12252013-04-05 23:07:11 +0200374 struct wl_callback *callback;
375
376 /* If this is the initial frame, we need to attach a buffer so that
377 * the compositor can map the surface and include it in its render
378 * loop. If the surface doesn't end up in the render loop, the frame
379 * callback won't be invoked. The buffer is transparent and of the
380 * same size as the future real output buffer. */
381 if (output->parent.draw_initial_frame) {
382 output->parent.draw_initial_frame = 0;
383
384 draw_initial_frame(output);
385 }
386
387 callback = wl_surface_frame(output->parent.surface);
388 wl_callback_add_listener(callback, &frame_listener, output);
389 wl_surface_commit(output->parent.surface);
Jason Ekstrand286ff682013-10-27 22:24:57 -0500390 wl_display_flush(wc->parent.wl_display);
Jonas Ådahle5a12252013-04-05 23:07:11 +0200391}
392
David Herrmann1edf44c2013-10-22 17:11:26 +0200393static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500394wayland_output_repaint_gl(struct weston_output *output_base,
395 pixman_region32_t *damage)
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400396{
397 struct wayland_output *output = (struct wayland_output *) output_base;
Kristian Høgsbergfa1be022012-09-05 22:49:55 -0400398 struct weston_compositor *ec = output->base.compositor;
Kristian Høgsbergd7c17262012-09-05 21:54:15 -0400399 struct wl_callback *callback;
Scott Moreau062be7e2012-04-20 13:37:33 -0600400
Kristian Høgsberg33418202011-08-16 23:01:28 -0400401 callback = wl_surface_frame(output->parent.surface);
402 wl_callback_add_listener(callback, &frame_listener, output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100403
Jason Ekstrand7744f712013-10-27 22:24:55 -0500404 wayland_output_update_gl_border(output);
405
Pekka Paalanenbc106382012-10-10 12:49:31 +0300406 ec->renderer->repaint_output(&output->base, damage);
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200407
408 pixman_region32_subtract(&ec->primary_plane.damage,
409 &ec->primary_plane.damage, damage);
David Herrmann1edf44c2013-10-22 17:11:26 +0200410 return 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100411}
412
Matt Roper361d2ad2011-08-29 13:52:23 -0700413static void
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500414wayland_output_update_shm_border(struct wayland_shm_buffer *buffer)
415{
416 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
417 cairo_t *cr;
418
419 if (!buffer->output->frame || !buffer->frame_damaged)
420 return;
421
422 cr = cairo_create(buffer->c_surface);
423
424 frame_interior(buffer->output->frame, &ix, &iy, &iwidth, &iheight);
425 fwidth = frame_width(buffer->output->frame);
426 fheight = frame_height(buffer->output->frame);
427
428 /* Set the clip so we don't unnecisaraly damage the surface */
429 cairo_move_to(cr, ix, iy);
430 cairo_rel_line_to(cr, iwidth, 0);
431 cairo_rel_line_to(cr, 0, iheight);
432 cairo_rel_line_to(cr, -iwidth, 0);
433 cairo_line_to(cr, ix, iy);
434 cairo_line_to(cr, 0, iy);
435 cairo_line_to(cr, 0, fheight);
436 cairo_line_to(cr, fwidth, fheight);
437 cairo_line_to(cr, fwidth, 0);
438 cairo_line_to(cr, 0, 0);
439 cairo_line_to(cr, 0, iy);
440 cairo_close_path(cr);
441 cairo_clip(cr);
442
443 /* Draw using a pattern so that the final result gets clipped */
444 cairo_push_group(cr);
445 frame_repaint(buffer->output->frame, cr);
446 cairo_pop_group_to_source(cr);
447 cairo_set_operator(cr, CAIRO_OPERATOR_SOURCE);
448 cairo_paint(cr);
449
450 cairo_destroy(cr);
451}
452
453static void
454wayland_shm_buffer_attach(struct wayland_shm_buffer *sb)
455{
456 pixman_region32_t damage;
457 pixman_box32_t *rects;
458 int32_t ix, iy, iwidth, iheight, fwidth, fheight;
459 int i, n;
460
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500461 pixman_region32_init(&damage);
462 weston_transformed_region(sb->output->base.width,
463 sb->output->base.height,
464 sb->output->base.transform,
465 sb->output->base.current_scale,
466 &sb->damage, &damage);
467
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500468 if (sb->output->frame) {
469 frame_interior(sb->output->frame, &ix, &iy, &iwidth, &iheight);
470 fwidth = frame_width(sb->output->frame);
471 fheight = frame_height(sb->output->frame);
472
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500473 pixman_region32_translate(&damage, ix, iy);
474
475 if (sb->frame_damaged) {
476 pixman_region32_union_rect(&damage, &damage,
477 0, 0, fwidth, iy);
478 pixman_region32_union_rect(&damage, &damage,
479 0, iy, ix, iheight);
480 pixman_region32_union_rect(&damage, &damage,
481 ix + iwidth, iy,
482 fwidth - (ix + iwidth), iheight);
483 pixman_region32_union_rect(&damage, &damage,
484 0, iy + iheight,
485 fwidth, fheight - (iy + iheight));
486 }
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500487 }
488
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500489 rects = pixman_region32_rectangles(&damage, &n);
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500490 wl_surface_attach(sb->output->parent.surface, sb->buffer, 0, 0);
491 for (i = 0; i < n; ++i)
492 wl_surface_damage(sb->output->parent.surface, rects[i].x1,
493 rects[i].y1, rects[i].x2 - rects[i].x1,
494 rects[i].y2 - rects[i].y1);
495
496 if (sb->output->frame)
497 pixman_region32_fini(&damage);
498}
499
500static int
501wayland_output_repaint_pixman(struct weston_output *output_base,
502 pixman_region32_t *damage)
503{
504 struct wayland_output *output = (struct wayland_output *) output_base;
505 struct wayland_compositor *c =
506 (struct wayland_compositor *)output->base.compositor;
507 struct wl_callback *callback;
508 struct wayland_shm_buffer *sb;
509
510 if (output->frame) {
511 if (frame_status(output->frame) & FRAME_STATUS_REPAINT)
512 wl_list_for_each(sb, &output->shm.buffers, link)
513 sb->frame_damaged = 1;
514 }
515
516 wl_list_for_each(sb, &output->shm.buffers, link)
517 pixman_region32_union(&sb->damage, &sb->damage, damage);
518
519 sb = wayland_output_get_shm_buffer(output);
520
521 wayland_output_update_shm_border(sb);
522 pixman_renderer_output_set_buffer(output_base, sb->pm_image);
523 c->base.renderer->repaint_output(output_base, &sb->damage);
524
525 wayland_shm_buffer_attach(sb);
526
527 callback = wl_surface_frame(output->parent.surface);
528 wl_callback_add_listener(callback, &frame_listener, output);
529 wl_surface_commit(output->parent.surface);
530 wl_display_flush(c->parent.wl_display);
531
532 pixman_region32_fini(&sb->damage);
533 pixman_region32_init(&sb->damage);
534 sb->frame_damaged = 0;
535
536 pixman_region32_subtract(&c->base.primary_plane.damage,
537 &c->base.primary_plane.damage, damage);
538 return 0;
539}
540
541static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500542wayland_output_destroy(struct weston_output *output_base)
Matt Roper361d2ad2011-08-29 13:52:23 -0700543{
544 struct wayland_output *output = (struct wayland_output *) output_base;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500545 struct wayland_compositor *c =
546 (struct wayland_compositor *) output->base.compositor;
Matt Roper361d2ad2011-08-29 13:52:23 -0700547
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500548 if (c->use_pixman) {
549 pixman_renderer_output_destroy(output_base);
550 } else {
551 gl_renderer->output_destroy(output_base);
552 }
John Kåre Alsaker94659272012-11-13 19:10:18 +0100553
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500554 wl_egl_window_destroy(output->gl.egl_window);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500555 wl_surface_destroy(output->parent.surface);
556 wl_shell_surface_destroy(output->parent.shell_surface);
557
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600558 if (output->frame)
Jason Ekstrand7744f712013-10-27 22:24:55 -0500559 frame_destroy(output->frame);
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600560
561 cairo_surface_destroy(output->gl.border.top);
562 cairo_surface_destroy(output->gl.border.left);
563 cairo_surface_destroy(output->gl.border.right);
564 cairo_surface_destroy(output->gl.border.bottom);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500565
566 weston_output_destroy(&output->base);
Matt Roper361d2ad2011-08-29 13:52:23 -0700567 free(output);
568
569 return;
570}
571
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300572static const struct wl_shell_surface_listener shell_surface_listener;
573
Benjamin Franzke431da9a2011-04-20 11:02:58 +0200574static int
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500575wayland_output_init_gl_renderer(struct wayland_output *output)
576{
Jason Ekstrand00b84282013-10-27 22:24:59 -0500577 int32_t fwidth = 0, fheight = 0;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500578
579 if (output->frame) {
580 fwidth = frame_width(output->frame);
581 fheight = frame_height(output->frame);
582 } else {
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500583 fwidth = output->base.current_mode->width;
584 fheight = output->base.current_mode->height;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500585 }
586
587 output->gl.egl_window =
588 wl_egl_window_create(output->parent.surface,
589 fwidth, fheight);
590 if (!output->gl.egl_window) {
591 weston_log("failure to create wl_egl_window\n");
592 return -1;
593 }
594
595 if (gl_renderer->output_create(&output->base,
Neil Roberts77c1a5b2014-03-07 18:05:50 +0000596 output->gl.egl_window,
597 gl_renderer->alpha_attribs,
598 NULL) < 0)
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500599 goto cleanup_window;
600
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500601 return 0;
602
603cleanup_window:
604 wl_egl_window_destroy(output->gl.egl_window);
605 return -1;
606}
607
608static int
609wayland_output_init_pixman_renderer(struct wayland_output *output)
610{
611 return pixman_renderer_output_create(&output->base);
612}
613
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600614static void
615wayland_output_resize_surface(struct wayland_output *output)
616{
617 struct wayland_compositor *c =
618 (struct wayland_compositor *)output->base.compositor;
619 struct wayland_shm_buffer *buffer, *next;
620 int32_t ix, iy, iwidth, iheight;
621 int32_t width, height;
622 struct wl_region *region;
623
624 width = output->base.current_mode->width;
625 height = output->base.current_mode->height;
626
627 if (output->frame) {
628 frame_resize_inside(output->frame, width, height);
629
630 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
631 region = wl_compositor_create_region(c->parent.compositor);
632 wl_region_add(region, ix, iy, iwidth, iheight);
633 wl_surface_set_input_region(output->parent.surface, region);
634 wl_region_destroy(region);
635
636 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
637 region = wl_compositor_create_region(c->parent.compositor);
638 wl_region_add(region, ix, iy, iwidth, iheight);
639 wl_surface_set_opaque_region(output->parent.surface, region);
640 wl_region_destroy(region);
641
642 width = frame_width(output->frame);
643 height = frame_height(output->frame);
644 } else {
645 region = wl_compositor_create_region(c->parent.compositor);
646 wl_region_add(region, 0, 0, width, height);
647 wl_surface_set_input_region(output->parent.surface, region);
648 wl_region_destroy(region);
649
650 region = wl_compositor_create_region(c->parent.compositor);
651 wl_region_add(region, 0, 0, width, height);
652 wl_surface_set_opaque_region(output->parent.surface, region);
653 wl_region_destroy(region);
654 }
655
656 if (output->gl.egl_window) {
657 wl_egl_window_resize(output->gl.egl_window,
658 width, height, 0, 0);
659
660 /* These will need to be re-created due to the resize */
661 gl_renderer->output_set_border(&output->base,
662 GL_RENDERER_BORDER_TOP,
663 0, 0, 0, NULL);
664 cairo_surface_destroy(output->gl.border.top);
665 output->gl.border.top = NULL;
666 gl_renderer->output_set_border(&output->base,
667 GL_RENDERER_BORDER_LEFT,
668 0, 0, 0, NULL);
669 cairo_surface_destroy(output->gl.border.left);
670 output->gl.border.left = NULL;
671 gl_renderer->output_set_border(&output->base,
672 GL_RENDERER_BORDER_RIGHT,
673 0, 0, 0, NULL);
674 cairo_surface_destroy(output->gl.border.right);
675 output->gl.border.right = NULL;
676 gl_renderer->output_set_border(&output->base,
677 GL_RENDERER_BORDER_BOTTOM,
678 0, 0, 0, NULL);
679 cairo_surface_destroy(output->gl.border.bottom);
680 output->gl.border.bottom = NULL;
681 }
682
683 /* Throw away any remaining SHM buffers */
684 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, link)
685 wayland_shm_buffer_destroy(buffer);
686 /* These will get thrown away when they get released */
687 wl_list_for_each(buffer, &output->shm.buffers, link)
688 buffer->output = NULL;
689}
690
691static int
692wayland_output_set_windowed(struct wayland_output *output)
693{
694 struct wayland_compositor *c =
695 (struct wayland_compositor *)output->base.compositor;
696 int tlen;
697 char *title;
698
699 if (output->frame)
700 return 0;
701
702 if (output->name) {
703 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
704 title = malloc(tlen + 1);
705 if (!title)
706 return -1;
707
708 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
709 } else {
710 title = strdup(WINDOW_TITLE);
711 }
712
713 if (!c->theme) {
714 c->theme = theme_create();
715 if (!c->theme) {
716 free(title);
717 return -1;
718 }
719 }
720 output->frame = frame_create(c->theme, 100, 100,
721 FRAME_BUTTON_CLOSE, title);
722 free(title);
723 if (!output->frame)
724 return -1;
725
726 if (output->keyboard_count)
727 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
728
729 wayland_output_resize_surface(output);
730
731 wl_shell_surface_set_toplevel(output->parent.shell_surface);
732
733 return 0;
734}
735
736static void
737wayland_output_set_fullscreen(struct wayland_output *output,
738 enum wl_shell_surface_fullscreen_method method,
739 uint32_t framerate, struct wl_output *target)
740{
741 if (output->frame) {
742 frame_destroy(output->frame);
743 output->frame = NULL;
744 }
745
746 wayland_output_resize_surface(output);
747
748 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
749 method, framerate, target);
750}
751
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500752static struct wayland_output *
753wayland_output_create(struct wayland_compositor *c, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600754 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500755 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100756{
757 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500758 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600759
760 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
761 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100762
Peter Huttererf3d62272013-08-08 11:57:05 +1000763 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100764 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500765 return NULL;
766
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600767 output->name = name ? strdup(name) : NULL;
768 output->base.make = "waywayland";
769 output->base.model = "none";
770
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500771 output_width = width * scale;
772 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100773
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600774 output->parent.surface =
775 wl_compositor_create_surface(c->parent.compositor);
776 if (!output->parent.surface)
777 goto err_name;
778 wl_surface_set_user_data(output->parent.surface, output);
779
780 output->parent.draw_initial_frame = 1;
781 output->parent.shell_surface =
782 wl_shell_get_shell_surface(c->parent.shell,
783 output->parent.surface);
784 if (!output->parent.shell_surface)
785 goto err_surface;
786 wl_shell_surface_add_listener(output->parent.shell_surface,
787 &shell_surface_listener, output);
788
789 if (fullscreen) {
790 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
791 0, 0, NULL);
792 wl_display_roundtrip(c->parent.wl_display);
793 if (!width)
794 output_width = output->parent.configure_width;
795 if (!height)
796 output_height = output->parent.configure_height;
797 }
798
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400799 output->mode.flags =
800 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500801 output->mode.width = output_width;
802 output->mode.height = output_height;
803 output->mode.refresh = 60000;
804 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400805 wl_list_init(&output->base.mode_list);
806 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +0200807 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500808
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500809 wl_list_init(&output->shm.buffers);
810 wl_list_init(&output->shm.free_buffers);
811
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600812 weston_output_init(&output->base, &c->base, x, y, width, height,
813 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100814
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500815 if (c->use_pixman) {
816 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600817 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500818 output->base.repaint = wayland_output_repaint_pixman;
819 } else {
820 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600821 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500822 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100823 }
824
Jonas Ådahle5a12252013-04-05 23:07:11 +0200825 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -0700826 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800827 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200828 output->base.set_backlight = NULL;
829 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800830 output->base.switch_mode = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100831
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100832 wl_list_insert(c->base.output_list.prev, &output->base.link);
833
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500834 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100835
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600836err_output:
837 weston_output_destroy(&output->base);
838 wl_shell_surface_destroy(output->parent.shell_surface);
839err_surface:
840 wl_surface_destroy(output->parent.surface);
841err_name:
842 free(output->name);
843
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500844 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +0100845 free(output);
846
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500847 return NULL;
848}
849
850static struct wayland_output *
851wayland_output_create_for_config(struct wayland_compositor *c,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500852 struct weston_config_section *config_section,
Jason Ekstrand0cf39352013-11-07 20:13:31 -0600853 int option_width, int option_height,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -0600854 int option_scale, int32_t x, int32_t y)
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500855{
856 struct wayland_output *output;
857 char *mode, *t, *name, *str;
858 int width, height, scale;
859 uint32_t transform;
860 unsigned int i, slen;
861
862 static const struct { const char *name; uint32_t token; } transform_names[] = {
863 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
864 { "90", WL_OUTPUT_TRANSFORM_90 },
865 { "180", WL_OUTPUT_TRANSFORM_180 },
866 { "270", WL_OUTPUT_TRANSFORM_270 },
867 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
868 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
869 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
870 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
871 };
872
873 weston_config_section_get_string(config_section, "name", &name, NULL);
874 if (name) {
875 slen = strlen(name);
876 slen += strlen(WINDOW_TITLE " - ");
877 str = malloc(slen + 1);
878 if (str)
879 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
880 free(name);
881 name = str;
882 }
883 if (!name)
U. Artie Eoff1a08d112014-01-17 12:22:50 -0800884 name = strdup(WINDOW_TITLE);
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500885
886 weston_config_section_get_string(config_section,
887 "mode", &mode, "1024x600");
888 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
889 weston_log("Invalid mode \"%s\" for output %s\n",
890 mode, name);
891 width = 1024;
892 height = 640;
893 }
894 free(mode);
895
Jason Ekstrand0cf39352013-11-07 20:13:31 -0600896 if (option_width)
897 width = option_width;
898 if (option_height)
899 height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500900
901 weston_config_section_get_int(config_section, "scale", &scale, 1);
902
Jason Ekstrand12c6dd92013-11-07 20:13:32 -0600903 if (option_scale)
904 scale = option_scale;
905
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500906 weston_config_section_get_string(config_section,
907 "transform", &t, "normal");
908 transform = WL_OUTPUT_TRANSFORM_NORMAL;
909 for (i = 0; i < ARRAY_LENGTH(transform_names); i++) {
910 if (strcmp(transform_names[i].name, t) == 0) {
911 transform = transform_names[i].token;
912 break;
913 }
914 }
915 if (i >= ARRAY_LENGTH(transform_names))
916 weston_log("Invalid transform \"%s\" for output %s\n", t, name);
917 free(t);
918
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600919 output = wayland_output_create(c, x, y, width, height, name, 0,
920 transform, scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500921 free(name);
922
923 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100924}
925
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300926static void
927shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
928 uint32_t serial)
929{
930 wl_shell_surface_pong(shell_surface, serial);
931}
932
933static void
934shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
935 uint32_t edges, int32_t width, int32_t height)
936{
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600937 struct wayland_output *output = data;
938
939 output->parent.configure_width = width;
940 output->parent.configure_height = height;
941
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300942 /* FIXME: implement resizing */
943}
944
945static void
946shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
947{
948}
949
950static const struct wl_shell_surface_listener shell_surface_listener = {
951 shell_surface_ping,
952 shell_surface_configure,
953 shell_surface_popup_done
954};
955
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100956/* Events received from the wayland-server this compositor is client of: */
957
Jason Ekstrand7744f712013-10-27 22:24:55 -0500958/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400959static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500960input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400961{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400962
Jason Ekstrand7744f712013-10-27 22:24:55 -0500963 struct wl_buffer *buffer;
964 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400965
Jason Ekstrand7744f712013-10-27 22:24:55 -0500966 if (!input->compositor->cursor)
967 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400968
Jason Ekstrand7744f712013-10-27 22:24:55 -0500969 image = input->compositor->cursor->images[0];
970 buffer = wl_cursor_image_get_buffer(image);
Hardening842a36a2014-03-18 14:12:50 +0100971 if (!buffer)
972 return;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500973
974 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
975 input->parent.cursor.surface,
976 image->hotspot_x, image->hotspot_y);
977
978 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
979 wl_surface_damage(input->parent.cursor.surface, 0, 0,
980 image->width, image->height);
981 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400982}
983
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100984static void
Daniel Stone37816df2012-05-16 18:45:18 +0100985input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
986 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400987 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100988{
989 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500990 int32_t fx, fy;
991 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100992
Daniel Stone50692802012-06-22 13:21:41 +0100993 /* XXX: If we get a modifier event immediately before the focus,
994 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400995 input->enter_serial = serial;
996 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500997
998 if (input->output->frame) {
999 location = frame_pointer_enter(input->output->frame, input,
1000 wl_fixed_to_int(x),
1001 wl_fixed_to_int(y));
1002 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1003 x -= wl_fixed_from_int(fx);
1004 y -= wl_fixed_from_int(fy);
1005
1006 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1007 weston_output_schedule_repaint(&input->output->base);
1008 } else {
1009 location = THEME_LOCATION_CLIENT_AREA;
1010 }
1011
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001012 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001013
1014 if (location == THEME_LOCATION_CLIENT_AREA) {
1015 input->focus = 1;
1016 notify_pointer_focus(&input->base, &input->output->base, x, y);
1017 wl_pointer_set_cursor(input->parent.pointer,
1018 input->enter_serial, NULL, 0, 0);
1019 } else {
1020 input->focus = 0;
1021 notify_pointer_focus(&input->base, NULL, 0, 0);
1022 input_set_cursor(input);
1023 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001024}
1025
1026static void
Daniel Stone37816df2012-05-16 18:45:18 +01001027input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1028 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001029{
1030 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001031
Jason Ekstrand7744f712013-10-27 22:24:55 -05001032 if (input->output->frame) {
1033 frame_pointer_leave(input->output->frame, input);
1034
1035 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1036 weston_output_schedule_repaint(&input->output->base);
1037 }
1038
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001039 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001040 input->output = NULL;
1041 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001042}
1043
1044static void
Daniel Stone37816df2012-05-16 18:45:18 +01001045input_handle_motion(void *data, struct wl_pointer *pointer,
1046 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1047{
1048 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001049 int32_t fx, fy;
1050 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001051
Jason Ekstrand7744f712013-10-27 22:24:55 -05001052 if (input->output->frame) {
1053 location = frame_pointer_motion(input->output->frame, input,
1054 wl_fixed_to_int(x),
1055 wl_fixed_to_int(y));
1056 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1057 x -= wl_fixed_from_int(fx);
1058 y -= wl_fixed_from_int(fy);
1059
1060 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1061 weston_output_schedule_repaint(&input->output->base);
1062 } else {
1063 location = THEME_LOCATION_CLIENT_AREA;
1064 }
1065
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001066 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001067
1068 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
1069 input_set_cursor(input);
1070 notify_pointer_focus(&input->base, NULL, 0, 0);
1071 input->focus = 0;
1072 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
1073 wl_pointer_set_cursor(input->parent.pointer,
1074 input->enter_serial, NULL, 0, 0);
1075 notify_pointer_focus(&input->base, &input->output->base, x, y);
1076 input->focus = 1;
1077 }
1078
1079 if (location == THEME_LOCATION_CLIENT_AREA)
1080 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +01001081}
1082
1083static void
1084input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001085 uint32_t serial, uint32_t time, uint32_t button,
1086 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001087{
1088 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001089 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001090 enum frame_button_state fstate;
1091 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001092
Jason Ekstrand7744f712013-10-27 22:24:55 -05001093 if (input->output->frame) {
1094 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1095 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1096
1097 location = frame_pointer_button(input->output->frame, input,
1098 button, fstate);
1099
1100 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1101
1102 wl_shell_surface_move(input->output->parent.shell_surface,
1103 input->parent.seat, serial);
1104 frame_status_clear(input->output->frame,
1105 FRAME_STATUS_MOVE);
1106 return;
1107 }
1108
1109 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE)
1110 wl_display_terminate(input->compositor->base.wl_display);
1111
1112 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1113 weston_output_schedule_repaint(&input->output->base);
1114 } else {
1115 location = THEME_LOCATION_CLIENT_AREA;
1116 }
1117
1118 if (location == THEME_LOCATION_CLIENT_AREA)
1119 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +01001120}
1121
1122static void
1123input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001124 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001125{
1126 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001127
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001128 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +01001129}
1130
1131static const struct wl_pointer_listener pointer_listener = {
1132 input_handle_pointer_enter,
1133 input_handle_pointer_leave,
1134 input_handle_motion,
1135 input_handle_button,
1136 input_handle_axis,
1137};
1138
1139static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001140input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1141 int fd, uint32_t size)
1142{
1143 struct wayland_input *input = data;
1144 struct xkb_keymap *keymap;
1145 char *map_str;
1146
1147 if (!data) {
1148 close(fd);
1149 return;
1150 }
1151
1152 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1153 close(fd);
1154 return;
1155 }
1156
1157 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1158 if (map_str == MAP_FAILED) {
1159 close(fd);
1160 return;
1161 }
1162
1163 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
1164 map_str,
1165 XKB_KEYMAP_FORMAT_TEXT_V1,
1166 0);
1167 munmap(map_str, size);
1168 close(fd);
1169
1170 if (!keymap) {
Martin Minarik6d118362012-06-07 18:01:59 +02001171 weston_log("failed to compile keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001172 return;
1173 }
1174
Rui Matos0c194ce2013-10-10 19:44:21 +02001175 if (input->base.keyboard)
1176 weston_seat_update_keymap(&input->base, keymap);
1177 else
1178 weston_seat_init_keyboard(&input->base, keymap);
1179
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001180 xkb_map_unref(keymap);
1181}
1182
1183static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001184input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001185 struct wl_keyboard *keyboard,
1186 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001187 struct wl_surface *surface,
1188 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001189{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001190 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001191 struct wayland_output *focus;
1192
1193 focus = input->keyboard_focus;
1194 if (focus) {
1195 /* This shouldn't happen */
1196 focus->keyboard_count--;
1197 if (!focus->keyboard_count && focus->frame)
1198 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1199 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1200 weston_output_schedule_repaint(&focus->base);
1201 }
1202
1203 input->keyboard_focus = wl_surface_get_user_data(surface);
1204 input->keyboard_focus->keyboard_count++;
1205
1206 focus = input->keyboard_focus;
1207 if (focus->frame) {
1208 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1209 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1210 weston_output_schedule_repaint(&focus->base);
1211 }
1212
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001213
Daniel Stone50692802012-06-22 13:21:41 +01001214 /* XXX: If we get a modifier event immediately before the focus,
1215 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001216 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001217 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001218}
1219
1220static void
1221input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001222 struct wl_keyboard *keyboard,
1223 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001224 struct wl_surface *surface)
1225{
1226 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001227 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001228
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001229 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001230
1231 focus = input->keyboard_focus;
1232 if (!focus)
1233 return; /* This shouldn't happen */
1234
1235 focus->keyboard_count--;
1236 if (!focus->keyboard_count && focus->frame) {
1237 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1238 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1239 weston_output_schedule_repaint(&focus->base);
1240 }
1241
1242 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001243}
1244
Daniel Stone37816df2012-05-16 18:45:18 +01001245static void
1246input_handle_key(void *data, struct wl_keyboard *keyboard,
1247 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1248{
1249 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001250
Daniel Stone50692802012-06-22 13:21:41 +01001251 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001252 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001253 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001254 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stone50692802012-06-22 13:21:41 +01001255 STATE_UPDATE_NONE);
Daniel Stone37816df2012-05-16 18:45:18 +01001256}
1257
Daniel Stone351eb612012-05-31 15:27:47 -04001258static void
1259input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001260 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001261 uint32_t mods_latched, uint32_t mods_locked,
1262 uint32_t group)
1263{
Daniel Stone50692802012-06-22 13:21:41 +01001264 struct wayland_input *input = data;
1265 struct wayland_compositor *c = input->compositor;
1266 uint32_t serial_out;
1267
1268 /* If we get a key event followed by a modifier event with the
1269 * same serial number, then we try to preserve those semantics by
1270 * reusing the same serial number on the way out too. */
1271 if (serial_in == input->key_serial)
1272 serial_out = wl_display_get_serial(c->base.wl_display);
1273 else
1274 serial_out = wl_display_next_serial(c->base.wl_display);
1275
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001276 xkb_state_update_mask(input->base.keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001277 mods_depressed, mods_latched,
1278 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001279 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001280}
1281
Daniel Stone37816df2012-05-16 18:45:18 +01001282static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001283 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001284 input_handle_keyboard_enter,
1285 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001286 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001287 input_handle_modifiers,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001288};
1289
1290static void
Daniel Stone37816df2012-05-16 18:45:18 +01001291input_handle_capabilities(void *data, struct wl_seat *seat,
1292 enum wl_seat_capability caps)
1293{
1294 struct wayland_input *input = data;
1295
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001296 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1297 input->parent.pointer = wl_seat_get_pointer(seat);
1298 wl_pointer_set_user_data(input->parent.pointer, input);
1299 wl_pointer_add_listener(input->parent.pointer,
1300 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001301 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001302 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1303 wl_pointer_destroy(input->parent.pointer);
1304 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001305 }
1306
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001307 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1308 input->parent.keyboard = wl_seat_get_keyboard(seat);
1309 wl_keyboard_set_user_data(input->parent.keyboard, input);
1310 wl_keyboard_add_listener(input->parent.keyboard,
1311 &keyboard_listener, input);
1312 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1313 wl_keyboard_destroy(input->parent.keyboard);
1314 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001315 }
1316}
1317
1318static const struct wl_seat_listener seat_listener = {
1319 input_handle_capabilities,
1320};
1321
1322static void
1323display_add_seat(struct wayland_compositor *c, uint32_t id)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001324{
1325 struct wayland_input *input;
1326
Peter Huttererf3d62272013-08-08 11:57:05 +10001327 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001328 if (input == NULL)
1329 return;
1330
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001331 weston_seat_init(&input->base, &c->base, "default");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001332 input->compositor = c;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001333 input->parent.seat = wl_registry_bind(c->parent.registry, id,
1334 &wl_seat_interface, 1);
Jason Ekstrand06ced802013-11-07 20:13:29 -06001335 wl_list_insert(c->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001336
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001337 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1338 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001339
1340 input->parent.cursor.surface =
1341 wl_compositor_create_surface(c->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001342}
1343
1344static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001345registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1346 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001347{
1348 struct wayland_compositor *c = data;
1349
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001350 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001351 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001352 wl_registry_bind(registry, name,
1353 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001354 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001355 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001356 wl_registry_bind(registry, name,
1357 &wl_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01001358 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001359 display_add_seat(c, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001360 } else if (strcmp(interface, "wl_shm") == 0) {
1361 c->parent.shm =
1362 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001363 }
1364}
1365
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001366static const struct wl_registry_listener registry_listener = {
1367 registry_handle_global
1368};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001369
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001370static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001371wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1372{
1373 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001374 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001375
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001376 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1377 wl_display_terminate(c->base.wl_display);
1378 return 0;
1379 }
1380
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001381 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001382 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05001383 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001384 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001385
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001386 if (mask == 0) {
1387 count = wl_display_dispatch_pending(c->parent.wl_display);
1388 wl_display_flush(c->parent.wl_display);
1389 }
1390
1391 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001392}
1393
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001394static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001395wayland_restore(struct weston_compositor *ec)
1396{
1397}
1398
1399static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001400wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001401{
Jonas Ådahle5a12252013-04-05 23:07:11 +02001402 struct wayland_compositor *c = (struct wayland_compositor *) ec;
1403
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001404 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001405
Jonas Ådahle5a12252013-04-05 23:07:11 +02001406 if (c->parent.shm)
1407 wl_shm_destroy(c->parent.shm);
1408
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001409 free(ec);
1410}
1411
Jason Ekstrand7744f712013-10-27 22:24:55 -05001412static const char *left_ptrs[] = {
1413 "left_ptr",
1414 "default",
1415 "top_left_arrow",
1416 "left-arrow"
1417};
1418
1419static void
1420create_cursor(struct wayland_compositor *c, struct weston_config *config)
1421{
1422 struct weston_config_section *s;
1423 int size;
1424 char *theme = NULL;
1425 unsigned int i;
1426
1427 s = weston_config_get_section(config, "shell", NULL, NULL);
1428 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1429 weston_config_section_get_int(s, "cursor-size", &size, 32);
1430
1431 c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
Hardening842a36a2014-03-18 14:12:50 +01001432 if (!c->cursor_theme) {
1433 fprintf(stderr, "could not load cursor theme\n");
1434 return;
1435 }
Jason Ekstrand7744f712013-10-27 22:24:55 -05001436
U. Artie Eoffff755002014-01-17 12:36:58 -08001437 free(theme);
1438
Jason Ekstrand7744f712013-10-27 22:24:55 -05001439 c->cursor = NULL;
1440 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1441 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
1442 left_ptrs[i]);
1443 if (!c->cursor) {
1444 fprintf(stderr, "could not load left cursor\n");
1445 return;
1446 }
1447}
1448
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001449static void
1450fullscreen_binding(struct weston_seat *seat_base, uint32_t time, uint32_t key,
1451 void *data)
1452{
1453 struct wayland_compositor *c = data;
1454 struct wayland_input *input = NULL;
1455
1456 wl_list_for_each(input, &c->input_list, link)
1457 if (&input->base == seat_base)
1458 break;
1459
1460 if (!input || !input->output)
1461 return;
1462
1463 if (input->output->frame)
1464 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
1465 else
1466 wayland_output_set_windowed(input->output);
1467
1468 weston_output_schedule_repaint(&input->output->base);
1469}
1470
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001471static struct wayland_compositor *
1472wayland_compositor_create(struct wl_display *display, int use_pixman,
1473 const char *display_name, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001474 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001475{
1476 struct wayland_compositor *c;
1477 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001478 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001479
Peter Huttererf3d62272013-08-08 11:57:05 +10001480 c = zalloc(sizeof *c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001481 if (c == NULL)
1482 return NULL;
1483
Daniel Stone725c2c32012-06-22 14:04:36 +01001484 if (weston_compositor_init(&c->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001485 config) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +02001486 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001487
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001488 c->parent.wl_display = wl_display_connect(display_name);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001489
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001490 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001491 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001492 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001493 }
1494
Jason Ekstrand06ced802013-11-07 20:13:29 -06001495 wl_list_init(&c->input_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001496 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1497 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001498 wl_display_roundtrip(c->parent.wl_display);
1499
1500 create_cursor(c, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001501
1502 c->base.wl_display = display;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001503
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001504 c->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001505
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001506 if (!c->use_pixman) {
1507 gl_renderer = weston_load_module("gl-renderer.so",
1508 "gl_renderer_interface");
1509 if (!gl_renderer)
1510 c->use_pixman = 1;
1511 }
1512
1513 if (!c->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001514 if (gl_renderer->create(&c->base, c->parent.wl_display,
1515 gl_renderer->alpha_attribs,
1516 NULL) < 0) {
1517 weston_log("Failed to initialize the GL renderer; "
1518 "falling back to pixman.\n");
1519 c->use_pixman = 1;
1520 }
1521 }
1522
1523 if (c->use_pixman) {
1524 if (pixman_renderer_init(&c->base) < 0) {
1525 weston_log("Failed to initialize pixman renderer\n");
1526 goto err_display;
1527 }
1528 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001529
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001530 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001531 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001532
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001533 loop = wl_display_get_event_loop(c->base.wl_display);
1534
1535 fd = wl_display_get_fd(c->parent.wl_display);
1536 c->parent.wl_source =
1537 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1538 wayland_compositor_handle_event, c);
1539 if (c->parent.wl_source == NULL)
1540 goto err_renderer;
1541
1542 wl_event_source_check(c->parent.wl_source);
1543
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001544 weston_compositor_add_key_binding(&c->base, KEY_F,
1545 MODIFIER_CTRL | MODIFIER_ALT,
1546 fullscreen_binding, c);
1547
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001548 return c;
1549err_renderer:
1550 c->base.renderer->destroy(&c->base);
1551err_display:
1552 wl_display_disconnect(c->parent.wl_display);
1553err_compositor:
1554 weston_compositor_shutdown(&c->base);
1555err_free:
1556 free(c);
1557 return NULL;
1558}
1559
1560static void
1561wayland_compositor_destroy(struct wayland_compositor *c)
1562{
1563 struct weston_output *output;
1564
1565 wl_list_for_each(output, &c->base.output_list, link)
1566 wayland_output_destroy(output);
1567
1568 c->base.renderer->destroy(&c->base);
1569 wl_display_disconnect(c->parent.wl_display);
1570
1571 if (c->theme)
1572 theme_destroy(c->theme);
1573 if (c->frame_device)
1574 cairo_device_destroy(c->frame_device);
1575 wl_cursor_theme_destroy(c->cursor_theme);
1576
1577 weston_compositor_shutdown(&c->base);
1578 free(c);
1579}
1580
1581WL_EXPORT struct weston_compositor *
1582backend_init(struct wl_display *display, int *argc, char *argv[],
1583 struct weston_config *config)
1584{
1585 struct wayland_compositor *c;
1586 struct wayland_output *output;
1587 struct weston_config_section *section;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001588 int x, count, width, height, scale, use_pixman, fullscreen;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001589 const char *section_name, *display_name;
1590 char *name;
1591
1592 const struct weston_option wayland_options[] = {
1593 { WESTON_OPTION_INTEGER, "width", 0, &width },
1594 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001595 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001596 { WESTON_OPTION_STRING, "display", 0, &display_name },
1597 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
1598 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001599 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001600 };
1601
1602 width = 0;
1603 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001604 scale = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001605 display_name = NULL;
1606 use_pixman = 0;
1607 count = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001608 fullscreen = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001609 parse_options(wayland_options,
1610 ARRAY_LENGTH(wayland_options), argc, argv);
1611
1612 c = wayland_compositor_create(display, use_pixman, display_name,
1613 argc, argv, config);
1614 if (!c)
1615 return NULL;
1616
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001617 if (fullscreen) {
1618 output = wayland_output_create(c, 0, 0, width, height,
1619 NULL, 1, 0, 1);
1620 if (!output)
1621 goto err_outputs;
1622
Axel Davydd8b88d2013-11-17 21:34:16 +01001623 wayland_output_set_fullscreen(output, 0, 0, NULL);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001624 return &c->base;
1625 }
1626
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001627 section = NULL;
1628 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001629 while (weston_config_next_section(config, &section, &section_name)) {
1630 if (!section_name || strcmp(section_name, "output") != 0)
1631 continue;
1632 weston_config_section_get_string(section, "name", &name, NULL);
1633 if (name == NULL)
1634 continue;
1635
1636 if (name[0] != 'W' || name[1] != 'L') {
1637 free(name);
1638 continue;
1639 }
1640 free(name);
1641
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001642 output = wayland_output_create_for_config(c, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001643 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001644 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001645 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001646 if (wayland_output_set_windowed(output))
1647 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001648
1649 x += output->base.width;
1650 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001651 }
1652
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001653 if (!width)
1654 width = 1024;
1655 if (!height)
1656 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001657 if (!scale)
1658 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001659 while (count > 0) {
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001660 output = wayland_output_create(c, x, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001661 NULL, 0, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001662 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001663 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001664 if (wayland_output_set_windowed(output))
1665 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001666
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001667 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001668 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001669 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001670
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001671 return &c->base;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001672
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001673err_outputs:
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001674 wayland_compositor_destroy(c);
Martin Olssonc5db50f2012-07-08 03:03:43 +02001675 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001676}