-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpiclock_messages.cpp
More file actions
469 lines (426 loc) · 13.1 KB
/
piclock_messages.cpp
File metadata and controls
469 lines (426 loc) · 13.1 KB
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
#include <b64/decode.h>
#include <memory>
#include <sstream>
#include <queue>
#include <nanovg.h>
#include "piclock_messages.h"
static std::shared_ptr<std::string> get_arg_p(const std::string & input, int index, bool bTerminated = true)
{
size_t start_index = 0;
while(index > 0)
{
auto found = input.find(':', start_index);
if(found == std::string::npos)
return std::shared_ptr<std::string>();
start_index = found + 1;
index--;
}
if(!bTerminated)
return std::make_shared<std::string>(input.substr(start_index));
size_t end = input.find(':',start_index);
if(end != std::string::npos)
end -= start_index;
return std::make_shared<std::string>(input.substr(start_index,end));
}
std::string get_arg(const std::string & input, int index, bool bTerminated)
{
auto ret = get_arg_p(input, index, bTerminated);
if(ret)
return *ret;
return std::string();
}
#ifndef TCP_TEST_CLIENT
//this is probably horribly inefficient, because it gets copied multiple times, but it does work, and
//it won't be called in our main display thread
static std::string get_arg_b64(const std::string & input, int index, bool bTerminated = true)
{
auto source = get_arg_p(input, index, bTerminated);
if(!source)
return std::string();
base64::decoder dec;
std::ostringstream oss;
std::istringstream iss(*source);
dec.decode(iss,oss);
return oss.str();
}
static int get_arg_int(const std::string &input, int index, bool bTerminated = true)
{
try
{
auto arg = get_arg_p(input, index, bTerminated);
if(arg && arg->size() > 0)
return std::stoi(*arg);
}
catch(...)
{
}
return 0;
}
static long get_arg_l(const std::string &input, int index, bool bTerminated = true)
{
try
{
auto arg = get_arg_p(input, index, bTerminated);
if(arg && arg->size() > 0)
return std::stol(*arg);
}
catch(...)
{
}
return 0;
}
static long long get_arg_ll(const std::string &input, int index, bool bTerminated = true)
{
try
{
auto arg = get_arg_p(input, index, bTerminated);
if(arg && arg->size() > 0)
return std::stoll(*arg);
}
catch(...)
{
}
return 0;
}
static VGfloat get_arg_f(const std::string &input, int index, bool bTerminated = true)
{
return std::stod(get_arg(input, index, bTerminated));
}
static std::shared_ptr<long long> get_arg_pll(const std::string &input, int index, bool bTerminated = true)
{
auto str = get_arg(input, index, bTerminated);
if(str.length() <= 0)
return std::shared_ptr<long long>();
return std::make_shared<long long>(std::stoull(str));
}
static bool get_arg_bool(const std::string &input, int index, bool bTerminated = true)
{
return get_arg_int(input, index, bTerminated) != 0;
}
std::shared_ptr<int> ClockMsg::ParseCmd(const std::string & message, std::string &cmd)
{
cmd = get_arg(message,0);
if(cmd.length() > 0 && isdigit(cmd[0]))
{
char * pEnd;
int ret = strtoul(cmd.c_str(), &pEnd, 10);
std::string newCmd = pEnd;
cmd = newCmd;
return std::make_shared<int>(ret);
}
else
{
return std::shared_ptr<int>();
}
}
ClockMsg_SetGPO::ClockMsg_SetGPO(const std::string & message)
{
gpoIndex = get_arg_int(message,1);
bValue = get_arg_bool(message,2);
}
ClockMsg_ClearImages::ClockMsg_ClearImages()
{}
ClockMsg_ClearFonts::ClockMsg_ClearFonts()
{}
ClockMsg_StoreFont::ClockMsg_StoreFont(const std::string & message)
:name(get_arg(message, 1)),
data(get_arg_b64(message, 2, false))
{
}
Magick::Blob ClockMsg_StoreImage::base64_to_blob(const std::string & message, int idx)
{
std::string data = get_arg_b64(message,2,false);
return Magick::Blob((void *)data.data(), data.size());
}
ClockMsg_StoreImage::ClockMsg_StoreImage(const std::string & message)
:name(get_arg(message, 1)),
pSourceBlob(std::make_shared<Magick::Blob>(base64_to_blob(message, 2))),
pParsedImage(std::make_shared<Magick::Image>(*pSourceBlob))
{
//ImageMagick does scan lines, etc in the opposite order to OpenVG, so flip vertically
pParsedImage->flip();
}
ClockMsg_SetGlobal::ClockMsg_SetGlobal(const std::string & message)
{
bLandscape = get_arg_bool(message,1);
bScreenSaver = get_arg_bool(message,2);
}
ClockMsg_SetFonts::ClockMsg_SetFonts(const std::string & message)
:tally(get_arg(message,1)),
tally_label(get_arg(message,2)),
status(get_arg(message,3)),
digital(get_arg(message,4)),
date(get_arg(message,5)),
hours(get_arg(message,6))
{
}
ClockMsg_Region::ClockMsg_Region(const std::shared_ptr<int> ®ion, const std::string & message)
{
if(region)
{
bHasRegionIndex = true;
regionIndex = *region;
}
}
ClockMsg_SetLayout::ClockMsg_SetLayout(const std::shared_ptr<int> ®ion, const std::string & message)
:ClockMsg_Region(region, message)
{
#define UPDATE_VAL(val,idx) (val) = get_arg_bool(message,(idx));
UPDATE_VAL(bAnalogueClock, 1)
UPDATE_VAL(bLegacyAnalogueClockLocal, 2)
UPDATE_VAL(bLegacyDigitalClockUTC, 3)
UPDATE_VAL(bLegacyDigitalClockLocal, 4)
UPDATE_VAL(bDate, 5)
UPDATE_VAL(bLegacyDateLocal, 6)
//Skip Landscape parameter, this is now global, still transmitted by driver for legacy devices
UPDATE_VAL(bNumbersPresent, 8);
UPDATE_VAL(bNumbersOutside, 9);
UPDATE_VAL(bSecondsSweep, 10);
#undef UPDATE_VAL
#define UPDATE_VAL(val,idx) (val) = get_arg(message,(idx));
UPDATE_VAL(sImageClockFace, 11);
UPDATE_VAL(sImageClockHours, 12);
UPDATE_VAL(sImageClockMinutes, 13);
UPDATE_VAL(sImageClockSeconds, 14);
#undef UPDATE_VAL
}
void ClockMsg_SetLayout::Dump()
{
fprintf(stderr, "Analogue %d, LegacyAnalogueLocal %d, LegacyDigitalUTC %d, LegacyDigitalLocal %d, Date %d, LegacyLegacyDateLocal %d, NumbersPresent %d, NumbersOutside %d\n", bAnalogueClock, bLegacyAnalogueClockLocal, bLegacyDigitalClockUTC, bLegacyDigitalClockLocal, bDate, bLegacyDateLocal, bNumbersPresent, bNumbersOutside);
}
ClockMsg_SetClocks::ClockMsg_SetClocks(const std::shared_ptr<int> ®ion, const std::string & message)
:ClockMsg_Region(region, message)
{
tzDate = get_arg(message, 1);
tzAnalogue = get_arg(message, 2);
int idx = 3;
while(true)
{
auto tz = get_arg_p(message, idx++);
auto label = get_arg_p(message, idx++);
if(tz && !tz->empty())
{
if(label)
{
tzDigitals.push_back(std::pair<std::string,std::string>(*tz, *label));
}
else
{
tzDigitals.push_back(std::pair<std::string,std::string>(*tz, ""));
}
}
else
{
break;
}
}
}
ClockMsg_SetLocation::ClockMsg_SetLocation(const std::shared_ptr<int> ®ion, const std::string & message)
:ClockMsg_Region(region, message)
{
#define UPDATE_VAL(val,idx) (val) = get_arg_f(message,(idx));
UPDATE_VAL(x, 1)
UPDATE_VAL(y, 2)
UPDATE_VAL(width, 3)
UPDATE_VAL(height, 4)
#undef UPDATE_VAL
}
ClockMsg_SetFontSizeZones::ClockMsg_SetFontSizeZones(const std::shared_ptr<int> ®ion, const std::string & message)
:ClockMsg_Region(region, message)
{
auto &data = *pData;
std::string region_prefix = std::to_string(regionIndex);
region_prefix = "R" + region_prefix;
int i = 0;
while(auto pRow = get_arg_p(message, ++i))
{
std::vector<std::string> cols;
size_t start_index = -1;
const std::string & row = *pRow;
do
{
start_index++;
size_t end_index = row.find(',',start_index);
size_t length;
if(end_index == std::string::npos)
length = std::string::npos;
else
length = end_index - start_index;
std::string newVal = row.substr(start_index, length);
if(newVal.empty() || newVal[0] != 'G')
newVal = region_prefix + newVal;
cols.push_back(newVal);
start_index = end_index;
}
while(start_index != std::string::npos);
//Trim any empty elements off the end of the list
unsigned newSize;
for(newSize = cols.size(); newSize > 0 && (cols[newSize - 1]).empty(); newSize--)
;
if(newSize != cols.size())
cols.resize(newSize);
data.push_back(cols);
}
//Trim any empty elements off the end of the outer list
unsigned newSize;
for(newSize = data.size(); newSize > 0 && (data[newSize - 1]).empty(); newSize--)
;
if(newSize != data.size())
data.resize(newSize);
}
ClockMsg_SetRegionCount::ClockMsg_SetRegionCount(const std::string & message)
{
iCount = get_arg_int(message,1);
}
ClockMsg_SetProfile::ClockMsg_SetProfile(const std::string & message)
{
sProfile = get_arg(message,1);
}
ClockMsg_RowCol_Generic::ClockMsg_RowCol_Generic(const std::shared_ptr<int> ®ion, const std::string & message)
:ClockMsg_Region(region, message)
{
m_iRow = get_arg_int(message,1);
m_iCol = get_arg_int(message,2);
}
ClockMsg_SetSize::ClockMsg_SetSize(const std::shared_ptr<int> ®ion, const std::string & message)
:ClockMsg_RowCol_Generic(region, message),iRows(m_iRow),iCols(m_iCol)
{
}
ClockMsg_SetRow::ClockMsg_SetRow(const std::shared_ptr<int> ®ion, const std::string & message)
:ClockMsg_RowCol_Generic(region, message),iRow(m_iRow),iCols(m_iCol)
{
}
ClockMsg_RowCol::ClockMsg_RowCol(const std::shared_ptr<int> ®ion, const std::string & message)
:ClockMsg_RowCol_Generic(region, message),iRow(m_iRow),iCol(m_iCol)
{
}
ClockMsg_SetIndicator::ClockMsg_SetIndicator(const std::shared_ptr<int> ®ion, const std::string & message, int textIndex)
:ClockMsg_RowCol(region, message)
{
colFg = TallyColour(get_arg(message, 3));
colBg = TallyColour(get_arg(message, 4));
sText = get_arg(message, textIndex, false);
}
ClockMsg_SetCountdown::ClockMsg_SetCountdown(const std::shared_ptr<int> ®ion, const std::string & message, bool bExtendedArguments)
:ClockMsg_SetIndicator(region, message, bExtendedArguments? 14 : 8)
{
colFg = TallyColour(get_arg(message, 3));
colBg = TallyColour(get_arg(message, 4));
std::chrono::seconds seconds_since_epoch(get_arg_ll(message, 5));
target = sys_clock_data(seconds_since_epoch);
target += std::chrono::microseconds(get_arg_l(message,6));
auto flash = get_arg_pll(message, 7);
if((bHasFlashLimit = (bool)flash))
iFlashLimit = *flash;
if(bExtendedArguments)
{
daysMode = get_arg_int(message, 8);
}
}
ClockMsg_SetTally::ClockMsg_SetTally(const std::shared_ptr<int> ®ion, const std::string & message)
:ClockMsg_SetIndicator(region, message, 5)
{
}
ClockMsg_SetLabel::ClockMsg_SetLabel(const std::shared_ptr<int> ®ion, const std::string & message)
:ClockMsg_RowCol(region, message)
{
sText = get_arg(message, 3, false);
}
std::shared_ptr<ClockMsg> ClockMsg_Parse(const std::string &msg)
{
//Strip any newlines or carriage returns off the end of the line, duplicating the input so we can modify it
std::string message = msg;
while(!message.empty() && (message.back() == '\r' || message.back() == '\n'))
{
message.pop_back();
}
std::string cmd;
auto region = ClockMsg::ParseCmd(message, cmd);
if(cmd == "SETGPO")
return std::make_shared<ClockMsg_SetGPO>(message);
if(cmd == "CLEARFONTS")
return std::make_shared<ClockMsg_ClearFonts>();
if(cmd == "CLEARIMAGES")
return std::make_shared<ClockMsg_ClearImages>();
if(cmd == "STOREIMAGE")
{
try
{
return std::make_shared<ClockMsg_StoreImage>(message);
}
catch(const std::exception &e)
{
std::cerr << "Caught exception handling STOREIMAGE \"" << e.what() << "\"\n";
return std::shared_ptr<ClockMsg>();
}
catch(...)
{
std::cerr << "Caught unknown exception handling STOREIMAGE\n";
return std::shared_ptr<ClockMsg>();
}
}
if(cmd == "STOREFONT")
{
try
{
return std::make_shared<ClockMsg_StoreFont>(message);
}
catch(const std::exception &e)
{
std::cerr << "Caught exception handling STOREFONT \"" << e.what() << "\"\n";
return std::shared_ptr<ClockMsg>();
}
catch(...)
{
std::cerr << "Caught unknown exception handling STOREFONT\n";
return std::shared_ptr<ClockMsg>();
}
}
if(cmd == "SETGLOBAL")
return std::make_shared<ClockMsg_SetGlobal>(message);
if(cmd == "SETREGIONCOUNT")
return std::make_shared<ClockMsg_SetRegionCount>(message);
if(cmd == "SETPROFILE")
return std::make_shared<ClockMsg_SetProfile>(message);
if(cmd == "SETFONTS")
return std::make_shared<ClockMsg_SetFonts>(message);
if(cmd == "SETLOCATION")
return std::make_shared<ClockMsg_SetLocation>(region, message);
if(cmd == "SETSIZE")
return std::make_shared<ClockMsg_SetSize>(region, message);
if(cmd == "SETROW")
return std::make_shared<ClockMsg_SetRow>(region, message);
if(cmd == "SETTALLY")
return std::make_shared<ClockMsg_SetTally>(region, message);
if(cmd == "SETCOUNTDOWN")
return std::make_shared<ClockMsg_SetCountdown>(region, message, false);
if(cmd == "SETCOUNTDOWNEXTENDED")
return std::make_shared<ClockMsg_SetCountdown>(region, message, true);
if(cmd == "SETLABEL")
return std::make_shared<ClockMsg_SetLabel>(region, message);
if(cmd == "SETLAYOUT")
return std::make_shared<ClockMsg_SetLayout>(region, message);
if(cmd == "SETCLOCKS")
return std::make_shared<ClockMsg_SetClocks>(region, message);
if(cmd == "SETFONTSIZEZONES")
return std::make_shared<ClockMsg_SetFontSizeZones>(region, message);
return std::shared_ptr<ClockMsg>();
}
void MessageQueue::Add(const std::shared_ptr<ClockMsg> & pMsg)
{
std::lock_guard<std::mutex> hold_lock(m_access_mutex);
m_queue.push(pMsg);
}
bool MessageQueue::Get(std::queue<std::shared_ptr<ClockMsg> > &output)
{
std::lock_guard<std::mutex> hold_lock(m_access_mutex);
if(!m_queue.empty())
{
output.swap(m_queue);
std::queue<std::shared_ptr<ClockMsg> > empty_q;
m_queue.swap(empty_q);
return true;
}
return false;
}
#endif