Debian Codegraph @

src/udev/v4l_id/v4l_id.c

raw
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Copyright (c) 2009 Filippo Argiolas <[email protected]>
4 */
5
6 #include <fcntl.h>
7 #include <linux/videodev2.h>
8 #include <stdio.h>
9 #include <sys/ioctl.h>
10
11 #include "build.h"
12 #include "errno-util.h"
13 #include "fd-util.h"
14 #include "format-table.h"
15 #include "help-util.h"
16 #include "log.h"
17 #include "main-func.h"
18 #include "options.h"
19 #include "string-util.h"
20 #include "strv.h"
21 #include "utf8.h"
22
23 static const char *arg_device = NULL;
24
25 static int help(void) {
26 _cleanup_(table_unrefp) Table *options = NULL;
27 int r;
28
29 r = option_parser_get_help_table(&options);
30 if (r < 0)
31 return r;
32
33 help_cmdline("[OPTIONS...] DEVICE");
34 help_abstract("Video4Linux device identification.");
35 help_section("Options");
36
37 return table_print_or_warn(options);
38 }
39
40 static int parse_argv(int argc, char *argv[]) {
41 assert(argc >= 0);
42 assert(argv);
43
44 OptionParser opts = { argc, argv };
45
46 FOREACH_OPTION_OR_RETURN(c, &opts)
47 switch (c) {
48
49 OPTION_COMMON_HELP:
50 return help();
51
52 OPTION_COMMON_VERSION:
53 return version();
54 }
55
56 char **args = option_parser_get_args(&opts);
57 if (strv_length(args) != 1)
58 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
59 "Need exactly one DEVICE argument.");
60
61 arg_device = args[0];
62 return 1;
63 }
64
65 static int run(int argc, char *argv[]) {
66 _cleanup_close_ int fd = -EBADF;
67 struct v4l2_capability v2cap;
68 int r;
69
70 r = parse_argv(argc, argv);
71 if (r <= 0)
72 return r;
73
74 fd = open(arg_device, O_RDONLY|O_CLOEXEC|O_NOCTTY);
75 if (fd < 0) {
76 bool ignore = ERRNO_IS_DEVICE_ABSENT_OR_EMPTY(errno);
77 log_full_errno(ignore ? LOG_DEBUG : LOG_WARNING, errno,
78 "Failed to open device node '%s'%s: %m",
79 arg_device, ignore ? ", ignoring" : "");
80 return ignore ? 0 : -errno;
81 }
82
83 if (ioctl(fd, VIDIOC_QUERYCAP, &v2cap) == 0) {
84 int capabilities;
85
86 printf("ID_V4L_VERSION=2\n");
87 if (utf8_is_valid((char *)v2cap.card) && !string_has_cc((char *)v2cap.card, /* ok= */ NULL))
88 printf("ID_V4L_PRODUCT=%s\n", v2cap.card);
89 printf("ID_V4L_CAPABILITIES=:");
90
91 if (v2cap.capabilities & V4L2_CAP_DEVICE_CAPS)
92 capabilities = v2cap.device_caps;
93 else
94 capabilities = v2cap.capabilities;
95
96 if ((capabilities & V4L2_CAP_VIDEO_CAPTURE) > 0 ||
97 (capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE) > 0)
98 printf("capture:");
99 if ((capabilities & V4L2_CAP_VIDEO_OUTPUT) > 0 ||
100 (capabilities & V4L2_CAP_VIDEO_OUTPUT_MPLANE) > 0)
101 printf("video_output:");
102 if ((capabilities & V4L2_CAP_VIDEO_OVERLAY) > 0)
103 printf("video_overlay:");
104 if ((capabilities & V4L2_CAP_AUDIO) > 0)
105 printf("audio:");
106 if ((capabilities & V4L2_CAP_TUNER) > 0)
107 printf("tuner:");
108 if ((capabilities & V4L2_CAP_RADIO) > 0)
109 printf("radio:");
110 printf("\n");
111 }
112
113 return 0;
114 }
115
116 DEFINE_MAIN_FUNCTION(run);