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"
27 #include <dali/public-api/common/dali-vector.h>
42 const char * const opt;
43 const char * const optDescription;
47 const std::ios_base::fmtflags flags = std::cout.flags();
48 std::cout << std::left << " --";
49 std::cout.width( 18 );
51 std::cout << optDescription;
52 std::cout << std::endl;
53 std::cout.flags( flags );
57 Argument EXPECTED_ARGS[] =
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]" },
80 typedef Dali::Vector< int > UnhandledContainer;
84 std::cout << "Available options:" << std::endl;
85 Argument* arg = EXPECTED_ARGS;
93 } // unnamed namespace
95 CommandLineOptions::CommandLineOptions(int *argc, char **argv[])
97 stageWidth(0), stageHeight(0),
101 // Exit gracefully if no arguments provided
102 if ( !argc || !argv )
109 // We do not want to print out errors.
110 int origOptErrValue( opterr );
115 const struct option options[]=
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
127 int shortOption( 0 );
128 int optionIndex( 0 );
130 const char* optString = "-w:h:d:v:s:"; // The '-' ensures that argv is NOT permuted
131 bool optionProcessed( false );
133 UnhandledContainer unhandledOptions; // We store indices of options we do not handle here
137 shortOption = getopt_long( *argc, *argv, optString, options, &optionIndex );
139 switch ( shortOption )
143 // Check if we want help
147 optionProcessed = true;
156 stageWidth = atoi( optarg );
157 optionProcessed = true;
166 stageHeight = atoi( optarg );
167 optionProcessed = true;
176 stageDPI.assign( optarg );
177 optionProcessed = true;
186 viewMode = atoi(optarg);
187 optionProcessed = true;
196 stereoBase = atoi(optarg);
197 optionProcessed = true;
204 // All command-line options have been parsed.
210 unhandledOptions.PushBack( optind - 1 );
214 } while ( shortOption != -1 );
216 // Take out the options we have processed
217 if ( optionProcessed )
219 if ( unhandledOptions.Count() > 0 )
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 )
227 (*argv)[ index++ ] = (*argv)[ *iter ];
229 *argc = unhandledOptions.Count() + 1; // +1 for the program name
233 // There are no unhandled options, so we should just have the program name
237 optind = 1; // Reset to start
240 opterr = origOptErrValue; // Reset opterr value.
244 CommandLineOptions::~CommandLineOptions()
248 } // namespace Adaptor
250 } // namespace Internal