forked from rainmeter/rainmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConfigParser.cpp
2161 lines (1898 loc) · 55.6 KB
/
ConfigParser.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
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (C) 2004 Rainmeter Project Developers
*
* This Source Code Form is subject to the terms of the GNU General Public
* License; either version 2 of the License, or (at your option) any later
* version. If a copy of the GPL was not distributed with this file, You can
* obtain one at <https://www.gnu.org/licenses/gpl-2.0.html>. */
#include "StdAfx.h"
#include "../Common/MathParser.h"
#include "../Common/PathUtil.h"
#include "ConfigParser.h"
#include "Util.h"
#include "Rainmeter.h"
#include "System.h"
#include "Measure.h"
#include "MeasurePlugin.h"
#include "MeasureScript.h"
#include "MeasureTime.h"
#include "Meter.h"
#include "resource.h"
namespace {
struct PairInfo
{
const WCHAR begin;
const WCHAR end;
};
const std::unordered_map<PairedPunctuation, PairInfo> s_PairedPunct =
{
{ PairedPunctuation::SingleQuote, { L'\'', L'\'' } },
{ PairedPunctuation::DoubleQuote, { L'"', L'"' } },
{ PairedPunctuation::BothQuotes, { L'"', L'\'' } },
{ PairedPunctuation::Parentheses, { L'(', L')' } },
{ PairedPunctuation::Brackets, { L'[', L']' } },
{ PairedPunctuation::Braces, { L'{', L'}' } },
{ PairedPunctuation::Guillemet, { L'<', L'>' } }
};
} // namespace
std::unordered_map<std::wstring, std::wstring> ConfigParser::c_MonitorVariables;
std::unordered_map<ConfigParser::VariableType, WCHAR> ConfigParser::c_VariableMap;
ConfigParser::ConfigParser() :
m_LastReplaced(false),
m_LastDefaultUsed(false),
m_LastValueDefined(false),
m_CurrentSection(),
m_Skin()
{
if (c_VariableMap.empty())
{
c_VariableMap.emplace(VariableType::Section, L'&');
c_VariableMap.emplace(VariableType::Variable, L'#');
c_VariableMap.emplace(VariableType::Mouse, L'$');
c_VariableMap.emplace(VariableType::CharacterReference, L'\\');
}
}
ConfigParser::~ConfigParser()
{
}
void ConfigParser::Initialize(const std::wstring& filename, Skin* skin, LPCTSTR skinSection, const std::wstring* resourcePath)
{
m_Skin = skin;
m_Measures.clear();
m_Sections.clear();
m_Values.clear();
m_BuiltInVariables.clear();
m_Variables.clear();
m_OriginalVariableNames.clear();
m_StyleTemplate.clear();
m_LastReplaced = false;
m_LastDefaultUsed = false;
m_LastValueDefined = false;
m_CurrentSection = nullptr;
m_SectionInsertPos = m_Sections.end();
// Set the built-in variables. Do this before the ini file is read so that the paths can be used with @include
SetBuiltInVariables(filename, resourcePath, skin);
ResetMonitorVariables(skin);
System::UpdateIniFileMappingList();
ReadIniFile(filename, skinSection);
ReadVariables();
// Clear and minimize
m_FoundSections.clear();
m_ListVariables.clear();
m_SectionInsertPos = m_Sections.end();
}
void ConfigParser::SetBuiltInVariables(const std::wstring& filename, const std::wstring* resourcePath, Skin* skin)
{
auto insertVariable = [&](const WCHAR* name, std::wstring value)
{
return m_BuiltInVariables.emplace(name, value);
};
insertVariable(L"PROGRAMPATH", GetRainmeter().GetPath());
insertVariable(L"PROGRAMDRIVE", GetRainmeter().GetDrive());
insertVariable(L"SETTINGSPATH", GetRainmeter().GetSettingsPath());
insertVariable(L"SKINSPATH", GetRainmeter().GetSkinPath());
insertVariable(L"PLUGINSPATH", GetRainmeter().GetPluginPath());
insertVariable(L"CURRENTPATH", PathUtil::GetFolderFromFilePath(filename));
insertVariable(L"ADDONSPATH", GetRainmeter().GetAddonPath());
insertVariable(L"CONFIGEDITOR", GetRainmeter().GetSkinEditor());
if (skin)
{
insertVariable(L"CURRENTFILE", skin->GetFileName());
insertVariable(L"CURRENTCONFIG", skin->GetFolderPath());
insertVariable(L"ROOTCONFIG", skin->GetRootName());
insertVariable(L"ROOTCONFIGPATH", skin->GetRootPath());
}
insertVariable(L"CRLF", L"\n");
m_CurrentSection = &(insertVariable(L"CURRENTSECTION", L"").first->second); // shortcut
if (resourcePath)
{
SetVariable(L"@", *resourcePath);
}
}
/*
** Sets all user-defined variables.
**
*/
void ConfigParser::ReadVariables()
{
std::list<std::wstring>::const_iterator iter = m_ListVariables.begin();
for ( ; iter != m_ListVariables.end(); ++iter)
{
SetVariable((*iter), ReadString(L"Variables", (*iter).c_str(), L"", false));
}
}
void ConfigParser::SetVariable(std::wstring strVariable, const std::wstring& strValue)
{
std::wstring original = strVariable;
StrToUpperC(strVariable);
m_Variables[strVariable] = strValue;
if (m_OriginalVariableNames.find(strVariable) == m_OriginalVariableNames.end())
{
m_OriginalVariableNames[strVariable] = original;
}
}
void ConfigParser::SetBuiltInVariable(const std::wstring& strVariable, const std::wstring& strValue)
{
m_BuiltInVariables[strVariable] = strValue;
}
/*
** Gets a value for the variable. Returns nullptr if not found.
**
*/
const std::wstring* ConfigParser::GetVariable(const std::wstring& strVariable)
{
const std::wstring strTmp = StrToUpper(strVariable);
// #1: Built-in variables
std::unordered_map<std::wstring, std::wstring>::const_iterator iter = m_BuiltInVariables.find(strTmp);
if (iter != m_BuiltInVariables.end())
{
return &(*iter).second;
}
// #2: Monitor variables
iter = c_MonitorVariables.find(strTmp);
if (iter != c_MonitorVariables.end())
{
return &(*iter).second;
}
// #3: User-defined variables
iter = m_Variables.find(strTmp);
if (iter != m_Variables.end())
{
return &(*iter).second;
}
return nullptr;
}
const std::wstring* ConfigParser::GetVariableOriginalName(const std::wstring& strVariable)
{
const std::wstring strTmp = StrToUpper(strVariable);
// User-defined variables
std::unordered_map<std::wstring, std::wstring>::const_iterator iter = m_OriginalVariableNames.find(strTmp);
if (iter != m_OriginalVariableNames.end())
{
return &(*iter).second;
}
return nullptr;
}
/*
** Gets the value of a section variable. Returns true if strValue is set.
** The selector is stripped from strVariable.
**
*/
bool ConfigParser::GetSectionVariable(std::wstring& strVariable, std::wstring& strValue, void* logEntry)
{
if (!m_Skin) return false;
const size_t firstParens = strVariable.find_first_of(L'('); // Assume section names do not have a left parenthesis?
size_t colonPos = strVariable.find_last_of(L':', firstParens);
if (colonPos == std::wstring::npos)
{
return false;
}
const std::wstring selector = strVariable.substr(colonPos + 1);
const WCHAR* selectorSz = selector.c_str();
strVariable.resize(colonPos);
bool isKeySelector = (!selector.empty() && iswalpha(selectorSz[0]));
if (isKeySelector)
{
// [Meter:X], [Meter:Y], [Meter:W], [Meter:H]
Meter* meter = m_Skin->GetMeter(strVariable);
if (meter)
{
WCHAR buffer[32] = { 0 };
if (_wcsicmp(selectorSz, L"X") == 0)
{
_itow_s(meter->GetX(), buffer, 10);
}
else if (_wcsicmp(selectorSz, L"Y") == 0)
{
_itow_s(meter->GetY(), buffer, 10);
}
else if (_wcsicmp(selectorSz, L"W") == 0)
{
_itow_s(meter->GetW(), buffer, 10);
}
else if (_wcsicmp(selectorSz, L"H") == 0)
{
_itow_s(meter->GetH(), buffer, 10);
}
else if (_wcsicmp(selectorSz, L"XW") == 0)
{
_itow_s(meter->GetX() + meter->GetW(), buffer, 10);
}
else if (_wcsicmp(selectorSz, L"YH") == 0)
{
_itow_s(meter->GetY() + meter->GetH(), buffer, 10);
}
else
{
return false;
}
strValue = buffer;
return true;
}
}
// Number: [Measure:], [Measure:dec]
// Percentual: [Measure:%], [Measure:%, dec]
// Scale: [Measure:/scale], [Measure:/scale, dec]
// Max/Min: [Measure:MaxValue], [Measure:MaxValue:/scale, dec] ('%' cannot be used)
// EscapeRegExp: [Measure:EscapeRegExp] (Escapes regular expression syntax, used for 'IfMatch')
// EncodeUrl: [Measure:EncodeUrl] (Escapes URL reserved characters)
// TimeStamp: [TimeMeasure:TimeStamp] (ONLY for Time measures, returns the Windows timestamp of the measure)
// Script: [ScriptMeasure:SomeFunction()], [ScriptMeasure:Something('Something')]
// NOTE: Parenthesis are required. Arguments enclosed in single or double quotes are treated as strings, otherwise
// they are treated as numbers. If the lua function returns a number, it will be converted to a string.
enum class ValueType
{
Raw,
Percentual,
Max,
Min,
EscapeRegExp,
EncodeUrl,
TimeStamp,
Script,
Plugin
} valueType = ValueType::Raw;
if (isKeySelector)
{
if (_wcsicmp(selectorSz, L"MaxValue") == 0)
{
valueType = ValueType::Max;
}
else if (_wcsicmp(selectorSz, L"MinValue") == 0)
{
valueType = ValueType::Min;
}
else if (_wcsicmp(selectorSz, L"EscapeRegExp") == 0)
{
valueType = ValueType::EscapeRegExp;
}
else if (_wcsicmp(selectorSz, L"EncodeUrl") == 0)
{
valueType = ValueType::EncodeUrl;
}
else if (_wcsicmp(selectorSz, L"TimeStamp") == 0)
{
valueType = ValueType::TimeStamp;
}
else
{
// Check if calling a Script/Plugin measure
Measure* measure = m_Skin->GetMeasure(strVariable);
if (!measure) return false;
// Lua (and possibly plugins) can reset the style template when
// reading values, so save the style template here and reset it
// back after the lua/plugin has returned.
std::vector<std::wstring> meterStyle = m_StyleTemplate;
bool retValue = false;
const auto type = measure->GetTypeID();
if (type == TypeID<MeasureScript>())
{
valueType = ValueType::Script; // Needed?
MeasureScript* script = (MeasureScript*)measure;
retValue = script->CommandWithReturn(selectorSz, strValue, logEntry);
}
else if (type == TypeID<MeasurePlugin>())
{
valueType = ValueType::Plugin; // Needed?
MeasurePlugin* plugin = (MeasurePlugin*)measure;
retValue = plugin->CommandWithReturn(selectorSz, strValue, logEntry);
}
m_StyleTemplate = meterStyle;
return retValue;
}
selectorSz = L"";
}
else
{
colonPos = strVariable.find_last_of(L':');
if (colonPos != std::wstring::npos)
{
do
{
const WCHAR* keySelectorSz = strVariable.c_str() + colonPos + 1ULL;
if (_wcsicmp(keySelectorSz, L"MaxValue") == 0)
{
valueType = ValueType::Max;
}
else if (_wcsicmp(keySelectorSz, L"MinValue") == 0)
{
valueType = ValueType::Min;
}
else
{
// Section name contains ':' ?
break;
}
strVariable.resize(colonPos);
}
while (0);
}
}
Measure* measure = m_Skin->GetMeasure(strVariable);
if (measure)
{
if (valueType == ValueType::EscapeRegExp)
{
const WCHAR* tmp = measure->GetStringValue();
strValue = tmp ? tmp : L"";
StringUtil::EscapeRegExp(strValue);
return true;
}
else if (valueType == ValueType::EncodeUrl)
{
const WCHAR* tmp = measure->GetStringValue();
strValue = tmp ? tmp : L"";
StringUtil::EncodeUrl(strValue);
return true;
}
else if (measure->GetTypeID() == TypeID<MeasureTime>() && valueType == ValueType::TimeStamp)
{
MeasureTime* time = (MeasureTime*)measure;
strValue = std::to_wstring(time->GetTimeStamp().QuadPart / 10000000LL);
return true;
}
int scale = 1;
const WCHAR* decimalsSz = wcschr(selectorSz, L',');
if (decimalsSz)
{
++decimalsSz;
}
if (*selectorSz == L'%') // Percentual
{
if (valueType == ValueType::Max || valueType == ValueType::Min)
{
// '%' cannot be used with Max/Min values.
return false;
}
valueType = ValueType::Percentual;
}
else if (*selectorSz == L'/') // Scale
{
errno = 0;
scale = _wtoi(selectorSz + 1);
if (errno == EINVAL || scale == 0)
{
// Invalid scale value.
return false;
}
}
else
{
if (decimalsSz)
{
return false;
}
decimalsSz = selectorSz;
}
const double value =
(valueType == ValueType::Percentual) ? measure->GetRelativeValue() * 100.0 :
(valueType == ValueType::Max) ? measure->GetMaxValue() / scale :
(valueType == ValueType::Min) ? measure->GetMinValue() / scale :
measure->GetValue() / scale;
int decimals = 10;
if (decimalsSz)
{
while (iswspace(*decimalsSz)) ++decimalsSz;
if (*decimalsSz)
{
decimals = _wtoi(decimalsSz);
decimals = max(0, decimals);
decimals = min(32, decimals);
}
else
{
decimalsSz = nullptr;
}
}
WCHAR format[32] = { 0 };
WCHAR buffer[128] = { 0 };
_snwprintf_s(format, _TRUNCATE, L"%%.%if", decimals);
int bufferLen = _snwprintf_s(buffer, _TRUNCATE, format, value);
if (!decimalsSz)
{
// Remove trailing zeros if decimal count was not specified.
measure->RemoveTrailingZero(buffer, bufferLen);
bufferLen = (int)wcslen(buffer);
}
strValue.assign(buffer, bufferLen);
return true;
}
return false;
}
void ConfigParser::ResetMonitorVariables(Skin* skin)
{
// Set the SCREENAREA/WORKAREA variables
if (c_MonitorVariables.empty())
{
SetMultiMonitorVariables(true);
}
// Set the SCREENAREA/WORKAREA variables for present monitor
SetAutoSelectedMonitorVariables(skin);
}
/*
** Sets new values for the SCREENAREA/WORKAREA variables.
**
*/
void ConfigParser::SetMultiMonitorVariables(bool reset)
{
auto setMonitorVariable = [&](const WCHAR* variable, const WCHAR* value)
{
c_MonitorVariables[variable] = value;
};
if (!reset && c_MonitorVariables.empty())
{
reset = true; // Set all variables
}
const size_t numOfMonitors = System::GetMonitorCount(); // intentional
const MultiMonitorInfo& monitorsInfo = System::GetMultiMonitorInfo();
const std::vector<MonitorInfo>& monitors = monitorsInfo.monitors;
WCHAR buffer[32] = { 0 };
const int monitorIndex = monitorsInfo.primary - 1;
const RECT workArea = monitors[monitorIndex].work;
const RECT scrArea = monitors[monitorIndex].screen;
_itow_s(workArea.left, buffer, 10);
setMonitorVariable(L"WORKAREAX", buffer);
setMonitorVariable(L"PWORKAREAX", buffer);
_itow_s(workArea.top, buffer, 10);
setMonitorVariable(L"WORKAREAY", buffer);
setMonitorVariable(L"PWORKAREAY", buffer);
_itow_s(workArea.right - workArea.left, buffer, 10);
setMonitorVariable(L"WORKAREAWIDTH", buffer);
setMonitorVariable(L"PWORKAREAWIDTH", buffer);
_itow_s(workArea.bottom - workArea.top, buffer, 10);
setMonitorVariable(L"WORKAREAHEIGHT", buffer);
setMonitorVariable(L"PWORKAREAHEIGHT", buffer);
if (reset)
{
_itow_s(scrArea.left, buffer, 10);
setMonitorVariable(L"SCREENAREAX", buffer);
setMonitorVariable(L"PSCREENAREAX", buffer);
_itow_s(scrArea.top, buffer, 10);
setMonitorVariable(L"SCREENAREAY", buffer);
setMonitorVariable(L"PSCREENAREAY", buffer);
_itow_s(scrArea.right - scrArea.left, buffer, 10);
setMonitorVariable(L"SCREENAREAWIDTH", buffer);
setMonitorVariable(L"PSCREENAREAWIDTH", buffer);
_itow_s(scrArea.bottom - scrArea.top, buffer, 10);
setMonitorVariable(L"SCREENAREAHEIGHT", buffer);
setMonitorVariable(L"PSCREENAREAHEIGHT", buffer);
_itow_s(monitorsInfo.vsL, buffer, 10);
setMonitorVariable(L"VSCREENAREAX", buffer);
_itow_s(monitorsInfo.vsT, buffer, 10);
setMonitorVariable(L"VSCREENAREAY", buffer);
_itow_s(monitorsInfo.vsW, buffer, 10);
setMonitorVariable(L"VSCREENAREAWIDTH", buffer);
_itow_s(monitorsInfo.vsH, buffer, 10);
setMonitorVariable(L"VSCREENAREAHEIGHT", buffer);
}
int i = 1;
for (auto iter = monitors.cbegin(); iter != monitors.cend(); ++iter, ++i)
{
WCHAR buffer2[64] = { 0 };
const RECT work = ((*iter).active) ? (*iter).work : workArea;
_itow_s(work.left, buffer, 10);
_snwprintf_s(buffer2, _TRUNCATE, L"WORKAREAX@%i", i);
setMonitorVariable(buffer2, buffer);
_itow_s(work.top, buffer, 10);
_snwprintf_s(buffer2, _TRUNCATE, L"WORKAREAY@%i", i);
setMonitorVariable(buffer2, buffer);
_itow_s(work.right - work.left, buffer, 10);
_snwprintf_s(buffer2, _TRUNCATE, L"WORKAREAWIDTH@%i", i);
setMonitorVariable(buffer2, buffer);
_itow_s(work.bottom - work.top, buffer, 10);
_snwprintf_s(buffer2, _TRUNCATE, L"WORKAREAHEIGHT@%i", i);
setMonitorVariable(buffer2, buffer);
if (reset)
{
const RECT screen = ((*iter).active) ? (*iter).screen : scrArea;
_itow_s(screen.left, buffer, 10);
_snwprintf_s(buffer2, _TRUNCATE, L"SCREENAREAX@%i", i);
setMonitorVariable(buffer2, buffer);
_itow_s(screen.top, buffer, 10);
_snwprintf_s(buffer2, _TRUNCATE, L"SCREENAREAY@%i", i);
setMonitorVariable(buffer2, buffer);
_itow_s(screen.right - screen.left, buffer, 10);
_snwprintf_s(buffer2, _TRUNCATE, L"SCREENAREAWIDTH@%i", i);
setMonitorVariable(buffer2, buffer);
_itow_s(screen.bottom - screen.top, buffer, 10);
_snwprintf_s(buffer2, _TRUNCATE, L"SCREENAREAHEIGHT@%i", i);
setMonitorVariable(buffer2, buffer);
}
}
}
/*
** Sets new SCREENAREA/WORKAREA variables for present monitor.
**
*/
void ConfigParser::SetAutoSelectedMonitorVariables(Skin* skin)
{
if (skin)
{
const int numOfMonitors = (int)System::GetMonitorCount();
const MultiMonitorInfo& monitorsInfo = System::GetMultiMonitorInfo();
const std::vector<MonitorInfo>& monitors = monitorsInfo.monitors;
WCHAR buffer[32] = { 0 };
int w1 = 0, w2 = 0, s1 = 0, s2 = 0;
int screenIndex = 0;
// Set X / WIDTH
screenIndex = monitorsInfo.primary;
if (skin->GetXScreenDefined())
{
int i = skin->GetXScreen();
const int index = i - 1;
if (i >= 0 && (i == 0 || i <= numOfMonitors && monitors[index].active))
{
screenIndex = i;
}
}
if (screenIndex == 0)
{
s1 = w1 = monitorsInfo.vsL;
s2 = w2 = monitorsInfo.vsW;
}
else
{
const int monitorIndex = screenIndex - 1;
w1 = monitors[monitorIndex].work.left;
w2 = monitors[monitorIndex].work.right - monitors[monitorIndex].work.left;
s1 = monitors[monitorIndex].screen.left;
s2 = monitors[monitorIndex].screen.right - monitors[monitorIndex].screen.left;
}
_itow_s(w1, buffer, 10);
SetBuiltInVariable(L"WORKAREAX", buffer);
_itow_s(w2, buffer, 10);
SetBuiltInVariable(L"WORKAREAWIDTH", buffer);
_itow_s(s1, buffer, 10);
SetBuiltInVariable(L"SCREENAREAX", buffer);
_itow_s(s2, buffer, 10);
SetBuiltInVariable(L"SCREENAREAWIDTH", buffer);
// Set Y / HEIGHT
screenIndex = monitorsInfo.primary;
if (skin->GetYScreenDefined())
{
const int i = skin->GetYScreen();
const int index = i - 1;
if (i >= 0 && (i == 0 || i <= numOfMonitors && monitors[index].active))
{
screenIndex = i;
}
}
if (screenIndex == 0)
{
s1 = w1 = monitorsInfo.vsL;
s2 = w2 = monitorsInfo.vsW;
}
else
{
const int monitorIndex = screenIndex - 1;
w1 = monitors[monitorIndex].work.top;
w2 = monitors[monitorIndex].work.bottom - monitors[monitorIndex].work.top;
s1 = monitors[monitorIndex].screen.top;
s2 = monitors[monitorIndex].screen.bottom - monitors[monitorIndex].screen.top;
}
_itow_s(w1, buffer, 10);
SetBuiltInVariable(L"WORKAREAY", buffer);
_itow_s(w2, buffer, 10);
SetBuiltInVariable(L"WORKAREAHEIGHT", buffer);
_itow_s(s1, buffer, 10);
SetBuiltInVariable(L"SCREENAREAY", buffer);
_itow_s(s2, buffer, 10);
SetBuiltInVariable(L"SCREENAREAHEIGHT", buffer);
}
}
/*
** Replaces environment and internal variables in the given string.
**
*/
bool ConfigParser::ReplaceVariables(std::wstring& result, bool isNewStyle)
{
bool replaced = false;
PathUtil::ExpandEnvironmentVariables(result);
if (c_MonitorVariables.empty())
{
SetMultiMonitorVariables(true);
}
// Check for new-style variables ([#VAR])
// Note: Most new-style variables are parsed later (when section variables are parsed),
// however, there are a few places where we just want to parse only variables (without
// section variables).
if (isNewStyle)
{
replaced = ParseVariables(result, VariableType::Variable);
}
else
{
// Special parsing for [#CURRENTSECTION] for use in actions
size_t start = 0ULL;
bool loop = true;
const std::wstring strVariable = L"[#CURRENTSECTION]";
const size_t length = strVariable.length();
do
{
start = result.find(strVariable, start);
if (start != std::wstring::npos)
{
const std::wstring* value = GetVariable(L"CURRENTSECTION");
if (value)
{
// Variable found, replace it with the value
result.replace(start, length, *value);
start += length;
replaced = true;
}
}
else
{
loop = false;
}
}
while (loop);
}
// Check for old-style variables (#VAR#)
size_t start = 0ULL, end = 0ULL;
bool loop = true;
do
{
start = result.find(L'#', start);
if (start != std::wstring::npos)
{
size_t si = start + 1ULL;
end = result.find(L'#', si);
if (end != std::wstring::npos)
{
size_t ei = end - 1ULL;
if (si != ei && result[si] == L'*' && result[ei] == L'*')
{
result.erase(ei, 1ULL);
result.erase(si, 1ULL);
start = ei;
}
else
{
std::wstring strVariable = result.substr(si, end - si);
const std::wstring* value = GetVariable(strVariable);
if (value)
{
// Variable found, replace it with the value
result.replace(start, end - start + 1ULL, *value);
start += (*value).length();
replaced = true;
}
else
{
start = end;
}
}
}
else
{
loop = false;
}
}
else
{
loop = false;
}
}
while (loop);
return replaced;
}
/*
** Replaces measures in the given string.
**
*/
bool ConfigParser::ReplaceMeasures(std::wstring& result)
{
// Check for new-style measures (and section variables) [&Measure], [&Meter]
// Note: This also parses regular variables as well (in case of nested variable types) eg. [#Var[&Measure]]
bool replaced = ParseVariables(result, VariableType::Section);
// Check for old-style measures and section variables. [Measure], [Meter:X], etc.
size_t start = 0ULL;
while ((start = result.find(L'[', start)) != std::wstring::npos)
{
size_t si = start + 1ULL;
size_t end = result.find(L']', si);
if (end == std::wstring::npos)
{
break;
}
size_t next = result.find(L'[', si);
if (next == std::wstring::npos || end < next)
{
size_t ei = end - 1ULL;
if (si != ei && result[si] == L'*' && result[ei] == L'*')
{
result.erase(ei, 1ULL);
result.erase(si, 1ULL);
start = ei;
}
else
{
std::wstring var = result.substr(si, end - si);
Measure* measure = GetMeasure(var);
if (measure)
{
const WCHAR* value = measure->GetStringOrFormattedValue(AUTOSCALE_OFF, 1.0, -1, false);
size_t valueLen = wcslen(value);
// Measure found, replace it with the value
result.replace(start, end - start + 1, value, valueLen);
start += valueLen;
replaced = true;
}
else
{
// It is possible for a variable to be reset when calling a custom function in a plugin or lua.
// Copy the result here, and replace it before returning.
std::wstring str = result;
std::wstring value;
if (GetSectionVariable(var, value))
{
// Replace section variable with the value.
str.replace(start, end - start + 1, value);
start += value.length();
replaced = true;
result = str;
}
else
{
start = end;
}
}
}
}
else
{
start = next;
}
}
return replaced;
}
/*
** Replaces nested measure/section variables, regular variables, and mouse variables in the given string.
**
*/
bool ConfigParser::ParseVariables(std::wstring& str, const VariableType type, Meter* meter)
{
// Since actions are parsed when executed, get the current active
// section in case the current section variable is used.
bool hasCurrentAction = false;
if (m_Skin && (m_CurrentSection->empty() || meter))
{
Section* section = m_Skin->GetCurrentActionSection();
if (section || meter)
{
m_CurrentSection->assign(meter ? meter->GetName() : section->GetName());
hasCurrentAction = true;
}
}
// It is possible for a variable to be reset when calling a custom function in a plugin or lua.
// Copy the result here, and replace it before returning.
std::wstring result = str;
bool replaced = false;
size_t previousStart = 0UL;
std::wstring previousVariable;
Logger::Entry delayedLogEntry = { Logger::Level::Debug, L"", L"", L"" };
// Find the innermost section variable(s) first, then move outward (working left to right)
size_t end = 0UL;
while ((end = result.find(L']', end)) != std::wstring::npos)
{
bool found = false;
const size_t ei = end - 1UL;
size_t start = ei;
while ((start = result.rfind(L'[', start)) != std::wstring::npos)
{
found = false;
size_t si = start + 2UL; // Start index where escaped variable "should" be: [ * *]
// Check for escaped variables first, if found, skip to the next variable
if (si != ei && result[si] == L'*' && result[ei] == L'*')
{
// Normally we remove the *'s for escaped variable names here, however mouse actions
// are parsed before being sent to the command handler where the rest of the variables
// are parsed. So we need to leave the escape *'s when called from the mouse parser.
if (type != VariableType::Mouse)
{
result.erase(ei, 1UL);
result.erase(si, 1UL);
}
break; // Break out of inner "start" loop and continue to the next nested variable
}
--si; // Move index to the "key" character (if it exists)
// Avoid empty commands
std::wstring original = result.substr(si, end - si);
if (original.empty())
{
break; // Break out of inner "start" loop and continue to the next nested variable
}
// Avoid self references
if (previousStart == start && _wcsicmp(original.c_str(), previousVariable.c_str()) == 0)
{
LogErrorSF(m_Skin, m_CurrentSection->c_str(),
L"Cannot replace variable with itself: \"%s\"", original.c_str());
break; // Break out of inner "start" loop and continue to the next nested variable
}
previousVariable = original;
previousStart = start;
// Separate "key" character from variable
const WCHAR key = result.substr(si, 1UL).c_str()[0];
std::wstring variable = result.substr(si + 1UL, end - si - 1UL);
if (variable.empty())
{
break; // Break out of inner "start" loop and continue to the next nested variable
}
// Find "type" of key
bool isValid = false;
VariableType kType = VariableType::Section;
for (const auto& t : c_VariableMap)
{
if (t.second == key)
{
kType = t.first;
isValid = true;
break;
}
}
// |key| is invalid or variable name is empty ([#], [&], [$], [\])
if (!isValid)
{
if (start == 0UL) break; // Already at beginning of string, try next ending bracket
--start; // Check for any "starting" brackets in string prior to the current starting position
continue; // This is not a valid nested variable, check the next starting bracket
}
// Since regular variables are replaced just before section variables in most cases, we replace
// both types at the same time in case nesting of the different types occurs. The only side effect
// is new-style regular variables located in an action will now be "dynamic" just like section
// variables.
// Special case 1: Mouse variables cannot be used in the outer part of a nested variable. This is
// because mouse variables are parsed and replaced before the other new-style variables.
// Special case 2: Places where regular variables need to be parsed without any section variables
// parsed afterward. One example is when "@Include" is parsed.
// Special case 3: Always process escaped character references.
std::wstring foundValue;
if ((key == c_VariableMap.find(type)->second) || // Special cases 1, 2
(kType == VariableType::CharacterReference) || // Special case 3
(type == VariableType::Section && key == c_VariableMap[VariableType::Variable])) // Most cases
{
switch (kType)
{
case VariableType::Section:
{
Measure* measure = GetMeasure(variable);
if (measure)
{
const WCHAR* value = measure->GetStringOrFormattedValue(AUTOSCALE_OFF, 1.0, -1, false);
foundValue.assign(value, wcslen(value));