From 7030a194c84e1fad43ade86674a1e57381d825d1 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Mon, 3 Oct 2011 14:29:01 +0200 Subject: live: Locate the video output device node automatically Iterate through the omap_vout video nodes and use the one with the lowest minor as the video output device. Signed-off-by: Laurent Pinchart --- live.c | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/live.c b/live.c index bd97036..c102893 100644 --- a/live.c +++ b/live.c @@ -21,6 +21,7 @@ */ #define _BSD_SOURCE +#include #include #include #include @@ -36,7 +37,9 @@ #include #include #include +#include #include +#include #include #include @@ -235,6 +238,53 @@ static struct video_out_operations vo_ops = { .unwatch_fd = events_unwatch_fd, }; +static const char *video_out_find(void) +{ + unsigned int video_idx = 256; + static char devname[14]; + struct stat devstat; + struct dirent *ent; + char *end; + DIR *dir; + int ret; + + dir = opendir("/sys/bus/platform/devices/omap_vout/video4linux"); + if (dir == NULL) + return NULL; + + while ((ent = readdir(dir)) != NULL) { + unsigned int idx; + + if (strncmp(ent->d_name, "video", 5)) + continue; + + idx = strtoul(ent->d_name + 5, &end, 10); + if (*end != '\0') + continue; + + if (idx < video_idx) + video_idx = idx; + } + + closedir(dir); + + if (video_idx == 256) + return NULL; + + sprintf(devname, "/dev/video%u", video_idx); + ret = stat(devname, &devstat); + if (ret < 0) + return NULL; + + /* Sanity check: udev might have reordered the device nodes. + * Make sure the major/minor match. We should really use libudev. + */ + if (major(devstat.st_rdev) != 81 || minor(devstat.st_rdev) != video_idx) + return NULL; + + return devname; +} + /* ----------------------------------------------------------------------------- * Frame buffer */ @@ -320,6 +370,7 @@ int main(int argc __attribute__((__unused__)), char *argv[] __attribute__((__unu unsigned int count = 0; struct v4l2_rect rect; int exit_code = EXIT_FAILURE; + const char *vo_devname; float fps; int ret; int c; @@ -373,7 +424,13 @@ int main(int argc __attribute__((__unused__)), char *argv[] __attribute__((__unu view_format.code, view_format.width, view_format.height); /* Initialize video output. */ - vo = vo_init(VIDEOOUT_DEVICE, &vo_ops, rect.width, rect.height); + vo_devname = video_out_find(); + if (vo_devname == NULL) { + printf("error: unable to find video output device\n"); + goto cleanup; + } + + vo = vo_init(vo_devname, &vo_ops, rect.width, rect.height); if (vo == NULL) { printf("error: unable to initialize video output\n"); goto cleanup; -- cgit v1.2.3