New, more flexible syntax for format
[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 <stdlib.h>
23 #include <getopt.h>
24 #include <stdio.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 (implies -v)\n");
46         printf("    --print-dot         Print the device topology as a dot graph (implies -v)\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("\tv4l2-mbusfmt    = 'fmt:' fcc '/' size ;\n");
63         printf("\tv4l2-crop       = 'crop:' '(' left ',' top ')' '/' size ;\n");
64         printf("\tv4l2-interval   = '@' numerator '/' denominator ;\n");
65         printf("\n");
66         printf("\tsize            = width 'x' height ;\n");
67         printf("\n");
68         printf("where the fields are\n");
69         printf("\tentity-number   Entity numeric identifier\n");
70         printf("\tentity-name     Entity name (string) \n");
71         printf("\tpad-number      Pad numeric identifier\n");
72         printf("\tflags           Link flags (0: inactive, 1: active)\n");
73         printf("\tfcc             Format FourCC\n");
74         printf("\twidth           Image width in pixels\n");
75         printf("\theight          Image height in pixels\n");
76         printf("\tnumerator       Frame interval numerator\n");
77         printf("\tdenominator     Frame interval denominator\n");
78 }
79
80 #define OPT_PRINT_DOT           256
81 #define OPT_GET_FORMAT          257
82
83 static struct option opts[] = {
84         {"device", 1, 0, 'd'},
85         {"entity", 1, 0, 'e'},
86         {"set-format", 1, 0, 'f'},
87         {"set-v4l2", 1, 0, 'V'},
88         {"get-format", 1, 0, OPT_GET_FORMAT},
89         {"get-v4l2", 1, 0, OPT_GET_FORMAT},
90         {"help", 0, 0, 'h'},
91         {"interactive", 0, 0, 'i'},
92         {"links", 1, 0, 'l'},
93         {"print-dot", 0, 0, OPT_PRINT_DOT},
94         {"print-topology", 0, 0, 'p'},
95         {"reset", 0, 0, 'r'},
96         {"verbose", 0, 0, 'v'},
97 };
98
99 int parse_cmdline(int argc, char **argv)
100 {
101         int opt;
102
103         if (argc == 1) {
104                 usage(argv[0], 0);
105                 return 1;
106         }
107
108         /* parse options */
109         while ((opt = getopt_long(argc, argv, "d:e:f:hil:prvV:", opts, NULL)) != -1) {
110                 switch (opt) {
111                 case 'd':
112                         media_opts.devname = optarg;
113                         break;
114
115                 case 'e':
116                         media_opts.entity = optarg;
117                         break;
118
119                 /* 'f' is supported for backward compatibility reasons and will
120                  * be removed later.
121                  */
122                 case 'f':
123                         fprintf(stderr, "Warning: the -f option is deprecated "
124                                 "and has been replaced by -V.\n");
125                 case 'V':
126                         media_opts.formats = optarg;
127                         break;
128
129                 case 'h':
130                         usage(argv[0], 1);
131                         exit(0);
132
133                 case 'i':
134                         media_opts.interactive = 1;
135                         break;
136
137                 case 'l':
138                         media_opts.links = optarg;
139                         break;
140
141                 case 'p':
142                         media_opts.print = 1;
143                         media_opts.verbose = 1;
144                         break;
145
146                 case 'r':
147                         media_opts.reset = 1;
148                         break;
149
150                 case 'v':
151                         media_opts.verbose = 1;
152                         break;
153
154                 case OPT_PRINT_DOT:
155                         media_opts.print_dot = 1;
156                         break;
157
158                 case OPT_GET_FORMAT:
159                         media_opts.pad = optarg;
160                         break;
161
162                 default:
163                         printf("Invalid option -%c\n", opt);
164                         printf("Run %s -h for help.\n", argv[0]);
165                         return 1;
166                 }
167         }
168
169         return 0;
170 }
171