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