Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

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