Remove StereoMode
[platform/core/uifw/dali-adaptor.git] / dali / internal / system / common / command-line-options.cpp
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
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
18 // CLASS HEADER
19 #include <dali/internal/system/common/command-line-options.h>
20
21 // EXTERNAL INCLUDES
22 #include <getopt.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <iostream>
26
27 #include <dali/public-api/common/dali-vector.h>
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 std::ios_base::fmtflags flags = std::cout.flags();
48     std::cout << std::left << "  --";
49     std::cout.width( 18 );
50     std::cout << opt;
51     std::cout << optDescription;
52     std::cout << std::endl;
53     std::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   { "help",        "Help"                    },
64   { NULL,          NULL                      }
65 };
66
67 enum Option
68 {
69   OPTION_NO_VSYNC = 0,
70   OPTION_STAGE_WIDTH,
71   OPTION_STAGE_HEIGHT,
72   OPTION_DPI,
73   OPTION_HELP
74 };
75
76 typedef Dali::Vector< int32_t > 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(int32_t *argc, char **argv[])
92 : noVSyncOnRender(0),
93   stageWidth(0), stageHeight(0)
94 {
95   // Exit gracefully if no arguments provided
96   if ( !argc || !argv )
97   {
98     return;
99   }
100
101   if ( *argc > 1 )
102   {
103     // We do not want to print out errors.
104     int32_t origOptErrValue( opterr );
105     opterr = 0;
106
107     int32_t 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_HELP].opt,         no_argument,       &help,            '?' },  // "--help"
116       { 0, 0, 0, 0 } // end of options
117     };
118
119     int32_t shortOption( 0 );
120     int32_t optionIndex( 0 );
121
122     const char* optString = "-w:h:d:"; // The '-' ensures that argv is NOT permuted
123     bool optionProcessed( false );
124
125     UnhandledContainer unhandledOptions; // We store indices of options we do not handle here
126
127     do
128     {
129       shortOption = getopt_long( *argc, *argv, optString, options, &optionIndex );
130
131       switch ( shortOption )
132       {
133         case 0:
134         {
135           // Check if we want help
136           if ( help )
137           {
138             ShowHelp();
139             optionProcessed = true;
140           }
141           break;
142         }
143
144         case 'w':
145         {
146           if ( optarg )
147           {
148             stageWidth = atoi( optarg );
149             optionProcessed = true;
150           }
151           break;
152         }
153
154         case 'h':
155         {
156           if ( optarg )
157           {
158             stageHeight = atoi( optarg );
159             optionProcessed = true;
160           }
161           break;
162         }
163
164         case 'd':
165         {
166           if ( optarg )
167           {
168             stageDPI.assign( optarg );
169             optionProcessed = true;
170           }
171           break;
172         }
173
174         case -1:
175         {
176           // All command-line options have been parsed.
177           break;
178         }
179
180         default:
181         {
182           unhandledOptions.PushBack( optind - 1 );
183           break;
184         }
185       }
186     } while ( shortOption != -1 );
187
188     // Take out the options we have processed
189     if ( optionProcessed )
190     {
191       if ( unhandledOptions.Count() > 0 )
192       {
193         int32_t index( 1 );
194
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 )
198         {
199           (*argv)[ index++ ] = (*argv)[ *iter ];
200         }
201         *argc = unhandledOptions.Count() + 1; // +1 for the program name
202       }
203       else
204       {
205         // There are no unhandled options, so we should just have the program name
206         *argc = 1;
207       }
208
209       optind = 1; // Reset to start
210     }
211
212     opterr = origOptErrValue; // Reset opterr value.
213   }
214 }
215
216 CommandLineOptions::~CommandLineOptions()
217 {
218 }
219
220 } // namespace Adaptor
221
222 } // namespace Internal
223
224 } // namespace Dali