2 * Copyright (c) 2014 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include "command-line-options.h"
43 const char * const opt;
44 const char * const optDescription;
48 const ios_base::fmtflags flags = cout.flags();
49 cout << left << " --";
52 cout << optDescription;
58 Argument EXPECTED_ARGS[] =
60 { "no-vsync", "Disable VSync on Render" },
61 { "width", "Stage Width" },
62 { "height", "Stage Height" },
63 { "dpi", "Emulated DPI" },
64 { "view", "Stereocopic 3D view mode ([0]=MONO, 1=STEREO_HORZ, 2=STEREO_VERT, 3=STEREO_INTERLACED)" },
65 { "stereo-base", "Distance in millimeters between left/right cameras [65.0]" },
81 typedef vector< int > UnhandledContainer;
85 cout << "Available options:" << endl;
86 Argument* arg = EXPECTED_ARGS;
94 } // unnamed namespace
96 CommandLineOptions::CommandLineOptions(int *argc, char **argv[])
98 stageWidth(0), stageHeight(0),
104 // We do not want to print out errors.
105 int origOptErrValue( opterr );
110 const struct option options[]=
112 { EXPECTED_ARGS[OPTION_NO_VSYNC].opt, no_argument, &noVSyncOnRender, 1 }, // "--no-vsync"
113 { EXPECTED_ARGS[OPTION_STAGE_WIDTH].opt, required_argument, NULL, 'w' }, // "--width"
114 { EXPECTED_ARGS[OPTION_STAGE_HEIGHT].opt, required_argument, NULL, 'h' }, // "--height"
115 { EXPECTED_ARGS[OPTION_DPI].opt, required_argument, NULL, 'd' }, // "--dpi"
116 { EXPECTED_ARGS[OPTION_STEREO_MODE].opt, required_argument, NULL, 'v' }, // "--view"
117 { EXPECTED_ARGS[OPTION_STEREO_BASE].opt, required_argument, NULL, 's' }, // "--stereo-base"
118 { EXPECTED_ARGS[OPTION_HELP].opt, no_argument, &help, '?' }, // "--help"
119 { 0, 0, 0, 0 } // end of options
122 int shortOption( 0 );
123 int optionIndex( 0 );
125 const char* optString = "-w:h:d:v:s:"; // The '-' ensures that argv is NOT permuted
126 bool optionProcessed( false );
128 UnhandledContainer unhandledOptions; // We store indices of options we do not handle here
132 shortOption = getopt_long( *argc, *argv, optString, options, &optionIndex );
134 switch ( shortOption )
138 // Check if we want help
142 optionProcessed = true;
151 stageWidth = atoi( optarg );
152 optionProcessed = true;
161 stageHeight = atoi( optarg );
162 optionProcessed = true;
171 stageDPI.assign( optarg );
172 optionProcessed = true;
181 viewMode = atoi(optarg);
182 optionProcessed = true;
191 stereoBase = atoi(optarg);
192 optionProcessed = true;
199 // All command-line options have been parsed.
205 unhandledOptions.push_back( optind - 1 );
209 } while ( shortOption != -1 );
211 // Take out the options we have processed
212 if ( optionProcessed )
214 if ( !unhandledOptions.empty() )
218 // Overwrite the argv with the values from the unhandled indices
219 const UnhandledContainer::const_iterator endIter = unhandledOptions.end();
220 for ( UnhandledContainer::iterator iter = unhandledOptions.begin(); iter != endIter; ++iter )
222 (*argv)[ index++ ] = (*argv)[ *iter ];
224 *argc = unhandledOptions.size() + 1; // +1 for the program name
228 // There are no unhandled options, so we should just have the program name
232 optind = 1; // Reset to start
235 opterr = origOptErrValue; // Reset opterr value.
239 CommandLineOptions::~CommandLineOptions()
243 } // namespace Adaptor
245 } // namespace Internal