Move media-ctl to git://linuxtv.org/v4l-utils.git
[media-ctl.git] / src / options.c
1 /*
2  * Media controller test application
3  *
4  * Copyright (C) 2010-2011 Ideas on board SPRL
5  *
6  * Contact: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published
10  * by the Free Software Foundation; either version 2.1 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this program. If not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include <getopt.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26
27 #include "options.h"
28
29 #define MEDIA_DEVNAME_DEFAULT           "/dev/media0"
30
31 struct media_options media_opts = {
32         .devname = MEDIA_DEVNAME_DEFAULT,
33 };
34
35 static void usage(const char *argv0, int verbose)
36 {
37         printf("%s [options] device\n", argv0);
38         printf("-d, --device dev        Media device name (default: %s)\n", MEDIA_DEVNAME_DEFAULT);
39         printf("-e, --entity name       Print the device name associated with the given entity\n");
40         printf("-V, --set-v4l2 v4l2     Comma-separated list of formats to setup\n");
41         printf("    --get-v4l2 pad      Print the active format on a given pad\n");
42         printf("-h, --help              Show verbose help and exit\n");
43         printf("-i, --interactive       Modify links interactively\n");
44         printf("-l, --links             Comma-separated list of links descriptors to setup\n");
45         printf("-p, --print-topology    Print the device topology\n");
46         printf("    --print-dot         Print the device topology as a dot graph\n");
47         printf("-r, --reset             Reset all links to inactive\n");
48         printf("-v, --verbose           Be verbose\n");
49
50         if (!verbose)
51                 return;
52
53         printf("\n");
54         printf("Links and formats are defined as\n");
55         printf("\tlink            = pad '->' pad '[' flags ']' ;\n");
56         printf("\tpad             = entity ':' pad-number ;\n");
57         printf("\tentity          = entity-number | ( '\"' entity-name '\"' ) ;\n");
58         printf("\n");
59         printf("\tv4l2            = pad '[' v4l2-properties ']' ;\n");
60         printf("\tv4l2-properties = v4l2-property { ',' v4l2-property } ;\n");
61         printf("\tv4l2-property   = v4l2-mbusfmt | v4l2-crop | v4l2-interval\n");
62         printf("\t                | v4l2-compose | v4l2-interval ;\n");
63         printf("\tv4l2-mbusfmt    = 'fmt:' fcc '/' size ;\n");
64         printf("\tv4l2-crop       = 'crop:' rectangle ;\n");
65         printf("\tv4l2-compose    = 'compose:' rectangle ;\n");
66         printf("\tv4l2-interval   = '@' numerator '/' denominator ;\n");
67         printf("\n");
68         printf("\trectangle       = '(' left ',' top, ')' '/' size ;\n");
69         printf("\tsize            = width 'x' height ;\n");
70         printf("\n");
71         printf("where the fields are\n");
72         printf("\tentity-number   Entity numeric identifier\n");
73         printf("\tentity-name     Entity name (string) \n");
74         printf("\tpad-number      Pad numeric identifier\n");
75         printf("\tflags           Link flags (0: inactive, 1: active)\n");
76         printf("\tfcc             Format FourCC\n");
77         printf("\twidth           Image width in pixels\n");
78         printf("\theight          Image height in pixels\n");
79         printf("\tnumerator       Frame interval numerator\n");
80         printf("\tdenominator     Frame interval denominator\n");
81 }
82
83 #define OPT_PRINT_DOT           256
84 #define OPT_GET_FORMAT          257
85
86 static struct option opts[] = {
87         {"device", 1, 0, 'd'},
88         {"entity", 1, 0, 'e'},
89         {"set-format", 1, 0, 'f'},
90         {"set-v4l2", 1, 0, 'V'},
91         {"get-format", 1, 0, OPT_GET_FORMAT},
92         {"get-v4l2", 1, 0, OPT_GET_FORMAT},
93         {"help", 0, 0, 'h'},
94         {"interactive", 0, 0, 'i'},
95         {"links", 1, 0, 'l'},
96         {"print-dot", 0, 0, OPT_PRINT_DOT},
97         {"print-topology", 0, 0, 'p'},
98         {"reset", 0, 0, 'r'},
99         {"verbose", 0, 0, 'v'},
100 };
101
102 int parse_cmdline(int argc, char **argv)
103 {
104         int opt;
105
106         if (argc == 1) {
107                 usage(argv[0], 0);
108                 return 1;
109         }
110
111         /* parse options */
112         while ((opt = getopt_long(argc, argv, "d:e:f:hil:prvV:", opts, NULL)) != -1) {
113                 switch (opt) {
114                 case 'd':
115                         media_opts.devname = optarg;
116                         break;
117
118                 case 'e':
119                         media_opts.entity = optarg;
120                         break;
121
122                 /* 'f' is supported for backward compatibility reasons and will
123                  * be removed later.
124                  */
125                 case 'f':
126                         fprintf(stderr, "Warning: the -f option is deprecated "
127                                 "and has been replaced by -V.\n");
128                 case 'V':
129                         media_opts.formats = optarg;
130                         break;
131
132                 case 'h':
133                         usage(argv[0], 1);
134                         exit(0);
135
136                 case 'i':
137                         media_opts.interactive = 1;
138                         break;
139
140                 case 'l':
141                         media_opts.links = optarg;
142                         break;
143
144                 case 'p':
145                         media_opts.print = 1;
146                         break;
147
148                 case 'r':
149                         media_opts.reset = 1;
150                         break;
151
152                 case 'v':
153                         media_opts.verbose = 1;
154                         break;
155
156                 case OPT_PRINT_DOT:
157                         media_opts.print_dot = 1;
158                         break;
159
160                 case OPT_GET_FORMAT:
161                         media_opts.pad = optarg;
162                         break;
163
164                 default:
165                         printf("Invalid option -%c\n", opt);
166                         printf("Run %s -h for help.\n", argv[0]);
167                         return 1;
168                 }
169         }
170
171         return 0;
172 }
173