Remove vsync-monitor since is not used anymore and move egl-image-extensions-ubuntu...
[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   { "width",       "Stage Width"             },
60   { "height",      "Stage Height"            },
61   { "dpi",         "Emulated DPI"            },
62   { "help",        "Help"                    },
63   { NULL,          NULL                      }
64 };
65
66 enum Option
67 {
68   OPTION_STAGE_WIDTH = 0,
69   OPTION_STAGE_HEIGHT,
70   OPTION_DPI,
71   OPTION_HELP
72 };
73
74 typedef Dali::Vector< int32_t > UnhandledContainer;
75
76 void ShowHelp()
77 {
78   std::cout << "Available options:" << std::endl;
79   Argument* arg = EXPECTED_ARGS;
80   while ( arg->opt )
81   {
82     arg->Print();
83     ++arg;
84   }
85 }
86
87 } // unnamed namespace
88
89 CommandLineOptions::CommandLineOptions(int32_t *argc, char **argv[])
90 : stageWidth(0),
91   stageHeight(0)
92 {
93   // Exit gracefully if no arguments provided
94   if ( !argc || !argv )
95   {
96     return;
97   }
98
99   if ( *argc > 1 )
100   {
101     // We do not want to print out errors.
102     int32_t origOptErrValue( opterr );
103     opterr = 0;
104
105     int32_t help( 0 );
106
107     const struct option options[]=
108     {
109       { EXPECTED_ARGS[OPTION_STAGE_WIDTH].opt,  required_argument, NULL,             'w' },  // "--width"
110       { EXPECTED_ARGS[OPTION_STAGE_HEIGHT].opt, required_argument, NULL,             'h' },  // "--height"
111       { EXPECTED_ARGS[OPTION_DPI].opt,          required_argument, NULL,             'd' },  // "--dpi"
112       { EXPECTED_ARGS[OPTION_HELP].opt,         no_argument,       &help,            '?' },  // "--help"
113       { 0, 0, 0, 0 } // end of options
114     };
115
116     int32_t shortOption( 0 );
117     int32_t optionIndex( 0 );
118
119     const char* optString = "-w:h:d:"; // The '-' ensures that argv is NOT permuted
120     bool optionProcessed( false );
121
122     UnhandledContainer unhandledOptions; // We store indices of options we do not handle here
123
124     do
125     {
126       shortOption = getopt_long( *argc, *argv, optString, options, &optionIndex );
127
128       switch ( shortOption )
129       {
130         case 0:
131         {
132           // Check if we want help
133           if ( help )
134           {
135             ShowHelp();
136             optionProcessed = true;
137           }
138           break;
139         }
140
141         case 'w':
142         {
143           if ( optarg )
144           {
145             stageWidth = atoi( optarg );
146             optionProcessed = true;
147           }
148           break;
149         }
150
151         case 'h':
152         {
153           if ( optarg )
154           {
155             stageHeight = atoi( optarg );
156             optionProcessed = true;
157           }
158           break;
159         }
160
161         case 'd':
162         {
163           if ( optarg )
164           {
165             stageDPI.assign( optarg );
166             optionProcessed = true;
167           }
168           break;
169         }
170
171         case -1:
172         {
173           // All command-line options have been parsed.
174           break;
175         }
176
177         default:
178         {
179           unhandledOptions.PushBack( optind - 1 );
180           break;
181         }
182       }
183     } while ( shortOption != -1 );
184
185     // Take out the options we have processed
186     if ( optionProcessed )
187     {
188       if ( unhandledOptions.Count() > 0 )
189       {
190         int32_t index( 1 );
191
192         // Overwrite the argv with the values from the unhandled indices
193         const UnhandledContainer::ConstIterator endIter = unhandledOptions.End();
194         for ( UnhandledContainer::Iterator iter = unhandledOptions.Begin(); iter != endIter; ++iter )
195         {
196           (*argv)[ index++ ] = (*argv)[ *iter ];
197         }
198         *argc = unhandledOptions.Count() + 1; // +1 for the program name
199       }
200       else
201       {
202         // There are no unhandled options, so we should just have the program name
203         *argc = 1;
204       }
205
206       optind = 1; // Reset to start
207     }
208
209     opterr = origOptErrValue; // Reset opterr value.
210   }
211 }
212
213 CommandLineOptions::~CommandLineOptions()
214 {
215 }
216
217 } // namespace Adaptor
218
219 } // namespace Internal
220
221 } // namespace Dali