rpi: remove weston_plane support
[platform/upstream/weston.git] / src / compositor-rpi.c
1 /*
2  * Copyright © 2008-2011 Kristian Høgsberg
3  * Copyright © 2011 Intel Corporation
4  * Copyright © 2012-2013 Raspberry Pi Foundation
5  *
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"
47 #include "rpi-renderer.h"
48 #include "evdev.h"
49
50 #if 0
51 #define DBG(...) \
52         weston_log(__VA_ARGS__)
53 #else
54 #define DBG(...) do {} while (0)
55 #endif
56
57 struct rpi_compositor;
58 struct rpi_output;
59
60 struct rpi_flippipe {
61         int readfd;
62         int writefd;
63         struct wl_event_source *source;
64 };
65
66 struct rpi_output {
67         struct rpi_compositor *compositor;
68         struct weston_output base;
69         int single_buffer;
70
71         struct weston_mode mode;
72         struct rpi_flippipe flippipe;
73
74         DISPMANX_DISPLAY_HANDLE_T display;
75 };
76
77 struct 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
86 struct rpi_compositor {
87         struct weston_compositor base;
88         uint32_t prev_state;
89
90         struct udev *udev;
91         struct tty *tty;
92
93         int single_buffer;
94 };
95
96 static inline struct rpi_output *
97 to_rpi_output(struct weston_output *base)
98 {
99         return container_of(base, struct rpi_output, base);
100 }
101
102 static inline struct rpi_seat *
103 to_rpi_seat(struct weston_seat *base)
104 {
105         return container_of(base, struct rpi_seat, base);
106 }
107
108 static inline struct rpi_compositor *
109 to_rpi_compositor(struct weston_compositor *base)
110 {
111         return container_of(base, struct rpi_compositor, base);
112 }
113
114 static uint64_t
115 rpi_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
124 static void
125 rpi_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;
129         uint64_t time;
130         ssize_t ret;
131
132         /* manufacture flip completion timestamp */
133         time = rpi_get_current_time();
134
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
141 static int
142 rpi_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
159 static void
160 rpi_output_update_complete(struct rpi_output *output, uint64_t time);
161
162 static int
163 rpi_flippipe_handler(int fd, uint32_t mask, void *data)
164 {
165         struct rpi_output *output = data;
166         ssize_t ret;
167         uint64_t time;
168
169         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
181         return 1;
182 }
183
184 static int
185 rpi_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
210 static void
211 rpi_flippipe_release(struct rpi_flippipe *flippipe)
212 {
213         wl_event_source_remove(flippipe->source);
214         close(flippipe->readfd);
215         close(flippipe->writefd);
216 }
217
218 static void
219 rpi_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
227 static void
228 rpi_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;
232         struct weston_plane *primary_plane = &compositor->base.primary_plane;
233         DISPMANX_UPDATE_HANDLE_T update;
234
235         DBG("frame update start\n");
236
237         /* Update priority higher than in rpi-renderer's
238          * output destroy function, see rpi_output_destroy().
239          */
240         update = vc_dispmanx_update_start(1);
241
242         rpi_renderer_set_update_handle(&output->base, update);
243         compositor->base.renderer->repaint_output(&output->base, damage);
244
245         pixman_region32_subtract(&primary_plane->damage,
246                                  &primary_plane->damage, damage);
247
248         /* schedule callback to rpi_output_update_complete() */
249         rpi_dispmanx_update_submit(update, output);
250         DBG("frame update submitted\n");
251 }
252
253 static void
254 rpi_output_update_complete(struct rpi_output *output, uint64_t time)
255 {
256         DBG("frame update complete(%" PRIu64 ")\n", time);
257         rpi_renderer_finish_frame(&output->base);
258         weston_output_finish_frame(&output->base, time);
259 }
260
261 static void
262 rpi_output_destroy(struct weston_output *base)
263 {
264         struct rpi_output *output = to_rpi_output(base);
265
266         DBG("%s\n", __func__);
267
268         rpi_renderer_output_destroy(base);
269
270         /* 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
279         wl_list_remove(&output->base.link);
280         weston_output_destroy(&output->base);
281
282         vc_dispmanx_display_close(output->display);
283
284         free(output);
285 }
286
287 static 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
298 static int
299 str2transform(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
310 static const char *
311 transform2str(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
319 static int
320 rpi_output_create(struct rpi_compositor *compositor, uint32_t transform)
321 {
322         struct rpi_output *output;
323         DISPMANX_MODEINFO_T modeinfo;
324         int ret;
325         float mm_width, mm_height;
326
327         output = calloc(1, sizeof *output);
328         if (!output)
329                 return -1;
330
331         output->compositor = compositor;
332         output->single_buffer = compositor->single_buffer;
333
334         if (rpi_flippipe_init(&output->flippipe, output) < 0) {
335                 weston_log("Creating message pipe failed.\n");
336                 goto out_free;
337         }
338
339         output->display = vc_dispmanx_display_open(DISPMANX_ID_HDMI);
340         if (!output->display) {
341                 weston_log("Failed to open dispmanx HDMI display.\n");
342                 goto out_pipe;
343         }
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
351         output->base.start_repaint_loop = rpi_output_start_repaint_loop;
352         output->base.repaint = rpi_output_repaint;
353         output->base.destroy = rpi_output_destroy;
354         output->base.assign_planes = NULL;
355         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),
385                            transform, 1);
386
387         if (rpi_renderer_output_create(&output->base, output->display) < 0)
388                 goto out_output;
389
390         wl_list_insert(compositor->base.output_list.prev, &output->base.link);
391
392         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);
396         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");
401
402         return 0;
403
404 out_output:
405         weston_output_destroy(&output->base);
406
407 out_dmx_close:
408         vc_dispmanx_display_close(output->display);
409
410 out_pipe:
411         rpi_flippipe_release(&output->flippipe);
412
413 out_free:
414         free(output);
415         return -1;
416 }
417
418 static void
419 rpi_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
428 static const char default_seat[] = "seat0";
429
430 static void
431 device_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
466 static void
467 evdev_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
507 static int
508 evdev_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
541 out:
542         udev_device_unref(udev_device);
543
544         return 0;
545 }
546
547 static int
548 evdev_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
583 static void
584 evdev_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
597 static void
598 evdev_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
622 static void
623 evdev_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
631         if (seat->base.keyboard)
632                 notify_keyboard_focus_out(&seat->base);
633 }
634
635 static void
636 evdev_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
648 static void
649 rpi_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
660         compositor->base.renderer->destroy(&compositor->base);
661         tty_destroy(compositor->tty);
662
663         bcm_host_deinit();
664         free(compositor);
665 }
666
667 static void
668 vt_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;
694                 weston_compositor_offscreen(&compositor->base);
695
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
699                  * vt switched away.  The OFFSCREEN state will prevent
700                  * 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
713 static void
714 rpi_restore(struct weston_compositor *base)
715 {
716         struct rpi_compositor *compositor = to_rpi_compositor(base);
717
718         tty_reset(compositor->tty);
719 }
720
721 static void
722 switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
723 {
724         struct rpi_compositor *ec = data;
725
726         tty_activate_vt(ec->tty, key - KEY_F1 + 1);
727 }
728
729 struct rpi_parameters {
730         int tty;
731         struct rpi_renderer_parameters renderer;
732         uint32_t output_transform;
733 };
734
735 static struct weston_compositor *
736 rpi_compositor_create(struct wl_display *display, int *argc, char *argv[],
737                       int config_fd, struct rpi_parameters *param)
738 {
739         struct rpi_compositor *compositor;
740         const char *seat = default_seat;
741         uint32_t key;
742
743         weston_log("initializing Raspberry Pi backend\n");
744
745         compositor = calloc(1, sizeof *compositor);
746         if (compositor == NULL)
747                 return NULL;
748
749         if (weston_compositor_init(&compositor->base, display, argc, argv,
750                                    config_fd) < 0)
751                 goto out_free;
752
753         compositor->udev = udev_new();
754         if (compositor->udev == NULL) {
755                 weston_log("Failed to initialize udev context.\n");
756                 goto out_compositor;
757         }
758
759         compositor->tty = tty_create(&compositor->base, vt_func, param->tty);
760         if (!compositor->tty) {
761                 weston_log("Failed to initialize tty.\n");
762                 goto out_udev;
763         }
764
765         compositor->base.destroy = rpi_compositor_destroy;
766         compositor->base.restore = rpi_restore;
767
768         compositor->base.focus = 1;
769         compositor->prev_state = WESTON_COMPOSITOR_ACTIVE;
770         compositor->single_buffer = param->renderer.single_buffer;
771
772         weston_log("Dispmanx planes are %s buffered.\n",
773                    compositor->single_buffer ? "single" : "double");
774
775         for (key = KEY_F1; key < KEY_F9; key++)
776                 weston_compositor_add_key_binding(&compositor->base, key,
777                                                   MODIFIER_CTRL | MODIFIER_ALT,
778                                                   switch_vt_binding, compositor);
779
780         /*
781          * bcm_host_init() creates threads.
782          * Therefore we must have all signal handlers set and signals blocked
783          * before calling it. Otherwise the signals may end in the bcm
784          * threads and cause the default behaviour there. For instance,
785          * SIGUSR1 used for VT switching caused Weston to terminate there.
786          */
787         bcm_host_init();
788
789         if (rpi_renderer_create(&compositor->base, &param->renderer) < 0)
790                 goto out_tty;
791
792         if (rpi_output_create(compositor, param->output_transform) < 0)
793                 goto out_renderer;
794
795         evdev_input_create(&compositor->base, compositor->udev, seat);
796
797         return &compositor->base;
798
799 out_renderer:
800         compositor->base.renderer->destroy(&compositor->base);
801
802 out_tty:
803         tty_destroy(compositor->tty);
804
805 out_udev:
806         udev_unref(compositor->udev);
807
808 out_compositor:
809         weston_compositor_shutdown(&compositor->base);
810
811 out_free:
812         bcm_host_deinit();
813         free(compositor);
814
815         return NULL;
816 }
817
818 WL_EXPORT struct weston_compositor *
819 backend_init(struct wl_display *display, int *argc, char *argv[],
820              int config_fd)
821 {
822         const char *transform = "normal";
823         int ret;
824
825         struct rpi_parameters param = {
826                 .tty = 0, /* default to current tty */
827                 .renderer.single_buffer = 0,
828                 .output_transform = WL_OUTPUT_TRANSFORM_NORMAL,
829         };
830
831         const struct weston_option rpi_options[] = {
832                 { WESTON_OPTION_INTEGER, "tty", 0, &param.tty },
833                 { WESTON_OPTION_BOOLEAN, "single-buffer", 0,
834                   &param.renderer.single_buffer },
835                 { WESTON_OPTION_STRING, "transform", 0, &transform },
836         };
837
838         parse_options(rpi_options, ARRAY_LENGTH(rpi_options), argc, argv);
839
840         ret = str2transform(transform);
841         if (ret < 0)
842                 weston_log("invalid transform \"%s\"\n", transform);
843         else
844                 param.output_transform = ret;
845
846         return rpi_compositor_create(display, argc, argv, config_fd, &param);
847 }