Merge "Fix for the cursor position with the arabic script." into devel/master
[platform/core/uifw/dali-toolkit.git] / node-addon / javascript-application-options.cpp
1 /*
2  * Copyright (c) 2015 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 // HEADER
19 #include "javascript-application-options.h"
20
21 // EXTERNAL INCLUDES
22 #include <cstring>
23
24 // INTERNAL INCLUDES
25 #include <v8-utils.h>
26
27 namespace DaliNodeAddon
28 {
29
30 namespace
31 {
32
33 struct StereoInfo
34 {
35   const char* const name;
36   Dali::ViewMode mode;
37 };
38 StereoInfo StereoModeTable[] = {
39     { "mono",                 Dali::MONO},
40     { "stereoHorizontal",     Dali::STEREO_HORIZONTAL },
41     { "stereoVertical",       Dali::STEREO_VERTICAL },
42     { "stereoInterlaced",     Dali::STEREO_INTERLACED },
43 };
44
45 const unsigned int numberViewModes = sizeof( StereoModeTable ) / sizeof( StereoModeTable[0] );
46
47 bool GetViewMode( const std::string& modeString, Dali::ViewMode& mode )
48 {
49   for( unsigned int i = 0; i < numberViewModes; ++i )
50   {
51     const StereoInfo& info (StereoModeTable[i]);
52
53     if( strcmp ( modeString.c_str() , info.name ) == 0 )
54     {
55       mode = info.mode;
56       return true;
57     }
58   }
59   // mode not found
60   mode = Dali::MONO;
61   return false;
62 }
63
64
65 // Note we can't parse the enviroment options for window width / height because
66 // adaptor which holds the environment option class has not been created
67 // and we can't create it, until we have a window
68 bool ParseWindowOptions( v8::Isolate* isolate, const v8::Local<v8::Object>& obj,  WindowOptions& window )
69 {
70   v8::HandleScope scope(isolate);
71
72   v8::Local<v8::Value> xValue = obj->Get( v8::String::NewFromUtf8( isolate, "x" ) );
73   v8::Local<v8::Value> yValue = obj->Get( v8::String::NewFromUtf8( isolate, "y" ) );
74   v8::Local<v8::Value> widthValue = obj->Get( v8::String::NewFromUtf8( isolate, "width" ) );
75   v8::Local<v8::Value> heightValue = obj->Get( v8::String::NewFromUtf8( isolate, "height" ) );
76   v8::Local<v8::Value> nameValue = obj->Get( v8::String::NewFromUtf8( isolate, "name" ) );
77   v8::Local<v8::Value> transparencyValue = obj->Get( v8::String::NewFromUtf8( isolate, "transparent" ) );
78
79   // if x,y are optional
80   if( xValue->IsUint32() )
81   {
82     window.positionSize.x = xValue->ToUint32()->Value();
83   }
84   if( yValue->IsUint32() )
85   {
86     window.positionSize.y = yValue->ToUint32()->Value();
87   }
88
89   // width and height are optional but will only accept them if they are both set
90   if( widthValue->IsUint32() &&  heightValue->IsUint32() )
91   {
92     window.positionSize.width = widthValue->ToUint32()->Value();
93     window.positionSize.height = heightValue->ToUint32()->Value();
94   }
95
96   // get the window name
97   if( nameValue->IsString() )
98   {
99     window.name = Dali::V8Plugin::V8Utils::v8StringToStdString( nameValue );
100   }
101   else
102   {
103     window.name ="DALi application";
104   }
105
106   if( transparencyValue->IsBoolean() )
107   {
108     window.transparent = transparencyValue->ToBoolean()->Value();
109   }
110   return true;
111 }
112
113 bool ParseStereoScopicOptions( v8::Isolate* isolate, const v8::Local<v8::Object>& stereoObject,  StereoScopicOptions& options )
114 {
115   v8::HandleScope scope(isolate);
116
117   v8::Local<v8::Value> modeValue = stereoObject->Get( v8::String::NewFromUtf8( isolate, "stereoscopicMode" ) );
118   v8::Local<v8::Value> stereoBaseValue = stereoObject->Get( v8::String::NewFromUtf8( isolate, "stereoBase" ) );
119
120   if( !modeValue->IsString() )
121   {
122     return true;
123   }
124
125   std::string mode = Dali::V8Plugin::V8Utils::v8StringToStdString( modeValue );
126   bool ok = GetViewMode( mode,  options.viewMode);
127   if( !ok )
128   {
129     return false;
130   }
131   if( stereoBaseValue->IsNumber() )
132   {
133     options.stereoBase = stereoBaseValue->ToNumber()->Value();
134   }
135
136   return true;
137 }
138
139 } // unnamed namespace
140
141 bool GetApplicationOptions(const v8::FunctionCallbackInfo<v8::Value>& args, ApplicationOptions& options )
142 {
143   v8::Isolate* isolate = args.GetIsolate();
144   v8::HandleScope scope(isolate);
145   bool ok( false );
146
147   if( !args[ 0 ]->IsObject() )
148   {
149     return false;
150   }
151
152   v8::Local<v8::Object> object = args[ 0 ]->ToObject();
153
154   // get the window settings
155   v8::Local<v8::Value> windowValue= object->Get( v8::String::NewFromUtf8( isolate, "window" ) );
156   if( windowValue->IsObject() )
157   {
158     ok = ParseWindowOptions( isolate, windowValue->ToObject(), options.window );
159     if( !ok )
160     {
161       return false; // missing window size
162     }
163   }
164
165   // get the stereoscopic settings
166   v8::Local<v8::Value> stereoValue= object->Get( v8::String::NewFromUtf8( isolate, "viewMode" ) );
167   if( stereoValue->IsObject() )
168   {
169     ok = ParseStereoScopicOptions( isolate,  stereoValue->ToObject(), options.stereo );
170     if( !ok )
171     {
172       return false;  // incorrect stereoscopic mode
173     }
174   }
175
176   // get the style sheet
177   v8::Local<v8::Value> stylesheetValue= object->Get( v8::String::NewFromUtf8( isolate, "styleSheet" ) );
178   if( stylesheetValue->IsString() )
179   {
180     options.stylesheet = Dali::V8Plugin::V8Utils::v8StringToStdString( stylesheetValue );
181   }
182
183   return true;
184 }
185
186 } // namespace DaliNodeAddon