-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathoverallstate.cpp
More file actions
346 lines (323 loc) · 9.23 KB
/
overallstate.cpp
File metadata and controls
346 lines (323 loc) · 9.23 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
#include "overallstate.h"
#include "countdownclock.h"
#include "globals.h"
#include "gpio.h"
#include <set>
bool OverallState::RotationReqd(VGfloat width, VGfloat height) const
{
if(m_bLandscape)
return width < height;
else
return width > height;
}
bool OverallState::Landscape() const
{
return m_bLandscape;
}
bool OverallState::ScreenSaver() const
{
return m_bScreenSaver;
}
const std::string & OverallState::FontTally() const
{
return font_Tally;
}
const std::string & OverallState::FontTally(bool bIsDigitalClock) const
{
return bIsDigitalClock? font_Digital: font_Tally;
}
const std::string & OverallState::FontTallyLabel() const
{
return font_TallyLabel;
}
const std::string & OverallState::FontStatus() const
{
return font_Status;
}
const std::string & OverallState::FontDigital() const
{
return font_Digital;
}
const std::string & OverallState::FontDate() const
{
return font_Date;
}
const std::string & OverallState::FontHours() const
{
return font_Hours;
}
void OverallState::UpdateFromMessage(const std::shared_ptr<ClockMsg_SetGlobal> &pMsg)
{
UpdateFromMessage(*pMsg);
}
bool OverallState::UpdateFromMessage(const std::shared_ptr<ClockMsg_SetFonts> &pMsg)
{
return UpdateFromMessage(*pMsg);
}
void OverallState::UpdateFromMessage(const ClockMsg_SetGlobal &message)
{
m_bLandscape = message.bLandscape;
m_bScreenSaver = message.bScreenSaver;
}
bool OverallState::updateFont(std::string & target, const std::string & newVal, const std::string & defaultVal)
{
if(newVal.size() > 0 && FontData.find(newVal) != FontData.end())
{
if(target == newVal)
return false;
target = newVal;
return true;
}
if(target == defaultVal)
return false;
target = defaultVal;
return true;
}
void OverallState::resetFonts(NVGcontext *vg, const std::string &remove_font)
{
nvgClearFonts(vg);
if(remove_font.size() > 0)
{
FontData.erase(remove_font);
}
else
{
FontData.clear();
}
//Standard default fonts
nvgCreateFont(vg, FONT_SERIF,"/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf");
nvgCreateFont(vg, FONT_SANS,"/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf");
nvgCreateFont(vg, FONT_MONO,"/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf");
//Fonts that weren't removed
for(const auto & pair : FontData)
{
nvgCreateFontMem(vg, pair.first.c_str(), (unsigned char *)pair.second.data(), pair.second.size(), 0);
}
}
void OverallState::NvgInit(NVGcontext *vg)
{
resetFonts(vg,std::string());
}
bool OverallState::UpdateFromMessage(const ClockMsg_SetFonts &message)
{
//Use bitwise or to force full evaluation
bool bChanged = updateFont(font_Tally, message.tally, DEFAULT_FONT_TALLY);
bChanged = updateFont(font_TallyLabel, message.tally_label, font_Tally) || bChanged;
bChanged = updateFont(font_Status, message.status, font_Tally) || bChanged;
bChanged = updateFont(font_Digital, message.digital, DEFAULT_FONT_DIGITAL) || bChanged;
bChanged = updateFont(font_Date, message.date, DEFAULT_FONT_DATE) || bChanged;
bChanged = updateFont(font_Hours, message.hours, DEFAULT_FONT_HOURS) || bChanged;
return bChanged;
}
void OverallState::SetLandscape(bool bLandscape)
{
m_bLandscape = bLandscape;
}
bool OverallState::HandleClockMessages(NVGcontext *vg, std::queue<std::shared_ptr<ClockMsg> > &msgs, const sys_clock_data & now)
{
bool bSizeChanged = false;
while(!msgs.empty())
{
auto pMsg = msgs.front();
msgs.pop();
if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetGPO>(pMsg))
{
write_gpo(castCmd->gpoIndex, castCmd->bValue);
continue;
}
if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetGlobal>(pMsg))
{
UpdateFromMessage(castCmd);
continue;
}
if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetFonts>(pMsg))
{
if(UpdateFromMessage(castCmd))
{
for(auto &item :Regions)
{
item.second->ForceRecalc();
}
bSizeChanged = true;
}
continue;
}
int regionIndex = 0;
bool bMaxRegion = false;
#define UPDATE_CHECK(target, source) { auto tmp = (source); bSizeChanged = bSizeChanged || ((target) != tmp); (target) = tmp;}
if(auto regionCmd = std::dynamic_pointer_cast<ClockMsg_Region>(pMsg))
{
if(regionCmd->bHasRegionIndex)
regionIndex = regionCmd->regionIndex;
else
{
if(UpdateRegionCount(1))
{
bSizeChanged = true;
}
bMaxRegion = true;
}
}
if(!Regions[regionIndex])
{
Regions[regionIndex] = std::make_shared<RegionState>();
}
std::shared_ptr<RegionState> pRS = Regions[regionIndex];
if(bMaxRegion && (pRS->x() != 0.0f || pRS->y() != 1.0f || pRS->width() != 1.0f || pRS->height() != 1.0f))
{
bSizeChanged = true;
pRS->UpdateFromMessage(std::make_shared<ClockMsg_SetLocation>(std::make_shared<int>(regionIndex), "SETLOCATION:0:0:1:1"));
}
if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_ClearImages>(pMsg))
{
Images.clear();
}
else if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_ClearFonts>(pMsg))
{
resetFonts(vg, std::string());
}
else if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_StoreImage>(pMsg))
{
if(!Images[castCmd->name].IsSameSource(castCmd->pSourceBlob))
Images[castCmd->name] = ScalingImage(castCmd->pParsedImage, castCmd->pSourceBlob);
}
else if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_StoreFont>(pMsg))
{
const auto iter = FontData.find(castCmd->name);
if(iter != FontData.end())
{
//If it's identical to the font we already have, ignore it
if(iter->second == castCmd->data)
{
continue;
}
//To delete the old font we have to reset all the fonts and not re-add it
resetFonts(vg, castCmd->name);
}
FontData[castCmd->name] = castCmd->data;
auto & data = FontData[castCmd->name];
nvgCreateFontMem(vg, castCmd->name.c_str(), (unsigned char *)data.data(), data.size(), 0);
}
else if(auto castCmd = std::dynamic_pointer_cast<ResizedImage>(pMsg))
{
Images[castCmd->Name].UpdateFromResize(castCmd);
}
else if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetRegionCount>(pMsg))
{
if(UpdateRegionCount(castCmd->iCount))
{
bSizeChanged = true;
}
}
else if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetSize>(pMsg))
{
UPDATE_CHECK(pRS->TD.nRows, castCmd->iRows)
UPDATE_CHECK(pRS->TD.nCols_default, castCmd->iCols)
}
else if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetLocation>(pMsg))
{
if(pRS->UpdateFromMessage(castCmd))
{
bSizeChanged = true;
}
}
else if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetRow>(pMsg))
{
UPDATE_CHECK(pRS->TD.nCols[castCmd->iRow], castCmd->iCols);
}
else if(auto indCmd = std::dynamic_pointer_cast<ClockMsg_SetIndicator>(pMsg))
{
std::shared_ptr<TallyState> pNewState;
int row = indCmd->iRow;
int col = indCmd->iCol;
auto pOldState = pRS->TD.displays[row][col];
if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetCountdown>(pMsg))
{
pNewState = std::make_shared<CountdownClock>(castCmd);
}
else if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetTally>(pMsg))
{
pNewState = std::make_shared<SimpleTallyState>(castCmd, pOldState);
}
if(!pNewState || !pOldState)
bSizeChanged = true;
else if(!bSizeChanged)
{
auto textOld = pOldState->Text(now);
auto textNew = pNewState->Text(now);
bool bDigitalOld = pOldState->IsDigitalClock();
bool bDigitalNew = pNewState->IsDigitalClock();
if(bDigitalOld != bDigitalNew)
bSizeChanged = true;
else if((bool)textOld != (bool)textNew)
bSizeChanged = true;
else if(textOld && textNew)
{
if(bDigitalNew)
{
if(textOld->size() != textNew->size())
bSizeChanged = true;
}
else if(*textOld != *textNew)
bSizeChanged = true;
}
}
pRS->TD.displays[row][col] = pNewState;
}
else if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetLabel>(pMsg))
{
int row = castCmd->iRow;
int col = castCmd->iCol;
if(pRS->TD.displays[row][col])
{
auto pNew = pRS->TD.displays[row][col]->SetLabel(castCmd->sText);
//Returns an empty pointer if the label is the same (ie no change required)
if(pNew)
{
bSizeChanged = true;
pRS->TD.displays[row][col] = pNew;
}
}
}
else if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetProfile>(pMsg))
{
//No size calculation depends upon the setting of this string, so just store it.
pRS->TD.sProfName = castCmd->sProfile;
}
else if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetLayout>(pMsg))
{
pRS->UpdateFromMessage(castCmd);
}
else if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetClocks>(pMsg))
{
pRS->UpdateFromMessage(castCmd);
}
else if(auto castCmd = std::dynamic_pointer_cast<ClockMsg_SetFontSizeZones>(pMsg))
{
bSizeChanged |= pRS->UpdateFromMessage(castCmd);
}
else
{
fprintf(stderr, "IMPOSSIBLE HAPPENED!! Received unknown command in message queue: %s\n",typeid(pMsg.get()).name());
}
}
return bSizeChanged;
}
bool OverallState::UpdateRegionCount(int newCount)
{
std::set<int> removeIndices;
for(const auto & item: Regions)
{
if(item.first >= newCount || item.first < 0)
removeIndices.insert(item.first);
}
if(!removeIndices.empty())
{
for(int index: removeIndices)
{
Regions.erase(index);
}
return true;
}
return false;
}