blob: af62b30ff10841b8f94ef37f6cef525df77d554e [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
Daniel Stonec228e232013-05-22 18:03:19 +030025#include "config.h"
Pekka Paalanene8de35c2012-11-07 12:25:14 +020026
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
Pekka Paalanene8de35c2012-11-07 12:25:14 +020038#ifdef HAVE_BCM_HOST
39# include <bcm_host.h>
40#else
41# include "rpi-bcm-stubs.h"
42#endif
43
44#include "compositor.h"
Pekka Paalanene31e0532013-05-22 18:03:07 +030045#include "rpi-renderer.h"
Pekka Paalanene8de35c2012-11-07 12:25:14 +020046#include "evdev.h"
47
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +020048#if 0
49#define DBG(...) \
50 weston_log(__VA_ARGS__)
51#else
52#define DBG(...) do {} while (0)
53#endif
54
Pekka Paalanene8de35c2012-11-07 12:25:14 +020055struct rpi_compositor;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +020056struct rpi_output;
57
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +020058struct rpi_flippipe {
59 int readfd;
60 int writefd;
61 struct wl_event_source *source;
62};
Pekka Paalanene8de35c2012-11-07 12:25:14 +020063
64struct rpi_output {
65 struct rpi_compositor *compositor;
66 struct weston_output base;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +020067 int single_buffer;
Pekka Paalanene8de35c2012-11-07 12:25:14 +020068
69 struct weston_mode mode;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +020070 struct rpi_flippipe flippipe;
Pekka Paalanene8de35c2012-11-07 12:25:14 +020071
72 DISPMANX_DISPLAY_HANDLE_T display;
Pekka Paalanene8de35c2012-11-07 12:25:14 +020073};
74
75struct rpi_seat {
76 struct weston_seat base;
77 struct wl_list devices_list;
78
79 struct udev_monitor *udev_monitor;
80 struct wl_event_source *udev_monitor_source;
81 char *seat_id;
82};
83
84struct rpi_compositor {
85 struct weston_compositor base;
86 uint32_t prev_state;
87
88 struct udev *udev;
89 struct tty *tty;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +020090
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +020091 int single_buffer;
Pekka Paalanene8de35c2012-11-07 12:25:14 +020092};
93
94static inline struct rpi_output *
95to_rpi_output(struct weston_output *base)
96{
97 return container_of(base, struct rpi_output, base);
98}
99
100static inline struct rpi_seat *
101to_rpi_seat(struct weston_seat *base)
102{
103 return container_of(base, struct rpi_seat, base);
104}
105
106static inline struct rpi_compositor *
107to_rpi_compositor(struct weston_compositor *base)
108{
109 return container_of(base, struct rpi_compositor, base);
110}
111
Jonas Ådahle5a12252013-04-05 23:07:11 +0200112static uint64_t
113rpi_get_current_time(void)
114{
115 struct timeval tv;
116
117 /* XXX: use CLOCK_MONOTONIC instead? */
118 gettimeofday(&tv, NULL);
119 return (uint64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
120}
121
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200122static void
123rpi_flippipe_update_complete(DISPMANX_UPDATE_HANDLE_T update, void *data)
124{
125 /* This function runs in a different thread. */
126 struct rpi_flippipe *flippipe = data;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200127 uint64_t time;
128 ssize_t ret;
129
130 /* manufacture flip completion timestamp */
Jonas Ådahle5a12252013-04-05 23:07:11 +0200131 time = rpi_get_current_time();
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200132
133 ret = write(flippipe->writefd, &time, sizeof time);
134 if (ret != sizeof time)
135 weston_log("ERROR: %s failed to write, ret %zd, errno %d\n",
136 __func__, ret, errno);
137}
138
139static int
140rpi_dispmanx_update_submit(DISPMANX_UPDATE_HANDLE_T update,
141 struct rpi_output *output)
142{
143 /*
144 * The callback registered here will eventually be called
145 * in a different thread context. Therefore we cannot call
146 * the usual functions from rpi_flippipe_update_complete().
147 * Instead, we have a pipe for passing the message from the
148 * thread, waking up the Weston main event loop, calling
149 * rpi_flippipe_handler(), and then ending up in
150 * rpi_output_update_complete() in the main thread context,
151 * where we can do the frame finishing work.
152 */
153 return vc_dispmanx_update_submit(update, rpi_flippipe_update_complete,
154 &output->flippipe);
155}
156
157static void
158rpi_output_update_complete(struct rpi_output *output, uint64_t time);
159
160static int
161rpi_flippipe_handler(int fd, uint32_t mask, void *data)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200162{
163 struct rpi_output *output = data;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200164 ssize_t ret;
165 uint64_t time;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200166
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200167 if (mask != WL_EVENT_READABLE)
168 weston_log("ERROR: unexpected mask 0x%x in %s\n",
169 mask, __func__);
170
171 ret = read(fd, &time, sizeof time);
172 if (ret != sizeof time) {
173 weston_log("ERROR: %s failed to read, ret %zd, errno %d\n",
174 __func__, ret, errno);
175 }
176
177 rpi_output_update_complete(output, time);
178
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200179 return 1;
180}
181
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200182static int
183rpi_flippipe_init(struct rpi_flippipe *flippipe, struct rpi_output *output)
184{
185 struct wl_event_loop *loop;
186 int fd[2];
187
188 if (pipe2(fd, O_CLOEXEC) == -1)
189 return -1;
190
191 flippipe->readfd = fd[0];
192 flippipe->writefd = fd[1];
193
194 loop = wl_display_get_event_loop(output->compositor->base.wl_display);
195 flippipe->source = wl_event_loop_add_fd(loop, flippipe->readfd,
196 WL_EVENT_READABLE,
197 rpi_flippipe_handler, output);
198
199 if (!flippipe->source) {
200 close(flippipe->readfd);
201 close(flippipe->writefd);
202 return -1;
203 }
204
205 return 0;
206}
207
208static void
209rpi_flippipe_release(struct rpi_flippipe *flippipe)
210{
211 wl_event_source_remove(flippipe->source);
212 close(flippipe->readfd);
213 close(flippipe->writefd);
214}
215
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200216static void
Jonas Ådahle5a12252013-04-05 23:07:11 +0200217rpi_output_start_repaint_loop(struct weston_output *output)
218{
219 uint64_t time;
220
221 time = rpi_get_current_time();
222 weston_output_finish_frame(output, time);
223}
224
225static void
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200226rpi_output_repaint(struct weston_output *base, pixman_region32_t *damage)
227{
228 struct rpi_output *output = to_rpi_output(base);
229 struct rpi_compositor *compositor = output->compositor;
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200230 struct weston_plane *primary_plane = &compositor->base.primary_plane;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200231 DISPMANX_UPDATE_HANDLE_T update;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200232
Pekka Paalanene31e0532013-05-22 18:03:07 +0300233 DBG("frame update start\n");
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200234
Pekka Paalanene31e0532013-05-22 18:03:07 +0300235 /* Update priority higher than in rpi-renderer's
236 * output destroy function, see rpi_output_destroy().
237 */
238 update = vc_dispmanx_update_start(1);
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200239
Pekka Paalanene31e0532013-05-22 18:03:07 +0300240 rpi_renderer_set_update_handle(&output->base, update);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200241 compositor->base.renderer->repaint_output(&output->base, damage);
242
Ander Conselvan de Oliveira0a887722012-11-22 15:57:00 +0200243 pixman_region32_subtract(&primary_plane->damage,
244 &primary_plane->damage, damage);
245
Pekka Paalanene31e0532013-05-22 18:03:07 +0300246 /* schedule callback to rpi_output_update_complete() */
247 rpi_dispmanx_update_submit(update, output);
248 DBG("frame update submitted\n");
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200249}
250
251static void
252rpi_output_update_complete(struct rpi_output *output, uint64_t time)
253{
Pekka Paalanene31e0532013-05-22 18:03:07 +0300254 DBG("frame update complete(%" PRIu64 ")\n", time);
Pekka Paalanene31e0532013-05-22 18:03:07 +0300255 rpi_renderer_finish_frame(&output->base);
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200256 weston_output_finish_frame(&output->base, time);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200257}
258
259static void
260rpi_output_destroy(struct weston_output *base)
261{
262 struct rpi_output *output = to_rpi_output(base);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200263
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200264 DBG("%s\n", __func__);
265
Pekka Paalanene31e0532013-05-22 18:03:07 +0300266 rpi_renderer_output_destroy(base);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200267
Pekka Paalanene31e0532013-05-22 18:03:07 +0300268 /* rpi_renderer_output_destroy() will schedule a removal of
269 * all Dispmanx Elements, and wait for the update to complete.
270 * Assuming updates are sequential, the wait should guarantee,
271 * that any pending rpi_flippipe_update_complete() callbacks
272 * have happened already. Therefore we can destroy the flippipe
273 * now.
274 */
275 rpi_flippipe_release(&output->flippipe);
276
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200277 wl_list_remove(&output->base.link);
278 weston_output_destroy(&output->base);
279
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200280 vc_dispmanx_display_close(output->display);
281
282 free(output);
283}
284
Pekka Paalanene31e0532013-05-22 18:03:07 +0300285static const char *transform_names[] = {
286 [WL_OUTPUT_TRANSFORM_NORMAL] = "normal",
287 [WL_OUTPUT_TRANSFORM_90] = "90",
288 [WL_OUTPUT_TRANSFORM_180] = "180",
289 [WL_OUTPUT_TRANSFORM_270] = "270",
290 [WL_OUTPUT_TRANSFORM_FLIPPED] = "flipped",
291 [WL_OUTPUT_TRANSFORM_FLIPPED_90] = "flipped-90",
292 [WL_OUTPUT_TRANSFORM_FLIPPED_180] = "flipped-180",
293 [WL_OUTPUT_TRANSFORM_FLIPPED_270] = "flipped-270",
294};
295
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200296static int
Pekka Paalanene31e0532013-05-22 18:03:07 +0300297str2transform(const char *name)
298{
299 unsigned i;
300
301 for (i = 0; i < ARRAY_LENGTH(transform_names); i++)
302 if (strcmp(name, transform_names[i]) == 0)
303 return i;
304
305 return -1;
306}
307
308static const char *
309transform2str(uint32_t output_transform)
310{
311 if (output_transform >= ARRAY_LENGTH(transform_names))
312 return "<illegal value>";
313
314 return transform_names[output_transform];
315}
316
317static int
318rpi_output_create(struct rpi_compositor *compositor, uint32_t transform)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200319{
320 struct rpi_output *output;
321 DISPMANX_MODEINFO_T modeinfo;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200322 int ret;
323 float mm_width, mm_height;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200324
325 output = calloc(1, sizeof *output);
326 if (!output)
327 return -1;
328
329 output->compositor = compositor;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200330 output->single_buffer = compositor->single_buffer;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200331
332 if (rpi_flippipe_init(&output->flippipe, output) < 0) {
333 weston_log("Creating message pipe failed.\n");
334 goto out_free;
335 }
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200336
337 output->display = vc_dispmanx_display_open(DISPMANX_ID_HDMI);
338 if (!output->display) {
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200339 weston_log("Failed to open dispmanx HDMI display.\n");
340 goto out_pipe;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200341 }
342
343 ret = vc_dispmanx_display_get_info(output->display, &modeinfo);
344 if (ret < 0) {
345 weston_log("Failed to get display mode information.\n");
346 goto out_dmx_close;
347 }
348
Jonas Ådahle5a12252013-04-05 23:07:11 +0200349 output->base.start_repaint_loop = rpi_output_start_repaint_loop;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200350 output->base.repaint = rpi_output_repaint;
351 output->base.destroy = rpi_output_destroy;
Pekka Paalanene31e0532013-05-22 18:03:07 +0300352 output->base.assign_planes = NULL;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200353 output->base.set_backlight = NULL;
354 output->base.set_dpms = NULL;
355 output->base.switch_mode = NULL;
356
357 /* XXX: use tvservice to get information from and control the
358 * HDMI and SDTV outputs. See:
359 * /opt/vc/include/interface/vmcs_host/vc_tvservice.h
360 */
361
362 /* only one static mode in list */
363 output->mode.flags =
364 WL_OUTPUT_MODE_CURRENT | WL_OUTPUT_MODE_PREFERRED;
365 output->mode.width = modeinfo.width;
366 output->mode.height = modeinfo.height;
367 output->mode.refresh = 60000;
368 wl_list_init(&output->base.mode_list);
369 wl_list_insert(&output->base.mode_list, &output->mode.link);
370
371 output->base.current = &output->mode;
372 output->base.origin = &output->mode;
373 output->base.subpixel = WL_OUTPUT_SUBPIXEL_UNKNOWN;
374 output->base.make = "unknown";
375 output->base.model = "unknown";
376
377 /* guess 96 dpi */
378 mm_width = modeinfo.width * (25.4f / 96.0f);
379 mm_height = modeinfo.height * (25.4f / 96.0f);
380
381 weston_output_init(&output->base, &compositor->base,
382 0, 0, round(mm_width), round(mm_height),
Pekka Paalanene31e0532013-05-22 18:03:07 +0300383 transform, 1);
John Kåre Alsaker94659272012-11-13 19:10:18 +0100384
Pekka Paalanene31e0532013-05-22 18:03:07 +0300385 if (rpi_renderer_output_create(&output->base, output->display) < 0)
John Kåre Alsaker94659272012-11-13 19:10:18 +0100386 goto out_output;
387
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200388 wl_list_insert(compositor->base.output_list.prev, &output->base.link);
389
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200390 weston_log("Raspberry Pi HDMI output %dx%d px\n",
391 output->mode.width, output->mode.height);
392 weston_log_continue(STAMP_SPACE "guessing %d Hz and 96 dpi\n",
393 output->mode.refresh / 1000);
Pekka Paalanene31e0532013-05-22 18:03:07 +0300394 weston_log_continue(STAMP_SPACE "orientation: %s\n",
395 transform2str(output->base.transform));
396
397 if (!strncmp(transform2str(output->base.transform), "flipped", 7))
398 weston_log("warning: flipped output transforms may not work\n");
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200399
400 return 0;
401
John Kåre Alsaker94659272012-11-13 19:10:18 +0100402out_output:
403 weston_output_destroy(&output->base);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200404
405out_dmx_close:
406 vc_dispmanx_display_close(output->display);
407
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200408out_pipe:
409 rpi_flippipe_release(&output->flippipe);
410
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200411out_free:
412 free(output);
413 return -1;
414}
415
416static void
417rpi_led_update(struct weston_seat *seat_base, enum weston_led leds)
418{
419 struct rpi_seat *seat = to_rpi_seat(seat_base);
420 struct evdev_device *device;
421
422 wl_list_for_each(device, &seat->devices_list, link)
423 evdev_led_update(device, leds);
424}
425
426static const char default_seat[] = "seat0";
427
428static void
429device_added(struct udev_device *udev_device, struct rpi_seat *master)
430{
431 struct evdev_device *device;
432 const char *devnode;
433 const char *device_seat;
434 int fd;
435
436 device_seat = udev_device_get_property_value(udev_device, "ID_SEAT");
437 if (!device_seat)
438 device_seat = default_seat;
439
440 if (strcmp(device_seat, master->seat_id))
441 return;
442
443 devnode = udev_device_get_devnode(udev_device);
444
445 /* Use non-blocking mode so that we can loop on read on
446 * evdev_device_data() until all events on the fd are
447 * read. mtdev_get() also expects this. */
448 fd = open(devnode, O_RDWR | O_NONBLOCK | O_CLOEXEC);
449 if (fd < 0) {
450 weston_log("opening input device '%s' failed.\n", devnode);
451 return;
452 }
453
454 device = evdev_device_create(&master->base, devnode, fd);
455 if (!device) {
456 close(fd);
457 weston_log("not using input device '%s'.\n", devnode);
458 return;
459 }
460
461 wl_list_insert(master->devices_list.prev, &device->link);
462}
463
464static void
465evdev_add_devices(struct udev *udev, struct weston_seat *seat_base)
466{
467 struct rpi_seat *seat = to_rpi_seat(seat_base);
468 struct udev_enumerate *e;
469 struct udev_list_entry *entry;
470 struct udev_device *device;
471 const char *path, *sysname;
472
473 e = udev_enumerate_new(udev);
474 udev_enumerate_add_match_subsystem(e, "input");
475 udev_enumerate_scan_devices(e);
476 udev_list_entry_foreach(entry, udev_enumerate_get_list_entry(e)) {
477 path = udev_list_entry_get_name(entry);
478 device = udev_device_new_from_syspath(udev, path);
479
480 sysname = udev_device_get_sysname(device);
481 if (strncmp("event", sysname, 5) != 0) {
482 udev_device_unref(device);
483 continue;
484 }
485
486 device_added(device, seat);
487
488 udev_device_unref(device);
489 }
490 udev_enumerate_unref(e);
491
492 evdev_notify_keyboard_focus(&seat->base, &seat->devices_list);
493
494 if (wl_list_empty(&seat->devices_list)) {
495 weston_log(
496 "warning: no input devices on entering Weston. "
497 "Possible causes:\n"
498 "\t- no permissions to read /dev/input/event*\n"
499 "\t- seats misconfigured "
500 "(Weston backend option 'seat', "
501 "udev device property ID_SEAT)\n");
502 }
503}
504
505static int
506evdev_udev_handler(int fd, uint32_t mask, void *data)
507{
508 struct rpi_seat *seat = data;
509 struct udev_device *udev_device;
510 struct evdev_device *device, *next;
511 const char *action;
512 const char *devnode;
513
514 udev_device = udev_monitor_receive_device(seat->udev_monitor);
515 if (!udev_device)
516 return 1;
517
518 action = udev_device_get_action(udev_device);
519 if (!action)
520 goto out;
521
522 if (strncmp("event", udev_device_get_sysname(udev_device), 5) != 0)
523 goto out;
524
525 if (!strcmp(action, "add")) {
526 device_added(udev_device, seat);
527 }
528 else if (!strcmp(action, "remove")) {
529 devnode = udev_device_get_devnode(udev_device);
530 wl_list_for_each_safe(device, next, &seat->devices_list, link)
531 if (!strcmp(device->devnode, devnode)) {
532 weston_log("input device %s, %s removed\n",
533 device->devname, device->devnode);
534 evdev_device_destroy(device);
535 break;
536 }
537 }
538
539out:
540 udev_device_unref(udev_device);
541
542 return 0;
543}
544
545static int
546evdev_enable_udev_monitor(struct udev *udev, struct weston_seat *seat_base)
547{
548 struct rpi_seat *master = to_rpi_seat(seat_base);
549 struct wl_event_loop *loop;
550 struct weston_compositor *c = master->base.compositor;
551 int fd;
552
553 master->udev_monitor = udev_monitor_new_from_netlink(udev, "udev");
554 if (!master->udev_monitor) {
555 weston_log("udev: failed to create the udev monitor\n");
556 return 0;
557 }
558
559 udev_monitor_filter_add_match_subsystem_devtype(master->udev_monitor,
560 "input", NULL);
561
562 if (udev_monitor_enable_receiving(master->udev_monitor)) {
563 weston_log("udev: failed to bind the udev monitor\n");
564 udev_monitor_unref(master->udev_monitor);
565 return 0;
566 }
567
568 loop = wl_display_get_event_loop(c->wl_display);
569 fd = udev_monitor_get_fd(master->udev_monitor);
570 master->udev_monitor_source =
571 wl_event_loop_add_fd(loop, fd, WL_EVENT_READABLE,
572 evdev_udev_handler, master);
573 if (!master->udev_monitor_source) {
574 udev_monitor_unref(master->udev_monitor);
575 return 0;
576 }
577
578 return 1;
579}
580
581static void
582evdev_disable_udev_monitor(struct weston_seat *seat_base)
583{
584 struct rpi_seat *seat = to_rpi_seat(seat_base);
585
586 if (!seat->udev_monitor)
587 return;
588
589 udev_monitor_unref(seat->udev_monitor);
590 seat->udev_monitor = NULL;
591 wl_event_source_remove(seat->udev_monitor_source);
592 seat->udev_monitor_source = NULL;
593}
594
595static void
596evdev_input_create(struct weston_compositor *c, struct udev *udev,
597 const char *seat_id)
598{
599 struct rpi_seat *seat;
600
601 seat = malloc(sizeof *seat);
602 if (seat == NULL)
603 return;
604
605 memset(seat, 0, sizeof *seat);
Rob Bradford9af5f9e2013-05-31 18:09:50 +0100606 weston_seat_init(&seat->base, c, "default");
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200607 seat->base.led_update = rpi_led_update;
608
609 wl_list_init(&seat->devices_list);
610 seat->seat_id = strdup(seat_id);
611 if (!evdev_enable_udev_monitor(udev, &seat->base)) {
612 free(seat->seat_id);
613 free(seat);
614 return;
615 }
616
617 evdev_add_devices(udev, &seat->base);
618}
619
620static void
621evdev_remove_devices(struct weston_seat *seat_base)
622{
623 struct rpi_seat *seat = to_rpi_seat(seat_base);
624 struct evdev_device *device, *next;
625
626 wl_list_for_each_safe(device, next, &seat->devices_list, link)
627 evdev_device_destroy(device);
628
Kristian Høgsberge3148752013-05-06 23:19:49 -0400629 if (seat->base.keyboard)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200630 notify_keyboard_focus_out(&seat->base);
631}
632
633static void
634evdev_input_destroy(struct weston_seat *seat_base)
635{
636 struct rpi_seat *seat = to_rpi_seat(seat_base);
637
638 evdev_remove_devices(seat_base);
639 evdev_disable_udev_monitor(&seat->base);
640
641 weston_seat_release(seat_base);
642 free(seat->seat_id);
643 free(seat);
644}
645
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200646static void
647rpi_compositor_destroy(struct weston_compositor *base)
648{
649 struct rpi_compositor *compositor = to_rpi_compositor(base);
650 struct weston_seat *seat, *next;
651
652 wl_list_for_each_safe(seat, next, &compositor->base.seat_list, link)
653 evdev_input_destroy(seat);
654
655 /* destroys outputs, too */
656 weston_compositor_shutdown(&compositor->base);
657
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +0300658 compositor->base.renderer->destroy(&compositor->base);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200659 tty_destroy(compositor->tty);
660
661 bcm_host_deinit();
662 free(compositor);
663}
664
665static void
666vt_func(struct weston_compositor *base, int event)
667{
668 struct rpi_compositor *compositor = to_rpi_compositor(base);
669 struct weston_seat *seat;
670 struct weston_output *output;
671
672 switch (event) {
673 case TTY_ENTER_VT:
674 weston_log("entering VT\n");
675 compositor->base.focus = 1;
676 compositor->base.state = compositor->prev_state;
677 weston_compositor_damage_all(&compositor->base);
678 wl_list_for_each(seat, &compositor->base.seat_list, link) {
679 evdev_add_devices(compositor->udev, seat);
680 evdev_enable_udev_monitor(compositor->udev, seat);
681 }
682 break;
683 case TTY_LEAVE_VT:
684 weston_log("leaving VT\n");
685 wl_list_for_each(seat, &compositor->base.seat_list, link) {
686 evdev_disable_udev_monitor(seat);
687 evdev_remove_devices(seat);
688 }
689
690 compositor->base.focus = 0;
691 compositor->prev_state = compositor->base.state;
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100692 weston_compositor_offscreen(&compositor->base);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200693
694 /* If we have a repaint scheduled (either from a
695 * pending pageflip or the idle handler), make sure we
696 * cancel that so we don't try to pageflip when we're
Philipp Brüschweiler57edf7f2013-03-29 13:01:56 +0100697 * vt switched away. The OFFSCREEN state will prevent
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200698 * further attemps at repainting. When we switch
699 * back, we schedule a repaint, which will process
700 * pending frame callbacks. */
701
702 wl_list_for_each(output,
703 &compositor->base.output_list, link) {
704 output->repaint_needed = 0;
705 }
706
707 break;
708 };
709}
710
711static void
712rpi_restore(struct weston_compositor *base)
713{
714 struct rpi_compositor *compositor = to_rpi_compositor(base);
715
716 tty_reset(compositor->tty);
717}
718
719static void
Kristian Høgsberge3148752013-05-06 23:19:49 -0400720switch_vt_binding(struct weston_seat *seat, uint32_t time, uint32_t key, void *data)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200721{
722 struct rpi_compositor *ec = data;
723
724 tty_activate_vt(ec->tty, key - KEY_F1 + 1);
725}
726
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200727struct rpi_parameters {
728 int tty;
Pekka Paalanene31e0532013-05-22 18:03:07 +0300729 struct rpi_renderer_parameters renderer;
730 uint32_t output_transform;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200731};
732
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200733static struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500734rpi_compositor_create(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400735 struct weston_config *config,
736 struct rpi_parameters *param)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200737{
738 struct rpi_compositor *compositor;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200739 const char *seat = default_seat;
740 uint32_t key;
741
742 weston_log("initializing Raspberry Pi backend\n");
743
744 compositor = calloc(1, sizeof *compositor);
745 if (compositor == NULL)
746 return NULL;
747
748 if (weston_compositor_init(&compositor->base, display, argc, argv,
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400749 config) < 0)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200750 goto out_free;
751
752 compositor->udev = udev_new();
753 if (compositor->udev == NULL) {
754 weston_log("Failed to initialize udev context.\n");
755 goto out_compositor;
756 }
757
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200758 compositor->tty = tty_create(&compositor->base, vt_func, param->tty);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200759 if (!compositor->tty) {
760 weston_log("Failed to initialize tty.\n");
761 goto out_udev;
762 }
763
764 compositor->base.destroy = rpi_compositor_destroy;
765 compositor->base.restore = rpi_restore;
766
767 compositor->base.focus = 1;
768 compositor->prev_state = WESTON_COMPOSITOR_ACTIVE;
Pekka Paalanene31e0532013-05-22 18:03:07 +0300769 compositor->single_buffer = param->renderer.single_buffer;
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200770
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200771 weston_log("Dispmanx planes are %s buffered.\n",
772 compositor->single_buffer ? "single" : "double");
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200773
774 for (key = KEY_F1; key < KEY_F9; key++)
775 weston_compositor_add_key_binding(&compositor->base, key,
776 MODIFIER_CTRL | MODIFIER_ALT,
777 switch_vt_binding, compositor);
778
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200779 /*
780 * bcm_host_init() creates threads.
781 * Therefore we must have all signal handlers set and signals blocked
782 * before calling it. Otherwise the signals may end in the bcm
783 * threads and cause the default behaviour there. For instance,
784 * SIGUSR1 used for VT switching caused Weston to terminate there.
785 */
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200786 bcm_host_init();
787
Pekka Paalanene31e0532013-05-22 18:03:07 +0300788 if (rpi_renderer_create(&compositor->base, &param->renderer) < 0)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200789 goto out_tty;
790
Pekka Paalanene31e0532013-05-22 18:03:07 +0300791 if (rpi_output_create(compositor, param->output_transform) < 0)
792 goto out_renderer;
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200793
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200794 evdev_input_create(&compositor->base, compositor->udev, seat);
795
796 return &compositor->base;
797
Pekka Paalanene31e0532013-05-22 18:03:07 +0300798out_renderer:
Vasily Khoruzhick52cfd612013-01-08 19:09:01 +0300799 compositor->base.renderer->destroy(&compositor->base);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200800
801out_tty:
802 tty_destroy(compositor->tty);
803
804out_udev:
805 udev_unref(compositor->udev);
806
807out_compositor:
808 weston_compositor_shutdown(&compositor->base);
809
810out_free:
811 bcm_host_deinit();
812 free(compositor);
813
814 return NULL;
815}
816
817WL_EXPORT struct weston_compositor *
Kristian Høgsberg4172f662013-02-20 15:27:49 -0500818backend_init(struct wl_display *display, int *argc, char *argv[],
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400819 struct weston_config *config)
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200820{
Pekka Paalanene31e0532013-05-22 18:03:07 +0300821 const char *transform = "normal";
822 int ret;
823
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200824 struct rpi_parameters param = {
825 .tty = 0, /* default to current tty */
Pekka Paalanene31e0532013-05-22 18:03:07 +0300826 .renderer.single_buffer = 0,
827 .output_transform = WL_OUTPUT_TRANSFORM_NORMAL,
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200828 };
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200829
830 const struct weston_option rpi_options[] = {
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200831 { WESTON_OPTION_INTEGER, "tty", 0, &param.tty },
Pekka Paalanen7fb46fb2012-11-07 12:25:15 +0200832 { WESTON_OPTION_BOOLEAN, "single-buffer", 0,
Pekka Paalanene31e0532013-05-22 18:03:07 +0300833 &param.renderer.single_buffer },
834 { WESTON_OPTION_STRING, "transform", 0, &transform },
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200835 };
836
837 parse_options(rpi_options, ARRAY_LENGTH(rpi_options), argc, argv);
838
Pekka Paalanene31e0532013-05-22 18:03:07 +0300839 ret = str2transform(transform);
840 if (ret < 0)
841 weston_log("invalid transform \"%s\"\n", transform);
842 else
843 param.output_transform = ret;
844
Kristian Høgsberg14e438c2013-05-26 21:48:14 -0400845 return rpi_compositor_create(display, argc, argv, config, &param);
Pekka Paalanene8de35c2012-11-07 12:25:14 +0200846}