Added new test harness
[platform/core/uifw/dali-adaptor.git] / adaptors / tizen / internal / command-line-options.cpp
1 //
2 // Copyright (c) 2014 Samsung Electronics Co., Ltd.
3 //
4 // Licensed under the Flora License, Version 1.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://floralicense.org/license/
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 // CLASS HEADER
18 #include "command-line-options.h"
19
20 // EXTERNAL INCLUDES
21 #include <getopt.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <iostream>
25 #include <vector>
26
27 namespace Dali
28 {
29
30 namespace Internal
31 {
32
33 namespace Adaptor
34 {
35
36 namespace
37 {
38 struct Argument
39 {
40   const char * const opt;
41   const char * const optDescription;
42
43   void Print()
44   {
45     std::cout << std::left << "  --";
46     std::cout.width( 18 );
47     std::cout << opt;
48     std::cout << optDescription;
49     std::cout << std::endl;
50   }
51 };
52
53 Argument EXPECTED_ARGS[] =
54 {
55   { "no-vsync",    "Disable VSync on Render" },
56   { "width",       "Stage Width"             },
57   { "height",      "Stage Height"            },
58   { "dpi",         "Emulated DPI"            },
59   { "view",        "Stereocopic 3D view mode ([0]=MONO, 1=STEREO_HORZ, 2=STEREO_VERT, 3=STEREO_INTERLACED)" },
60   { "stereo-base", "Distance in millimeters between left/right cameras [65.0]" },
61   { "help",        "Help"                    },
62   { NULL,          NULL                      }
63 };
64
65 enum Option
66 {
67   OPTION_NO_VSYNC = 0,
68   OPTION_STAGE_WIDTH,
69   OPTION_STAGE_HEIGHT,
70   OPTION_DPI,
71   OPTION_STEREO_MODE,
72   OPTION_STEREO_BASE,
73   OPTION_HELP
74 };
75
76 typedef std::vector< int > UnhandledContainer;
77
78 void ShowHelp()
79 {
80   std::cout << "Available options:" << std::endl;
81   Argument* arg = EXPECTED_ARGS;
82   while ( arg->opt )
83   {
84     arg->Print();
85     ++arg;
86   }
87 }
88
89 } // unnamed namespace
90
91 CommandLineOptions::CommandLineOptions(int *argc, char **argv[])
92 : noVSyncOnRender(0),
93   stageWidth(0), stageHeight(0),
94   viewMode(0),
95   stereoBase(65)
96 {
97   if ( *argc > 1 )
98   {
99     // We do not want to print out errors.
100     int origOptErrValue( opterr );
101     opterr = 0;
102
103     int help( 0 );
104
105     const struct option options[]=
106     {
107       { EXPECTED_ARGS[OPTION_NO_VSYNC].opt,     no_argument,       &noVSyncOnRender, 1   },  // "--no-vsync"
108       { EXPECTED_ARGS[OPTION_STAGE_WIDTH].opt,  required_argument, NULL,             'w' },  // "--width"
109       { EXPECTED_ARGS[OPTION_STAGE_HEIGHT].opt, required_argument, NULL,             'h' },  // "--height"
110       { EXPECTED_ARGS[OPTION_DPI].opt,          required_argument, NULL,             'd' },  // "--dpi"
111       { EXPECTED_ARGS[OPTION_STEREO_MODE].opt,  required_argument, NULL,             'v' },  // "--view"
112       { EXPECTED_ARGS[OPTION_STEREO_BASE].opt,  required_argument, NULL,             's' },  // "--stereo-base"
113       { EXPECTED_ARGS[OPTION_HELP].opt,         no_argument,       &help,            '?' },  // "--help"
114       { 0, 0, 0, 0 } // end of options
115     };
116
117     int shortOption( 0 );
118     int optionIndex( 0 );
119
120     const char* optString = "-w:h:d:v:s:"; // The '-' ensures that argv is NOT permuted
121     bool optionProcessed( false );
122
123     UnhandledContainer unhandledOptions; // We store indices of options we do not handle here
124
125     do
126     {
127       shortOption = getopt_long( *argc, *argv, optString, options, &optionIndex );
128
129       switch ( shortOption )
130       {
131         case 0:
132         {
133           // Check if we want help
134           if ( help )
135           {
136             ShowHelp();
137             optionProcessed = true;
138           }
139           break;
140         }
141
142         case 'w':
143         {
144           if ( optarg )
145           {
146             stageWidth = atoi( optarg );
147             optionProcessed = true;
148           }
149           break;
150         }
151
152         case 'h':
153         {
154           if ( optarg )
155           {
156             stageHeight = atoi( optarg );
157             optionProcessed = true;
158           }
159           break;
160         }
161
162         case 'd':
163         {
164           if ( optarg )
165           {
166             stageDPI.assign( optarg );
167             optionProcessed = true;
168           }
169           break;
170         }
171
172         case 'v':
173         {
174           if ( optarg )
175           {
176             viewMode = atoi(optarg);
177             optionProcessed = true;
178           }
179           break;
180         }
181
182         case 's':
183         {
184           if ( optarg )
185           {
186             stereoBase = atoi(optarg);
187             optionProcessed = true;
188           }
189           break;
190         }
191
192         case -1:
193         {
194           // All command-line options have been parsed.
195           break;
196         }
197
198         default:
199         {
200           unhandledOptions.push_back( optind - 1 );
201           break;
202         }
203       }
204     } while ( shortOption != -1 );
205
206     // Take out the options we have processed
207     if ( optionProcessed )
208     {
209       if ( !unhandledOptions.empty() )
210       {
211         int index( 1 );
212
213         // Overwrite the argv with the values from the unhandled indices
214         const UnhandledContainer::const_iterator endIter = unhandledOptions.end();
215         for ( UnhandledContainer::iterator iter = unhandledOptions.begin(); iter != endIter; ++iter )
216         {
217           (*argv)[ index++ ] = (*argv)[ *iter ];
218         }
219         *argc = unhandledOptions.size() + 1; // +1 for the program name
220       }
221       else
222       {
223         // There are no unhandled options, so we should just have the program name
224         *argc = 1;
225       }
226
227       optind = 1; // Reset to start
228     }
229
230     opterr = origOptErrValue; // Reset opterr value.
231   }
232 }
233
234 CommandLineOptions::~CommandLineOptions()
235 {
236 }
237
238 } // namespace Adaptor
239
240 } // namespace Internal
241
242 } // namespace Dali