-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWhatPulseLogitech.cpp
608 lines (508 loc) · 16.9 KB
/
WhatPulseLogitech.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
/*
* WhatPulse Logitech Gaming Keyboard Widget
*
* This widget uses the WhatPulse Client API to retrieve statistics from the WhatPulse Client
* and display them on a Logitech Gaming Keyboard LCD screen. It also serves as an open example
* of how the Client API can be used.
*
* This has been tested on a G19 and G510. It requires the WhatPulse Client and that the
* client API has been enabled in the Settings tab.
*
* WhatPulse (c) 2016 - http://whatpulse.org
*/
#include "stdafx.h"
#include "json\json.h"
#include "WhatPulseLogitechApp.h"
#include "WhatPulseLogitech.h"
#include "WinHTTPClient\WinHttpClient.h"
#include "IniHandler.h"
#include <codecvt>
#include <sstream>
#include <locale>
#ifdef UNICODE
typedef std::wostringstream tstringstream;
#else
typedef std::ostringstream tstringstream;
#endif
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
extern LPWSTR iniFile;
WhatPulseLogitech::WhatPulseLogitech(CWnd* pParent /*=NULL*/) : CDialog(WhatPulseLogitech::IDD, pParent)
{
}
void WhatPulseLogitech::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(WhatPulseLogitech, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_DESTROY()
ON_WM_TIMER()
ON_WM_WINDOWPOSCHANGING()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL WhatPulseLogitech::OnInitDialog()
{
CDialog::OnInitDialog();
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_alertClientAPINotWorking = false;
settingsDlg = new SettingsDialog(this);
settingsDlg->Create(SettingsDialog::IDD);
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
ShowWindow(SW_HIDE);
SetWindowPos(NULL, 0, 0, 0, 0, NULL);
HRESULT hRes = m_lcd.Initialize(_T("WhatPulse Statistics"), LG_DUAL_MODE, FALSE, TRUE);
if (hRes != S_OK)
{
MessageBox(_T("Unable to connect to Logitech Display. Is the Logitech Gaming Software running? Exiting Widget."));
PostQuitMessage(0);
return FALSE;
}
// Initialise default settings if they don't exist yet
CSimpleIniA ini;
ini.SetUnicode();
ini.LoadFile(iniFile);
const char *pVal = ini.GetValue("Settings", "ClientAPIURL", "");
if (strcmp(pVal, "") == 0) {
ini.SetValue("Settings", "ClientAPIURL", "http://localhost:3490/v1/unpulsed");
}
pVal = ini.GetValue("Settings", "RefreshSeconds", "");
if (strcmp(pVal, "") == 0) {
ini.SetValue("Settings", "RefreshSeconds", "5");
}
// Save ini file
ini.SaveFile(iniFile);
// Setup both the monochrome and color display layouts
InitLCDObjectsMonochrome();
InitLCDObjectsColor();
// Initial refresh of WhatPulse stats
refeshWhatPulseStats();
// Check for button usage every 30ms
SetTimer(1, 30, NULL);
// Start statistics timer
restartStatsTimer();
return TRUE;
}
void
WhatPulseLogitech::restartStatsTimer()
{
// Stop timer first
KillTimer(2);
// Get the refresh rate from the ini configuration file
CSimpleIniA ini;
ini.SetUnicode();
ini.LoadFile(iniFile);
// Get the value for the stats refresh timer
const char *pVal = ini.GetValue("Settings", "RefreshSeconds", "5");
int refreshRate = atoi(pVal);
// Sanity check
if (refreshRate > 300 || refreshRate < 1)
refreshRate = 5;
// Update the statistics every 5 seconds (default)
SetTimer(2, refreshRate * 1000, NULL);
}
void WhatPulseLogitech::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
HCURSOR WhatPulseLogitech::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
void WhatPulseLogitech::OnDestroy()
{
KillTimer(1);
KillTimer(2);
}
void WhatPulseLogitech::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent == 1)
{
// Check whether the menu button has been pushed
if (m_lcd.ButtonTriggered(LG_BUTTON_MENU))
{
// Show settings dialog in Windows
if (!settingsDlg->IsWindowVisible())
settingsDlg->ShowWindow(SW_SHOW);
}
// Update LCD screen
m_lcd.Update();
}
// Timer 2 goes every 5 seconds, update the WhatPulse stats
else if (nIDEvent == 2)
{
refeshWhatPulseStats();
}
}
void WhatPulseLogitech::OnWindowPosChanging(WINDOWPOS* lpwndpos)
{
ASSERT(NULL != lpwndpos);
if(NULL != lpwndpos)
{
lpwndpos->flags &= ~SWP_SHOWWINDOW;
}
CDialog::OnWindowPosChanging(lpwndpos);
}
// Initialise the monochrome screen layout
// this function paints text on a coordinate scheme
VOID WhatPulseLogitech::InitLCDObjectsMonochrome()
{
m_lcd.ModifyDisplay(LG_MONOCHROME);
/***************/
/* FIRST PAGE */
/***************/
int y = 3;
int x_text = 5;
int x_value = 55;
// Keys
m_keysTextMono = m_lcd.AddText(LG_STATIC_TEXT, LG_SMALL, DT_LEFT, LGLCD_BW_BMP_WIDTH, 3);
m_lcd.SetOrigin(m_keysTextMono, x_text, y);
m_lcd.SetText(m_keysTextMono, _T("Keys:"));
m_keysValueMono = m_lcd.AddText(LG_STATIC_TEXT, LG_SMALL, DT_LEFT, LGLCD_BW_BMP_WIDTH, 3);
m_lcd.SetOrigin(m_keysValueMono, x_value, y);
m_lcd.SetText(m_keysValueMono, _T("n/a"));
// Move down
y += 10;
// Clicks
m_clicksTextMono = m_lcd.AddText(LG_STATIC_TEXT, LG_SMALL, DT_LEFT, LGLCD_BW_BMP_WIDTH, 3);
m_lcd.SetOrigin(m_clicksTextMono, x_text, y);
m_lcd.SetText(m_clicksTextMono, _T("Clicks:"));
m_clicksValueMono = m_lcd.AddText(LG_STATIC_TEXT, LG_SMALL, DT_LEFT, LGLCD_BW_BMP_WIDTH, 3);
m_lcd.SetOrigin(m_clicksValueMono, x_value, y);
m_lcd.SetText(m_clicksValueMono, _T("n/a"));
// Move down
y += 10;
// Download & Upload
m_downAndUploadTextMono = m_lcd.AddText(LG_STATIC_TEXT, LG_SMALL, DT_LEFT, LGLCD_BW_BMP_WIDTH, 3);
m_lcd.SetOrigin(m_downAndUploadTextMono, x_text, y);
m_lcd.SetText(m_downAndUploadTextMono, _T("Down / Up:"));
m_downAndUploadValueMono = m_lcd.AddText(LG_STATIC_TEXT, LG_SMALL, DT_LEFT, LGLCD_BW_BMP_WIDTH, 3);
m_lcd.SetOrigin(m_downAndUploadValueMono, x_value, y);
m_lcd.SetText(m_downAndUploadValueMono, _T("n/a"));
// Move down
y += 10;
// Uptime
m_uptimeTextMono = m_lcd.AddText(LG_STATIC_TEXT, LG_SMALL, DT_LEFT, LGLCD_BW_BMP_WIDTH, 3);
m_lcd.SetOrigin(m_uptimeTextMono, x_text, y);
m_lcd.SetText(m_uptimeTextMono, _T("Uptime:"));
m_uptimeValueMono = m_lcd.AddText(LG_STATIC_TEXT, LG_SMALL, DT_LEFT, LGLCD_BW_BMP_WIDTH, 3);
m_lcd.SetOrigin(m_uptimeValueMono, x_value, y);
m_lcd.SetText(m_uptimeValueMono, _T("n/a"));
}
// Initialise the color screen layout
// this function paints text on a coordinate scheme
VOID WhatPulseLogitech::InitLCDObjectsColor()
{
m_lcd.ModifyDisplay(LG_COLOR);
// Title
m_titleText = m_lcd.AddText(LG_STATIC_TEXT, LG_BIG, DT_LEFT, LGLCD_QVGA_BMP_WIDTH, 3);
m_lcd.SetOrigin(m_titleText, 35, 25);
m_lcd.SetText(m_titleText, _T("WhatPulse Stats"));
m_lcd.SetTextFontColor(m_titleText, RGB(255, 255, 255));
int y = 80;
int x_text = 35;
int x_value = 145;
// Keys
m_keysText = m_lcd.AddText(LG_STATIC_TEXT, LG_MEDIUM, DT_LEFT, LGLCD_QVGA_BMP_WIDTH, 3);
m_lcd.SetOrigin(m_keysText, x_text, y);
m_lcd.SetText(m_keysText, _T("Keys:"));
m_lcd.SetTextFontColor(m_keysText, RGB(255, 255, 255));
m_keysValue = m_lcd.AddText(LG_STATIC_TEXT, LG_MEDIUM, DT_LEFT, LGLCD_QVGA_BMP_WIDTH, 3);
m_lcd.SetOrigin(m_keysValue, x_value, y);
m_lcd.SetText(m_keysValue, _T("n/a"));
m_lcd.SetTextFontColor(m_keysValue, RGB(255, 255, 255));
// Move down
y += 25;
// Clicks
m_clicksText = m_lcd.AddText(LG_STATIC_TEXT, LG_MEDIUM, DT_LEFT, LGLCD_QVGA_BMP_WIDTH, 3);
m_lcd.SetOrigin(m_clicksText, x_text, y);
m_lcd.SetText(m_clicksText, _T("Clicks:"));
m_lcd.SetTextFontColor(m_clicksText, RGB(255, 255, 255));
m_clicksValue = m_lcd.AddText(LG_STATIC_TEXT, LG_MEDIUM, DT_LEFT, LGLCD_QVGA_BMP_WIDTH, 3);
m_lcd.SetOrigin(m_clicksValue, x_value, y);
m_lcd.SetText(m_clicksValue, _T("n/a"));
m_lcd.SetTextFontColor(m_clicksValue, RGB(255, 255, 255));
// Move down
y += 25;
// Download
m_downloadText = m_lcd.AddText(LG_STATIC_TEXT, LG_MEDIUM, DT_LEFT, LGLCD_QVGA_BMP_WIDTH, 3);
m_lcd.SetOrigin(m_downloadText, x_text, y);
m_lcd.SetText(m_downloadText, _T("Download:"));
m_lcd.SetTextFontColor(m_downloadText, RGB(255, 255, 255));
m_downloadValue = m_lcd.AddText(LG_STATIC_TEXT, LG_MEDIUM, DT_LEFT, LGLCD_QVGA_BMP_WIDTH, 3);
m_lcd.SetOrigin(m_downloadValue, x_value, y);
m_lcd.SetText(m_downloadValue, _T("n/a"));
m_lcd.SetTextFontColor(m_downloadValue, RGB(255, 255, 255));
// Move down
y += 25;
// Upload
m_uploadText = m_lcd.AddText(LG_STATIC_TEXT, LG_MEDIUM, DT_LEFT, LGLCD_QVGA_BMP_WIDTH, 3);
m_lcd.SetOrigin(m_uploadText, x_text, y);
m_lcd.SetText(m_uploadText, _T("Upload:"));
m_lcd.SetTextFontColor(m_uploadText, RGB(255, 255, 255));
m_uploadValue = m_lcd.AddText(LG_STATIC_TEXT, LG_MEDIUM, DT_LEFT, LGLCD_QVGA_BMP_WIDTH, 3);
m_lcd.SetOrigin(m_uploadValue, x_value, y);
m_lcd.SetText(m_uploadValue, _T("n/a"));
m_lcd.SetTextFontColor(m_uploadValue, RGB(255, 255, 255));
// Move down
y += 25;
// Uptime
m_uptimeText = m_lcd.AddText(LG_STATIC_TEXT, LG_MEDIUM, DT_LEFT, LGLCD_QVGA_BMP_WIDTH, 3);
m_lcd.SetOrigin(m_uptimeText, x_text, y);
m_lcd.SetText(m_uptimeText, _T("Uptime:"));
m_lcd.SetTextFontColor(m_uptimeText, RGB(255, 255, 255));
m_uptimeValue = m_lcd.AddText(LG_STATIC_TEXT, LG_MEDIUM, DT_LEFT, LGLCD_QVGA_BMP_WIDTH, 3);
m_lcd.SetOrigin(m_uptimeValue, x_value, y);
m_lcd.SetText(m_uptimeValue, _T("n/a"));
m_lcd.SetTextFontColor(m_uptimeValue, RGB(255, 255, 255));
}
// Convert a wstring/wchar to a string
std::string
convertWStringToString(std::wstring sourceString)
{
// Setup the codectv converter
typedef std::codecvt_utf8<wchar_t> convert_type;
std::wstring_convert<convert_type, wchar_t> converter;
// convert the wstring httpResponseContent to a string
std::string converted_str = converter.to_bytes(sourceString);
return converted_str;
}
std::wstring
convertCharToWString(const char* sourceString)
{
// Take the easy way out
tstringstream stros;
stros << sourceString;
return stros.str();
}
// Refresh the WhatPulse statistics from the Client API to the LCD screen
VOID WhatPulseLogitech::refeshWhatPulseStats()
{
// Load Client API URL from the settings ini file
CSimpleIniA ini;
ini.SetUnicode();
ini.LoadFile(iniFile);
const char *pVal = ini.GetValue("Settings", "ClientAPIURL", "http://localhost:3490/v1/unpulsed");
long bandwidthMultiplier = 1;
if (std::strstr(pVal, "account-totals") != NULL) {
bandwidthMultiplier = 1024 * 1024; // the bandwidth in account-totals in returned in MB
}
// Send a GET HTTP Request to the WhatPulse Client API
WinHttpClient client(convertCharToWString(pVal));
client.SendHttpRequest();
// Check the header, we need it to be '200 OK'
wstring httpResponseHeader = client.GetResponseHeader();
if (httpResponseHeader.find(L"200 OK") != 0)
{
// Get the response body, which contains the JSON the Client API generates
wstring httpResponseContent = client.GetResponseContent();
// We need to convert the wstring from WinHttpClient to a string so we can give that to the JSON parser
std::string json_string = convertWStringToString(httpResponseContent);
// Convert the string to a JSON array so that we can extract the statistics
Json::Value root;
Json::Reader reader;
bool parsingSuccessful = reader.parse(json_string, root);
if (parsingSuccessful)
{
// The JSON array has now been converted...We can update the text values!
tstringstream stros;
// Check if key is in the JSON output
if (!root["keys"].empty())
{
stros << FormatWithCommas((long)root["keys"].asInt64()).c_str();
m_lcd.SetText(m_keysValue, stros.str().c_str());
m_lcd.SetText(m_keysValueMono, stros.str().c_str());
stros.str(L"");
}
else {
// If the key is not in the JSON output, put "n/a" as the value
m_lcd.SetText(m_keysValue, _T("n/a"));
m_lcd.SetText(m_keysValueMono, _T("n/a"));
}
// Check if key is in the JSON output
if (!root["clicks"].empty())
{
stros << FormatWithCommas((long long)root["clicks"].asInt64()).c_str();
m_lcd.SetText(m_clicksValue, stros.str().c_str());
m_lcd.SetText(m_clicksValueMono, stros.str().c_str());
stros.str(L"");
}
else {
// If the key is not in the JSON output, put "n/a" as the value
m_lcd.SetText(m_clicksValue, _T("n/a"));
m_lcd.SetText(m_clicksValueMono, _T("n/a"));
}
// Check if key is in the JSON output
if (!root["download"].empty())
{
stros << formatBandwidth((long long)(root["download"].asInt64() * bandwidthMultiplier)).c_str();
m_lcd.SetText(m_downloadValue, stros.str().c_str());
stros.str(L"");
}
else {
// If the key is not in the JSON output, put "n/a" as the value
m_lcd.SetText(m_downloadValue, _T("n/a"));
}
// Check if key is in the JSON output
if (!root["upload"].empty())
{
stros << formatBandwidth((long long)root["upload"].asInt64() * bandwidthMultiplier).c_str();
m_lcd.SetText(m_uploadValue, stros.str().c_str());
stros.str(L"");
}
else {
// If the key is not in the JSON output, put "n/a" as the value
m_lcd.SetText(m_uploadValue, _T("n/a"));
}
// Check if key is in the JSON output
if (!root["upload"].empty() && !root["download"].empty())
{
stros << formatBandwidth((long long)root["download"].asInt64() * bandwidthMultiplier).c_str();
stros << " / ";
stros << formatBandwidth((long long)root["upload"].asInt64() * bandwidthMultiplier).c_str();
m_lcd.SetText(m_downAndUploadValueMono, stros.str().c_str());
stros.str(L"");
}
else {
// If the key is not in the JSON output, put "n/a" as the value
m_lcd.SetText(m_downAndUploadValueMono, _T("n/a"));
}
// Check if key is in the JSON output
if (!root["uptime"].empty())
{
stros << formatDateTimeShort((long long)root["uptime"].asInt64(), false).c_str();
m_lcd.SetText(m_uptimeValue, stros.str().c_str());
m_lcd.SetText(m_uptimeValueMono, stros.str().c_str());
stros.str(L"");
}
else {
// If the key is not in the JSON output, put "n/a" as the value
m_lcd.SetText(m_uptimeValue, _T("n/a"));
m_lcd.SetText(m_uptimeValueMono, _T("n/a"));
}
m_alertClientAPINotWorking = false;
}
else
{
// Alert the user if the Client API is not working as expected, just once
if (!m_alertClientAPINotWorking)
{
MessageBox(_T("The WhatPulse Client API is not responding. Do you have it enabled in the WhatPulse Settings?"));
m_alertClientAPINotWorking = true;
}
}
}
else
{
// Alert the user if the Client API is not working as expected, just once
if (!m_alertClientAPINotWorking)
{
MessageBox(_T("The WhatPulse Client API is not responding. Do you have it enabled in the WhatPulse Settings?"));
m_alertClientAPINotWorking = true;
}
}
}
// Convert an integer to a localized number
std::string WhatPulseLogitech::FormatWithCommas(long long value)
{
std::stringstream ss;
ss.imbue(std::locale(""));
ss << std::fixed << value;
return ss.str();
}
// Convert seconds into a readable format
std::string
WhatPulseLogitech::formatDateTimeShort(long long diff, bool includeSeconds)
{
if (diff < 60) {
return "<1 min";
}
else
{
std::stringstream msg;
bool append_comma = false;
long long years = diff / (3600 * 24 * 356);
diff -= years * (3600 * 24 * 356);
if (years >= 1) {
msg << years << "y";
append_comma = true;
}
long long days = diff / (3600 * 24);
diff -= days * (3600 * 24);
if (days >= 1)
{
if (append_comma) {
msg << ", ";
}
msg << days << "d";
append_comma = true;
}
long long hours = diff / 3600;
diff -= hours * 3600;
if (hours >= 1)
{
if (append_comma) {
msg << ", ";
}
msg << hours << "h";
append_comma = true;
}
long long minutes = diff / 60;
diff -= minutes * 60;
if (minutes >= 1)
{
if (append_comma) {
msg << ", ";
}
msg << minutes << "m";
append_comma = true;
}
if (diff > 0 && includeSeconds)
msg << diff << "s";
return msg.str();
}
}
// Round a double to the requested precision
double GetFloatPrecision(double value, double precision)
{
return (floor((value * pow(10, precision) + 0.5)) / pow(10, precision));
}
// Convert bytes to human readable filesize
std::string
WhatPulseLogitech::formatBandwidth(long long bytes)
{
std::stringstream msg;
if (bytes >= 1099511627776.0)
msg << GetFloatPrecision(bytes / 1099511627776.0, 2) << "TB";
else if (bytes >= 1073741824)
msg << GetFloatPrecision(bytes / 1073741824.0, 2) << "GB";
else if (bytes >= 1048576.0)
msg << GetFloatPrecision(bytes / 1048576.0, 2) << "MB";
else if (bytes >= 1024)
msg << GetFloatPrecision(bytes / 1024.0, 2) << "KB";
else
msg << GetFloatPrecision(bytes / 1.0, 2) << "B";
return msg.str();
}