summaryrefslogtreecommitdiff
path: root/ivi-layermanagement-examples/LayerManagerControl/src/sceneio.cpp
blob: 54ecbf172343feb64c8273dc2a0f9a352ff2a6d4 (plain)
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/***************************************************************************
 *
 * Copyright 2012 BMW Car IT GmbH
 *
 *
 * 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.
 *
 ****************************************************************************/
#include "LMControl.h"
#include "Expression.h"
#include "ExpressionInterpreter.h"
#include "SceneStore.h"
#include <cstdio>
#include <cmath>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iterator>
#include <cstring>
#include <pthread.h>
#include <stdarg.h>

using namespace std;


namespace {
void captureSceneDataHelper(t_ilm_surface surfaceId, t_scene_data* pSceneData, IlmSurface* pSurface)
{
    ilmSurfaceProperties& props = pSceneData->surfaceProperties[surfaceId];

    pSurface->set("id", surfaceId);
    pSurface->set("destHeight", props.destHeight);
    pSurface->set("destWidth", props.destWidth);
    pSurface->set("destX", props.destX);
    pSurface->set("destY", props.destY);
    pSurface->set("frameCounter", props.frameCounter);
    pSurface->set("opacity", props.opacity);
    pSurface->set("origSourceHeight", props.origSourceHeight);
    pSurface->set("origSourceWidth", props.origSourceWidth);
    pSurface->set("sourceHeight", props.sourceHeight);
    pSurface->set("sourceWidth", props.sourceWidth);
    pSurface->set("sourceX", props.sourceX);
    pSurface->set("sourceY", props.sourceY);
    pSurface->set("visibility", props.visibility);
}

void captureSceneDataHelper(t_ilm_layer layerId, t_scene_data* pSceneData, IlmLayer* pLayer)
{
    ilmLayerProperties& props = pSceneData->layerProperties[layerId];
    pLayer->set("id", layerId);
    pLayer->set("destHeight", props.destHeight);
    pLayer->set("destWidth", props.destWidth);
    pLayer->set("destX", props.destX);
    pLayer->set("destY", props.destY);
    pLayer->set("opacity", props.opacity);
    pLayer->set("sourceHeight", props.sourceHeight);
    pLayer->set("sourceWidth", props.sourceWidth);
    pLayer->set("sourceX", props.sourceX);
    pLayer->set("sourceY", props.sourceY);
    pLayer->set("visibility", props.visibility);

    if (pSceneData->layerSurfaces.find(layerId) != pSceneData->layerSurfaces.end())
    {
        for (vector<t_ilm_surface>::iterator it = pSceneData->layerSurfaces[layerId].begin();
                it != pSceneData->layerSurfaces[layerId].end(); ++it)
        {
            IlmSurface* pIlmsurface = new IlmSurface;
            captureSceneDataHelper(*it, pSceneData, pIlmsurface);
            pLayer->add(pIlmsurface);
        }
    }
}

void captureSceneData(IlmScene* scene)
{
    t_scene_data sceneStruct;
    captureSceneData(&sceneStruct);

    for (vector<t_ilm_display>::iterator it = sceneStruct.screens.begin();
            it != sceneStruct.screens.end(); ++it)
    {
        t_ilm_display displayId = *it;
        IlmDisplay* pIlmdisplay = new IlmDisplay;
        pIlmdisplay->set("id", displayId);
        pIlmdisplay->set("width", sceneStruct.screenWidth);
        pIlmdisplay->set("height", sceneStruct.screenHeight);

        if (sceneStruct.screenLayers.find(displayId) != sceneStruct.screenLayers.end())
        {
            for (vector<t_ilm_layer>::iterator it = sceneStruct.screenLayers[displayId].begin();
                    it != sceneStruct.screenLayers[displayId].end(); ++it)
            {
                IlmLayer* pIlmlayer = new IlmLayer;
                captureSceneDataHelper(*it, &sceneStruct, pIlmlayer);

                pIlmdisplay->add(pIlmlayer);
            }
        }

        scene->add(pIlmdisplay);
    }

    for (vector<t_ilm_layer>::iterator it = sceneStruct.layers.begin();
            it != sceneStruct.layers.end(); ++it)
    {
        if (sceneStruct.layerScreen.find(*it) == sceneStruct.layerScreen.end())
        {
            IlmLayer* pIlmlayer = new IlmLayer;
            captureSceneDataHelper(*it, &sceneStruct, pIlmlayer);

            scene->add(pIlmlayer);
        }
    }

    for (vector<t_ilm_surface>::iterator it = sceneStruct.surfaces.begin();
            it != sceneStruct.surfaces.end(); ++it)
    {
        if (sceneStruct.surfaceLayer.find(*it) == sceneStruct.surfaceLayer.end())
        {
            IlmSurface* pIlmsurface = new IlmSurface;
            captureSceneDataHelper(*it, &sceneStruct, pIlmsurface);

            scene->add(pIlmsurface);
        }
    }
}

string encodeEscapesequences(string s)
{
    map<string, string> code;

    code["\\"] = "\\[\\]";
    code["\n"] = "\\[n]";
    code["\t"] = "\\[t]";
    code["\v"] = "\\[v]";
    code["\b"] = "\\[b]";
    code["\f"] = "\\[f]";
    code["\r"] = "\\[r]";

    return replaceAll(s, code);
}

void exportSceneToTXTHelper(ostream& stream, StringMapTree* tree, string prefix = "")
{
    stream << prefix << encodeEscapesequences(tree->mNodeLabel) + ":{\n";
    for (map<string, pair<string, string> >::iterator it = tree->mNodeValues.begin();
            it != tree->mNodeValues.end(); ++it)
    {
        stream << prefix + "\t[" + encodeEscapesequences(it->first) + ":"
            + encodeEscapesequences(it->second.first) + "]=["
            + encodeEscapesequences(it->second.second) + "]\n";
    }

    for (list<StringMapTree*>::iterator it = tree->mChildren.begin(); it != tree->mChildren.end(); ++it)
    {
        exportSceneToTXTHelper(stream, *it, prefix + "\t");
        stream << "\n";
    }

    stream << prefix + "}";
}

string makeValidXMLCharacters(string s)
{
    map<string, string> code;
    code["<"] = "&lt;";
    code[">"] = "&gt;";
    code["&"] = "&amp;";
    code["\'"] = "&apos;";
    code["\""] = "&quot;";
    return replaceAll(s, code);
}

void exportSceneToXMLHelper(ostream& stream, StringMapTree* tree, string prefix = "")
{
    stream << prefix << "<" << tree->mNodeLabel << ">\n";

    for (map<string, pair<string, string> >::iterator it = tree->mNodeValues.begin();
            it != tree->mNodeValues.end(); ++it)
    {
        stream << prefix + "\t<Property name=\"" << it->first << "\" type=\"" << it->second.first << "\">\n";
        stream << prefix << "\t\t" << makeValidXMLCharacters(it->second.second) + "\n";
        stream << prefix << "\t</Property>\n";
    }

    for (list<StringMapTree*>::iterator it = tree->mChildren.begin();
            it != tree->mChildren.end(); ++it)
    {
        exportSceneToXMLHelper(stream, *it, prefix + "\t");
        stream << "\n";
    }

    stream << prefix << "</" << tree->mNodeLabel << ">\n";
}
} //end of anonymous namespace

void exportSceneToFile(string filename)
{
    IlmScene ilmscene;
    IlmScene* pScene = &ilmscene;
    captureSceneData(&ilmscene);
    stringstream buffer;
    StringMapTree sceneTree;
    pScene->toStringMapTree(&sceneTree);

    //check extension
    if (filename.find_last_of(".") != string::npos)
    {
        string extension = filename.substr(filename.find_last_of("."));
        cout << extension << endl;

        if (extension == ".xml")
        {
            buffer << "<\?xml version=\"1.0\"\?>\n";
            exportSceneToXMLHelper(buffer, &sceneTree);
            cout << "DONE WRITING XML" << endl;
        }
        else if (extension == ".txt")
        {
            exportSceneToTXTHelper(buffer, &sceneTree);
            cout << "DONE WRITING TXT" << endl;
        }
    }
    else
    {
        //defult:
        exportSceneToTXTHelper(buffer, &sceneTree);
        cout << "DONE WRITING TXT" << endl;
    }

    fstream stream(filename.c_str(), ios::out);

    cout << buffer.str() << endl;
    stream << buffer.str();
    stream.flush();
    stream.close();
}

void exportXtext(string fileName, string grammar, string url)
{
    string name = grammar.substr(grammar.find_last_of('.') + 1);
    //make sure first character is lower case
    std::transform(name.begin(), ++(name.begin()), name.begin(), ::tolower);

    IlmScene scene;
    StringMapTree grammarTree;
    scene.toGrammarMapTree(&grammarTree);

    //writing to file
    stringstream buffer;
    buffer << "grammar " << grammar << " with org.eclipse.xtext.common.Terminals" << endl;
    buffer << "generate " << name << " \"" << url << "\"" << endl;

    list<string> doneTypes;
    list<StringMapTree*> waitingNodes;
    waitingNodes.push_back(&grammarTree);
    while (!waitingNodes.empty())
    {
        //pop first element of the waiting types
        StringMapTree* typeNode = *(waitingNodes.begin());
        waitingNodes.pop_front();
        string type = typeNode->mNodeLabel;

        //if the type was not printed before
        if (find(doneTypes.begin(), doneTypes.end(), type) == doneTypes.end())
        {
            doneTypes.push_back(type);

            buffer << type << ":" << endl;
            buffer << "\t\'" << type << ":\'" << endl;

            for (map<string, pair<string, string> >::iterator it = typeNode->mNodeValues.begin();
                    it != typeNode->mNodeValues.end(); ++it)
            {
                buffer << "\t\t\'" << it->first << ":\' " << it->first << "=" <<
                        (it->second.second.size() > 0 ? it->second.second : it->second.first) << endl;
            }

            for (list<StringMapTree*>::iterator it = typeNode->mChildren.begin();
                    it != typeNode->mChildren.end(); ++it)
            {
                waitingNodes.push_back(*it);
                string childName = (*it)->mNodeLabel;
                //make lower case
                std::transform(childName.begin(), childName.end(), childName.begin(), ::tolower);
                childName += "s";

                buffer << "\t\t" << childName << "+=" << (*it)->mNodeLabel << "*" << endl;
            }

            buffer << ";" << endl;
        }
    }

    cout << "Xtext:[" << buffer.str() << "]" << endl;

    fstream fileout(fileName.c_str(), ios::out);
    fileout << buffer.str();
    fileout.flush();
    fileout.close();
}