22a0af8bb950c18a94f64d3b16ae75126a3807da
[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 using namespace std;
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 ios_base::fmtflags flags = cout.flags();
48     cout << left << "  --";
49     cout.width( 18 );
50     cout << opt;
51     cout << optDescription;
52     cout << endl;
53     cout.flags( flags );
54   }
55 };
56
57 Argument EXPECTED_ARGS[] =
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 {
71   OPTION_NO_VSYNC = 0,
72   OPTION_STAGE_WIDTH,
73   OPTION_STAGE_HEIGHT,
74   OPTION_DPI,
75   OPTION_STEREO_MODE,
76   OPTION_STEREO_BASE,
77   OPTION_HELP
78 };
79
80 typedef vector< int > UnhandledContainer;
81
82 void ShowHelp()
83 {
84   cout << "Available options:" << 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   if ( *argc > 1 )
102   {
103     // We do not want to print out errors.
104     int origOptErrValue( opterr );
105     opterr = 0;
106
107     int help( 0 );
108
109     const struct option options[]=
110     {
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_STEREO_MODE].opt,  required_argument, NULL,             'v' },  // "--view"
116       { EXPECTED_ARGS[OPTION_STEREO_BASE].opt,  required_argument, NULL,             's' },  // "--stereo-base"
117       { EXPECTED_ARGS[OPTION_HELP].opt,         no_argument,       &help,            '?' },  // "--help"
118       { 0, 0, 0, 0 } // end of options
119     };
120
121     int shortOption( 0 );
122     int optionIndex( 0 );
123
124     const char* optString = "-w:h:d:v:s:"; // The '-' ensures that argv is NOT permuted
125     bool optionProcessed( false );
126
127     UnhandledContainer unhandledOptions; // We store indices of options we do not handle here
128
129     do
130     {
131       shortOption = getopt_long( *argc, *argv, optString, options, &optionIndex );
132
133       switch ( shortOption )
134       {
135         case 0:
136         {
137           // Check if we want help
138           if ( help )
139           {
140             ShowHelp();
141             optionProcessed = true;
142           }
143           break;
144         }
145
146         case 'w':
147         {
148           if ( optarg )
149           {
150             stageWidth = atoi( optarg );
151             optionProcessed = true;
152           }
153           break;
154         }
155
156         case 'h':
157         {
158           if ( optarg )
159           {
160             stageHeight = atoi( optarg );
161             optionProcessed = true;
162           }
163           break;
164         }
165
166         case 'd':
167         {
168           if ( optarg )
169           {
170             stageDPI.assign( optarg );
171             optionProcessed = true;
172           }
173           break;
174         }
175
176         case 'v':
177         {
178           if ( optarg )
179           {
180             viewMode = atoi(optarg);
181             optionProcessed = true;
182           }
183           break;
184         }
185
186         case 's':
187         {
188           if ( optarg )
189           {
190             stereoBase = atoi(optarg);
191             optionProcessed = true;
192           }
193           break;
194         }
195
196         case -1:
197         {
198           // All command-line options have been parsed.
199           break;
200         }
201
202         default:
203         {
204           unhandledOptions.push_back( optind - 1 );
205           break;
206         }
207       }
208     } while ( shortOption != -1 );
209
210     // Take out the options we have processed
211     if ( optionProcessed )
212     {
213       if ( !unhandledOptions.empty() )
214       {
215         int index( 1 );
216
217         // Overwrite the argv with the values from the unhandled indices
218         const UnhandledContainer::const_iterator endIter = unhandledOptions.end();
219         for ( UnhandledContainer::iterator iter = unhandledOptions.begin(); iter != endIter; ++iter )
220         {
221           (*argv)[ index++ ] = (*argv)[ *iter ];
222         }
223         *argc = unhandledOptions.size() + 1; // +1 for the program name
224       }
225       else
226       {
227         // There are no unhandled options, so we should just have the program name
228         *argc = 1;
229       }
230
231       optind = 1; // Reset to start
232     }
233
234     opterr = origOptErrValue; // Reset opterr value.
235   }
236 }
237
238 CommandLineOptions::~CommandLineOptions()
239 {
240 }
241
242 } // namespace Adaptor
243
244 } // namespace Internal
245
246 } // namespace Dali