blob: 238946b2055aeb85a93e52629ced2c842bf7de01 [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);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400971
Jason Ekstrand7744f712013-10-27 22:24:55 -0500972
973 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
974 input->parent.cursor.surface,
975 image->hotspot_x, image->hotspot_y);
976
977 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
978 wl_surface_damage(input->parent.cursor.surface, 0, 0,
979 image->width, image->height);
980 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400981}
982
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100983static void
Daniel Stone37816df2012-05-16 18:45:18 +0100984input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
985 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400986 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100987{
988 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500989 int32_t fx, fy;
990 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100991
Daniel Stone50692802012-06-22 13:21:41 +0100992 /* XXX: If we get a modifier event immediately before the focus,
993 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400994 input->enter_serial = serial;
995 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500996
997 if (input->output->frame) {
998 location = frame_pointer_enter(input->output->frame, input,
999 wl_fixed_to_int(x),
1000 wl_fixed_to_int(y));
1001 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1002 x -= wl_fixed_from_int(fx);
1003 y -= wl_fixed_from_int(fy);
1004
1005 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1006 weston_output_schedule_repaint(&input->output->base);
1007 } else {
1008 location = THEME_LOCATION_CLIENT_AREA;
1009 }
1010
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001011 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001012
1013 if (location == THEME_LOCATION_CLIENT_AREA) {
1014 input->focus = 1;
1015 notify_pointer_focus(&input->base, &input->output->base, x, y);
1016 wl_pointer_set_cursor(input->parent.pointer,
1017 input->enter_serial, NULL, 0, 0);
1018 } else {
1019 input->focus = 0;
1020 notify_pointer_focus(&input->base, NULL, 0, 0);
1021 input_set_cursor(input);
1022 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001023}
1024
1025static void
Daniel Stone37816df2012-05-16 18:45:18 +01001026input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1027 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001028{
1029 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001030
Jason Ekstrand7744f712013-10-27 22:24:55 -05001031 if (input->output->frame) {
1032 frame_pointer_leave(input->output->frame, input);
1033
1034 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1035 weston_output_schedule_repaint(&input->output->base);
1036 }
1037
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001038 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001039 input->output = NULL;
1040 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001041}
1042
1043static void
Daniel Stone37816df2012-05-16 18:45:18 +01001044input_handle_motion(void *data, struct wl_pointer *pointer,
1045 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1046{
1047 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001048 int32_t fx, fy;
1049 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001050
Jason Ekstrand7744f712013-10-27 22:24:55 -05001051 if (input->output->frame) {
1052 location = frame_pointer_motion(input->output->frame, input,
1053 wl_fixed_to_int(x),
1054 wl_fixed_to_int(y));
1055 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1056 x -= wl_fixed_from_int(fx);
1057 y -= wl_fixed_from_int(fy);
1058
1059 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1060 weston_output_schedule_repaint(&input->output->base);
1061 } else {
1062 location = THEME_LOCATION_CLIENT_AREA;
1063 }
1064
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001065 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001066
1067 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
1068 input_set_cursor(input);
1069 notify_pointer_focus(&input->base, NULL, 0, 0);
1070 input->focus = 0;
1071 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
1072 wl_pointer_set_cursor(input->parent.pointer,
1073 input->enter_serial, NULL, 0, 0);
1074 notify_pointer_focus(&input->base, &input->output->base, x, y);
1075 input->focus = 1;
1076 }
1077
1078 if (location == THEME_LOCATION_CLIENT_AREA)
1079 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +01001080}
1081
1082static void
1083input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001084 uint32_t serial, uint32_t time, uint32_t button,
1085 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001086{
1087 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001088 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001089 enum frame_button_state fstate;
1090 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001091
Jason Ekstrand7744f712013-10-27 22:24:55 -05001092 if (input->output->frame) {
1093 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1094 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1095
1096 location = frame_pointer_button(input->output->frame, input,
1097 button, fstate);
1098
1099 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1100
1101 wl_shell_surface_move(input->output->parent.shell_surface,
1102 input->parent.seat, serial);
1103 frame_status_clear(input->output->frame,
1104 FRAME_STATUS_MOVE);
1105 return;
1106 }
1107
1108 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE)
1109 wl_display_terminate(input->compositor->base.wl_display);
1110
1111 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1112 weston_output_schedule_repaint(&input->output->base);
1113 } else {
1114 location = THEME_LOCATION_CLIENT_AREA;
1115 }
1116
1117 if (location == THEME_LOCATION_CLIENT_AREA)
1118 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +01001119}
1120
1121static void
1122input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001123 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001124{
1125 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001126
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001127 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +01001128}
1129
1130static const struct wl_pointer_listener pointer_listener = {
1131 input_handle_pointer_enter,
1132 input_handle_pointer_leave,
1133 input_handle_motion,
1134 input_handle_button,
1135 input_handle_axis,
1136};
1137
1138static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001139input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1140 int fd, uint32_t size)
1141{
1142 struct wayland_input *input = data;
1143 struct xkb_keymap *keymap;
1144 char *map_str;
1145
1146 if (!data) {
1147 close(fd);
1148 return;
1149 }
1150
1151 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1152 close(fd);
1153 return;
1154 }
1155
1156 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1157 if (map_str == MAP_FAILED) {
1158 close(fd);
1159 return;
1160 }
1161
1162 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
1163 map_str,
1164 XKB_KEYMAP_FORMAT_TEXT_V1,
1165 0);
1166 munmap(map_str, size);
1167 close(fd);
1168
1169 if (!keymap) {
Martin Minarik6d118362012-06-07 18:01:59 +02001170 weston_log("failed to compile keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001171 return;
1172 }
1173
Rui Matos0c194ce2013-10-10 19:44:21 +02001174 if (input->base.keyboard)
1175 weston_seat_update_keymap(&input->base, keymap);
1176 else
1177 weston_seat_init_keyboard(&input->base, keymap);
1178
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001179 xkb_map_unref(keymap);
1180}
1181
1182static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001183input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001184 struct wl_keyboard *keyboard,
1185 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001186 struct wl_surface *surface,
1187 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001188{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001189 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001190 struct wayland_output *focus;
1191
1192 focus = input->keyboard_focus;
1193 if (focus) {
1194 /* This shouldn't happen */
1195 focus->keyboard_count--;
1196 if (!focus->keyboard_count && focus->frame)
1197 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1198 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1199 weston_output_schedule_repaint(&focus->base);
1200 }
1201
1202 input->keyboard_focus = wl_surface_get_user_data(surface);
1203 input->keyboard_focus->keyboard_count++;
1204
1205 focus = input->keyboard_focus;
1206 if (focus->frame) {
1207 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1208 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1209 weston_output_schedule_repaint(&focus->base);
1210 }
1211
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001212
Daniel Stone50692802012-06-22 13:21:41 +01001213 /* XXX: If we get a modifier event immediately before the focus,
1214 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001215 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001216 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001217}
1218
1219static void
1220input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001221 struct wl_keyboard *keyboard,
1222 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001223 struct wl_surface *surface)
1224{
1225 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001226 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001227
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001228 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001229
1230 focus = input->keyboard_focus;
1231 if (!focus)
1232 return; /* This shouldn't happen */
1233
1234 focus->keyboard_count--;
1235 if (!focus->keyboard_count && focus->frame) {
1236 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1237 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1238 weston_output_schedule_repaint(&focus->base);
1239 }
1240
1241 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001242}
1243
Daniel Stone37816df2012-05-16 18:45:18 +01001244static void
1245input_handle_key(void *data, struct wl_keyboard *keyboard,
1246 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1247{
1248 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001249
Daniel Stone50692802012-06-22 13:21:41 +01001250 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001251 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001252 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001253 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stone50692802012-06-22 13:21:41 +01001254 STATE_UPDATE_NONE);
Daniel Stone37816df2012-05-16 18:45:18 +01001255}
1256
Daniel Stone351eb612012-05-31 15:27:47 -04001257static void
1258input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001259 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001260 uint32_t mods_latched, uint32_t mods_locked,
1261 uint32_t group)
1262{
Daniel Stone50692802012-06-22 13:21:41 +01001263 struct wayland_input *input = data;
1264 struct wayland_compositor *c = input->compositor;
1265 uint32_t serial_out;
1266
1267 /* If we get a key event followed by a modifier event with the
1268 * same serial number, then we try to preserve those semantics by
1269 * reusing the same serial number on the way out too. */
1270 if (serial_in == input->key_serial)
1271 serial_out = wl_display_get_serial(c->base.wl_display);
1272 else
1273 serial_out = wl_display_next_serial(c->base.wl_display);
1274
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001275 xkb_state_update_mask(input->base.keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001276 mods_depressed, mods_latched,
1277 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001278 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001279}
1280
Daniel Stone37816df2012-05-16 18:45:18 +01001281static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001282 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001283 input_handle_keyboard_enter,
1284 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001285 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001286 input_handle_modifiers,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001287};
1288
1289static void
Daniel Stone37816df2012-05-16 18:45:18 +01001290input_handle_capabilities(void *data, struct wl_seat *seat,
1291 enum wl_seat_capability caps)
1292{
1293 struct wayland_input *input = data;
1294
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001295 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1296 input->parent.pointer = wl_seat_get_pointer(seat);
1297 wl_pointer_set_user_data(input->parent.pointer, input);
1298 wl_pointer_add_listener(input->parent.pointer,
1299 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001300 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001301 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1302 wl_pointer_destroy(input->parent.pointer);
1303 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001304 }
1305
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001306 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1307 input->parent.keyboard = wl_seat_get_keyboard(seat);
1308 wl_keyboard_set_user_data(input->parent.keyboard, input);
1309 wl_keyboard_add_listener(input->parent.keyboard,
1310 &keyboard_listener, input);
1311 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1312 wl_keyboard_destroy(input->parent.keyboard);
1313 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001314 }
1315}
1316
1317static const struct wl_seat_listener seat_listener = {
1318 input_handle_capabilities,
1319};
1320
1321static void
1322display_add_seat(struct wayland_compositor *c, uint32_t id)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001323{
1324 struct wayland_input *input;
1325
Peter Huttererf3d62272013-08-08 11:57:05 +10001326 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001327 if (input == NULL)
1328 return;
1329
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001330 weston_seat_init(&input->base, &c->base, "default");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001331 input->compositor = c;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001332 input->parent.seat = wl_registry_bind(c->parent.registry, id,
1333 &wl_seat_interface, 1);
Jason Ekstrand06ced802013-11-07 20:13:29 -06001334 wl_list_insert(c->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001335
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001336 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1337 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001338
1339 input->parent.cursor.surface =
1340 wl_compositor_create_surface(c->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001341}
1342
1343static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001344registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1345 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001346{
1347 struct wayland_compositor *c = data;
1348
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001349 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001350 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001351 wl_registry_bind(registry, name,
1352 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001353 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001354 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001355 wl_registry_bind(registry, name,
1356 &wl_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01001357 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001358 display_add_seat(c, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001359 } else if (strcmp(interface, "wl_shm") == 0) {
1360 c->parent.shm =
1361 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001362 }
1363}
1364
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001365static const struct wl_registry_listener registry_listener = {
1366 registry_handle_global
1367};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001368
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001369static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001370wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1371{
1372 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001373 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001374
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001375 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1376 wl_display_terminate(c->base.wl_display);
1377 return 0;
1378 }
1379
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001380 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001381 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05001382 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001383 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001384
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001385 if (mask == 0) {
1386 count = wl_display_dispatch_pending(c->parent.wl_display);
1387 wl_display_flush(c->parent.wl_display);
1388 }
1389
1390 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001391}
1392
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001393static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001394wayland_restore(struct weston_compositor *ec)
1395{
1396}
1397
1398static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001399wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001400{
Jonas Ådahle5a12252013-04-05 23:07:11 +02001401 struct wayland_compositor *c = (struct wayland_compositor *) ec;
1402
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001403 weston_compositor_shutdown(ec);
Matt Roper361d2ad2011-08-29 13:52:23 -07001404
Jonas Ådahle5a12252013-04-05 23:07:11 +02001405 if (c->parent.shm)
1406 wl_shm_destroy(c->parent.shm);
1407
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001408 free(ec);
1409}
1410
Jason Ekstrand7744f712013-10-27 22:24:55 -05001411static const char *left_ptrs[] = {
1412 "left_ptr",
1413 "default",
1414 "top_left_arrow",
1415 "left-arrow"
1416};
1417
1418static void
1419create_cursor(struct wayland_compositor *c, struct weston_config *config)
1420{
1421 struct weston_config_section *s;
1422 int size;
1423 char *theme = NULL;
1424 unsigned int i;
1425
1426 s = weston_config_get_section(config, "shell", NULL, NULL);
1427 weston_config_section_get_string(s, "cursor-theme", &theme, NULL);
1428 weston_config_section_get_int(s, "cursor-size", &size, 32);
1429
1430 c->cursor_theme = wl_cursor_theme_load(theme, size, c->parent.shm);
1431
U. Artie Eoffff755002014-01-17 12:36:58 -08001432 free(theme);
1433
Jason Ekstrand7744f712013-10-27 22:24:55 -05001434 c->cursor = NULL;
1435 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1436 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
1437 left_ptrs[i]);
1438 if (!c->cursor) {
1439 fprintf(stderr, "could not load left cursor\n");
1440 return;
1441 }
1442}
1443
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001444static void
1445fullscreen_binding(struct weston_seat *seat_base, uint32_t time, uint32_t key,
1446 void *data)
1447{
1448 struct wayland_compositor *c = data;
1449 struct wayland_input *input = NULL;
1450
1451 wl_list_for_each(input, &c->input_list, link)
1452 if (&input->base == seat_base)
1453 break;
1454
1455 if (!input || !input->output)
1456 return;
1457
1458 if (input->output->frame)
1459 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
1460 else
1461 wayland_output_set_windowed(input->output);
1462
1463 weston_output_schedule_repaint(&input->output->base);
1464}
1465
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001466static struct wayland_compositor *
1467wayland_compositor_create(struct wl_display *display, int use_pixman,
1468 const char *display_name, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001469 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001470{
1471 struct wayland_compositor *c;
1472 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001473 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001474
Peter Huttererf3d62272013-08-08 11:57:05 +10001475 c = zalloc(sizeof *c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001476 if (c == NULL)
1477 return NULL;
1478
Daniel Stone725c2c32012-06-22 14:04:36 +01001479 if (weston_compositor_init(&c->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001480 config) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +02001481 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001482
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001483 c->parent.wl_display = wl_display_connect(display_name);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001484
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001485 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001486 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001487 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001488 }
1489
Jason Ekstrand06ced802013-11-07 20:13:29 -06001490 wl_list_init(&c->input_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001491 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1492 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001493 wl_display_roundtrip(c->parent.wl_display);
1494
1495 create_cursor(c, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001496
1497 c->base.wl_display = display;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001498
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001499 c->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001500
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001501 if (!c->use_pixman) {
1502 gl_renderer = weston_load_module("gl-renderer.so",
1503 "gl_renderer_interface");
1504 if (!gl_renderer)
1505 c->use_pixman = 1;
1506 }
1507
1508 if (!c->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001509 if (gl_renderer->create(&c->base, c->parent.wl_display,
1510 gl_renderer->alpha_attribs,
1511 NULL) < 0) {
1512 weston_log("Failed to initialize the GL renderer; "
1513 "falling back to pixman.\n");
1514 c->use_pixman = 1;
1515 }
1516 }
1517
1518 if (c->use_pixman) {
1519 if (pixman_renderer_init(&c->base) < 0) {
1520 weston_log("Failed to initialize pixman renderer\n");
1521 goto err_display;
1522 }
1523 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001524
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001525 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001526 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001527
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001528 loop = wl_display_get_event_loop(c->base.wl_display);
1529
1530 fd = wl_display_get_fd(c->parent.wl_display);
1531 c->parent.wl_source =
1532 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1533 wayland_compositor_handle_event, c);
1534 if (c->parent.wl_source == NULL)
1535 goto err_renderer;
1536
1537 wl_event_source_check(c->parent.wl_source);
1538
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001539 weston_compositor_add_key_binding(&c->base, KEY_F,
1540 MODIFIER_CTRL | MODIFIER_ALT,
1541 fullscreen_binding, c);
1542
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001543 return c;
1544err_renderer:
1545 c->base.renderer->destroy(&c->base);
1546err_display:
1547 wl_display_disconnect(c->parent.wl_display);
1548err_compositor:
1549 weston_compositor_shutdown(&c->base);
1550err_free:
1551 free(c);
1552 return NULL;
1553}
1554
1555static void
1556wayland_compositor_destroy(struct wayland_compositor *c)
1557{
1558 struct weston_output *output;
1559
1560 wl_list_for_each(output, &c->base.output_list, link)
1561 wayland_output_destroy(output);
1562
1563 c->base.renderer->destroy(&c->base);
1564 wl_display_disconnect(c->parent.wl_display);
1565
1566 if (c->theme)
1567 theme_destroy(c->theme);
1568 if (c->frame_device)
1569 cairo_device_destroy(c->frame_device);
1570 wl_cursor_theme_destroy(c->cursor_theme);
1571
1572 weston_compositor_shutdown(&c->base);
1573 free(c);
1574}
1575
1576WL_EXPORT struct weston_compositor *
1577backend_init(struct wl_display *display, int *argc, char *argv[],
1578 struct weston_config *config)
1579{
1580 struct wayland_compositor *c;
1581 struct wayland_output *output;
1582 struct weston_config_section *section;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001583 int x, count, width, height, scale, use_pixman, fullscreen;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001584 const char *section_name, *display_name;
1585 char *name;
1586
1587 const struct weston_option wayland_options[] = {
1588 { WESTON_OPTION_INTEGER, "width", 0, &width },
1589 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001590 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001591 { WESTON_OPTION_STRING, "display", 0, &display_name },
1592 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
1593 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001594 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001595 };
1596
1597 width = 0;
1598 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001599 scale = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001600 display_name = NULL;
1601 use_pixman = 0;
1602 count = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001603 fullscreen = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001604 parse_options(wayland_options,
1605 ARRAY_LENGTH(wayland_options), argc, argv);
1606
1607 c = wayland_compositor_create(display, use_pixman, display_name,
1608 argc, argv, config);
1609 if (!c)
1610 return NULL;
1611
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001612 if (fullscreen) {
1613 output = wayland_output_create(c, 0, 0, width, height,
1614 NULL, 1, 0, 1);
1615 if (!output)
1616 goto err_outputs;
1617
Axel Davydd8b88d2013-11-17 21:34:16 +01001618 wayland_output_set_fullscreen(output, 0, 0, NULL);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001619 return &c->base;
1620 }
1621
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001622 section = NULL;
1623 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001624 while (weston_config_next_section(config, &section, &section_name)) {
1625 if (!section_name || strcmp(section_name, "output") != 0)
1626 continue;
1627 weston_config_section_get_string(section, "name", &name, NULL);
1628 if (name == NULL)
1629 continue;
1630
1631 if (name[0] != 'W' || name[1] != 'L') {
1632 free(name);
1633 continue;
1634 }
1635 free(name);
1636
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001637 output = wayland_output_create_for_config(c, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001638 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001639 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001640 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001641 if (wayland_output_set_windowed(output))
1642 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001643
1644 x += output->base.width;
1645 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001646 }
1647
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001648 if (!width)
1649 width = 1024;
1650 if (!height)
1651 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001652 if (!scale)
1653 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001654 while (count > 0) {
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001655 output = wayland_output_create(c, x, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001656 NULL, 0, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001657 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001658 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001659 if (wayland_output_set_windowed(output))
1660 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001661
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001662 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001663 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001664 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001665
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001666 return &c->base;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001667
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001668err_outputs:
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001669 wayland_compositor_destroy(c);
Martin Olssonc5db50f2012-07-08 03:03:43 +02001670 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001671}