Subversion Repositories tpanel

Rev

Rev 446 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
446 andreas 1
/*
2
 * Copyright (C) 2021, 2022 by Andreas Theofilu <andreas@theosys.at>
3
 *
4
 * This program is free software; you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation; either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program; if not, write to the Free Software Foundation,
16
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
17
 */
18
 
19
#include "texternal.h"
20
#include "terror.h"
21
#include "tconfig.h"
22
#include "texpat++.h"
486 andreas 23
#include "ttpinit.h"
446 andreas 24
 
25
#if __cplusplus < 201402L
26
#   error "This module requires at least C++14 standard!"
27
#else
28
#   if __cplusplus < 201703L
29
#       include <experimental/filesystem>
30
namespace fs = std::experimental::filesystem;
31
#       warning "Support for C++14 and experimental filesystem will be removed in a future version!"
32
#   else
33
#       include <filesystem>
34
#       ifdef __ANDROID__
35
namespace fs = std::__fs::filesystem;
36
#       else
37
namespace fs = std::filesystem;
38
#       endif
39
#   endif
40
#endif
41
 
42
using std::string;
43
using std::vector;
44
using namespace Expat;
45
 
46
TExternal::TExternal()
47
{
48
    DECL_TRACER("TExternal::TExternal()");
49
 
50
    string projectPath = TConfig::getProjectPath();
51
 
52
    if (!fs::exists(projectPath + "/prj.xma"))
53
        projectPath += "/__system";
54
 
55
    makeFileName(projectPath, "external.xma");
56
 
57
    if (!isValidFile())
58
    {
59
        TError::setErrorMsg(TERRERROR, "Invalid file name " + getFileName());
60
        return;
61
    }
62
 
63
    mFileName = getFileName();
64
    initialize();
65
}
66
 
67
void TExternal::setFileName(const string &fn)
68
{
69
    DECL_TRACER("TExternal::setFileName(const string &fn)");
70
 
71
    string projectPath = TConfig::getProjectPath();
72
 
73
    if (!fs::exists(projectPath + "/prj.xma"))
74
        projectPath += "/__system";
75
 
76
    makeFileName(projectPath, fn);
77
 
78
    if (!isValidFile())
79
    {
80
        TError::setErrorMsg(TERRERROR, "Invalid file name " + getFileName());
81
        return;
82
    }
83
 
84
    mFileName = getFileName();
85
    initialize();
86
 
87
}
88
 
89
void TExternal::initialize()
90
{
91
    DECL_TRACER("TExternal::initialize()");
92
 
93
    TExpat xml(mFileName);
94
 
486 andreas 95
    if (!TTPInit::isTP5())
96
        xml.setEncoding(ENC_CP1250);
97
    else
98
        xml.setEncoding(ENC_UTF8);
99
 
446 andreas 100
    if (!xml.parse())
101
        return;
102
 
103
    int depth = 0;
104
    size_t index = 0;
105
    size_t oldIndex = 0;
106
 
107
    if (xml.getElementIndex("externalButtons", &depth) == TExpat::npos)
108
    {
109
        MSG_ERROR("Element \"externalButtons\" was not found!");
110
        TError::setError();
111
        return;
112
    }
113
 
114
    EXTPAGE_t page;
115
    depth++;
116
 
117
    while ((index = xml.getNextElementIndex("page", depth)) != TExpat::npos)
118
    {
119
        string name, content;
120
        vector<ATTRIBUTE_t> attrs;
121
 
122
        while ((index = xml.getNextElementFromIndex(index, &name, &content, &attrs)) != TExpat::npos)
123
        {
124
            string e = name;
125
 
126
            if (e.compare("pageID") == 0)
127
                page.pageID = xml.convertElementToInt(content);
128
            else if (e.compare("name") == 0)
129
                page.name = content;
130
            else if (e.compare("button") == 0)
131
            {
132
                EXTBUTTON_t button;
133
                string atype = xml.getAttribute("type", attrs);
134
 
135
                if (!atype.empty() && atype.compare("external") != 0)
136
                {
137
                    MSG_WARNING("Found unknown button type " << atype << "!");
138
                    continue;
139
                }
140
 
141
                while ((index = xml.getNextElementFromIndex(index, &name, &content, &attrs)) != TExpat::npos)
142
                {
143
                    if (name.compare("bi") == 0)
144
                        button.bi = xml.convertElementToInt(content);
145
                    else if (name.compare("bc") == 0)
146
                    {
147
                        button.bc = content;
148
 
149
                        if (button.bc.compare("cursorLeft") == 0)
150
                            button.type = EXT_CURSOR_LEFT;
151
                        else if (button.bc.compare("cursorRight") == 0)
152
                            button.type = EXT_CURSOR_RIGHT;
153
                        else if (button.bc.compare("cursorUp") == 0)
154
                            button.type = EXT_CURSOR_UP;
155
                        else if (button.bc.compare("cursorDown") == 0)
156
                            button.type = EXT_CURSOR_DOWN;
157
                        else if (button.bc.compare("cursorSel") == 0)
158
                            button.type = EXT_CURSOR_SELECT;
159
                        else if (button.bc.compare("cursorRotateRight") == 0)
160
                            button.type = EXT_CURSOR_ROTATE_RIGHT;
161
                        else if (button.bc.compare("cursorRotateLeft") == 0)
162
                            button.type = EXT_CURSOR_ROTATE_LEFT;
163
                        else if (button.bc.compare("gestureLeft") == 0)
164
                            button.type = EXT_GESTURE_LEFT;
165
                        else if (button.bc.compare("gestureRight") == 0)
166
                            button.type = EXT_GESTURE_RIGHT;
167
                        else if (button.bc.compare("gestureUp") == 0)
168
                            button.type = EXT_GESTURE_UP;
169
                        else if (button.bc.compare("gestureDown") == 0)
170
                            button.type = EXT_GESTURE_DOWN;
171
                        else if (button.bc.compare("gestureRotateLeft") == 0)
172
                            button.type = EXT_GESTURE_ROTATE_LEFT;
173
                        else if (button.bc.compare("gestureRotateRight") == 0)
174
                            button.type = EXT_GESTURE_ROTATE_RIGHT;
175
                        else if (button.bc.compare("gestureDoublePress") == 0)
176
                            button.type = EXT_GESTURE_DOUBLE_PRESS;
177
                        else if (button.bc.compare("general") == 0)
178
                            button.type = EXT_GENERAL;
179
                    }
180
                    else if (name.compare("na") == 0)
181
                        button.na = content;
182
                    else if (name.compare("da") == 0)
183
                        button.da = xml.convertElementToInt(content);
184
                    else if (name.compare("pp") == 0)
185
                        button.pp = content;
186
                    else if (name.compare("ap") == 0)
187
                        button.ap = xml.convertElementToInt(content);
188
                    else if (name.compare("ad") == 0)
189
                        button.ad = xml.convertElementToInt(content);
190
                    else if (name.compare("cp") == 0)
191
                        button.cp = xml.convertElementToInt(content);
192
                    else if (name.compare("ch") == 0)
193
                        button.ch = xml.convertElementToInt(content);
194
                    else if (name.compare("rt") == 0)
195
                        button.rt = content;
196
                    else if (name.compare("vt") == 0)
197
                        button.vt = content;
198
                    else if (name.compare("lp") == 0)
199
                        button.lp = xml.convertElementToInt(content);
200
                    else if (name.compare("lv") == 0)
201
                        button.lv = xml.convertElementToInt(content);
202
                    else if (name.compare("va") == 0)
203
                        button.va = xml.convertElementToInt(content);
204
                    else if (name.compare("rv") == 0)
205
                        button.rv = xml.convertElementToInt(content);
206
                    else if (name.compare("rl") == 0)
207
                        button.rl = xml.convertElementToInt(content);
208
                    else if (name.compare("rh") == 0)
209
                        button.rh = xml.convertElementToInt(content);
210
                    else if (name.compare("lu") == 0)
211
                        button.lu = xml.convertElementToInt(content);
212
                    else if (name.compare("ld") == 0)
213
                        button.ld = xml.convertElementToInt(content);
214
                    else if (name.compare("so") == 0)
215
                        button.so = xml.convertElementToInt(content);
216
                    else if (name.compare("co") == 0)
217
                        button.co = xml.convertElementToInt(content);
218
                    else if (name.compare("cm") == 0)
219
                        button.cm.push_back(content);
220
                    else if (name.compare("ac") == 0)
221
                    {
222
                        button.ac = content;
223
                        button.ac_de = xml.getAttributeInt("de", attrs);
224
                    }
225
                    else if (name.compare("at") == 0)
226
                        button.at = xml.convertElementToInt(content);
227
 
228
                    oldIndex = index;
229
                }
230
 
231
                page.buttons.push_back(button);
232
            }
233
 
234
            mPages.push_back(page);
235
            page.buttons.clear();
236
 
237
            if (index == TExpat::npos)
238
                index = oldIndex + 1;
239
            else
240
                oldIndex = index;
241
        }
242
 
243
        if (index == TExpat::npos)
244
            index = oldIndex + 1;
245
        else
246
            oldIndex = index;
247
 
248
        xml.setIndex(index);
249
    }
250
/*
251
    if (TStreamError::checkFilter(HLOG_DEBUG))
252
    {
253
        vector<EXTPAGE_t>::iterator iter;
254
 
255
        for (iter = mPages.begin(); iter != mPages.end(); ++iter)
256
        {
257
            MSG_DEBUG("Name  : " << iter->name);
258
            MSG_DEBUG("pageID: " << iter->pageID);
259
 
260
            vector<EXTBUTTON_t>::iterator ibut;
261
 
262
            for (ibut = iter->buttons.begin(); ibut != iter->buttons.end(); ++ibut)
263
            {
264
                MSG_DEBUG("   Button index: " << ibut->bi);
265
                MSG_DEBUG("   Button name : " << ibut->na);
266
                MSG_DEBUG("   Button bc   : " << ibut->bc);
267
            }
268
        }
269
    }
270
*/
271
}
272
 
273
EXTBUTTON_t TExternal::getButton(extButtons_t bt)
274
{
275
    DECL_TRACER("TExternal::getButton(extButtons_t bt)");
276
 
277
    EXTBUTTON_t empty;
278
 
279
    if (mPages.size() == 0)
280
    {
281
        MSG_DEBUG("No pages found.");
282
        return empty;
283
    }
284
 
285
    vector<EXTPAGE_t>::iterator piter;
286
 
287
    for (piter = mPages.begin(); piter != mPages.end(); ++piter)
288
    {
289
        MSG_DEBUG("Found page " << piter->name);
290
 
291
        if (piter->buttons.size() == 0)
292
            continue;
293
 
294
        vector<EXTBUTTON_t>::iterator btIter;
295
 
296
        for (btIter = piter->buttons.begin(); btIter != piter->buttons.end(); ++btIter)
297
        {
298
            MSG_DEBUG("Found button " << btIter->na << ", type=" << btIter->type << " (" << bt << ")");
299
 
300
            if (btIter->type == bt || (!mStrict && btIter->type == findCompatibel(bt)))
301
                return *btIter;
302
        }
303
    }
304
 
305
    return empty;
306
}
307
 
308
EXTBUTTON_t TExternal::getButton(int pageId, extButtons_t bt)
309
{
310
    DECL_TRACER("TExternal::getButton(int pageId, extButtons_t bt)");
311
 
312
    EXTBUTTON_t empty;
313
 
314
    if (mPages.size() == 0)
315
        return empty;
316
 
317
    vector<EXTPAGE_t>::iterator piter;
318
 
319
    for (piter = mPages.begin(); piter != mPages.end(); ++piter)
320
    {
321
        if (piter->pageID != pageId)
322
            continue;
323
 
324
        if (piter->buttons.size() == 0)
325
            continue;
326
 
327
        vector<EXTBUTTON_t>::iterator btIter;
328
 
329
        for (btIter = piter->buttons.begin(); btIter != piter->buttons.end(); ++btIter)
330
        {
331
            if (btIter->type == bt || (!mStrict && btIter->type == findCompatibel(bt)))
332
                return *btIter;
333
        }
334
    }
335
 
336
    return empty;
337
}
338
 
339
extButtons_t TExternal::findCompatibel(extButtons_t bt)
340
{
341
    DECL_TRACER("TExternal::findCompatibel(extButtons_t bt)");
342
 
343
    switch(bt)
344
    {
345
        case EXT_CURSOR_DOWN:           return EXT_GESTURE_DOWN;
346
        case EXT_CURSOR_LEFT:           return EXT_GESTURE_LEFT;
347
        case EXT_CURSOR_RIGHT:          return EXT_GESTURE_RIGHT;
348
        case EXT_CURSOR_UP:             return EXT_GESTURE_UP;
349
        case EXT_CURSOR_ROTATE_LEFT:    return EXT_GESTURE_ROTATE_LEFT;
350
        case EXT_CURSOR_ROTATE_RIGHT:   return EXT_GESTURE_ROTATE_RIGHT;
351
        case EXT_CURSOR_SELECT:         return EXT_GESTURE_DOUBLE_PRESS;
352
 
353
        case EXT_GESTURE_DOWN:          return EXT_CURSOR_DOWN;
354
        case EXT_GESTURE_RIGHT:         return EXT_CURSOR_RIGHT;
355
        case EXT_GESTURE_UP:            return EXT_CURSOR_UP;
356
        case EXT_GESTURE_LEFT:          return EXT_CURSOR_LEFT;
357
        case EXT_GESTURE_ROTATE_LEFT:   return EXT_CURSOR_ROTATE_LEFT;
358
        case EXT_GESTURE_ROTATE_RIGHT:  return EXT_CURSOR_ROTATE_RIGHT;
359
        case EXT_GESTURE_DOUBLE_PRESS:  return EXT_CURSOR_SELECT;
360
 
361
        default:
362
            return bt;
363
    }
364
}