1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/* DreemGL is a collaboration between Teeming Society & Samsung Electronics, sponsored by Samsung and others.
   Copyright 2015-2016 Teeming Society. Licensed under the Apache License, Version 2.0 (the "License"); You may not use this file except in compliance with the License.
   You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing,
   software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and limitations under the License.*/

// Copied from compositionserver.js for dali. This assumes that the
// node server is running on the same machine where dali is running.

define.class(function(require){

        console.log('Loading bootdali', define.$environment);

        // composition_client references WebSocket
        //WebSocket = require('$system/base/nodewebsocket')

        //var Render = require('$system/platform/render')

        var path = require('path')
        var fs = require('fs')

        var ExternalApps = require('$system/server/externalapps')
        var FileWatcher = require('$system/server/filewatcher')

        var BusServer = require('$system/rpc/busserver')
        var HTMLParser = require('$system/parse/htmlparser')
        var ScriptError = require('$system/parse/scripterror')
        var legacy_support = 0

        var Texture = require('$system/platform/$platform/texture$platform')

        this.atConstructor = function(
                args, //Object: Process arguments
                compname, //String: name of the composition
                rootserver){ //TeemServer: teem server object

                define.$platform = 'dali'

                // Called from define.noderequirewrapper to load a texture
                define.loadImage = function(name){
                        //console.log('loadImage', name);
                        return Texture.fromImage({path:name});
                }

                // Dali stage settings (from command-line), or defaults
                this.width = parseInt(args['-width']) || 1920;
                this.height = parseInt(args['-height']) || 1080;
                this.name = args['-name'] || 'dreemgl';

                // Check if a remote composition is used
                if (compname.indexOf('http') >= 0) {
                        this.server = compname;
                        console.log('remote dreemgl server', compname);
                }

                // Detect location for Dali Node.js add-on. On Tizen
                // it can be required like any npm module, on Ubuntu
                // it's in the /dali-toolkit/node-addon/build/Release/dali
                // relative to Dali's root folder.
                this.onTizen = false;
                this.dalilib = 'dali';
                try {
                        this.onTizen = fs.statSync('/etc/tizen-release').isFile();
                } catch (e) {
                }

                // Dali/nodejs interface (nodejs package at top-level of dreemgl repository)
                if (! this.onTizen ) {
                        var daliRoot = process.env['DESKTOP_PREFIX'].replace('/dali-env/opt', '');
                        this.dalilib = daliRoot + '/dali-toolkit/node-addon/build/Release/dali'
                }

                if ('-dumpprog' in args) {
                        var dp = args['-dumpprog'];
                        this.dumpprog = (dp.length > 0) ? args['-dumpprog'] : 'stdout';
                }

                this.args = args
                this.compname = compname
                this.rootserver = rootserver

                this.busserver = new BusServer()

                // Initialize dali early. Otherwise, some composition can fail
                // when run from a remote server.
                this.initDali();

                // lets give it a session
                this.session = Math.random() * 1000000

                this.slow_watcher = new FileWatcher()

                this.slow_watcher.atChange = function(){
                        // lets reload this app
                        this.reload()
                }.bind(this)

                this.components = {}

                this.paths = ""
                for(var key in define.paths){
                        if(this.paths) this.paths += ',\n\t\t'
                        this.paths += '$'+key+':"$root/'+key+'"'
                }

                // lets compile and run the dreem composition
                define.atRequire = function(filename){
                        this.slow_watcher.watch(filename)
                }.bind(this)
                //
                this.reload()
        }

        // Called when any of the dependent files change for this composition
        this.atChange = function(){
        }

        // Destroys all objects maintained by the composition
        this.destroy = function(){
                if(this.mycomposition && this.mycomposition.destroy) this.mycomposition.destroy()
                this.mycomposition = undefined
        }

        // initialize dali runtime. This should be run before the composition is
        // loaded.
        this.initDali = function() {
                // Load DALi module
                this.DaliApi = require('./dali_api');
                this.DaliApi.initialize({width: this.width, height: this.height, name: this.name, dalilib: this.dalilib, dumpprog: this.dumpprog});
        }

        // Load the composition. If composition is defined, load it remotely
        this.loadComposition = function(composition){
                require.clearCache()

                if (composition) {
                        // Remote server
                        this.composition = new composition(undefined, undefined, this.server)
                }
                else {
                        // Local assets
                        var Composition = require(define.expandVariables(this.filename))
                        this.composition = new Composition(this.busserver, this.session)
                }
        }

        this.reload = function(){
                this.destroy()

                // lets fill
                require.clearCache()

                // lets see if our composition is a dir or a jsfile
                if (this.server) {
                        // Remote server
                        var self = this;
                        require.async(self.server).then(function(composition){
                                return self.loadComposition(composition);
                        }).catch(function(error){
                                console.log('Composition load failure', error.stack)
                        })
                }
                else {
                        // Use local files

                        var dir = '$root/'
                        var jsname = dir + this.compname+'.js'
                        try {
                                if(fs.existsSync(define.expandVariables(jsname))){
                                        this.filename = jsname
                                        return this.loadComposition()
                                }
                                else{
                                        var jsname = dir + this.compname + '/index.js'
                                        if(fs.existsSync(define.expandVariables(jsname))){
                                                this.filename = jsname
                                                return this.loadComposition()
                                        }
                                }
                        }
                        finally{
                                //console.log(e.stack)
                        }
                }


                  this.screenname = this.compname;

                // Reaching here indicates the path does not exist
                console.log('bootdali.reload: Unable to load', this.compname);
        }

        this.loadHTML = function(title, boot){
            return '';
        }

        this.loadTemplate = function(title, boot){
            console.warn('warning: bootdali.loadTemplate not written');
            return '';
        }

})