blob: 14ff4af327b0177ef1a0158fe0e0c50e916ff770 [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,
596 output->gl.egl_window) < 0)
597 goto cleanup_window;
598
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500599 return 0;
600
601cleanup_window:
602 wl_egl_window_destroy(output->gl.egl_window);
603 return -1;
604}
605
606static int
607wayland_output_init_pixman_renderer(struct wayland_output *output)
608{
609 return pixman_renderer_output_create(&output->base);
610}
611
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600612static void
613wayland_output_resize_surface(struct wayland_output *output)
614{
615 struct wayland_compositor *c =
616 (struct wayland_compositor *)output->base.compositor;
617 struct wayland_shm_buffer *buffer, *next;
618 int32_t ix, iy, iwidth, iheight;
619 int32_t width, height;
620 struct wl_region *region;
621
622 width = output->base.current_mode->width;
623 height = output->base.current_mode->height;
624
625 if (output->frame) {
626 frame_resize_inside(output->frame, width, height);
627
628 frame_input_rect(output->frame, &ix, &iy, &iwidth, &iheight);
629 region = wl_compositor_create_region(c->parent.compositor);
630 wl_region_add(region, ix, iy, iwidth, iheight);
631 wl_surface_set_input_region(output->parent.surface, region);
632 wl_region_destroy(region);
633
634 frame_opaque_rect(output->frame, &ix, &iy, &iwidth, &iheight);
635 region = wl_compositor_create_region(c->parent.compositor);
636 wl_region_add(region, ix, iy, iwidth, iheight);
637 wl_surface_set_opaque_region(output->parent.surface, region);
638 wl_region_destroy(region);
639
640 width = frame_width(output->frame);
641 height = frame_height(output->frame);
642 } else {
643 region = wl_compositor_create_region(c->parent.compositor);
644 wl_region_add(region, 0, 0, width, height);
645 wl_surface_set_input_region(output->parent.surface, region);
646 wl_region_destroy(region);
647
648 region = wl_compositor_create_region(c->parent.compositor);
649 wl_region_add(region, 0, 0, width, height);
650 wl_surface_set_opaque_region(output->parent.surface, region);
651 wl_region_destroy(region);
652 }
653
654 if (output->gl.egl_window) {
655 wl_egl_window_resize(output->gl.egl_window,
656 width, height, 0, 0);
657
658 /* These will need to be re-created due to the resize */
659 gl_renderer->output_set_border(&output->base,
660 GL_RENDERER_BORDER_TOP,
661 0, 0, 0, NULL);
662 cairo_surface_destroy(output->gl.border.top);
663 output->gl.border.top = NULL;
664 gl_renderer->output_set_border(&output->base,
665 GL_RENDERER_BORDER_LEFT,
666 0, 0, 0, NULL);
667 cairo_surface_destroy(output->gl.border.left);
668 output->gl.border.left = NULL;
669 gl_renderer->output_set_border(&output->base,
670 GL_RENDERER_BORDER_RIGHT,
671 0, 0, 0, NULL);
672 cairo_surface_destroy(output->gl.border.right);
673 output->gl.border.right = NULL;
674 gl_renderer->output_set_border(&output->base,
675 GL_RENDERER_BORDER_BOTTOM,
676 0, 0, 0, NULL);
677 cairo_surface_destroy(output->gl.border.bottom);
678 output->gl.border.bottom = NULL;
679 }
680
681 /* Throw away any remaining SHM buffers */
682 wl_list_for_each_safe(buffer, next, &output->shm.free_buffers, link)
683 wayland_shm_buffer_destroy(buffer);
684 /* These will get thrown away when they get released */
685 wl_list_for_each(buffer, &output->shm.buffers, link)
686 buffer->output = NULL;
687}
688
689static int
690wayland_output_set_windowed(struct wayland_output *output)
691{
692 struct wayland_compositor *c =
693 (struct wayland_compositor *)output->base.compositor;
694 int tlen;
695 char *title;
696
697 if (output->frame)
698 return 0;
699
700 if (output->name) {
701 tlen = strlen(output->name) + strlen(WINDOW_TITLE " - ");
702 title = malloc(tlen + 1);
703 if (!title)
704 return -1;
705
706 snprintf(title, tlen + 1, WINDOW_TITLE " - %s", output->name);
707 } else {
708 title = strdup(WINDOW_TITLE);
709 }
710
711 if (!c->theme) {
712 c->theme = theme_create();
713 if (!c->theme) {
714 free(title);
715 return -1;
716 }
717 }
718 output->frame = frame_create(c->theme, 100, 100,
719 FRAME_BUTTON_CLOSE, title);
720 free(title);
721 if (!output->frame)
722 return -1;
723
724 if (output->keyboard_count)
725 frame_set_flag(output->frame, FRAME_FLAG_ACTIVE);
726
727 wayland_output_resize_surface(output);
728
729 wl_shell_surface_set_toplevel(output->parent.shell_surface);
730
731 return 0;
732}
733
734static void
735wayland_output_set_fullscreen(struct wayland_output *output,
736 enum wl_shell_surface_fullscreen_method method,
737 uint32_t framerate, struct wl_output *target)
738{
739 if (output->frame) {
740 frame_destroy(output->frame);
741 output->frame = NULL;
742 }
743
744 wayland_output_resize_surface(output);
745
746 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
747 method, framerate, target);
748}
749
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500750static struct wayland_output *
751wayland_output_create(struct wayland_compositor *c, int x, int y,
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600752 int width, int height, const char *name, int fullscreen,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500753 uint32_t transform, int32_t scale)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100754{
755 struct wayland_output *output;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500756 int output_width, output_height;
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600757
758 weston_log("Creating %dx%d wayland output at (%d, %d)\n",
759 width, height, x, y);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100760
Peter Huttererf3d62272013-08-08 11:57:05 +1000761 output = zalloc(sizeof *output);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100762 if (output == NULL)
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500763 return NULL;
764
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600765 output->name = name ? strdup(name) : NULL;
766 output->base.make = "waywayland";
767 output->base.model = "none";
768
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500769 output_width = width * scale;
770 output_height = height * scale;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100771
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600772 output->parent.surface =
773 wl_compositor_create_surface(c->parent.compositor);
774 if (!output->parent.surface)
775 goto err_name;
776 wl_surface_set_user_data(output->parent.surface, output);
777
778 output->parent.draw_initial_frame = 1;
779 output->parent.shell_surface =
780 wl_shell_get_shell_surface(c->parent.shell,
781 output->parent.surface);
782 if (!output->parent.shell_surface)
783 goto err_surface;
784 wl_shell_surface_add_listener(output->parent.shell_surface,
785 &shell_surface_listener, output);
786
787 if (fullscreen) {
788 wl_shell_surface_set_fullscreen(output->parent.shell_surface,
789 0, 0, NULL);
790 wl_display_roundtrip(c->parent.wl_display);
791 if (!width)
792 output_width = output->parent.configure_width;
793 if (!height)
794 output_height = output->parent.configure_height;
795 }
796
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400797 output->mode.flags =
798 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500799 output->mode.width = output_width;
800 output->mode.height = output_height;
801 output->mode.refresh = 60000;
802 output->scale = scale;
Kristian Høgsberg8f0ce052011-06-21 11:16:58 -0400803 wl_list_init(&output->base.mode_list);
804 wl_list_insert(&output->base.mode_list, &output->mode.link);
Hardeningff39efa2013-09-18 23:56:35 +0200805 output->base.current_mode = &output->mode;
Kristian Høgsberg546a8122012-02-01 07:45:51 -0500806
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500807 wl_list_init(&output->shm.buffers);
808 wl_list_init(&output->shm.free_buffers);
809
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600810 weston_output_init(&output->base, &c->base, x, y, width, height,
811 transform, scale);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100812
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500813 if (c->use_pixman) {
814 if (wayland_output_init_pixman_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600815 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500816 output->base.repaint = wayland_output_repaint_pixman;
817 } else {
818 if (wayland_output_init_gl_renderer(output) < 0)
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600819 goto err_output;
Jason Ekstrandff2fd462013-10-27 22:24:58 -0500820 output->base.repaint = wayland_output_repaint_gl;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100821 }
822
Jonas Ådahle5a12252013-04-05 23:07:11 +0200823 output->base.start_repaint_loop = wayland_output_start_repaint_loop;
Matt Roper361d2ad2011-08-29 13:52:23 -0700824 output->base.destroy = wayland_output_destroy;
Jesse Barnes5308a5e2012-02-09 13:12:57 -0800825 output->base.assign_planes = NULL;
Tiago Vignatti8e53c7f2012-02-29 19:53:50 +0200826 output->base.set_backlight = NULL;
827 output->base.set_dpms = NULL;
Alex Wu2dda6042012-04-17 17:20:47 +0800828 output->base.switch_mode = NULL;
Benjamin Franzkeeefc36c2011-03-11 16:39:20 +0100829
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100830 wl_list_insert(c->base.output_list.prev, &output->base.link);
831
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500832 return output;
Benjamin Franzkebe014562011-02-18 17:04:24 +0100833
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600834err_output:
835 weston_output_destroy(&output->base);
836 wl_shell_surface_destroy(output->parent.shell_surface);
837err_surface:
838 wl_surface_destroy(output->parent.surface);
839err_name:
840 free(output->name);
841
Kristian Høgsberg8334bc12012-01-03 10:29:47 -0500842 /* FIXME: cleanup weston_output */
Benjamin Franzkebe014562011-02-18 17:04:24 +0100843 free(output);
844
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500845 return NULL;
846}
847
848static struct wayland_output *
849wayland_output_create_for_config(struct wayland_compositor *c,
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500850 struct weston_config_section *config_section,
Jason Ekstrand0cf39352013-11-07 20:13:31 -0600851 int option_width, int option_height,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -0600852 int option_scale, int32_t x, int32_t y)
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500853{
854 struct wayland_output *output;
855 char *mode, *t, *name, *str;
856 int width, height, scale;
857 uint32_t transform;
858 unsigned int i, slen;
859
860 static const struct { const char *name; uint32_t token; } transform_names[] = {
861 { "normal", WL_OUTPUT_TRANSFORM_NORMAL },
862 { "90", WL_OUTPUT_TRANSFORM_90 },
863 { "180", WL_OUTPUT_TRANSFORM_180 },
864 { "270", WL_OUTPUT_TRANSFORM_270 },
865 { "flipped", WL_OUTPUT_TRANSFORM_FLIPPED },
866 { "flipped-90", WL_OUTPUT_TRANSFORM_FLIPPED_90 },
867 { "flipped-180", WL_OUTPUT_TRANSFORM_FLIPPED_180 },
868 { "flipped-270", WL_OUTPUT_TRANSFORM_FLIPPED_270 },
869 };
870
871 weston_config_section_get_string(config_section, "name", &name, NULL);
872 if (name) {
873 slen = strlen(name);
874 slen += strlen(WINDOW_TITLE " - ");
875 str = malloc(slen + 1);
876 if (str)
877 snprintf(str, slen + 1, WINDOW_TITLE " - %s", name);
878 free(name);
879 name = str;
880 }
881 if (!name)
882 name = WINDOW_TITLE;
883
884 weston_config_section_get_string(config_section,
885 "mode", &mode, "1024x600");
886 if (sscanf(mode, "%dx%d", &width, &height) != 2) {
887 weston_log("Invalid mode \"%s\" for output %s\n",
888 mode, name);
889 width = 1024;
890 height = 640;
891 }
892 free(mode);
893
Jason Ekstrand0cf39352013-11-07 20:13:31 -0600894 if (option_width)
895 width = option_width;
896 if (option_height)
897 height = option_height;
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500898
899 weston_config_section_get_int(config_section, "scale", &scale, 1);
900
Jason Ekstrand12c6dd92013-11-07 20:13:32 -0600901 if (option_scale)
902 scale = option_scale;
903
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500904 weston_config_section_get_string(config_section,
905 "transform", &t, "normal");
906 transform = WL_OUTPUT_TRANSFORM_NORMAL;
907 for (i = 0; i < ARRAY_LENGTH(transform_names); i++) {
908 if (strcmp(transform_names[i].name, t) == 0) {
909 transform = transform_names[i].token;
910 break;
911 }
912 }
913 if (i >= ARRAY_LENGTH(transform_names))
914 weston_log("Invalid transform \"%s\" for output %s\n", t, name);
915 free(t);
916
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600917 output = wayland_output_create(c, x, y, width, height, name, 0,
918 transform, scale);
Jason Ekstrand48ce4212013-10-27 22:25:02 -0500919 free(name);
920
921 return output;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100922}
923
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300924static void
925shell_surface_ping(void *data, struct wl_shell_surface *shell_surface,
926 uint32_t serial)
927{
928 wl_shell_surface_pong(shell_surface, serial);
929}
930
931static void
932shell_surface_configure(void *data, struct wl_shell_surface *shell_surface,
933 uint32_t edges, int32_t width, int32_t height)
934{
Jason Ekstrand5ea04802013-11-07 20:13:33 -0600935 struct wayland_output *output = data;
936
937 output->parent.configure_width = width;
938 output->parent.configure_height = height;
939
Ander Conselvan de Oliveira563c5b82012-06-18 17:36:21 +0300940 /* FIXME: implement resizing */
941}
942
943static void
944shell_surface_popup_done(void *data, struct wl_shell_surface *shell_surface)
945{
946}
947
948static const struct wl_shell_surface_listener shell_surface_listener = {
949 shell_surface_ping,
950 shell_surface_configure,
951 shell_surface_popup_done
952};
953
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100954/* Events received from the wayland-server this compositor is client of: */
955
Jason Ekstrand7744f712013-10-27 22:24:55 -0500956/* parent input interface */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400957static void
Jason Ekstrand7744f712013-10-27 22:24:55 -0500958input_set_cursor(struct wayland_input *input)
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400959{
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400960
Jason Ekstrand7744f712013-10-27 22:24:55 -0500961 struct wl_buffer *buffer;
962 struct wl_cursor_image *image;
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400963
Jason Ekstrand7744f712013-10-27 22:24:55 -0500964 if (!input->compositor->cursor)
965 return; /* Couldn't load the cursor. Can't set it */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400966
Jason Ekstrand7744f712013-10-27 22:24:55 -0500967 image = input->compositor->cursor->images[0];
968 buffer = wl_cursor_image_get_buffer(image);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400969
Jason Ekstrand7744f712013-10-27 22:24:55 -0500970
971 wl_pointer_set_cursor(input->parent.pointer, input->enter_serial,
972 input->parent.cursor.surface,
973 image->hotspot_x, image->hotspot_y);
974
975 wl_surface_attach(input->parent.cursor.surface, buffer, 0, 0);
976 wl_surface_damage(input->parent.cursor.surface, 0, 0,
977 image->width, image->height);
978 wl_surface_commit(input->parent.cursor.surface);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400979}
980
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100981static void
Daniel Stone37816df2012-05-16 18:45:18 +0100982input_handle_pointer_enter(void *data, struct wl_pointer *pointer,
983 uint32_t serial, struct wl_surface *surface,
Kristian Høgsberge11bbe42012-05-09 12:19:04 -0400984 wl_fixed_t x, wl_fixed_t y)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100985{
986 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -0500987 int32_t fx, fy;
988 enum theme_location location;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +0100989
Daniel Stone50692802012-06-22 13:21:41 +0100990 /* XXX: If we get a modifier event immediately before the focus,
991 * we should try to keep the same serial. */
Kristian Høgsberg539d85f2012-08-13 23:29:53 -0400992 input->enter_serial = serial;
993 input->output = wl_surface_get_user_data(surface);
Jason Ekstrand7744f712013-10-27 22:24:55 -0500994
995 if (input->output->frame) {
996 location = frame_pointer_enter(input->output->frame, input,
997 wl_fixed_to_int(x),
998 wl_fixed_to_int(y));
999 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1000 x -= wl_fixed_from_int(fx);
1001 y -= wl_fixed_from_int(fy);
1002
1003 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1004 weston_output_schedule_repaint(&input->output->base);
1005 } else {
1006 location = THEME_LOCATION_CLIENT_AREA;
1007 }
1008
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001009 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001010
1011 if (location == THEME_LOCATION_CLIENT_AREA) {
1012 input->focus = 1;
1013 notify_pointer_focus(&input->base, &input->output->base, x, y);
1014 wl_pointer_set_cursor(input->parent.pointer,
1015 input->enter_serial, NULL, 0, 0);
1016 } else {
1017 input->focus = 0;
1018 notify_pointer_focus(&input->base, NULL, 0, 0);
1019 input_set_cursor(input);
1020 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001021}
1022
1023static void
Daniel Stone37816df2012-05-16 18:45:18 +01001024input_handle_pointer_leave(void *data, struct wl_pointer *pointer,
1025 uint32_t serial, struct wl_surface *surface)
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001026{
1027 struct wayland_input *input = data;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001028
Jason Ekstrand7744f712013-10-27 22:24:55 -05001029 if (input->output->frame) {
1030 frame_pointer_leave(input->output->frame, input);
1031
1032 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1033 weston_output_schedule_repaint(&input->output->base);
1034 }
1035
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001036 notify_pointer_focus(&input->base, NULL, 0, 0);
Kristian Høgsberg539d85f2012-08-13 23:29:53 -04001037 input->output = NULL;
1038 input->focus = 0;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001039}
1040
1041static void
Daniel Stone37816df2012-05-16 18:45:18 +01001042input_handle_motion(void *data, struct wl_pointer *pointer,
1043 uint32_t time, wl_fixed_t x, wl_fixed_t y)
1044{
1045 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001046 int32_t fx, fy;
1047 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001048
Jason Ekstrand7744f712013-10-27 22:24:55 -05001049 if (input->output->frame) {
1050 location = frame_pointer_motion(input->output->frame, input,
1051 wl_fixed_to_int(x),
1052 wl_fixed_to_int(y));
1053 frame_interior(input->output->frame, &fx, &fy, NULL, NULL);
1054 x -= wl_fixed_from_int(fx);
1055 y -= wl_fixed_from_int(fy);
1056
1057 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1058 weston_output_schedule_repaint(&input->output->base);
1059 } else {
1060 location = THEME_LOCATION_CLIENT_AREA;
1061 }
1062
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001063 weston_output_transform_coordinate(&input->output->base, x, y, &x, &y);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001064
1065 if (input->focus && location != THEME_LOCATION_CLIENT_AREA) {
1066 input_set_cursor(input);
1067 notify_pointer_focus(&input->base, NULL, 0, 0);
1068 input->focus = 0;
1069 } else if (!input->focus && location == THEME_LOCATION_CLIENT_AREA) {
1070 wl_pointer_set_cursor(input->parent.pointer,
1071 input->enter_serial, NULL, 0, 0);
1072 notify_pointer_focus(&input->base, &input->output->base, x, y);
1073 input->focus = 1;
1074 }
1075
1076 if (location == THEME_LOCATION_CLIENT_AREA)
1077 notify_motion_absolute(&input->base, time, x, y);
Daniel Stone37816df2012-05-16 18:45:18 +01001078}
1079
1080static void
1081input_handle_button(void *data, struct wl_pointer *pointer,
Daniel Stone4dbadb12012-05-30 16:31:51 +01001082 uint32_t serial, uint32_t time, uint32_t button,
1083 uint32_t state_w)
Daniel Stone37816df2012-05-16 18:45:18 +01001084{
1085 struct wayland_input *input = data;
Daniel Stone4dbadb12012-05-30 16:31:51 +01001086 enum wl_pointer_button_state state = state_w;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001087 enum frame_button_state fstate;
1088 enum theme_location location;
Daniel Stone37816df2012-05-16 18:45:18 +01001089
Jason Ekstrand7744f712013-10-27 22:24:55 -05001090 if (input->output->frame) {
1091 fstate = state == WL_POINTER_BUTTON_STATE_PRESSED ?
1092 FRAME_BUTTON_PRESSED : FRAME_BUTTON_RELEASED;
1093
1094 location = frame_pointer_button(input->output->frame, input,
1095 button, fstate);
1096
1097 if (frame_status(input->output->frame) & FRAME_STATUS_MOVE) {
1098
1099 wl_shell_surface_move(input->output->parent.shell_surface,
1100 input->parent.seat, serial);
1101 frame_status_clear(input->output->frame,
1102 FRAME_STATUS_MOVE);
1103 return;
1104 }
1105
1106 if (frame_status(input->output->frame) & FRAME_STATUS_CLOSE)
1107 wl_display_terminate(input->compositor->base.wl_display);
1108
1109 if (frame_status(input->output->frame) & FRAME_STATUS_REPAINT)
1110 weston_output_schedule_repaint(&input->output->base);
1111 } else {
1112 location = THEME_LOCATION_CLIENT_AREA;
1113 }
1114
1115 if (location == THEME_LOCATION_CLIENT_AREA)
1116 notify_button(&input->base, time, button, state);
Daniel Stone37816df2012-05-16 18:45:18 +01001117}
1118
1119static void
1120input_handle_axis(void *data, struct wl_pointer *pointer,
Daniel Stone2fce4022012-05-30 16:32:00 +01001121 uint32_t time, uint32_t axis, wl_fixed_t value)
Daniel Stone37816df2012-05-16 18:45:18 +01001122{
1123 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001124
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001125 notify_axis(&input->base, time, axis, value);
Daniel Stone37816df2012-05-16 18:45:18 +01001126}
1127
1128static const struct wl_pointer_listener pointer_listener = {
1129 input_handle_pointer_enter,
1130 input_handle_pointer_leave,
1131 input_handle_motion,
1132 input_handle_button,
1133 input_handle_axis,
1134};
1135
1136static void
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001137input_handle_keymap(void *data, struct wl_keyboard *keyboard, uint32_t format,
1138 int fd, uint32_t size)
1139{
1140 struct wayland_input *input = data;
1141 struct xkb_keymap *keymap;
1142 char *map_str;
1143
1144 if (!data) {
1145 close(fd);
1146 return;
1147 }
1148
1149 if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) {
1150 close(fd);
1151 return;
1152 }
1153
1154 map_str = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
1155 if (map_str == MAP_FAILED) {
1156 close(fd);
1157 return;
1158 }
1159
1160 keymap = xkb_map_new_from_string(input->compositor->base.xkb_context,
1161 map_str,
1162 XKB_KEYMAP_FORMAT_TEXT_V1,
1163 0);
1164 munmap(map_str, size);
1165 close(fd);
1166
1167 if (!keymap) {
Martin Minarik6d118362012-06-07 18:01:59 +02001168 weston_log("failed to compile keymap\n");
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001169 return;
1170 }
1171
Rui Matos0c194ce2013-10-10 19:44:21 +02001172 if (input->base.keyboard)
1173 weston_seat_update_keymap(&input->base, keymap);
1174 else
1175 weston_seat_init_keyboard(&input->base, keymap);
1176
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001177 xkb_map_unref(keymap);
1178}
1179
1180static void
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001181input_handle_keyboard_enter(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001182 struct wl_keyboard *keyboard,
1183 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001184 struct wl_surface *surface,
1185 struct wl_array *keys)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001186{
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001187 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001188 struct wayland_output *focus;
1189
1190 focus = input->keyboard_focus;
1191 if (focus) {
1192 /* This shouldn't happen */
1193 focus->keyboard_count--;
1194 if (!focus->keyboard_count && focus->frame)
1195 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1196 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1197 weston_output_schedule_repaint(&focus->base);
1198 }
1199
1200 input->keyboard_focus = wl_surface_get_user_data(surface);
1201 input->keyboard_focus->keyboard_count++;
1202
1203 focus = input->keyboard_focus;
1204 if (focus->frame) {
1205 frame_set_flag(focus->frame, FRAME_FLAG_ACTIVE);
1206 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1207 weston_output_schedule_repaint(&focus->base);
1208 }
1209
Kristian Høgsbergaf82bea2011-01-27 20:18:17 -05001210
Daniel Stone50692802012-06-22 13:21:41 +01001211 /* XXX: If we get a modifier event immediately before the focus,
1212 * we should try to keep the same serial. */
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001213 notify_keyboard_focus_in(&input->base, keys,
Daniel Stoned6da09e2012-06-22 13:21:29 +01001214 STATE_UPDATE_AUTOMATIC);
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001215}
1216
1217static void
1218input_handle_keyboard_leave(void *data,
Daniel Stone37816df2012-05-16 18:45:18 +01001219 struct wl_keyboard *keyboard,
1220 uint32_t serial,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001221 struct wl_surface *surface)
1222{
1223 struct wayland_input *input = data;
Jason Ekstrand7744f712013-10-27 22:24:55 -05001224 struct wayland_output *focus;
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001225
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001226 notify_keyboard_focus_out(&input->base);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001227
1228 focus = input->keyboard_focus;
1229 if (!focus)
1230 return; /* This shouldn't happen */
1231
1232 focus->keyboard_count--;
1233 if (!focus->keyboard_count && focus->frame) {
1234 frame_unset_flag(focus->frame, FRAME_FLAG_ACTIVE);
1235 if (frame_status(focus->frame) & FRAME_STATUS_REPAINT)
1236 weston_output_schedule_repaint(&focus->base);
1237 }
1238
1239 input->keyboard_focus = NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001240}
1241
Daniel Stone37816df2012-05-16 18:45:18 +01001242static void
1243input_handle_key(void *data, struct wl_keyboard *keyboard,
1244 uint32_t serial, uint32_t time, uint32_t key, uint32_t state)
1245{
1246 struct wayland_input *input = data;
Daniel Stone37816df2012-05-16 18:45:18 +01001247
Daniel Stone50692802012-06-22 13:21:41 +01001248 input->key_serial = serial;
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001249 notify_key(&input->base, time, key,
Daniel Stonec9785ea2012-05-30 16:31:52 +01001250 state ? WL_KEYBOARD_KEY_STATE_PRESSED :
Daniel Stone1b4e11f2012-06-22 13:21:37 +01001251 WL_KEYBOARD_KEY_STATE_RELEASED,
Daniel Stone50692802012-06-22 13:21:41 +01001252 STATE_UPDATE_NONE);
Daniel Stone37816df2012-05-16 18:45:18 +01001253}
1254
Daniel Stone351eb612012-05-31 15:27:47 -04001255static void
1256input_handle_modifiers(void *data, struct wl_keyboard *keyboard,
Daniel Stone50692802012-06-22 13:21:41 +01001257 uint32_t serial_in, uint32_t mods_depressed,
Daniel Stone351eb612012-05-31 15:27:47 -04001258 uint32_t mods_latched, uint32_t mods_locked,
1259 uint32_t group)
1260{
Daniel Stone50692802012-06-22 13:21:41 +01001261 struct wayland_input *input = data;
1262 struct wayland_compositor *c = input->compositor;
1263 uint32_t serial_out;
1264
1265 /* If we get a key event followed by a modifier event with the
1266 * same serial number, then we try to preserve those semantics by
1267 * reusing the same serial number on the way out too. */
1268 if (serial_in == input->key_serial)
1269 serial_out = wl_display_get_serial(c->base.wl_display);
1270 else
1271 serial_out = wl_display_next_serial(c->base.wl_display);
1272
Jonas Ådahl7395ea02013-12-03 09:14:26 +01001273 xkb_state_update_mask(input->base.keyboard->xkb_state.state,
Daniel Stone50692802012-06-22 13:21:41 +01001274 mods_depressed, mods_latched,
1275 mods_locked, 0, 0, group);
Kristian Høgsbergcb3eaae2012-08-10 09:50:11 -04001276 notify_modifiers(&input->base, serial_out);
Daniel Stone351eb612012-05-31 15:27:47 -04001277}
1278
Daniel Stone37816df2012-05-16 18:45:18 +01001279static const struct wl_keyboard_listener keyboard_listener = {
Daniel Stoneb7452fe2012-06-01 12:14:06 +01001280 input_handle_keymap,
Kristian Høgsberg06d58b72012-02-23 09:59:05 -05001281 input_handle_keyboard_enter,
1282 input_handle_keyboard_leave,
Daniel Stone37816df2012-05-16 18:45:18 +01001283 input_handle_key,
Daniel Stone351eb612012-05-31 15:27:47 -04001284 input_handle_modifiers,
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001285};
1286
1287static void
Daniel Stone37816df2012-05-16 18:45:18 +01001288input_handle_capabilities(void *data, struct wl_seat *seat,
1289 enum wl_seat_capability caps)
1290{
1291 struct wayland_input *input = data;
1292
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001293 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->parent.pointer) {
1294 input->parent.pointer = wl_seat_get_pointer(seat);
1295 wl_pointer_set_user_data(input->parent.pointer, input);
1296 wl_pointer_add_listener(input->parent.pointer,
1297 &pointer_listener, input);
Kristian Høgsberg7af7ced2012-08-10 10:01:33 -04001298 weston_seat_init_pointer(&input->base);
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001299 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->parent.pointer) {
1300 wl_pointer_destroy(input->parent.pointer);
1301 input->parent.pointer = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001302 }
1303
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001304 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->parent.keyboard) {
1305 input->parent.keyboard = wl_seat_get_keyboard(seat);
1306 wl_keyboard_set_user_data(input->parent.keyboard, input);
1307 wl_keyboard_add_listener(input->parent.keyboard,
1308 &keyboard_listener, input);
1309 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->parent.keyboard) {
1310 wl_keyboard_destroy(input->parent.keyboard);
1311 input->parent.keyboard = NULL;
Daniel Stone37816df2012-05-16 18:45:18 +01001312 }
1313}
1314
1315static const struct wl_seat_listener seat_listener = {
1316 input_handle_capabilities,
1317};
1318
1319static void
1320display_add_seat(struct wayland_compositor *c, uint32_t id)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001321{
1322 struct wayland_input *input;
1323
Peter Huttererf3d62272013-08-08 11:57:05 +10001324 input = zalloc(sizeof *input);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001325 if (input == NULL)
1326 return;
1327
Rob Bradford9af5f9e2013-05-31 18:09:50 +01001328 weston_seat_init(&input->base, &c->base, "default");
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001329 input->compositor = c;
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001330 input->parent.seat = wl_registry_bind(c->parent.registry, id,
1331 &wl_seat_interface, 1);
Jason Ekstrand06ced802013-11-07 20:13:29 -06001332 wl_list_insert(c->input_list.prev, &input->link);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001333
Jason Ekstrand8f89fcb2013-10-27 22:24:53 -05001334 wl_seat_add_listener(input->parent.seat, &seat_listener, input);
1335 wl_seat_set_user_data(input->parent.seat, input);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001336
1337 input->parent.cursor.surface =
1338 wl_compositor_create_surface(c->parent.compositor);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001339}
1340
1341static void
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001342registry_handle_global(void *data, struct wl_registry *registry, uint32_t name,
1343 const char *interface, uint32_t version)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001344{
1345 struct wayland_compositor *c = data;
1346
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001347 if (strcmp(interface, "wl_compositor") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001348 c->parent.compositor =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001349 wl_registry_bind(registry, name,
1350 &wl_compositor_interface, 1);
Benjamin Franzke080ab6c2011-04-30 10:41:27 +02001351 } else if (strcmp(interface, "wl_shell") == 0) {
Kristian Høgsbergf790c792011-08-19 14:41:57 -04001352 c->parent.shell =
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001353 wl_registry_bind(registry, name,
1354 &wl_shell_interface, 1);
Daniel Stone725c2c32012-06-22 14:04:36 +01001355 } else if (strcmp(interface, "wl_seat") == 0) {
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001356 display_add_seat(c, name);
Jonas Ådahle5a12252013-04-05 23:07:11 +02001357 } else if (strcmp(interface, "wl_shm") == 0) {
1358 c->parent.shm =
1359 wl_registry_bind(registry, name, &wl_shm_interface, 1);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001360 }
1361}
1362
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001363static const struct wl_registry_listener registry_listener = {
1364 registry_handle_global
1365};
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001366
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001367static int
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001368wayland_compositor_handle_event(int fd, uint32_t mask, void *data)
1369{
1370 struct wayland_compositor *c = data;
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001371 int count = 0;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001372
Kristian Høgsberg453de7a2013-10-30 23:15:44 -07001373 if ((mask & WL_EVENT_HANGUP) || (mask & WL_EVENT_ERROR)) {
1374 wl_display_terminate(c->base.wl_display);
1375 return 0;
1376 }
1377
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001378 if (mask & WL_EVENT_READABLE)
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001379 count = wl_display_dispatch(c->parent.wl_display);
Kristian Høgsbergf258a312011-12-28 22:51:20 -05001380 if (mask & WL_EVENT_WRITABLE)
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001381 wl_display_flush(c->parent.wl_display);
Kristian Høgsberg95d843d2011-04-22 13:01:26 -04001382
Kristian Høgsbergfeb3c1d2012-10-15 12:56:11 -04001383 if (mask == 0) {
1384 count = wl_display_dispatch_pending(c->parent.wl_display);
1385 wl_display_flush(c->parent.wl_display);
1386 }
1387
1388 return count;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001389}
1390
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001391static void
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001392wayland_restore(struct weston_compositor *ec)
1393{
1394}
1395
1396static void
Kristian Høgsberg8334bc12012-01-03 10:29:47 -05001397wayland_destroy(struct weston_compositor *ec)
Kristian Høgsbergcaa64422010-12-01 16:52:15 -05001398{
Jonas Ådahle5a12252013-04-05 23:07:11 +02001399 struct wayland_compositor *c = (struct wayland_compositor *) ec;
1400
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +03001401 ec->renderer->destroy(ec);
Kristian Høgsberg3a0de882012-09-06 21:44:24 -04001402
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
1432 c->cursor = NULL;
1433 for (i = 0; !c->cursor && i < ARRAY_LENGTH(left_ptrs); ++i)
1434 c->cursor = wl_cursor_theme_get_cursor(c->cursor_theme,
1435 left_ptrs[i]);
1436 if (!c->cursor) {
1437 fprintf(stderr, "could not load left cursor\n");
1438 return;
1439 }
1440}
1441
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001442static void
1443fullscreen_binding(struct weston_seat *seat_base, uint32_t time, uint32_t key,
1444 void *data)
1445{
1446 struct wayland_compositor *c = data;
1447 struct wayland_input *input = NULL;
1448
1449 wl_list_for_each(input, &c->input_list, link)
1450 if (&input->base == seat_base)
1451 break;
1452
1453 if (!input || !input->output)
1454 return;
1455
1456 if (input->output->frame)
1457 wayland_output_set_fullscreen(input->output, 0, 0, NULL);
1458 else
1459 wayland_output_set_windowed(input->output);
1460
1461 weston_output_schedule_repaint(&input->output->base);
1462}
1463
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001464static struct wayland_compositor *
1465wayland_compositor_create(struct wl_display *display, int use_pixman,
1466 const char *display_name, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001467 struct weston_config *config)
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001468{
1469 struct wayland_compositor *c;
1470 struct wl_event_loop *loop;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001471 int fd;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001472
Peter Huttererf3d62272013-08-08 11:57:05 +10001473 c = zalloc(sizeof *c);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001474 if (c == NULL)
1475 return NULL;
1476
Daniel Stone725c2c32012-06-22 14:04:36 +01001477 if (weston_compositor_init(&c->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -04001478 config) < 0)
Martin Olssonc5db50f2012-07-08 03:03:43 +02001479 goto err_free;
Daniel Stone725c2c32012-06-22 14:04:36 +01001480
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001481 c->parent.wl_display = wl_display_connect(display_name);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001482
Kristian Høgsberg362b6722012-06-18 15:13:51 -04001483 if (c->parent.wl_display == NULL) {
Martin Minarik6d118362012-06-07 18:01:59 +02001484 weston_log("failed to create display: %m\n");
Martin Olssonc5db50f2012-07-08 03:03:43 +02001485 goto err_compositor;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001486 }
1487
Jason Ekstrand06ced802013-11-07 20:13:29 -06001488 wl_list_init(&c->input_list);
Kristian Høgsbergfa80e112012-10-10 21:34:26 -04001489 c->parent.registry = wl_display_get_registry(c->parent.wl_display);
1490 wl_registry_add_listener(c->parent.registry, &registry_listener, c);
Jason Ekstrand7744f712013-10-27 22:24:55 -05001491 wl_display_roundtrip(c->parent.wl_display);
1492
1493 create_cursor(c, config);
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001494
1495 c->base.wl_display = display;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001496
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001497 c->use_pixman = use_pixman;
Ander Conselvan de Oliveira97f29522013-10-14 15:57:11 +03001498
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001499 if (!c->use_pixman) {
1500 gl_renderer = weston_load_module("gl-renderer.so",
1501 "gl_renderer_interface");
1502 if (!gl_renderer)
1503 c->use_pixman = 1;
1504 }
1505
1506 if (!c->use_pixman) {
Jason Ekstrandff2fd462013-10-27 22:24:58 -05001507 if (gl_renderer->create(&c->base, c->parent.wl_display,
1508 gl_renderer->alpha_attribs,
1509 NULL) < 0) {
1510 weston_log("Failed to initialize the GL renderer; "
1511 "falling back to pixman.\n");
1512 c->use_pixman = 1;
1513 }
1514 }
1515
1516 if (c->use_pixman) {
1517 if (pixman_renderer_init(&c->base) < 0) {
1518 weston_log("Failed to initialize pixman renderer\n");
1519 goto err_display;
1520 }
1521 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001522
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001523 c->base.destroy = wayland_destroy;
Kristian Høgsberg7b884bc2012-07-31 14:32:01 -04001524 c->base.restore = wayland_restore;
Benjamin Franzkeecfb2b92011-01-15 12:34:48 +01001525
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001526 loop = wl_display_get_event_loop(c->base.wl_display);
1527
1528 fd = wl_display_get_fd(c->parent.wl_display);
1529 c->parent.wl_source =
1530 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
1531 wayland_compositor_handle_event, c);
1532 if (c->parent.wl_source == NULL)
1533 goto err_renderer;
1534
1535 wl_event_source_check(c->parent.wl_source);
1536
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001537 weston_compositor_add_key_binding(&c->base, KEY_F,
1538 MODIFIER_CTRL | MODIFIER_ALT,
1539 fullscreen_binding, c);
1540
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001541 return c;
1542err_renderer:
1543 c->base.renderer->destroy(&c->base);
1544err_display:
1545 wl_display_disconnect(c->parent.wl_display);
1546err_compositor:
1547 weston_compositor_shutdown(&c->base);
1548err_free:
1549 free(c);
1550 return NULL;
1551}
1552
1553static void
1554wayland_compositor_destroy(struct wayland_compositor *c)
1555{
1556 struct weston_output *output;
1557
1558 wl_list_for_each(output, &c->base.output_list, link)
1559 wayland_output_destroy(output);
1560
1561 c->base.renderer->destroy(&c->base);
1562 wl_display_disconnect(c->parent.wl_display);
1563
1564 if (c->theme)
1565 theme_destroy(c->theme);
1566 if (c->frame_device)
1567 cairo_device_destroy(c->frame_device);
1568 wl_cursor_theme_destroy(c->cursor_theme);
1569
1570 weston_compositor_shutdown(&c->base);
1571 free(c);
1572}
1573
1574WL_EXPORT struct weston_compositor *
1575backend_init(struct wl_display *display, int *argc, char *argv[],
1576 struct weston_config *config)
1577{
1578 struct wayland_compositor *c;
1579 struct wayland_output *output;
1580 struct weston_config_section *section;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001581 int x, count, width, height, scale, use_pixman, fullscreen;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001582 const char *section_name, *display_name;
1583 char *name;
1584
1585 const struct weston_option wayland_options[] = {
1586 { WESTON_OPTION_INTEGER, "width", 0, &width },
1587 { WESTON_OPTION_INTEGER, "height", 0, &height },
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001588 { WESTON_OPTION_INTEGER, "scale", 0, &scale },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001589 { WESTON_OPTION_STRING, "display", 0, &display_name },
1590 { WESTON_OPTION_BOOLEAN, "use-pixman", 0, &use_pixman },
1591 { WESTON_OPTION_INTEGER, "output-count", 0, &count },
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001592 { WESTON_OPTION_BOOLEAN, "fullscreen", 0, &fullscreen },
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001593 };
1594
1595 width = 0;
1596 height = 0;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001597 scale = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001598 display_name = NULL;
1599 use_pixman = 0;
1600 count = 1;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001601 fullscreen = 0;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001602 parse_options(wayland_options,
1603 ARRAY_LENGTH(wayland_options), argc, argv);
1604
1605 c = wayland_compositor_create(display, use_pixman, display_name,
1606 argc, argv, config);
1607 if (!c)
1608 return NULL;
1609
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001610 if (fullscreen) {
1611 output = wayland_output_create(c, 0, 0, width, height,
1612 NULL, 1, 0, 1);
1613 if (!output)
1614 goto err_outputs;
1615
Axel Davydd8b88d2013-11-17 21:34:16 +01001616 wayland_output_set_fullscreen(output, 0, 0, NULL);
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001617 return &c->base;
1618 }
1619
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001620 section = NULL;
1621 x = 0;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001622 while (weston_config_next_section(config, &section, &section_name)) {
1623 if (!section_name || strcmp(section_name, "output") != 0)
1624 continue;
1625 weston_config_section_get_string(section, "name", &name, NULL);
1626 if (name == NULL)
1627 continue;
1628
1629 if (name[0] != 'W' || name[1] != 'L') {
1630 free(name);
1631 continue;
1632 }
1633 free(name);
1634
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001635 output = wayland_output_create_for_config(c, section, width,
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001636 height, scale, x, 0);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001637 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001638 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001639 if (wayland_output_set_windowed(output))
1640 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001641
1642 x += output->base.width;
1643 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001644 }
1645
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001646 if (!width)
1647 width = 1024;
1648 if (!height)
1649 height = 640;
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001650 if (!scale)
1651 scale = 1;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001652 while (count > 0) {
Jason Ekstrand12c6dd92013-11-07 20:13:32 -06001653 output = wayland_output_create(c, x, 0, width, height,
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001654 NULL, 0, 0, scale);
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001655 if (!output)
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001656 goto err_outputs;
Jason Ekstrand5ea04802013-11-07 20:13:33 -06001657 if (wayland_output_set_windowed(output))
1658 goto err_outputs;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001659
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001660 x += width;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001661 --count;
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001662 }
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001663
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001664 return &c->base;
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001665
Jason Ekstrand48ce4212013-10-27 22:25:02 -05001666err_outputs:
Jason Ekstrand0cf39352013-11-07 20:13:31 -06001667 wayland_compositor_destroy(c);
Martin Olssonc5db50f2012-07-08 03:03:43 +02001668 return NULL;
Benjamin Franzkeec2e6422010-11-27 19:04:12 +01001669}