/
config.cpp
716 lines (633 loc) · 18.3 KB
/
config.cpp
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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
/**
* \file
* Implementation of configuration file parsing.
*/
#include <QList>
#include <QString>
#include <QDesktopWidget>
#include <QFile>
#include "tokeniser.h"
#include "tokens.h"
#include "config.h"
#include "exception.h"
#include "app.h"
#include "expr.h"
#include "widgets/gauge.h"
#include "widgets/graph.h"
#include "widgets/compass.h"
#include "widgets/status.h"
#include "widgets/number.h"
#include "widgets/map.h"
#include "widgets/switch.h"
#include "widgets/momentary.h"
#include "widgets/slider.h"
#include "waypoint/waypoint.h"
#if DIAMOND
#include "diamond.h"
#endif
#include "datamgr.h"
int ConfigManager::port = -1;
int ConfigManager::udpSendPort = DEFAULT_SENDPORT;
char ConfigManager::udpSendAddr[256];
float ConfigManager::sendInterval = 2;
int ConfigManager::graphicalUpdateInterval=2000;
bool ConfigManager::inverse=false;
#if DIAMOND
QMap<DiamondTopicKey,QString> diamondMap;
QSet<QString> diamondSet; // set of topics subscribed
#endif
/// a structure to hold variable names and types for linkage
struct LinkedVarEntry {
char name[64];
int type;
float minVal,maxVal;
LinkedVarEntry(const char *n,int t,float mn,float mx){
strcpy(name,n);
type = t;
minVal=mn;
maxVal=mx;
}
};
/// used to hold a list of linked var names
static QList<LinkedVarEntry> linkedVars;
extern bool traceTokeniser;
/// tokeniser object
static Tokeniser tok;
class MyTError : public ITokeniserErrorHandler{
virtual void HandleTokeniserError(class Tokeniser *t){
throw UnexpException(t);
}
};
static MyTError tokerrorhandler;
/// handy function for creating a buffer of any type - mn and mx will be cast from float or just ignored,
/// whatever is appropriate for the type.
static RawDataBuffer *createVar(int type, const char *s, int size,float mn=0,float mx=1){
switch(type){
case T_NAMEFLOAT:
if(DataManager::findFloatBuffer(s))
throw Exception(tok.getline()).set("variable %s already exists",s);
else {
printf("Adding var %s, size %d, range (%f,%f)\n",s,size,mn,mx);
return DataManager::createFloatBuffer(s,size,mn,mx);
}
break;
default:
throw ParseException(&tok).set("unsupported type for variable %s",s);
}
return NULL;
}
static void parseAVar(bool diamond){
// it's a valid type
char buf[256];
bool autoRange=false;
float mn=0,mx=0;
int size;
RawDataBuffer *b;
tok.getnextident(buf); // get name
// if diamond, get topic and datum index
char tname[256];
int idx;
#if DIAMOND
if(diamond){
tok.getnextcheck(T_TOPIC);
tok.getnextstring(tname);
tok.getnextcheck(T_COMMA);
idx = tok.getnextint();
diamondMap[DiamondTopicKey(tname,idx)]=QString(buf);
diamondapparatus::subscribe(tname);
if(!diamondSet.contains(tname))
diamondSet.insert(tname);
}
#endif
size = tok.getnextint(); // and size
// now get the range
tok.getnextcheck(T_RANGE);
switch(tok.getnext()){
case T_INT:
case T_FLOAT:
mn = tok.getfloat();
tok.getnextcheck(T_TO);
mx = tok.getnextfloat();
break;
case T_AUTO:
autoRange=true;
break;
default:
throw UnexpException(&tok,"number or 'auto'");
break;
}
b=createVar(T_NAMEFLOAT,buf,size,mn,mx);
if(autoRange)
((DataBuffer<float>*)b)->setAutoRange();
}
void parseLinkedVars(bool diamond){
char buf[256];
int size,type;
RawDataBuffer *linkvar,*b;
if(tok.getnext()!=T_OPREN)
throw UnexpException(&tok,"( after linked");
linkedVars.clear();
for(;;){
double mn,mx;
char buf[256];
// get type
type = tok.getnext();
// get name
tok.getnextident(buf);
// if diamond, get topic and datum index
char tname[256];
int idx;
#if DIAMOND
if(diamond){
tok.getnextcheck(T_TOPIC);
tok.getnextstring(tname);
tok.getnextcheck(T_COMMA);
idx = tok.getnextint();
diamondMap[DiamondTopicKey(tname,idx)]=QString(buf);
if(!diamondSet.contains(tname))
diamondSet.insert(tname);
diamondapparatus::subscribe(tname);
}
#endif
// now the range
tok.getnextcheck(T_RANGE);
mn = tok.getnextfloat();
tok.getnextcheck(T_TO);
mx = tok.getnextfloat();
// add to a list!
linkedVars.append(LinkedVarEntry(buf,type,mn,mx));
if(tok.getnext()!=T_COMMA){
tok.rewind();
break;
}
}
if(tok.getnext()!=T_CPREN)
throw UnexpException(&tok,") after linked var list");
if(tok.getnext()!=T_INT)
throw UnexpException(&tok,"buffer size after linked var list");
size = tok.getint();
linkvar = NULL;
for(int i=0;i<linkedVars.size();i++){
b = createVar(linkedVars[i].type,
linkedVars[i].name,
size,
linkedVars[i].minVal,
linkedVars[i].maxVal);
if(linkvar)
linkvar->link(b);
else
linkvar = b;
}
}
/// parse a set of variable definitions of the form
/// \code
/// var <type> <name> <bufsize>, <type> <name> <bufsize>...
/// \endcode
/// also permits the creation of linked variables:
/// \code
/// var linked (<type> <name>,<type> <name>...) <bufsize>
/// \endcode
static void parseVars(){
tok.getnextcheck(T_OCURLY);
for(;;){
int t = tok.getnext();
switch(t){
case T_DIAMOND:
// diamond variables
#if !DIAMOND
throw ParseException(&tok,"diamond not supported in this build");
#else
if(!diamondapparatus::isRunning()){
diamondapparatus::init();
}
switch(t=tok.getnext()){
case T_NAMEFLOAT:
parseAVar(true);
break;
case T_LINKED:
parseLinkedVars(true);
break;
default:
throw UnexpException(&tok,"'float' or 'linked'");
break;
}
#endif
break;
case T_NAMEFLOAT:
parseAVar(false);
break;
case T_BOOL:
throw ParseException(&tok,"bools not currently supported");
break;
case T_LINKED:
parseLinkedVars(false);
break;
case T_CCURLY:
return;
default:printf("%d\n",tok.getcurrent());
throw UnexpException(&tok);
}
}
}
static void parseAudio(){
char warning[1024];
bool speech;
DataBuffer<float> *buf = ConfigManager::parseFloatSource();
switch(tok.getnext()){
case T_SAMPLE:speech=false;break;
case T_SPEECH:speech=true;break;
default:
throw UnexpException(&tok,"'speech' or 'sample'");
}
tok.getnextstring(warning);
getApp()->addAudio(warning,buf,speech);
}
static void parseFrame(QWidget *parent);
static void parseContainer(QWidget *parent){
// now start reading widgets
for(;;){
switch(tok.getnext()){
case T_FRAME:
parseFrame(parent);
break;
case T_GAUGE:
new Gauge(parent,&tok);
break;
case T_NUMBER:
new Number(parent,&tok);
break;
case T_COMPASS:
new Compass(parent,&tok);
break;
case T_GRAPH:
new Graph(parent,&tok);
break;
case T_MAP:
#if MARBLE
new MapWidget(parent,&tok);
#else
throw ParseException(&tok,"map not supported in this build");
#endif
break;
case T_STATUS:
new StatusBlockWrapper(parent,&tok);
break;
case T_SWITCH:
new Switch(parent,&tok);
break;
case T_MOMENTARY:
new Momentary(parent,&tok);
break;
case T_SLIDER:
new Slider(parent,&tok);
break;
case T_CCURLY:
return;
default:
throw UnexpException(&tok,"widget name");
}
}
}
static void parseFrame(QWidget *parent){
// first parse the pos block
ConfigRect pos = ConfigManager::parseRect();
// followed by some optional stuff
bool borderless=false;
int spacing=2;
bool done=false;
char label[256];
bool hasLabel=false;
while(!done){
switch(tok.getnext()){
case T_BORDERLESS:
borderless=true;
break;
case T_LABEL:
if(!tok.getnextstring(label))
throw UnexpException(&tok,"frame label");
hasLabel=true;
break;
case T_SPACING:
spacing=tok.getnextint();
break;
case T_OCURLY:
done = true;
break;
default:
throw UnexpException(&tok,"frame option or {");
}
}
// create frame and layout
QFrame *f = new QFrame;
f->setFrameStyle(borderless?
QFrame::NoFrame:
QFrame::Panel);
QGridLayout *l = new QGridLayout;
l->setSpacing(spacing);
f->setLayout(l);
parseContainer(f);
// if there's a label, we need a containing vbox
if(hasLabel){
QFrame *cont = new QFrame;
QVBoxLayout *bl = new QVBoxLayout;
cont->setLayout(bl);
QLabel *lab = new QLabel(label);
lab->setMaximumSize(10000,20);
bl->addWidget(lab);
bl->addWidget(f);
f=cont;
}
// add to the parent's layout
((QGridLayout*)parent->layout())->addWidget(f,pos.y,pos.x,pos.h,pos.w);
}
static void parseWindow(){
// option defaults
bool fullScreen = false; // should it be fullscreen?
bool disabled = false;
// what size? (default is fit around widgets. Ignored for fullscreen.)
int width=-1,height=-1;
// if set, move the window to a screen of the given dimensions
int swidth=-1,sheight=-1;
// title if any
char title[256];
// "tab" number - used to generate a shortcut to pull this window
// to the front
int number=-1;
title[0]=0;
int screensetline=-1;
// set this window to not inverse
ConfigManager::inverse=false;
// get window options
bool done = false;
while(!done){
switch(tok.getnext()){
case T_OCURLY:
done = true;
break;
case T_TITLE:
tok.getnextstring(title);
break;
case T_NUMBER:
number = tok.getnextint();
break;
case T_INVERSE:
ConfigManager::inverse=true;
break;
case T_FULLSCREEN:
fullScreen = true;
break;
case T_SIZE: // size of window if not fullscreen
width = tok.getnextint();
tok.getnextcheck(T_COMMA);
height = tok.getnextint();
break;
case T_SCREEN: // move to a screen of given dimensions
swidth = tok.getnextint();
tok.getnextcheck(T_COMMA);
screensetline = tok.getline();
sheight = tok.getnextint();
break;
case T_DISABLE: // the window is disabled and should be immediately closed
disabled=true;
break;
}
}
// create a window
Window *w = getApp()->createWindow();
if(number>=0)
getApp()->setWindowKey(number,w);
ConfigManager::setStyle(w);
// and parse the contents
parseContainer(w->centralWidget());
if(*title){
w->setWindowTitle(title);
}
// move the window if we want to
if(swidth>0){
QDesktopWidget *dt = QApplication::desktop();
QRect r;
int i;
for(i=0;i<dt->screenCount();i++){
r = dt->screenGeometry(i);
printf("Found display : %d x %d\n",r.width(),r.height());
if(r.width() == swidth && r.height()==sheight)
break;
}
if(i==dt->screenCount())
throw Exception(screensetline).set("could not find display of %d x %d",swidth,sheight);
w->move(r.topLeft());
}
// finally show the window and resize if required
if(disabled){
w->hide(); // marked "disabled" in the config
} else {
w->setWindowState(Qt::WindowActive);
w->raise();
w->activateWindow();
if(fullScreen){
w->showFullScreen();
} else {
if(width>0)
w->resize(width,height);
w->showNormal();
}
}
}
ConfigRect ConfigManager::parseRect(){
ConfigRect r;
r.x = tok.getnextint(); // either just x,y..
tok.getnextcheck(T_COMMA);
r.y = tok.getnextint();
if(tok.getnext()==T_COMMA){ // or x,y,w,h
r.w = tok.getnextint();
tok.getnextcheck(T_COMMA);
r.h = tok.getnextint();
}else{
tok.rewind();
r.w = 1; r.h = 1;
}
if(tok.getnext() == T_SIZE){
r.minsizex = tok.getnextint();
if(tok.getnext()==T_COMMA){
r.minsizey = tok.getnextint();
}else{
r.minsizey = r.minsizex;
tok.rewind();
}
}else
tok.rewind();
return r;
}
DataBuffer<float> *ConfigManager::parseFloatSource(){
DataBuffer<float> *b;
char buf[256];
switch(tok.getnext()){
case T_VAR:
tok.getnextident(buf);
b = DataManager::findFloatBuffer(buf);
if(!b)
throw ParseException(&tok).set("undefined variable '%s'",buf);
break;
case T_EXPR:
tok.getnextstring(buf);
// OK, we're going to lose a reference to this, but such is life.
// In this version we never delete expressions anyway.
try {
b = (new Expression(buf))->buffer;
} catch(Exception& e){
throw Exception(e,tok.getline());
}
// now parse the extra bits
tok.getnextcheck(T_RANGE);
float mn,mx;
switch(tok.getnext()){
case T_INT:
case T_FLOAT:
mn = tok.getfloat();
tok.getnextcheck(T_TO);
mx = tok.getnextfloat();
b->setMinMax(mn,mx);
break;
case T_AUTO:
b->setAutoRange();
break;
default:
throw UnexpException(&tok,"expected number or 'auto'");
}
break;
default:
throw UnexpException(&tok,"'var' or 'expr'");
}
return b;
}
static void parseWaypoint(){
tok.getnextcheck(T_OCURLY);
wpResetDefinitions();
for(;;){
char buf[256];
tok.getnextcheck(T_IDENT);
strcpy(buf,tok.getstring());
wpAddField(buf,tok.getnextfloat());
int t = tok.getnext();
if(t==T_CCURLY)
break;
else if(t!=T_COMMA)
throw UnexpException(&tok,"comma or '}'");
}
}
void ConfigManager::parseFile(QString fname){
tok.init();
if(traceTokeniser)
tok.settrace(true);
tok.seterrorhandler(&tokerrorhandler);
tok.settokens(tokens);
tok.setcommentlinesequence("#");
// read the entire file!
QFile file(fname);
if(!file.open(QIODevice::ReadOnly))
throw Exception().set("could not open config file");
QByteArray b = file.readAll();
if(b.isNull() || b.isEmpty())
throw Exception().set("could not read config file");
b.append((char)0);
file.close();
const char *data = b.constData();
tok.reset(data);
bool done = false;
while(!done){
// at the top level we parse frames and
// variables
int t = tok.getnext();
switch(t){
case T_VAR:
parseVars();
break;
case T_WINDOW:
parseWindow();
break;
case T_END:
done=true;
break;
case T_PORT:
port = tok.getnextint();
break;
case T_SENDPORT:
udpSendPort = tok.getnextint();
break;
// the UDP client now sets its address from the first packet received
// but this will override it
case T_SENDADDR:
tok.getnextstring(udpSendAddr);
UDPClient::getInstance()->setAddress(udpSendAddr);
break;
case T_VALIDTIME:
DataManager::dataValidInterval = tok.getnextfloat();
break;
case T_SENDINTERVAL:
sendInterval = tok.getnextfloat();
break;
case T_UPDATEINTERVAL:
graphicalUpdateInterval = tok.getnextfloat()*1000;
break;
case T_AUDIO:
parseAudio();
break;
case T_WAYPOINT:
parseWaypoint();
break;
default:
throw UnexpException(&tok,"'var', 'frame', config data or end of file");
}
}
}
QColor ConfigManager::parseColour(QColor deflt){
QColor c;
switch(tok.getnext()){
case T_IDENT:
case T_STRING:
c = QColor(tok.getstring());
return c;
break;
case T_DEFAULT:
return deflt;
default:
throw UnexpException(&tok,"colour name or \"#rgb\"");
}
}
NudgeType ConfigManager::parseNudgeType(){
switch(tok.getnext()){
case T_UP:
return UP;
case T_DOWN:
return DOWN;
case T_MIN:
return MIN;
case T_MAX:
return MAX;
case T_CENTRE:
return CENTRE;
default:
throw UnexpException(&tok,"nudge type (up, down, centre)");
}
}
void ConfigManager::setStyle(QWidget *w){
if(inverse)
w->setStyleSheet("background-color: white; color:black;");
else
w->setStyleSheet("background-color: black; color:white;");
}
/// set of 'nudgeable' widgets
static QHash<QString,Nudgeable *> nudgeables;
Nudgeable *ConfigManager::getNudgeable(const char *name){
QString key(name);
if(nudgeables.contains(key))
return nudgeables.value(key);
else
throw Exception().set("undefined widget output variable '%s'",name);
}
void ConfigManager::registerNudgeable(const char *name,Nudgeable *n){
QString key(name);
if(nudgeables.contains(key))
throw Exception().set("widget output variable '%s' already defined",name);
nudgeables.insert(key,n);
}