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