Dali 3D User Interface Engine
command-line-options.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17 
18 // CLASS HEADER
19 #include "command-line-options.h"
20 
21 // EXTERNAL INCLUDES
22 #include <getopt.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <iostream>
26 
28 
29 namespace Dali
30 {
31 
32 namespace Internal
33 {
34 
35 namespace Adaptor
36 {
37 
38 namespace
39 {
40 struct Argument
41 {
42  const char * const opt;
43  const char * const optDescription;
44 
45  void Print()
46  {
47  const std::ios_base::fmtflags flags = std::cout.flags();
48  std::cout << std::left << " --";
49  std::cout.width( 18 );
50  std::cout << opt;
51  std::cout << optDescription;
52  std::cout << std::endl;
53  std::cout.flags( flags );
54  }
55 };
56 
58 {
59  { "no-vsync", "Disable VSync on Render" },
60  { "width", "Stage Width" },
61  { "height", "Stage Height" },
62  { "dpi", "Emulated DPI" },
63  { "view", "Stereocopic 3D view mode ([0]=MONO, 1=STEREO_HORZ, 2=STEREO_VERT, 3=STEREO_INTERLACED)" },
64  { "stereo-base", "Distance in millimeters between left/right cameras [65.0]" },
65  { "help", "Help" },
66  { NULL, NULL }
67 };
68 
69 enum Option
70 {
78 };
79 
81 
82 void ShowHelp()
83 {
84  std::cout << "Available options:" << std::endl;
85  Argument* arg = EXPECTED_ARGS;
86  while ( arg->opt )
87  {
88  arg->Print();
89  ++arg;
90  }
91 }
92 
93 } // unnamed namespace
94 
95 CommandLineOptions::CommandLineOptions(int *argc, char **argv[])
96 : noVSyncOnRender(0),
97  stageWidth(0), stageHeight(0),
98  viewMode(0),
99  stereoBase(65)
100 {
101  // Exit gracefully if no arguments provided
102  if ( !argc || !argv )
103  {
104  return;
105  }
106 
107  if ( *argc > 1 )
108  {
109  // We do not want to print out errors.
110  int origOptErrValue( opterr );
111  opterr = 0;
112 
113  int help( 0 );
114 
115  const struct option options[]=
116  {
117  { EXPECTED_ARGS[OPTION_NO_VSYNC].opt, no_argument, &noVSyncOnRender, 1 }, // "--no-vsync"
118  { EXPECTED_ARGS[OPTION_STAGE_WIDTH].opt, required_argument, NULL, 'w' }, // "--width"
119  { EXPECTED_ARGS[OPTION_STAGE_HEIGHT].opt, required_argument, NULL, 'h' }, // "--height"
120  { EXPECTED_ARGS[OPTION_DPI].opt, required_argument, NULL, 'd' }, // "--dpi"
121  { EXPECTED_ARGS[OPTION_STEREO_MODE].opt, required_argument, NULL, 'v' }, // "--view"
122  { EXPECTED_ARGS[OPTION_STEREO_BASE].opt, required_argument, NULL, 's' }, // "--stereo-base"
123  { EXPECTED_ARGS[OPTION_HELP].opt, no_argument, &help, '?' }, // "--help"
124  { 0, 0, 0, 0 } // end of options
125  };
126 
127  int shortOption( 0 );
128  int optionIndex( 0 );
129 
130  const char* optString = "-w:h:d:v:s:"; // The '-' ensures that argv is NOT permuted
131  bool optionProcessed( false );
132 
133  UnhandledContainer unhandledOptions; // We store indices of options we do not handle here
134 
135  do
136  {
137  shortOption = getopt_long( *argc, *argv, optString, options, &optionIndex );
138 
139  switch ( shortOption )
140  {
141  case 0:
142  {
143  // Check if we want help
144  if ( help )
145  {
146  ShowHelp();
147  optionProcessed = true;
148  }
149  break;
150  }
151 
152  case 'w':
153  {
154  if ( optarg )
155  {
156  stageWidth = atoi( optarg );
157  optionProcessed = true;
158  }
159  break;
160  }
161 
162  case 'h':
163  {
164  if ( optarg )
165  {
166  stageHeight = atoi( optarg );
167  optionProcessed = true;
168  }
169  break;
170  }
171 
172  case 'd':
173  {
174  if ( optarg )
175  {
176  stageDPI.assign( optarg );
177  optionProcessed = true;
178  }
179  break;
180  }
181 
182  case 'v':
183  {
184  if ( optarg )
185  {
186  viewMode = atoi(optarg);
187  optionProcessed = true;
188  }
189  break;
190  }
191 
192  case 's':
193  {
194  if ( optarg )
195  {
196  stereoBase = atoi(optarg);
197  optionProcessed = true;
198  }
199  break;
200  }
201 
202  case -1:
203  {
204  // All command-line options have been parsed.
205  break;
206  }
207 
208  default:
209  {
210  unhandledOptions.PushBack( optind - 1 );
211  break;
212  }
213  }
214  } while ( shortOption != -1 );
215 
216  // Take out the options we have processed
217  if ( optionProcessed )
218  {
219  if ( unhandledOptions.Count() > 0 )
220  {
221  int index( 1 );
222 
223  // Overwrite the argv with the values from the unhandled indices
224  const UnhandledContainer::ConstIterator endIter = unhandledOptions.End();
225  for ( UnhandledContainer::Iterator iter = unhandledOptions.Begin(); iter != endIter; ++iter )
226  {
227  (*argv)[ index++ ] = (*argv)[ *iter ];
228  }
229  *argc = unhandledOptions.Count() + 1; // +1 for the program name
230  }
231  else
232  {
233  // There are no unhandled options, so we should just have the program name
234  *argc = 1;
235  }
236 
237  optind = 1; // Reset to start
238  }
239 
240  opterr = origOptErrValue; // Reset opterr value.
241  }
242 }
243 
245 {
246 }
247 
248 } // namespace Adaptor
249 
250 } // namespace Internal
251 
252 } // namespace Dali
Dali Docs Home
Read more about Dali