forked from rainmeter/rainmeter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMeasure.cpp
738 lines (650 loc) · 16.9 KB
/
Measure.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
/*
Copyright (C) 2000 Kimmo Pekkola
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "StdAfx.h"
#include "Measure.h"
#include "MeasureCPU.h"
#include "MeasureMemory.h"
#include "MeasurePhysicalMemory.h"
#include "MeasureVirtualMemory.h"
#include "MeasureNetIn.h"
#include "MeasureNetOut.h"
#include "MeasureNetTotal.h"
#include "MeasureDiskSpace.h"
#include "MeasureUptime.h"
#include "MeasurePlugin.h"
#include "MeasureRegistry.h"
#include "MeasureTime.h"
#include "MeasureCalc.h"
#include "MeasureScript.h"
#include "Rainmeter.h"
#include "Error.h"
#include "Litestep.h"
enum AUTOSCALE_INDEX
{
AUTOSCALE_INDEX_1024 = 0,
AUTOSCALE_INDEX_1000 = 1
};
static const double g_TblScale[2][4] = {
{
1024.0 * 1024.0 * 1024.0 * 1024.0,
1024.0 * 1024.0 * 1024.0,
1024.0 * 1024.0,
1024.0
},
{
1000.0 * 1000.0 * 1000.0 * 1000.0,
1000.0 * 1000.0 * 1000.0,
1000.0 * 1000.0,
1000.0
}
};
const int MEDIAN_SIZE = 7;
extern CRainmeter* Rainmeter;
/*
** CMeasure
**
** The constructor
**
*/
CMeasure::CMeasure(CMeterWindow* meterWindow, const WCHAR* name) : m_MeterWindow(meterWindow), m_Name(name), m_ANSIName(ConvertToAscii(name)),
m_DynamicVariables(false),
m_Invert(false),
m_LogMaxValue(false),
m_MinValue(),
m_MaxValue(1.0),
m_Value(),
m_MedianPos(),
m_AveragePos(),
m_AverageSize(),
m_IfEqualValue(),
m_IfAboveValue(),
m_IfBelowValue(),
m_IfEqualCommited(false),
m_IfAboveCommited(false),
m_IfBelowCommited(false),
m_Disabled(false),
m_UpdateDivider(1),
m_UpdateCounter(1),
m_Initialized(false)
{
}
/*
** ~CMeasure
**
** The destructor
**
*/
CMeasure::~CMeasure()
{
}
/*
** Initialize
**
** Initializes the measure.
**
*/
void CMeasure::Initialize()
{
m_Initialized = true;
}
/*
** ReadConfig
**
** Reads the common configs for all Measures. The inherited classes
** must call the base implementation if they overwrite this method.
**
*/
void CMeasure::ReadConfig(CConfigParser& parser, const WCHAR* section)
{
// Clear substitutes to prevent from being added more than once.
if (!m_Substitute.empty())
{
m_Substitute.clear();
}
m_Invert = 0!=parser.ReadInt(section, L"InvertMeasure", 0);
if (!m_Initialized)
{
m_Disabled = 0!=parser.ReadInt(section, L"Disabled", 0);
}
else
{
const std::wstring& result = parser.ReadString(section, L"Disabled", L"0");
if (parser.GetLastReplaced())
{
m_Disabled = 0!=(int)parser.ParseDouble(result, 0.0, true);
}
}
int updateDivider = parser.ReadInt(section, L"UpdateDivider", 1);
if (updateDivider != m_UpdateDivider)
{
m_UpdateCounter = m_UpdateDivider = updateDivider;
}
m_MinValue = parser.ReadFloat(section, L"MinValue", m_MinValue);
m_MaxValue = parser.ReadFloat(section, L"MaxValue", m_MaxValue);
// The ifabove/ifbelow define actions that are ran when the value goes above/below the given number.
m_IfAboveValue = parser.ReadFloat(section, L"IfAboveValue", 0.0);
m_IfAboveAction = parser.ReadString(section, L"IfAboveAction", L"", false);
m_IfBelowValue = parser.ReadFloat(section, L"IfBelowValue", 0.0);
m_IfBelowAction = parser.ReadString(section, L"IfBelowAction", L"", false);
m_IfEqualValue = parser.ReadFloat(section, L"IfEqualValue", 0.0);
m_IfEqualAction = parser.ReadString(section, L"IfEqualAction", L"", false);
m_AverageSize = parser.ReadInt(section, L"AverageSize", 0);
m_DynamicVariables = 0!=parser.ReadInt(section, L"DynamicVariables", 0);
std::wstring subs = parser.ReadString(section, L"Substitute", L"");
if (!subs.empty() &&
(subs[0] != L'\"' || subs[subs.length() - 1] != L'\'') &&
(subs[0] != L'\'' || subs[subs.length() - 1] != L'\"'))
{
// Add quotes since they are removed by the GetProfileString
subs.insert(0, L"\"");
subs.append(L"\"");
}
if (!ParseSubstitute(subs))
{
LogWithArgs(LOG_WARNING, L"Incorrect substitute string: %s", subs.c_str());
}
const std::wstring& group = parser.ReadString(section, L"Group", L"");
InitializeGroup(group);
}
/*
** CheckSubstitute
**
** Substitutes part of the text
*/
const WCHAR* CMeasure::CheckSubstitute(const WCHAR* buffer)
{
static std::wstring str;
if (!m_Substitute.empty())
{
str = buffer;
for (size_t i = 0, isize = m_Substitute.size(); i < isize; i += 2)
{
if (str.empty() && m_Substitute[i].empty())
{
// Empty result and empty substitute -> use second
str = m_Substitute[i + 1];
}
else if (!m_Substitute[i].empty())
{
size_t start = 0;
size_t pos = std::wstring::npos;
do
{
pos = str.find(m_Substitute[i], start);
if (pos != std::wstring::npos)
{
str.replace(str.begin() + pos, str.begin() + pos + m_Substitute[i].size(), m_Substitute[i + 1]);
start = pos + m_Substitute[i + 1].size();
}
} while(pos != std::wstring::npos);
}
}
return str.c_str();
}
else
{
return buffer;
}
}
/*
** ParseSubstitute
**
** Reads the buffer for "Name":"Value"-pairs separated with comma and
** fills the map with the parsed data.
*/
bool CMeasure::ParseSubstitute(std::wstring buffer)
{
if (buffer.empty()) return true;
while (!buffer.empty())
{
std::wstring word1 = ExtractWord(buffer);
std::wstring sep = ExtractWord(buffer);
if (sep != L":") return false;
std::wstring word2 = ExtractWord(buffer);
if (word1 != word2)
{
m_Substitute.push_back(word1);
m_Substitute.push_back(word2);
}
sep = ExtractWord(buffer);
if (!sep.empty() && sep != L",") return false;
}
return true;
}
/*
** ExtractWord
**
** Returns the first word from the buffer. The word can be inside quotes.
** If not, the separators are ' ', '\t', ',' and ':'. Whitespaces are removed
** and buffer _will_ be modified.
*/
std::wstring CMeasure::ExtractWord(std::wstring& buffer)
{
std::wstring::size_type end, len;
std::wstring ret;
if (buffer.empty()) return ret;
len = buffer.size();
// Remove whitespaces
end = 0;
while (end < len && (buffer[end] == L' ' || buffer[end] == L'\t' || buffer[end] == L'\n')) ++end;
if (end == len)
{
// End of line reached
end = std::wstring::npos;
}
else
{
buffer.erase(0, end);
len = buffer.size();
if (buffer[0] == L'\"' || buffer[0] == L'\'')
{
WCHAR quote = buffer[0];
end = 1; // Skip the '"'
// Quotes around the word
while (end < len && (buffer[end] != quote)) ++end;
if (end == len) end = std::wstring::npos;
if (end != std::wstring::npos)
{
ret = buffer.substr(1, end - 1);
++end;
}
else
{
// End of string reached - discard result
}
}
else
{
end = 0;
while (end < len && (buffer[end] != L',' && buffer[end] != L':' && buffer[end] != L' ' && buffer[end] != L'\t')) ++end;
if (end == len) end = std::wstring::npos;
if (end == std::wstring::npos)
{
// End of line reached
ret = buffer;
}
else
{
ret = buffer.substr(0, ++end); // The separator is also returned!
}
}
}
buffer.erase(0, end);
return ret;
}
/*
** PreUpdate
**
** The base implementation of the update method. This includes the code
** that is common for all measures. This is called every time the measure
** is updated. The inherited classes must call the base implementation if
** they overwrite this method. If this method returns false, the update
** needs not to be done.
**
*/
bool CMeasure::PreUpdate()
{
if (IsDisabled())
{
m_Value = 0.0; // Disable measures return 0 as value
return false;
}
// Only update the counter if the divider
++m_UpdateCounter;
if (m_UpdateCounter < m_UpdateDivider) return false;
m_UpdateCounter = 0;
// If we're logging the maximum value of the measure, check if
// the new value is greater than the old one, and update if necessary.
if(m_LogMaxValue)
{
if (m_MedianMaxValues.empty())
{
m_MedianMaxValues.resize(MEDIAN_SIZE, 0);
m_MedianMinValues.resize(MEDIAN_SIZE, 0);
}
m_MedianMaxValues[m_MedianPos] = m_Value;
m_MedianMinValues[m_MedianPos] = m_Value;
++m_MedianPos;
m_MedianPos %= MEDIAN_SIZE;
std::vector<double> medianArray;
medianArray = m_MedianMaxValues;
std::sort(medianArray.begin(), medianArray.end());
m_MaxValue = max(m_MaxValue, medianArray[MEDIAN_SIZE / 2]);
medianArray = m_MedianMinValues;
std::sort(medianArray.begin(), medianArray.end());
m_MinValue = min(m_MinValue, medianArray[MEDIAN_SIZE / 2]);
}
if (m_MeterWindow)
{
// Check the IfEqualValue
if(!m_IfEqualAction.empty())
{
if((int)m_Value == (int)m_IfEqualValue)
{
if(!m_IfEqualCommited)
{
m_IfEqualCommited = true; // To avoid crashing by !RainmeterUpdate due to infinite loop
Rainmeter->ExecuteCommand(m_IfEqualAction.c_str(), m_MeterWindow);
}
}
else
{
m_IfEqualCommited = false;
}
}
// Check the IfAboveValue
if(!m_IfAboveAction.empty())
{
if(m_Value > m_IfAboveValue)
{
if(!m_IfAboveCommited)
{
m_IfAboveCommited = true; // To avoid crashing by !RainmeterUpdate due to infinite loop
Rainmeter->ExecuteCommand(m_IfAboveAction.c_str(), m_MeterWindow);
}
}
else
{
m_IfAboveCommited = false;
}
}
// Check the IfBelowValue
if(!m_IfBelowAction.empty())
{
if(m_Value < m_IfBelowValue)
{
if(!m_IfBelowCommited)
{
m_IfBelowCommited = true; // To avoid crashing by !RainmeterUpdate due to infinite loop
Rainmeter->ExecuteCommand(m_IfBelowAction.c_str(), m_MeterWindow);
}
}
else
{
m_IfBelowCommited = false;
}
}
}
return true;
}
/*
** PostUpdate
**
** Does post measuring things to the value. All measures must call this
** after they have set the m_Value.
**
*/
bool CMeasure::PostUpdate()
{
if (m_AverageSize > 0)
{
if (!m_AverageValues.empty())
{
m_AverageValues.resize(m_AverageSize, m_Value);
}
m_AverageValues[m_AveragePos] = m_Value;
size_t averageValuesSize = m_AverageValues.size();
++m_AveragePos;
m_AveragePos %= averageValuesSize;
// Calculate the average value
m_Value = 0;
for (size_t i = 0; i < averageValuesSize; ++i)
{
m_Value += m_AverageValues[i];
}
m_Value /= (double)averageValuesSize;
}
return true;
}
/*
** GetValue
**
** Returns the value of the measure.
**
*/
double CMeasure::GetValue()
{
// Invert if so requested
if (m_Invert)
{
return m_MaxValue - m_Value + m_MinValue;
}
return m_Value;
}
/*
** GetRelativeValue
**
** Returns the relative value of the measure (0.0 - 1.0).
**
*/
double CMeasure::GetRelativeValue()
{
double range = GetValueRange();
if (range != 0.0)
{
double value = GetValue();
value = min(m_MaxValue, value);
value = max(m_MinValue, value);
value -= m_MinValue;
return value / range;
}
return 1.0;
}
/*
** GetValueRange
**
** Returns the value range.
**
*/
double CMeasure::GetValueRange()
{
return m_MaxValue - m_MinValue;
}
/*
** GetStringValue
**
** This method returns the value as text string. The actual value is
** get with GetValue() so we don't have to worry about m_Invert.
**
** autoScale If true, scale the value automatically to some sensible range.
** scale The scale to use if autoScale is false.
** decimals Number of decimals used in the value. If -1, get rid of ".00000" for dynamic variables.
** percentual Return the value as % from the maximum value.
*/
const WCHAR* CMeasure::GetStringValue(AUTOSCALE autoScale, double scale, int decimals, bool percentual)
{
static WCHAR buffer[MAX_LINE_LENGTH];
WCHAR format[32];
if(percentual)
{
double val = 100.0 * GetRelativeValue();
if (decimals == 0)
{
_snwprintf_s(buffer, _TRUNCATE, L"%i", (int)val);
}
else
{
_snwprintf_s(format, _TRUNCATE, L"%%.%if", decimals);
_snwprintf_s(buffer, _TRUNCATE, format, val);
}
}
else if(autoScale != AUTOSCALE_OFF)
{
GetScaledValue(autoScale, decimals, GetValue(), buffer, _countof(buffer));
}
else
{
double val = GetValue() / scale;
if(decimals == 0)
{
val += (val >= 0) ? 0.5 : -0.5;
_snwprintf_s(buffer, _TRUNCATE, L"%lli", (LONGLONG)val);
}
else if (decimals == -1)
{
_snwprintf_s(buffer, _TRUNCATE, L"%.5f", val);
size_t len = wcslen(buffer);
if (len >= 6 && wcscmp(&buffer[len - 6], L".00000") == 0)
{
buffer[len - 6] = L'\0';
}
}
else
{
_snwprintf_s(format, _TRUNCATE, L"%%.%if", decimals);
_snwprintf_s(buffer, _TRUNCATE, format, val);
}
}
return CheckSubstitute(buffer);
}
void CMeasure::GetScaledValue(AUTOSCALE autoScale, int decimals, double theValue, WCHAR* buffer, size_t sizeInWords)
{
WCHAR format[32];
double value = 0;
if(decimals == 0)
{
wcsncpy_s(format, L"%.0f", _TRUNCATE);
}
else
{
_snwprintf_s(format, _TRUNCATE, L"%%.%if", decimals);
}
int index = (autoScale == AUTOSCALE_1000 || autoScale == AUTOSCALE_1000K) ? AUTOSCALE_INDEX_1000 : AUTOSCALE_INDEX_1024;
if(theValue > (g_TblScale[index][0] * 0.99))
{
wcsncat_s(format, L" T", _TRUNCATE);
value = theValue / g_TblScale[index][0];
}
else if(theValue > (g_TblScale[index][1] * 0.99))
{
wcsncat_s(format, L" G", _TRUNCATE);
value = theValue / g_TblScale[index][1];
}
else if(theValue > (g_TblScale[index][2] * 0.99))
{
wcsncat_s(format, L" M", _TRUNCATE);
value = theValue / g_TblScale[index][2];
}
else if(autoScale == AUTOSCALE_1024K || autoScale == AUTOSCALE_1000K || theValue > (g_TblScale[index][3] * 0.99))
{
wcsncat_s(format, L" k", _TRUNCATE);
value = theValue / g_TblScale[index][3];
}
else
{
wcsncat_s(format, L" ", _TRUNCATE);
value = theValue;
}
_snwprintf_s(buffer, sizeInWords, _TRUNCATE, format, value);
}
/*
** GetStats
**
** Returns the stats as string. The stats are shown in the About dialog.
*/
const WCHAR* CMeasure::GetStats()
{
static std::wstring value;
value = GetStringValue(AUTOSCALE_ON, 1, 1, false);
return value.c_str();
}
/*
** Create
**
** Creates the given measure. This is the factory method for the measures.
** If new measures are implemented this method needs to be updated.
**
*/
CMeasure* CMeasure::Create(const WCHAR* measure, CMeterWindow* meterWindow, const WCHAR* name)
{
// Comparison is caseinsensitive
if(*measure == L'\0')
{
return NULL;
}
else if(_wcsicmp(L"CPU", measure) == 0)
{
return new CMeasureCPU(meterWindow, name);
}
else if(_wcsicmp(L"Memory", measure) == 0)
{
return new CMeasureMemory(meterWindow, name);
}
else if(_wcsicmp(L"NetIn", measure) == 0)
{
return new CMeasureNetIn(meterWindow, name);
}
else if(_wcsicmp(L"NetOut", measure) == 0)
{
return new CMeasureNetOut(meterWindow, name);
}
else if(_wcsicmp(L"NetTotal", measure) == 0)
{
return new CMeasureNetTotal(meterWindow, name);
}
else if(_wcsicmp(L"PhysicalMemory", measure) == 0)
{
return new CMeasurePhysicalMemory(meterWindow, name);
}
else if(_wcsicmp(L"SwapMemory", measure) == 0)
{
return new CMeasureVirtualMemory(meterWindow, name);
}
else if(_wcsicmp(L"FreeDiskSpace", measure) == 0)
{
return new CMeasureDiskSpace(meterWindow, name);
}
else if(_wcsicmp(L"Uptime", measure) == 0)
{
return new CMeasureUptime(meterWindow, name);
}
else if(_wcsicmp(L"Time", measure) == 0)
{
return new CMeasureTime(meterWindow, name);
}
else if(_wcsicmp(L"Plugin", measure) == 0)
{
return new CMeasurePlugin(meterWindow, name);
}
else if(_wcsicmp(L"Registry", measure) == 0)
{
return new CMeasureRegistry(meterWindow, name);
}
else if(_wcsicmp(L"Calc", measure) == 0)
{
return new CMeasureCalc(meterWindow, name);
}
else if(_wcsicmp(L"script", measure) == 0)
{
return new CMeasureScript(meterWindow, name);
}
// Error
std::wstring error = L"Measure=";
error += measure;
error += L" is not valid in section [";
error += name;
error += L"].";
throw CError(error, __LINE__, __FILE__);
return NULL;
}
/*
** ExecuteBang
**
** Executes a custom bang
*/
void CMeasure::ExecuteBang(const WCHAR* args)
{
LogWithArgs(LOG_WARNING, L"[%s] doesn't support this bang: %s", m_Name.c_str(), args);
}