[dali_1.3.41] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / node-addon / dali-addon.cpp
1 /* Copyright (c) 2015 Samsung Electronics Co., Ltd.
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  */
16
17 // EXTERNAL INCLUDES
18 #include <node.h>
19 #include <public-api/dali-core.h>
20 #include <dali-toolkit/dali-toolkit.h>
21 #include <integration-api/core.h>
22 #include <integration-api/adaptors/adaptor.h>
23 #include <public-api/adaptor-framework/window.h>
24 #include <devel-api/adaptor-framework/singleton-service.h>
25
26 // INTERNAL INCLUDES
27 #include <dali-wrapper.h>
28 #include "javascript-application-options.h"
29
30 using namespace Dali;
31
32 namespace DaliNodeAddon
33 {
34
35 class DaliApplication
36 {
37 public:
38
39     DaliApplication()
40     : mInitialized( false),
41       mSingletonService(SingletonService::New()),
42       mAdaptor( NULL )
43     {
44     }
45
46     ~DaliApplication()
47     {
48       mSingletonService.UnregisterAll();
49       delete mAdaptor;
50       mWindow.Reset();
51     }
52
53     bool Initialized() const
54     {
55       return mInitialized;
56     }
57
58     void Initialize(  ApplicationOptions options )
59     {
60       if( mInitialized )
61       {
62         return;
63       }
64
65       // 1. Create the window ( adaptor requires a window)
66       const WindowOptions& window( options.window);
67
68       mWindow = Window::New( window.positionSize, window.name, window.transparent );
69
70       // 2. create the adaptor
71       Adaptor* adaptor = &Adaptor::New( mWindow );
72
73       // 3. start the adaptor
74       adaptor->Start();
75
76       // Set the view modes
77
78       if( options.stereo.viewMode > Dali::MONO )
79       {
80         adaptor->SetStereoBase( options.stereo.stereoBase );
81         adaptor->SetViewMode( options.stereo.viewMode );
82       }
83
84       // fire the scene create signal
85       adaptor->NotifySceneCreated();
86
87       mInitialized = true;
88     }
89 private:
90
91     bool mInitialized;
92     SingletonService mSingletonService;
93     Adaptor* mAdaptor;
94     Window mWindow;
95
96 };
97
98 DaliApplication app;
99
100 void CreateDali(const v8::FunctionCallbackInfo<v8::Value>& args)
101 {
102   v8::Isolate* isolate = args.GetIsolate();
103   v8::HandleScope scope(isolate);
104
105   ApplicationOptions options;
106
107   bool ok = GetApplicationOptions( args, options);
108   if (!ok )
109   {
110     isolate->ThrowException( v8::Exception::TypeError( v8::String::NewFromUtf8(isolate, "Please check arguments passed to DALi require") ) );
111     return;
112   }
113
114   app.Initialize( options );
115
116   // the return value from calling the function   require('dali.js)(  options )
117   // is the dali object
118   args.GetReturnValue().Set( V8Plugin::DaliWrapper::CreateWrapperForNodeJS( isolate ) );
119 }
120
121
122 /**
123  * We make module.exports a function so that the developer can pass to
124  * parameters to DALi when it's 'required'
125  * E.g
126  *
127  *
128  * var window= {
129  *        x:10,
130  *        y:10,
131  *        width:800,
132  *        height: 600,
133  *        transparent: false,
134  *        name:'my-first-dali-app'
135  * };
136  *
137  * var viewMode {
138  *       'stereoscopic-mode':'stereo-vertical', // mono, stereo-horizontal, stereo-vertical, stereo-interlaced,
139  *       'stereo-base': 65 // Distance in millimeters between left/right cameras typically between (50-70mm)
140  * };
141  *
142  * var options= {
143  *    'window': window,
144  *    'view-mode': viewMode,
145  *    'style-sheet': 'my-theme.json'
146  * }
147  *
148  * var dali = require('dali.js')( options )
149  *
150  *
151  */
152 void ExportDaliModule(v8::Handle<v8::Object> exports, v8::Handle<v8::Object> module)
153 {
154   NODE_SET_METHOD(module, "exports", CreateDali);
155 }
156
157 } // namespace DaliNodeAddon
158
159 NODE_MODULE(dali, DaliNodeAddon::ExportDaliModule)
160
161
162
163