blob: 3d95d3037db684879136172d55124dc1057397c3 [file] [log] [blame]
Pekka Paalanene8de35c2012-11-07 12:25:14 +02001/*
2 * Copyright © 2008-2011 Kristian Høgsberg
3 * Copyright © 2011 Intel Corporation
Pekka Paalanene31e0532013-05-22 18:03:07 +03004 * Copyright © 2012-2013 Raspberry Pi Foundation
Pekka Paalanene8de35c2012-11-07 12:25:14 +02005 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that the above copyright notice appear in all copies and that both that
9 * copyright notice and this permission notice appear in supporting
10 * documentation, and that the name of the copyright holders not be used in
11 * advertising or publicity pertaining to distribution of the software
12 * without specific, written prior permission. The copyright holders make
13 * no representations about the suitability of this software for any
14 * purpose. It is provided "as is" without express or implied warranty.
15 *
16 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
17 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
21 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
22 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 */
24
25#define _GNU_SOURCE
26
27#include <errno.h>
28#include <stdlib.h>
29#include <stdio.h>
30#include <string.h>
31#include <math.h>
32#include <sys/types.h>
33#include <fcntl.h>
34#include <unistd.h>
35
36#include <libudev.h>
37
38#include "config.h"
39
40#ifdef HAVE_BCM_HOST
41# include <bcm_host.h>
42#else
43# include "rpi-bcm-stubs.h"
44#endif
45
46#include "compositor.h"
Pekka Paalanene31e0532013-05-22 18:03:07 +030047#include "rpi-renderer.h"
Pekka Paalanene8de35c2012-11-07 12:25:14 +020048#include "evdev.h"
49
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +020050#if 0
51#define DBG(...) \
52 weston_log(__VA_ARGS__)
53#else
54#define DBG(...) do {} while (0)
55#endif
56
Pekka Paalanene8de35c2012-11-07 12:25:14 +020057struct rpi_compositor;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +020058struct rpi_output;
59
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +020060struct rpi_flippipe {
61 int readfd;
62 int writefd;
63 struct wl_event_source *source;
64};
Pekka Paalanene8de35c2012-11-07 12:25:14 +020065
66struct rpi_output {
67 struct rpi_compositor *compositor;
68 struct weston_output base;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +020069 int single_buffer;
Pekka Paalanene8de35c2012-11-07 12:25:14 +020070
71 struct weston_mode mode;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +020072 struct rpi_flippipe flippipe;
Pekka Paalanene8de35c2012-11-07 12:25:14 +020073
74 DISPMANX_DISPLAY_HANDLE_T display;
Pekka Paalanene8de35c2012-11-07 12:25:14 +020075};
76
77struct rpi_seat {
78 struct weston_seat base;
79 struct wl_list devices_list;
80
81 struct udev_monitor *udev_monitor;
82 struct wl_event_source *udev_monitor_source;
83 char *seat_id;
84};
85
86struct rpi_compositor {
87 struct weston_compositor base;
88 uint32_t prev_state;
89
90 struct udev *udev;
91 struct tty *tty;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +020092
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +020093 int single_buffer;
Pekka Paalanene8de35c2012-11-07 12:25:14 +020094};
95
96static inline struct rpi_output *
97to_rpi_output(struct weston_output *base)
98{
99 return container_of(base, struct rpi_output, base);
100}
101
102static inline struct rpi_seat *
103to_rpi_seat(struct weston_seat *base)
104{
105 return container_of(base, struct rpi_seat, base);
106}
107
108static inline struct rpi_compositor *
109to_rpi_compositor(struct weston_compositor *base)
110{
111 return container_of(base, struct rpi_compositor, base);
112}
113
Jonas Ådahle5a12252013-04-05 23:07:11 +0200114static uint64_t
115rpi_get_current_time(void)
116{
117 struct timeval tv;
118
119 /* XXX: use CLOCK_MONOTONIC instead? */
120 gettimeofday(&tv, NULL);
121 return (uint64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
122}
123
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200124static void
125rpi_flippipe_update_complete(DISPMANX_UPDATE_HANDLE_T update, void *data)
126{
127 /* This function runs in a different thread. */
128 struct rpi_flippipe *flippipe = data;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200129 uint64_t time;
130 ssize_t ret;
131
132 /* manufacture flip completion timestamp */
Jonas Ådahle5a12252013-04-05 23:07:11 +0200133 time = rpi_get_current_time();
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200134
135 ret = write(flippipe->writefd, &time, sizeof time);
136 if (ret != sizeof time)
137 weston_log("ERROR: %s failed to write, ret %zd, errno %d\n",
138 __func__, ret, errno);
139}
140
141static int
142rpi_dispmanx_update_submit(DISPMANX_UPDATE_HANDLE_T update,
143 struct rpi_output *output)
144{
145 /*
146 * The callback registered here will eventually be called
147 * in a different thread context. Therefore we cannot call
148 * the usual functions from rpi_flippipe_update_complete().
149 * Instead, we have a pipe for passing the message from the
150 * thread, waking up the Weston main event loop, calling
151 * rpi_flippipe_handler(), and then ending up in
152 * rpi_output_update_complete() in the main thread context,
153 * where we can do the frame finishing work.
154 */
155 return vc_dispmanx_update_submit(update, rpi_flippipe_update_complete,
156 &output->flippipe);
157}
158
159static void
160rpi_output_update_complete(struct rpi_output *output, uint64_t time);
161
162static int
163rpi_flippipe_handler(int fd, uint32_t mask, void *data)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200164{
165 struct rpi_output *output = data;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200166 ssize_t ret;
167 uint64_t time;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200168
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200169 if (mask != WL_EVENT_READABLE)
170 weston_log("ERROR: unexpected mask 0x%x in %s\n",
171 mask, __func__);
172
173 ret = read(fd, &time, sizeof time);
174 if (ret != sizeof time) {
175 weston_log("ERROR: %s failed to read, ret %zd, errno %d\n",
176 __func__, ret, errno);
177 }
178
179 rpi_output_update_complete(output, time);
180
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200181 return 1;
182}
183
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200184static int
185rpi_flippipe_init(struct rpi_flippipe *flippipe, struct rpi_output *output)
186{
187 struct wl_event_loop *loop;
188 int fd[2];
189
190 if (pipe2(fd, O_CLOEXEC) == -1)
191 return -1;
192
193 flippipe->readfd = fd[0];
194 flippipe->writefd = fd[1];
195
196 loop = wl_display_get_event_loop(output->compositor->base.wl_display);
197 flippipe->source = wl_event_loop_add_fd(loop, flippipe->readfd,
198 WL_EVENT_READABLE,
199 rpi_flippipe_handler, output);
200
201 if (!flippipe->source) {
202 close(flippipe->readfd);
203 close(flippipe->writefd);
204 return -1;
205 }
206
207 return 0;
208}
209
210static void
211rpi_flippipe_release(struct rpi_flippipe *flippipe)
212{
213 wl_event_source_remove(flippipe->source);
214 close(flippipe->readfd);
215 close(flippipe->writefd);
216}
217
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200218static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200219rpi_output_start_repaint_loop(struct weston_output *output)
220{
221 uint64_t time;
222
223 time = rpi_get_current_time();
224 weston_output_finish_frame(output, time);
225}
226
227static void
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200228rpi_output_repaint(struct weston_output *base, pixman_region32_t *damage)
229{
230 struct rpi_output *output = to_rpi_output(base);
231 struct rpi_compositor *compositor = output->compositor;
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200232 struct weston_plane *primary_plane = &compositor->base.primary_plane;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200233 DISPMANX_UPDATE_HANDLE_T update;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200234
Pekka Paalanene31e0532013-05-22 18:03:07 +0300235 DBG("frame update start\n");
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200236
Pekka Paalanene31e0532013-05-22 18:03:07 +0300237 /* Update priority higher than in rpi-renderer's
238 * output destroy function, see rpi_output_destroy().
239 */
240 update = vc_dispmanx_update_start(1);
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200241
Pekka Paalanene31e0532013-05-22 18:03:07 +0300242 rpi_renderer_set_update_handle(&output->base, update);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200243 compositor->base.renderer->repaint_output(&output->base, damage);
244
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200245 pixman_region32_subtract(&primary_plane->damage,
246 &primary_plane->damage, damage);
247
Pekka Paalanene31e0532013-05-22 18:03:07 +0300248 /* schedule callback to rpi_output_update_complete() */
249 rpi_dispmanx_update_submit(update, output);
250 DBG("frame update submitted\n");
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200251}
252
253static void
254rpi_output_update_complete(struct rpi_output *output, uint64_t time)
255{
Pekka Paalanene31e0532013-05-22 18:03:07 +0300256 DBG("frame update complete(%" PRIu64 ")\n", time);
Pekka Paalanene31e0532013-05-22 18:03:07 +0300257 rpi_renderer_finish_frame(&output->base);
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200258 weston_output_finish_frame(&output->base, time);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200259}
260
261static void
262rpi_output_destroy(struct weston_output *base)
263{
264 struct rpi_output *output = to_rpi_output(base);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200265
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200266 DBG("%s\n", __func__);
267
Pekka Paalanene31e0532013-05-22 18:03:07 +0300268 rpi_renderer_output_destroy(base);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200269
Pekka Paalanene31e0532013-05-22 18:03:07 +0300270 /* rpi_renderer_output_destroy() will schedule a removal of
271 * all Dispmanx Elements, and wait for the update to complete.
272 * Assuming updates are sequential, the wait should guarantee,
273 * that any pending rpi_flippipe_update_complete() callbacks
274 * have happened already. Therefore we can destroy the flippipe
275 * now.
276 */
277 rpi_flippipe_release(&output->flippipe);
278
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200279 wl_list_remove(&output->base.link);
280 weston_output_destroy(&output->base);
281
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200282 vc_dispmanx_display_close(output->display);
283
284 free(output);
285}
286
Pekka Paalanene31e0532013-05-22 18:03:07 +0300287static const char *transform_names[] = {
288 [WL_OUTPUT_TRANSFORM_NORMAL] = "normal",
289 [WL_OUTPUT_TRANSFORM_90] = "90",
290 [WL_OUTPUT_TRANSFORM_180] = "180",
291 [WL_OUTPUT_TRANSFORM_270] = "270",
292 [WL_OUTPUT_TRANSFORM_FLIPPED] = "flipped",
293 [WL_OUTPUT_TRANSFORM_FLIPPED_90] = "flipped-90",
294 [WL_OUTPUT_TRANSFORM_FLIPPED_180] = "flipped-180",
295 [WL_OUTPUT_TRANSFORM_FLIPPED_270] = "flipped-270",
296};
297
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200298static int
Pekka Paalanene31e0532013-05-22 18:03:07 +0300299str2transform(const char *name)
300{
301 unsigned i;
302
303 for (i = 0; i < ARRAY_LENGTH(transform_names); i++)
304 if (strcmp(name, transform_names[i]) == 0)
305 return i;
306
307 return -1;
308}
309
310static const char *
311transform2str(uint32_t output_transform)
312{
313 if (output_transform >= ARRAY_LENGTH(transform_names))
314 return "<illegal value>";
315
316 return transform_names[output_transform];
317}
318
319static int
320rpi_output_create(struct rpi_compositor *compositor, uint32_t transform)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200321{
322 struct rpi_output *output;
323 DISPMANX_MODEINFO_T modeinfo;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200324 int ret;
325 float mm_width, mm_height;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200326
327 output = calloc(1, sizeof *output);
328 if (!output)
329 return -1;
330
331 output->compositor = compositor;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200332 output->single_buffer = compositor->single_buffer;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200333
334 if (rpi_flippipe_init(&output->flippipe, output) < 0) {
335 weston_log("Creating message pipe failed.\n");
336 goto out_free;
337 }
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200338
339 output->display = vc_dispmanx_display_open(DISPMANX_ID_HDMI);
340 if (!output->display) {
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200341 weston_log("Failed to open dispmanx HDMI display.\n");
342 goto out_pipe;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200343 }
344
345 ret = vc_dispmanx_display_get_info(output->display, &modeinfo);
346 if (ret < 0) {
347 weston_log("Failed to get display mode information.\n");
348 goto out_dmx_close;
349 }
350
Jonas Ådahle5a12252013-04-05 23:07:11 +0200351 output->base.start_repaint_loop = rpi_output_start_repaint_loop;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200352 output->base.repaint = rpi_output_repaint;
353 output->base.destroy = rpi_output_destroy;
Pekka Paalanene31e0532013-05-22 18:03:07 +0300354 output->base.assign_planes = NULL;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200355 output->base.set_backlight = NULL;
356 output->base.set_dpms = NULL;
357 output->base.switch_mode = NULL;
358
359 /* XXX: use tvservice to get information from and control the
360 * HDMI and SDTV outputs. See:
361 * /opt/vc/include/interface/vmcs_host/vc_tvservice.h
362 */
363
364 /* only one static mode in list */
365 output->mode.flags =
366 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
367 output->mode.width = modeinfo.width;
368 output->mode.height = modeinfo.height;
369 output->mode.refresh = 60000;
370 wl_list_init(&output->base.mode_list);
371 wl_list_insert(&output->base.mode_list, &output->mode.link);
372
373 output->base.current = &output->mode;
374 output->base.origin = &output->mode;
375 output->base.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
376 output->base.make = "unknown";
377 output->base.model = "unknown";
378
379 /* guess 96 dpi */
380 mm_width = modeinfo.width * (25.4f / 96.0f);
381 mm_height = modeinfo.height * (25.4f / 96.0f);
382
383 weston_output_init(&output->base, &compositor->base,
384 0, 0, round(mm_width), round(mm_height),
Pekka Paalanene31e0532013-05-22 18:03:07 +0300385 transform, 1);
John Kåre Alsaker94659272012-11-13 19:10:18 +0100386
Pekka Paalanene31e0532013-05-22 18:03:07 +0300387 if (rpi_renderer_output_create(&output->base, output->display) < 0)
John Kåre Alsaker94659272012-11-13 19:10:18 +0100388 goto out_output;
389
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200390 wl_list_insert(compositor->base.output_list.prev, &output->base.link);
391
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200392 weston_log("Raspberry Pi HDMI output %dx%d px\n",
393 output->mode.width, output->mode.height);
394 weston_log_continue(STAMP_SPACE "guessing %d Hz and 96 dpi\n",
395 output->mode.refresh / 1000);
Pekka Paalanene31e0532013-05-22 18:03:07 +0300396 weston_log_continue(STAMP_SPACE "orientation: %s\n",
397 transform2str(output->base.transform));
398
399 if (!strncmp(transform2str(output->base.transform), "flipped", 7))
400 weston_log("warning: flipped output transforms may not work\n");
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200401
402 return 0;
403
John Kåre Alsaker94659272012-11-13 19:10:18 +0100404out_output:
405 weston_output_destroy(&output->base);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200406
407out_dmx_close:
408 vc_dispmanx_display_close(output->display);
409
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200410out_pipe:
411 rpi_flippipe_release(&output->flippipe);
412
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200413out_free:
414 free(output);
415 return -1;
416}
417
418static void
419rpi_led_update(struct weston_seat *seat_base, enum weston_led leds)
420{
421 struct rpi_seat *seat = to_rpi_seat(seat_base);
422 struct evdev_device *device;
423
424 wl_list_for_each(device, &seat->devices_list, link)
425 evdev_led_update(device, leds);
426}
427
428static const char default_seat[] = "seat0";
429
430static void
431device_added(struct udev_device *udev_device, struct rpi_seat *master)
432{
433 struct evdev_device *device;
434 const char *devnode;
435 const char *device_seat;
436 int fd;
437
438 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
439 if (!device_seat)
440 device_seat = default_seat;
441
442 if (strcmp(device_seat, master->seat_id))
443 return;
444
445 devnode = udev_device_get_devnode(udev_device);
446
447 /* Use non-blocking mode so that we can loop on read on
448 * evdev_device_data() until all events on the fd are
449 * read. mtdev_get() also expects this. */
450 fd = open(devnode, O_RDWR | O_NONBLOCK | O_CLOEXEC);
451 if (fd < 0) {
452 weston_log("opening input device '%s' failed.\n", devnode);
453 return;
454 }
455
456 device = evdev_device_create(&master->base, devnode, fd);
457 if (!device) {
458 close(fd);
459 weston_log("not using input device '%s'.\n", devnode);
460 return;
461 }
462
463 wl_list_insert(master->devices_list.prev, &device->link);
464}
465
466static void
467evdev_add_devices(struct udev *udev, struct weston_seat *seat_base)
468{
469 struct rpi_seat *seat = to_rpi_seat(seat_base);
470 struct udev_enumerate *e;
471 struct udev_list_entry *entry;
472 struct udev_device *device;
473 const char *path, *sysname;
474
475 e = udev_enumerate_new(udev);
476 udev_enumerate_add_match_subsystem(e, "input");
477 udev_enumerate_scan_devices(e);
478 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
479 path = udev_list_entry_get_name(entry);
480 device = udev_device_new_from_syspath(udev, path);
481
482 sysname = udev_device_get_sysname(device);
483 if (strncmp("event", sysname, 5) != 0) {
484 udev_device_unref(device);
485 continue;
486 }
487
488 device_added(device, seat);
489
490 udev_device_unref(device);
491 }
492 udev_enumerate_unref(e);
493
494 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
495
496 if (wl_list_empty(&seat->devices_list)) {
497 weston_log(
498 "warning: no input devices on entering Weston. "
499 "Possible causes:\n"
500 "\t- no permissions to read /dev/input/event*\n"
501 "\t- seats misconfigured "
502 "(Weston backend option 'seat', "
503 "udev device property ID_SEAT)\n");
504 }
505}
506
507static int
508evdev_udev_handler(int fd, uint32_t mask, void *data)
509{
510 struct rpi_seat *seat = data;
511 struct udev_device *udev_device;
512 struct evdev_device *device, *next;
513 const char *action;
514 const char *devnode;
515
516 udev_device = udev_monitor_receive_device(seat->udev_monitor);
517 if (!udev_device)
518 return 1;
519
520 action = udev_device_get_action(udev_device);
521 if (!action)
522 goto out;
523
524 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
525 goto out;
526
527 if (!strcmp(action, "add")) {
528 device_added(udev_device, seat);
529 }
530 else if (!strcmp(action, "remove")) {
531 devnode = udev_device_get_devnode(udev_device);
532 wl_list_for_each_safe(device, next, &seat->devices_list, link)
533 if (!strcmp(device->devnode, devnode)) {
534 weston_log("input device %s, %s removed\n",
535 device->devname, device->devnode);
536 evdev_device_destroy(device);
537 break;
538 }
539 }
540
541out:
542 udev_device_unref(udev_device);
543
544 return 0;
545}
546
547static int
548evdev_enable_udev_monitor(struct udev *udev, struct weston_seat *seat_base)
549{
550 struct rpi_seat *master = to_rpi_seat(seat_base);
551 struct wl_event_loop *loop;
552 struct weston_compositor *c = master->base.compositor;
553 int fd;
554
555 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
556 if (!master->udev_monitor) {
557 weston_log("udev: failed to create the udev monitor\n");
558 return 0;
559 }
560
561 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
562 "input", NULL);
563
564 if (udev_monitor_enable_receiving(master->udev_monitor)) {
565 weston_log("udev: failed to bind the udev monitor\n");
566 udev_monitor_unref(master->udev_monitor);
567 return 0;
568 }
569
570 loop = wl_display_get_event_loop(c->wl_display);
571 fd = udev_monitor_get_fd(master->udev_monitor);
572 master->udev_monitor_source =
573 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
574 evdev_udev_handler, master);
575 if (!master->udev_monitor_source) {
576 udev_monitor_unref(master->udev_monitor);
577 return 0;
578 }
579
580 return 1;
581}
582
583static void
584evdev_disable_udev_monitor(struct weston_seat *seat_base)
585{
586 struct rpi_seat *seat = to_rpi_seat(seat_base);
587
588 if (!seat->udev_monitor)
589 return;
590
591 udev_monitor_unref(seat->udev_monitor);
592 seat->udev_monitor = NULL;
593 wl_event_source_remove(seat->udev_monitor_source);
594 seat->udev_monitor_source = NULL;
595}
596
597static void
598evdev_input_create(struct weston_compositor *c, struct udev *udev,
599 const char *seat_id)
600{
601 struct rpi_seat *seat;
602
603 seat = malloc(sizeof *seat);
604 if (seat == NULL)
605 return;
606
607 memset(seat, 0, sizeof *seat);
608 weston_seat_init(&seat->base, c);
609 seat->base.led_update = rpi_led_update;
610
611 wl_list_init(&seat->devices_list);
612 seat->seat_id = strdup(seat_id);
613 if (!evdev_enable_udev_monitor(udev, &seat->base)) {
614 free(seat->seat_id);
615 free(seat);
616 return;
617 }
618
619 evdev_add_devices(udev, &seat->base);
620}
621
622static void
623evdev_remove_devices(struct weston_seat *seat_base)
624{
625 struct rpi_seat *seat = to_rpi_seat(seat_base);
626 struct evdev_device *device, *next;
627
628 wl_list_for_each_safe(device, next, &seat->devices_list, link)
629 evdev_device_destroy(device);
630
Kristian Høgsberge3148752013-05-06 23:19:49 -0400631 if (seat->base.keyboard)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200632 notify_keyboard_focus_out(&seat->base);
633}
634
635static void
636evdev_input_destroy(struct weston_seat *seat_base)
637{
638 struct rpi_seat *seat = to_rpi_seat(seat_base);
639
640 evdev_remove_devices(seat_base);
641 evdev_disable_udev_monitor(&seat->base);
642
643 weston_seat_release(seat_base);
644 free(seat->seat_id);
645 free(seat);
646}
647
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200648static void
649rpi_compositor_destroy(struct weston_compositor *base)
650{
651 struct rpi_compositor *compositor = to_rpi_compositor(base);
652 struct weston_seat *seat, *next;
653
654 wl_list_for_each_safe(seat, next, &compositor->base.seat_list, link)
655 evdev_input_destroy(seat);
656
657 /* destroys outputs, too */
658 weston_compositor_shutdown(&compositor->base);
659
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +0300660 compositor->base.renderer->destroy(&compositor->base);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200661 tty_destroy(compositor->tty);
662
663 bcm_host_deinit();
664 free(compositor);
665}
666
667static void
668vt_func(struct weston_compositor *base, int event)
669{
670 struct rpi_compositor *compositor = to_rpi_compositor(base);
671 struct weston_seat *seat;
672 struct weston_output *output;
673
674 switch (event) {
675 case TTY_ENTER_VT:
676 weston_log("entering VT\n");
677 compositor->base.focus = 1;
678 compositor->base.state = compositor->prev_state;
679 weston_compositor_damage_all(&compositor->base);
680 wl_list_for_each(seat, &compositor->base.seat_list, link) {
681 evdev_add_devices(compositor->udev, seat);
682 evdev_enable_udev_monitor(compositor->udev, seat);
683 }
684 break;
685 case TTY_LEAVE_VT:
686 weston_log("leaving VT\n");
687 wl_list_for_each(seat, &compositor->base.seat_list, link) {
688 evdev_disable_udev_monitor(seat);
689 evdev_remove_devices(seat);
690 }
691
692 compositor->base.focus = 0;
693 compositor->prev_state = compositor->base.state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100694 weston_compositor_offscreen(&compositor->base);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200695
696 /* If we have a repaint scheduled (either from a
697 * pending pageflip or the idle handler), make sure we
698 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100699 * vt switched away. The OFFSCREEN state will prevent
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200700 * further attemps at repainting. When we switch
701 * back, we schedule a repaint, which will process
702 * pending frame callbacks. */
703
704 wl_list_for_each(output,
705 &compositor->base.output_list, link) {
706 output->repaint_needed = 0;
707 }
708
709 break;
710 };
711}
712
713static void
714rpi_restore(struct weston_compositor *base)
715{
716 struct rpi_compositor *compositor = to_rpi_compositor(base);
717
718 tty_reset(compositor->tty);
719}
720
721static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400722switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200723{
724 struct rpi_compositor *ec = data;
725
726 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
727}
728
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200729struct rpi_parameters {
730 int tty;
Pekka Paalanene31e0532013-05-22 18:03:07 +0300731 struct rpi_renderer_parameters renderer;
732 uint32_t output_transform;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200733};
734
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200735static struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500736rpi_compositor_create(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400737 struct weston_config *config,
738 struct rpi_parameters *param)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200739{
740 struct rpi_compositor *compositor;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200741 const char *seat = default_seat;
742 uint32_t key;
743
744 weston_log("initializing Raspberry Pi backend\n");
745
746 compositor = calloc(1, sizeof *compositor);
747 if (compositor == NULL)
748 return NULL;
749
750 if (weston_compositor_init(&compositor->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400751 config) < 0)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200752 goto out_free;
753
754 compositor->udev = udev_new();
755 if (compositor->udev == NULL) {
756 weston_log("Failed to initialize udev context.\n");
757 goto out_compositor;
758 }
759
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200760 compositor->tty = tty_create(&compositor->base, vt_func, param->tty);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200761 if (!compositor->tty) {
762 weston_log("Failed to initialize tty.\n");
763 goto out_udev;
764 }
765
766 compositor->base.destroy = rpi_compositor_destroy;
767 compositor->base.restore = rpi_restore;
768
769 compositor->base.focus = 1;
770 compositor->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanene31e0532013-05-22 18:03:07 +0300771 compositor->single_buffer = param->renderer.single_buffer;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200772
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200773 weston_log("Dispmanx planes are %s buffered.\n",
774 compositor->single_buffer ? "single" : "double");
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200775
776 for (key = KEY_F1; key < KEY_F9; key++)
777 weston_compositor_add_key_binding(&compositor->base, key,
778 MODIFIER_CTRL | MODIFIER_ALT,
779 switch_vt_binding, compositor);
780
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200781 /*
782 * bcm_host_init() creates threads.
783 * Therefore we must have all signal handlers set and signals blocked
784 * before calling it. Otherwise the signals may end in the bcm
785 * threads and cause the default behaviour there. For instance,
786 * SIGUSR1 used for VT switching caused Weston to terminate there.
787 */
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200788 bcm_host_init();
789
Pekka Paalanene31e0532013-05-22 18:03:07 +0300790 if (rpi_renderer_create(&compositor->base, &param->renderer) < 0)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200791 goto out_tty;
792
Pekka Paalanene31e0532013-05-22 18:03:07 +0300793 if (rpi_output_create(compositor, param->output_transform) < 0)
794 goto out_renderer;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200795
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200796 evdev_input_create(&compositor->base, compositor->udev, seat);
797
798 return &compositor->base;
799
Pekka Paalanene31e0532013-05-22 18:03:07 +0300800out_renderer:
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +0300801 compositor->base.renderer->destroy(&compositor->base);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200802
803out_tty:
804 tty_destroy(compositor->tty);
805
806out_udev:
807 udev_unref(compositor->udev);
808
809out_compositor:
810 weston_compositor_shutdown(&compositor->base);
811
812out_free:
813 bcm_host_deinit();
814 free(compositor);
815
816 return NULL;
817}
818
819WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500820backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400821 struct weston_config *config)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200822{
Pekka Paalanene31e0532013-05-22 18:03:07 +0300823 const char *transform = "normal";
824 int ret;
825
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200826 struct rpi_parameters param = {
827 .tty = 0, /* default to current tty */
Pekka Paalanene31e0532013-05-22 18:03:07 +0300828 .renderer.single_buffer = 0,
829 .output_transform = WL_OUTPUT_TRANSFORM_NORMAL,
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200830 };
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200831
832 const struct weston_option rpi_options[] = {
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200833 { WESTON_OPTION_INTEGER, "tty", 0, &param.tty },
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200834 { WESTON_OPTION_BOOLEAN, "single-buffer", 0,
Pekka Paalanene31e0532013-05-22 18:03:07 +0300835 &param.renderer.single_buffer },
836 { WESTON_OPTION_STRING, "transform", 0, &transform },
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200837 };
838
839 parse_options(rpi_options, ARRAY_LENGTH(rpi_options), argc, argv);
840
Pekka Paalanene31e0532013-05-22 18:03:07 +0300841 ret = str2transform(transform);
842 if (ret < 0)
843 weston_log("invalid transform \"%s\"\n", transform);
844 else
845 param.output_transform = ret;
846
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400847 return rpi_compositor_create(display, argc, argv, config, &param);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200848}