Subversion Repositories tpanel

Rev

Details | Last modification | View Log | RSS feed

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