2 * Copyright (c) 2018 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 <dali/internal/system/common/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" },
76 typedef Dali::Vector< int32_t > UnhandledContainer;
80 std::cout << "Available options:" << std::endl;
81 Argument* arg = EXPECTED_ARGS;
89 } // unnamed namespace
91 CommandLineOptions::CommandLineOptions(int32_t *argc, char **argv[])
93 stageWidth(0), stageHeight(0)
95 // Exit gracefully if no arguments provided
103 // We do not want to print out errors.
104 int32_t origOptErrValue( opterr );
109 const struct option options[]=
111 { EXPECTED_ARGS[OPTION_NO_VSYNC].opt, no_argument, &noVSyncOnRender, 1 }, // "--no-vsync"
112 { EXPECTED_ARGS[OPTION_STAGE_WIDTH].opt, required_argument, NULL, 'w' }, // "--width"
113 { EXPECTED_ARGS[OPTION_STAGE_HEIGHT].opt, required_argument, NULL, 'h' }, // "--height"
114 { EXPECTED_ARGS[OPTION_DPI].opt, required_argument, NULL, 'd' }, // "--dpi"
115 { EXPECTED_ARGS[OPTION_HELP].opt, no_argument, &help, '?' }, // "--help"
116 { 0, 0, 0, 0 } // end of options
119 int32_t shortOption( 0 );
120 int32_t optionIndex( 0 );
122 const char* optString = "-w:h:d:"; // The '-' ensures that argv is NOT permuted
123 bool optionProcessed( false );
125 UnhandledContainer unhandledOptions; // We store indices of options we do not handle here
129 shortOption = getopt_long( *argc, *argv, optString, options, &optionIndex );
131 switch ( shortOption )
135 // Check if we want help
139 optionProcessed = true;
148 stageWidth = atoi( optarg );
149 optionProcessed = true;
158 stageHeight = atoi( optarg );
159 optionProcessed = true;
168 stageDPI.assign( optarg );
169 optionProcessed = true;
176 // All command-line options have been parsed.
182 unhandledOptions.PushBack( optind - 1 );
186 } while ( shortOption != -1 );
188 // Take out the options we have processed
189 if ( optionProcessed )
191 if ( unhandledOptions.Count() > 0 )
195 // Overwrite the argv with the values from the unhandled indices
196 const UnhandledContainer::ConstIterator endIter = unhandledOptions.End();
197 for ( UnhandledContainer::Iterator iter = unhandledOptions.Begin(); iter != endIter; ++iter )
199 (*argv)[ index++ ] = (*argv)[ *iter ];
201 *argc = unhandledOptions.Count() + 1; // +1 for the program name
205 // There are no unhandled options, so we should just have the program name
209 optind = 1; // Reset to start
212 opterr = origOptErrValue; // Reset opterr value.
216 CommandLineOptions::~CommandLineOptions()
220 } // namespace Adaptor
222 } // namespace Internal