-
Notifications
You must be signed in to change notification settings - Fork 2
/
SysLat_SoftwareDlg.cpp
1312 lines (1153 loc) · 46.7 KB
/
SysLat_SoftwareDlg.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
// SysLat_SoftwareDlg.cpp : implementation file
//
// created by Unwinder
// modified by Skewjo
/////////////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "SysLat_Software.h"
#include "SysLat_SoftwareDlg.h"
#include "USBController.h"
#include "HTTP_Client_Async.h"
#include "HTTP_Client_Async_SSL.h"
#include "SysLatPreferences.h"
#include "AboutDlg.h"
#include "PreferencesDlg.h"
#include "TestCtrl.h" //this one should probably have a suffix of "dlg"...
#include "version.h"
//probably need to move the following 2 includes to stdafx.h
#include <memory>
#include <shellapi.h>
//TODO:
// Transfer TODO to GitHub Issues...
// DONE - Organize TODO...
// DONE - Core Functionality
// DONE - Menu
// DONE - Data Issues
// DONE - Optimization
// DONE - Organizational Issues
// DONE - Anti-Fraud
//
//
//Issues completed before the TODO reorg:
// NOT AVAILABLE(?) - Profile Setting - Set "Refresh Period" to 0 milliseconds - doesn't appear to be an option available via shared memory
// DONE - Profile Setting - Change default corner to bottom right
// DONE (opacity) - Profile Setting - Change "text"(foreground) color to white (255, 255, 255) with an opacity of 100
// DONE - Profile Setting - Change color of "background"(?) to black (0, 0, 0) with an opacity of 100
// DONE - Better yet - if I could change the box to use a plain black and plain white box so any other text isn't screwed up, that would be better
// DONE - Profile Setting - Set box size to what I want it to be?
// DONE - Change class/namespace name of RTSSSharedMemorySampleDlg to SysLatDlg
// DONE - Change class/namespace of RTSSSharedMemorySample to SysLat
// DONE - Add minimize button
// DONE(well... it half-ass works) - Make System Latency appear in OSD
// DONE - Save results to a table - using an array
// DONE - Determine active window vs window that RTSS is operating in?
// DONE(mostly) - Launch RTSS automatically in the background if it's not running
// DONE - Add hotkey to restart readings (F11?)
// DONE - Seperate some initialization that happens in "Refresh" function into a different "Refresh-like" function?? - partially done?
// DONE - Re-org this file into 3-4 new classes - Dialog related functions, RTSS related, DrawingThread related, and USB related
// DONE - BUT THERE ARE PROBLEMS(it just changed the priority of which client(syslat vs syslatStats) - Make the program statically linked so that it all packages together nicely in a single DLL
// DONE(BUT NOT GREAT) - Dynamically build the "drawSquare" string and change the P tag to account for the current corner and all other OSD text?
// DONE(Mostly...new issue created) - Then create an option to disable that setting - add keyboard arrow functionality to move it into place manually.
//
//
//
//
//Core Functionality:
// DONE - Add HTTP post function for uploading logs to website - use boost.beast library?
// Some errors currently appear very briefly and are overwritten when the refresh function runs - Clean up the refresh function, then come up with new error scheme.
// Either use error codes, or check all errors again in the refresh function(that doesn't make sense though, right?)... or maybe do dialog error pop-ups when errors occur outside of "refresh"?
// Move ExportData function out of SysLatData? Or just use it to retrieve a jsoncpp object & combine it with other jsoncpp objects
// DONE - Make executable/window names mesh better together? Need a map/lookup table or something? - JUST USE PID YA IDIOT
//
//
//Data Issues:
// Save fps and frametime and other stats as well?
// Add graph functionality
// Put elapsed time in log file
// Clear log files and put a configurable(?) cap on the number allowed
// Keep track of total tests performed in a config file vs. looking for existing log files and picking up from there?
// How many tests should we allow total? 100?
// Would it be fine if SysLat overwrote the tests every time it was restarted? ...I think it would
//
//
//Menu:
// Enumerate all 3D programs that RTSS can run in and display them in a menu
// Fix COM port change settings
// Add lots more menu options - USB options, debug output, data upload, RTSS options(text color)
// Box position manual override toggle
//
//
//Anti-Fraud:
// Create new dynamic build/installation process in order to obscure some code
// Think about hardware/software signatures for uploading data? This probably needs more consideration on the web side
// Obscure most functionality(things that don't need to be optimized) into DLLs(requires a new build/installation process)
// (Anti-Fraud, Optimization, and Data)Instead of recording certain variables on every measurement(such as RTSS XY position) record them once at the start and once at the end
//
//
//Optimization:
// Move data update at the end of the CreateDrawingThread function into a different thread(or co-routine?)
// Calculating the position of the box before we draw it adds unnecessary delay(?)
// Make flashing square resizeable
//
//
//Organizational Issues:
// Clean up(or get rid of) static vars in SysLat_SoftwareDlg class
// Clean up the refresh function a bit more by making some init functionality conditional
// Attempt to get rid of most Windows type names like CString, BOOL, and INT(DWORD?)
// Attempt to use a single style of string instead of "string", "char*", and "CString".
// Look further into Windows style guides & straighten out all member var names with "m_" and the type, or do away with it completely
// Look into file organization for .h and .cpp files because the repo is a mess(though it's fine in VS because of "filters")
// Look into class naming schemes and organization - make sure dialog classes end in "dlg"(?)
// Check whether or not my void "initialization" methods need to return ints or bools for success/failure or if I can just leave them as void
//////////////////////////////////////////////////////////////////////////////////////////////
//Major Bugs:
// DONE - (hid the error)The SysLat RTSSClient object cannot obtain the "0th" RTSS OSD slot when restarting a test
// Issue when switching COM ports to an existing device that isn't SysLat and back
// DONE - Arrow key functionality has been optimized away somehow
//
//
//Minor Bugs:
// SysLat may not play nicely with other applications that use RTSS such as MSI Afterburner, or with advanced RTSS setups
//
//////////////////////////////////////////////////////////////////////////////////////////////
const int ID_COMPORT_START = 2000;
const int ID_COMPORT_END = 2099;
const int ID_RTSSAPP_START = 2100;
const int ID_RTSSAPP_END = 2199;
const int WM_STMESSAGE = WM_USER + 1;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//As a non-static non-member variable? Is this a good idea? Need to at least remove the "m_" from the var name... Does this make it global??
SysLatPreferences SLPref;
#define SysLatOpt SLPref.m_SysLatOptions
#define PrivacyOpt SLPref.m_PrivacyOptions
#define DebugOpt SLPref.m_DebugOptions
#define RTSSOpt SLPref.m_RTSSOptions
NOTIFYICONDATA nid;
int dotCounter = 0;
//Define static variables - these should probably be done as inline or something... inlining is supposed to be available in C++17 and above, but Visual Studio throws a fit when I try to inline these.
//also, I should probably move most of them to be global variables and NOT member variables
CString CSysLat_SoftwareDlg::m_strStatus = "";
unsigned int CSysLat_SoftwareDlg::m_LoopCounterRefresh = 0;
unsigned int CSysLat_SoftwareDlg::m_loopSize = 0xFFFFFFFF;
CString CSysLat_SoftwareDlg::m_updateString = "";
CString CSysLat_SoftwareDlg::m_strError = "";
std::shared_ptr<CSysLatData> CSysLat_SoftwareDlg::m_pOperatingSLD = std::make_shared<CSysLatData>(); //does this need to be a unique_ptr?
CString CSysLat_SoftwareDlg::m_strBlack = "<C=000000><B=10,10><C>";
CString CSysLat_SoftwareDlg::m_strWhite = "<C=FFFFFF><B=10,10><C>";
DWORD CSysLat_SoftwareDlg::m_sysLatOwnedSlot = 0;
DWORD CSysLat_SoftwareDlg::m_AppArraySize = 0;
//Windows Dialog inherited function overrides
/////////////////////////////////////////////////////////////////////////////
// CSysLat_SoftwareDlg dialog
/////////////////////////////////////////////////////////////////////////////
CSysLat_SoftwareDlg::CSysLat_SoftwareDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CSysLat_SoftwareDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CSysLat_SoftwareDlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_strStatus = "";
m_strInstallPath = "";
m_bMultiLineOutput = TRUE;
m_bFormatTags = TRUE;
m_bFillGraphs = FALSE;
m_bConnected = FALSE;
}
CSysLat_SoftwareDlg::~CSysLat_SoftwareDlg() {
SLPref.WritePreferences();
}
void CSysLat_SoftwareDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSysLat_SoftwareDlg)
//}}AFX_DATA_MAP
}
/////////////////////////////////////////////////////////////////////////////
// CSysLat_SoftwareDlg message handlers
/////////////////////////////////////////////////////////////////////////////
BEGIN_MESSAGE_MAP(CSysLat_SoftwareDlg, CDialogEx)
//{{AFX_MSG_MAP(CSysLat_SoftwareDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_WM_TIMER()
ON_WM_DESTROY()
ON_COMMAND(ID_TOOLS_EXPORTDATA, CSysLat_SoftwareDlg::ExportData)
ON_COMMAND(ID_TOOLS_UPLOADDATA, CSysLat_SoftwareDlg::UploadData)
ON_COMMAND(ID_SETTINGS_DEBUGMODE, CSysLat_SoftwareDlg::DebugMode)
ON_COMMAND(ID_SETTINGS_TESTUPLOADMODE, CSysLat_SoftwareDlg::TestUploadMode)
ON_COMMAND(ID_SETTINGS_DISPLAYSYSLATINOSD, CSysLat_SoftwareDlg::DisplaySysLatInOSD)
ON_COMMAND(ID_TOOLS_NEWTEST, CSysLat_SoftwareDlg::ReInitThread)
ON_COMMAND(ID_SETTINGS_PREFERENCES, CSysLat_SoftwareDlg::OpenPreferences)
ON_COMMAND(ID_TOOLS_TESTCONTROL, CSysLat_SoftwareDlg::OpenTestCtrl)
ON_COMMAND_RANGE(ID_COMPORT_START, ID_COMPORT_END, CSysLat_SoftwareDlg::OnComPortChanged)
ON_COMMAND_RANGE(ID_RTSSAPP_START, ID_RTSSAPP_END, CSysLat_SoftwareDlg::OnTargetWindowChanged)
ON_MESSAGE(WM_STMESSAGE, CSysLat_SoftwareDlg::OnSTMessage)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CSysLat_SoftwareDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
m_color = RGB(136, 217, 242);
m_brush.CreateSolidBrush(m_color);
GetModuleFileName(NULL, pathToSysLat, MAX_PATH);
CWnd* pMainDlg = GetDlgItem(IDD_SYSLAT_SOFTWARE_DIALOG);
if (pMainDlg)
{
pMainDlg->GetClientRect(&m_clientRect);
}
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
SetIcon(m_hIcon, TRUE);
CWnd* pPlaceholder = GetDlgItem(IDC_PLACEHOLDER);
if (pPlaceholder)
{
CRect rect;
pPlaceholder->GetClientRect(&rect);
if (!m_richEditCtrl.Create(WS_VISIBLE | ES_READONLY | ES_MULTILINE | ES_AUTOHSCROLL | WS_HSCROLL | ES_AUTOVSCROLL | WS_VSCROLL, rect, this, 0))
return FALSE;
m_font.CreateFont(-11, 0, 0, 0, FW_REGULAR, 0, 0, 0, BALTIC_CHARSET, 0, 0, 0, 0, "Courier New");
m_richEditCtrl.SetFont(&m_font);
}
if (PrivacyOpt.m_bFirstRun) {
::MessageBox(NULL, "This appears to be the first time you've run SysLat from this directory. Please set your privacy options.", "SysLat First Run", MB_OK);
OpenPreferences();
PrivacyOpt.m_bFirstRun = false;
}
if (PrivacyOpt.m_bRunOnStartup) {
SetSURegValue(pathToSysLat);
}
else {
SetSURegValue("");
}
//m_bTestUploadMode = true;
if(PrivacyOpt.m_bAutoCheckUpdates){
CheckUpdate();
}
m_nTimerID = SetTimer(0x1234, 1000, NULL); //Used by OnTimer function to refresh dialog box & OSD
//time(&m_elapsedTimeStart); //Used to keep track of test length
m_hardwareID.ExportData(SysLatOpt.m_LogDir);
m_machineInfo.ExportData(SysLatOpt.m_LogDir);
Refresh();
unsigned threadID;
m_drawingThreadHandle = (HANDLE)_beginthreadex(0, 0, CreateDrawingThread, &myCounter, 0, &threadID);
SetThreadPriority(m_drawingThreadHandle, THREAD_PRIORITY_ABOVE_NORMAL);//31 is(apparently?) the highest possible thread priority - may be bad because it could cause deadlock using a loop? Need to read more here: https://docs.microsoft.com/en-us/windows/win32/procthread/scheduling-priorities
return TRUE; // return TRUE unless you set the focus to a control
}
void CSysLat_SoftwareDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else if ((nID & 0xFFF0) == SC_MINIMIZE)
{
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = m_hWnd;
nid.uID = 100;
nid.uVersion = NOTIFYICON_VERSION;
nid.uCallbackMessage = WM_STMESSAGE;
nid.hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
strcpy_s(nid.szTip, "SysLat");
nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
Shell_NotifyIcon(NIM_ADD, &nid);
ModifyStyleEx(WS_EX_APPWINDOW, WS_EX_TOOLWINDOW);
// Minimizing, post to main dialogue also.
AfxGetMainWnd()->ShowWindow(SW_MINIMIZE);
}
//else if ((nID & 0xFFF0) == SC_CLOSE) {
// nid.cbSize = sizeof(NOTIFYICONDATA);
// nid.hWnd = m_hWnd;
// nid.uID = 100;
// nid.uVersion = NOTIFYICON_VERSION;
// nid.uCallbackMessage = WM_STMESSAGE;
// nid.hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
// strcpy_s(nid.szTip, "SysLat");
// nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
// Shell_NotifyIcon(NIM_ADD, &nid);
// ModifyStyleEx(WS_EX_APPWINDOW, WS_EX_TOOLWINDOW);
// // Minimizing, post to main dialogue also.
// AfxGetMainWnd()->ShowWindow(SW_MINIMIZE);
//}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
void CSysLat_SoftwareDlg::OnPaint()
{
if (IsIconic())
{
//THIS CODE FOR THE MINIMIZE BUTTON IS NO LONGER(NEVER WAS??) NEEDED??
//CPaintDC dc(this); // device context for painting
//SendMessage(WM_ICONERASEBKGND, (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;
//dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
HCURSOR CSysLat_SoftwareDlg::OnQueryDragIcon()
{
return (HCURSOR)m_hIcon;
}
void CSysLat_SoftwareDlg::OnTimer(UINT nIDEvent)
{
Refresh();
CDialogEx::OnTimer(nIDEvent);
}
void CSysLat_SoftwareDlg::OnDestroy()
{
if (m_nTimerID)
KillTimer(m_nTimerID);
m_nTimerID = NULL;
MSG msg;
while (PeekMessage(&msg, m_hWnd, WM_TIMER, WM_TIMER, PM_REMOVE));
TerminateThread(m_drawingThreadHandle, 0); //Does exit code need to be 0 for this?
m_SysLatStatsClient.ReleaseOSD();
//m_pOperatingSLD->CloseSLDMutex();
CDialogEx::OnDestroy();
}
LRESULT CSysLat_SoftwareDlg::OnSTMessage(WPARAM wParam, LPARAM lParam) {
UNREFERENCED_PARAMETER(wParam);
UNREFERENCED_PARAMETER(lParam);
if (lParam == WM_LBUTTONDBLCLK){
if (IsIconic()) {
ModifyStyleEx(WS_EX_TOOLWINDOW, WS_EX_APPWINDOW);
ShowWindow(SW_SHOWNORMAL);
Shell_NotifyIcon(NIM_DELETE, &nid);
}
}
return 0;
}
//Dialog functions
string CSysLat_SoftwareDlg::GetProcessNameFromPID(DWORD processID) {
string ret;
HANDLE Handle = OpenProcess(
PROCESS_QUERY_LIMITED_INFORMATION,
FALSE,
processID
);
if (Handle)
{
DWORD buffSize = 1024;
CHAR Buffer[1024];
if (QueryFullProcessImageName(Handle, 0, Buffer, &buffSize))
{
ret = strrchr(Buffer, '\\') + 1; // I can't believe this works
}
else
{
printf("Error GetModuleBaseName : %lu", GetLastError());
}
CloseHandle(Handle);
}
else
{
printf("Error OpenProcess : %lu", GetLastError());
}
return ret;
}
string CSysLat_SoftwareDlg::GetActiveWindowTitle()
{
char wnd_title[256];
CWnd* pWnd = GetForegroundWindow();
::GetWindowText((HWND)*pWnd, wnd_title, 256); //Had to use scope resolution because this function is defined in both WinUser.h and afxwin.h
return wnd_title;
}
void CSysLat_SoftwareDlg::ProcessNameTrim(string& processName, string& activeWindowTitle){
size_t pos = processName.find(".exe");
if (pos != string::npos) {
processName.replace(pos, processName.size(), "");
}
while ((pos = processName.find(" ")) != string::npos) {
processName.replace(pos, 1, "");
}
std::transform(processName.begin(), processName.end(), processName.begin(), [](unsigned char c) { return std::tolower(c); });
while ((pos = activeWindowTitle.find(" ")) != string::npos) {
activeWindowTitle.replace(pos, 1, "");
}
std::transform(activeWindowTitle.begin(), activeWindowTitle.end(), activeWindowTitle.begin(), [](unsigned char c) { return std::tolower(c); });
}
BOOL CSysLat_SoftwareDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN)
{
switch (pMsg->wParam)
{
case VK_F11:
ReInitThread();
return TRUE;
case ' ':
if (!m_bConnected)
{
if (!CRTSSClient::m_strInstallPath.IsEmpty())
ShellExecute(GetSafeHwnd(), "open", CRTSSClient::m_strInstallPath, NULL, NULL, SW_SHOWNORMAL);
}
return TRUE;
case VK_UP:
if (RTSSOpt.m_internalY > 0) {
RTSSOpt.m_internalY--;
}
//else appendError ??
RTSSOpt.m_bPositionManualOverride = true;
return TRUE;
case VK_DOWN:
if (RTSSOpt.m_internalY < 255) {
RTSSOpt.m_internalY++;
}
RTSSOpt.m_bPositionManualOverride = true;
return TRUE;
case VK_LEFT:
if (RTSSOpt.m_internalX > 0) {
RTSSOpt.m_internalX--;
}
RTSSOpt.m_bPositionManualOverride = true;
return TRUE;
case VK_RIGHT:
if (RTSSOpt.m_internalX < 255) {
RTSSOpt.m_internalX++;
}
RTSSOpt.m_bPositionManualOverride = true;
return TRUE;
case 'R':
CRTSSClient::SetProfileProperty("", "BaseColor", 0xFF0000);
return TRUE;
case 'G':
CRTSSClient::SetProfileProperty("", "BaseColor", 0x00FF00);
return TRUE;
case 'B':
CRTSSClient::SetProfileProperty("", "BaseColor", 0x0000FF);
return TRUE;
case 'F':
if (m_bConnected)
{
m_bFormatTags = !m_bFormatTags;
Refresh();
}
break;
case 'I':
if (m_bConnected)
{
m_bFillGraphs = !m_bFillGraphs;
Refresh();
}
break;
}
}
return CDialogEx::PreTranslateMessage(pMsg);
}
void CSysLat_SoftwareDlg::Refresh()
{
//Had to add this section because it was initializing fine in debug mode, but then initializing COMPortCount too fast in release mode??? Probably some stupid issue with my debug macros...
CMenu* MainMenu = GetMenu();
CMenu* SettingsMenu = MainMenu->GetSubMenu(1);
CMenu* ComPortMenu = SettingsMenu->GetSubMenu(0);
char menuCString[256];
MainMenu->GetMenuString(ID_USBPORT_PLACEHOLDER, (LPSTR)menuCString, 256, (UINT)MF_BYCOMMAND);
if (strcmp(menuCString, "Placeholder") == 0) {
m_COMPortCount = 0;
}
CUSBController usbController;
usbController.EnumSerialPorts(m_COMPortInfo, FALSE);
if (m_COMPortInfo.GetSize() != m_COMPortCount) {
R_DynamicComPortMenu();
}
if (m_bConnected) {//m_bConnected is never set to true??
DWORD AppArraySize = CRTSSClient::GetAppArray();
if (m_AppArraySize != AppArraySize) {
R_DynamicAppMenu();
m_AppArraySize = AppArraySize;
DEBUG_PRINT("AppArraySize: " + to_string(AppArraySize))
}
}
if (!(CRTSSClient::m_profileInterface.IsInitialized())) {
CRTSSClient::InitRTSSInterface();
}
RTSSOpt.m_positionX = CRTSSClient::GetProfileProperty("", "PositionX");
RTSSOpt.m_positionY = CRTSSClient::GetProfileProperty("", "PositionY");
if (m_bConnected && !m_bRTSSInitConfig) {
R_GetRTSSConfigs();
}
else {
m_bRTSSInitConfig = false;
}
m_strStatus = "";
if (!R_SysLatStats()) return;
if (m_bDebugMode) {
R_Position();
R_ProcessNames();
}
R_StrOSD();
if (!m_bSysLatInOSD) { //need to add another condition to make this only happen once so that it will clear whatever exists in the buffer... or maybe use the releaseOSD function properly? IDK
m_SysLatStatsClient.UpdateOSD("");
}
//Need to make a new function & boolean for displaying controls/hints
if (CRTSSClient::m_profileInterface.IsInitialized())
{
m_strStatus += "\n\n-Press <Up>,<Down>,<Left>,<Right> to change OSD position in global profile";
if (DebugOpt.m_bSysLatInOSD) {
m_strStatus += "\n-Press <R>,<G>,<B> to change OSD color in global profile";
}
}
if (!m_strError.IsEmpty())
{
m_strStatus += "\n\nErrors:";
m_strStatus.Append(m_strError);
m_strError = "";
}
m_richEditCtrl.SetWindowText(m_strStatus);
}
void CSysLat_SoftwareDlg::R_GetRTSSConfigs() {
m_dwSharedMemoryVersion = CRTSSClient::GetSharedMemoryVersion();
m_dwMaxTextSize = (m_dwSharedMemoryVersion >= 0x00020007) ? sizeof(RTSS_SHARED_MEMORY::RTSS_SHARED_MEMORY_OSD_ENTRY().szOSDEx) : sizeof(RTSS_SHARED_MEMORY::RTSS_SHARED_MEMORY_OSD_ENTRY().szOSD);
m_bFormatTagsSupported = (m_dwSharedMemoryVersion >= 0x0002000b); //text format tags are supported for shared memory v2.11 and higher
m_bObjTagsSupported = (m_dwSharedMemoryVersion >= 0x0002000c); //embedded object tags are supporoted for shared memory v2.12 and higher
m_bRTSSInitConfig = true;
}
BOOL CSysLat_SoftwareDlg::R_SysLatStats() {
//Introduced a new bug here (but only in debug mode) where it looks like I'm getting some kind of under/overflow value.
//Instead of duration starting at 00:00::00, it will start a value that looks something like "238609:17:-40"
auto& CurrentTestDuration = m_pOperatingSLD->GetCurrentDuration();
if (duration_cast<std::chrono::minutes>(CurrentTestDuration).count() >= SysLatOpt.m_maxTestDuration) {
ReInitThread();
}
auto& data = m_pOperatingSLD->GetData();
double measurementsPerSecond = static_cast<double>(data.m_statistics.counter) / static_cast<double>(duration_cast<std::chrono::seconds>(CurrentTestDuration).count());
string CurrentTestDurationString = format("%R:%OS", CurrentTestDuration);
m_strStatus.Append(("Elapsed Time: " + CurrentTestDurationString).c_str());
if (m_pOperatingSLD->GetSystemLatency() < 500) { //using "c_str()" here because "stoi(m_pOperatingSLD->GetStringResult())" was getting immediate exceptions for some reason...
m_strStatus.AppendFormat("\nSystem Latency: %i", m_pOperatingSLD->GetSystemLatency());
}
else {
m_strStatus.AppendFormat("\nSystem Latency: Waiting");
if (dotCounter == 1) {
m_strStatus.AppendFormat(".");
}
else if (dotCounter == 2) {
m_strStatus.AppendFormat("..");
}
else if (dotCounter == 3) {
m_strStatus.AppendFormat("...");
}
}
m_strStatus.AppendFormat("\nLoop Counter : %d", data.m_statistics.counter);
if (isnan(measurementsPerSecond)) {
m_strStatus.AppendFormat("\nMeasurements Per Second: 0.00");
}
else {
m_strStatus.AppendFormat("\nMeasurements Per Second: %.2f", measurementsPerSecond); //This value should probably be in the SLD...
}
if (data.m_statistics.average < 500) {
m_strStatus.AppendFormat("\nSystem Latency Average: %.2f", data.m_statistics.average);
}
else {
m_strStatus.AppendFormat("\nSystem Latency Average: Waiting");
if (dotCounter == 1) {
m_strStatus.AppendFormat(".");
}
else if (dotCounter == 2) {
m_strStatus.AppendFormat("..");
}
else if (dotCounter == 3) {
m_strStatus.AppendFormat("...");
}
}
dotCounter++;
if (dotCounter >= 4) {
dotCounter = 0;
}
m_strStatus.AppendFormat("\nLoop Counter EVR(expected value range, 3-100): %d ", data.m_statisticsEVR.counter);
m_strStatus.AppendFormat("\nSystem Latency Average(EVR): %.2f", data.m_statisticsEVR.average);
return true; //this return value needs to change or be removed
}
void CSysLat_SoftwareDlg::R_Position() {
m_strStatus.AppendFormat("\n\nClients num: %d", CRTSSClient::clientsNum);
m_strStatus.AppendFormat("\nSysLat Owned Slot: %d", m_sysLatOwnedSlot);
m_strStatus.AppendFormat("\nPositionX: %d", RTSSOpt.m_positionX);
m_strStatus.AppendFormat("\nPositionY: %d", RTSSOpt.m_positionY);
}
void CSysLat_SoftwareDlg::R_ProcessNames() {
DWORD dwLastForegroundAppProcessID = CRTSSClient::GetLastForegroundAppID();
m_strStatus.Append("\n\nLast RTSS Foreground App Name: ");
string processName = GetProcessNameFromPID(dwLastForegroundAppProcessID);
m_strStatus += processName.c_str();
m_strStatus.Append("\nCurrently active window: ");
string activeWindowTitle = GetActiveWindowTitle();
m_strStatus += activeWindowTitle.c_str();
ProcessNameTrim(processName, activeWindowTitle);
m_strStatus.Append("\nTrimmed:");
m_strStatus += processName.c_str();
m_strStatus.Append("\nTrimmed:");
m_strStatus += activeWindowTitle.c_str();
}
void CSysLat_SoftwareDlg::R_StrOSD() {
//BOOL bTruncated = FALSE;
string strOSD;// = strOSDBuilder.Get(bTruncated);
strOSD += m_pOperatingSLD->GetSystemLatency();
if (!(strOSD.size() == 0))
{
bool bResult = m_SysLatStatsClient.UpdateOSD(strOSD.c_str());
m_bConnected = bResult;
if (bResult)
{
m_strStatus += "\nTarget Window: ";
m_strStatus += (SysLatOpt.m_targetApp).c_str();
/*
m_strStatus += "\n\nThe following text is being forwarded to OSD:\nFrom SysLat client: " + m_updateString + "\nFrom SysLatStats client: " + strOSD;
if (m_bFormatTagsSupported)
m_strStatus += "\n-Press <F> to toggle OSD text formatting tags";
if (m_bFormatTagsSupported)
m_strStatus += "\n-Press <I> to toggle graphs fill mode";
*/
//if (bTruncated)
// AppendError("Warning: The text is too long to be displayed in OSD, some info has been truncated!");
}
else
{
if (CRTSSClient::m_strInstallPath.IsEmpty())
AppendError("Error: Failed to connect to RTSS shared memory!\nHints:\n-Install RivaTuner Statistics Server");
else
AppendError("Error: Failed to connect to RTSS shared memory!\nHints:\n-Press <Space> to start RivaTuner Statistics Server");
}
//TODO: 1-6-21 - I THOUGHT I FREAKING FIXED THIS??
if (m_sysLatOwnedSlot != 0) {
//AppendError("The SysLat client is unable to occupy RTSS client slot 0.\nThis may cause issues with the blinking square appearing in the corner.\nTo resolve this error try one of the following:\n\t1. Close other applications that use RTSS(such as MSI Afterburner)\n\t2. Restart RTSS\n\t3. Restart the testing phase(by pressing <F11>).");
}
}
}
void CSysLat_SoftwareDlg::R_DynamicComPortMenu()
{
CMenu* MainMenu = GetMenu();
CMenu* SettingsMenu = MainMenu->GetSubMenu(1);
CMenu* ComPortMenu = SettingsMenu->GetSubMenu(0);
if (ComPortMenu)
{
BOOL appended = false;
BOOL deleted = false;
m_COMPortCount = 0;
for(auto i = 0; i < m_COMPortInfo.GetSize(); i++) {
ComPortMenu->DeleteMenu(ID_COMPORT_START + m_COMPortCount, MF_BYCOMMAND);
if (m_COMPortCount < ID_COMPORT_END - ID_COMPORT_START) {
string usb_info = m_COMPortInfo[i].strFriendlyName;
DEBUG_PRINT("Friendly Name: " + usb_info)
appended = ComPortMenu->AppendMenu(MF_STRING, ID_COMPORT_START + m_COMPortCount, m_COMPortInfo[i].strFriendlyName);
string menuString = m_COMPortInfo[i].strFriendlyName;
size_t pos = menuString.rfind("(");
menuString.replace(0, pos + 1, "");
pos = menuString.rfind(")");
menuString.replace(pos, menuString.size(), "");
if (strcmp(menuString.c_str(), SysLatOpt.m_PortSpecifier.c_str()) == 0) {
MainMenu->CheckMenuItem(ID_COMPORT_START + m_COMPortCount, MF_CHECKED);
}
m_COMPortCount++;
}
else { //catch or throw errors here maybe?
break;
}
}
deleted = ComPortMenu->DeleteMenu(ID_USBPORT_PLACEHOLDER, MF_BYCOMMAND);
DEBUG_PRINT(("String appended: " + to_string(appended)).c_str())
DEBUG_PRINT(("Placeholder deleted: " + to_string(deleted)).c_str())
}
DrawMenuBar();
}
void CSysLat_SoftwareDlg::R_DynamicAppMenu()
{
CMenu* MainMenu = GetMenu();
CMenu* SettingsMenu = MainMenu->GetSubMenu(1);
CMenu* TargetAppMenu = SettingsMenu->GetSubMenu(1);
if (TargetAppMenu)
{
BOOL appended = false;
BOOL deleted = false;
int count = 0;
for (auto const& [pid, pName] : CRTSSClient::m_vszAppArr) {
SettingsMenu->DeleteMenu(ID_RTSSAPP_START + count, MF_BYCOMMAND);
if (count < ID_RTSSAPP_END - ID_RTSSAPP_START) {
if (pName != "SysLat_Software") {
string id_name = pName + " (" + to_string(pid) + ")";
appended = TargetAppMenu->AppendMenu(MF_STRING, ID_RTSSAPP_START + count, id_name.c_str());
if (strcmp(pName.c_str(), SysLatOpt.m_targetApp.c_str()) == 0) {
MainMenu->CheckMenuItem(ID_RTSSAPP_START + count, MF_CHECKED);
}
count++;
}
}
else { //error here?
break;
}
}
deleted = TargetAppMenu->DeleteMenu(ID_TARGETWINDOW_PLACEHOLDER, MF_BYCOMMAND);
DEBUG_PRINT(("String appended: " + to_string(appended)).c_str())
DEBUG_PRINT(("Placeholder deleted: " + to_string(deleted)).c_str())
}
DrawMenuBar();
}
void CSysLat_SoftwareDlg::AppendError(const CString& error)
{
m_strError.Append("\n");
m_strError.Append(error);
m_strError.Append("\n");
}
//SysLat thread functions
void CSysLat_SoftwareDlg::ReInitThread() {
m_loopSize = 0;
WaitForSingleObjectEx(m_drawingThreadHandle, INFINITE, false);
m_pOperatingSLD->m_targetApp = SysLatOpt.m_targetApp;
m_pOperatingSLD->SetEnd();
m_loopSize = 0xFFFFFFFF;
myCounter = 0;
//Save the data from the test that just completed in a vector of "SysLatData"s
m_vpPreviousSLD.push_back(m_pOperatingSLD);
m_pOperatingSLD = std::make_shared<CSysLatData>();
//Restart the thread
m_drawingThreadHandle = (HANDLE)_beginthreadex(0, 0, CreateDrawingThread, &myCounter, 0, 0);
SetThreadPriority(m_drawingThreadHandle, THREAD_PRIORITY_ABOVE_NORMAL);
//Convert the previous data to JSON
m_vpPreviousSLD.back()->CreateJSONSLD();
//Export and upload the data if enabled
if (PrivacyOpt.m_bAutoExportLogs && SysLatOpt.m_maxLogs > 0) {
ExportData();
}
if (PrivacyOpt.m_bAutoUploadLogs) {
UploadData();
}
}
unsigned int __stdcall CSysLat_SoftwareDlg::CreateDrawingThread(void* data) //this is probably dangerous, right?
{
int TIMEOUT = 5; //this should probably be a defined constant
int serialReadData = 0;
string sysLatResults;
CRTSSClient sysLatClient("SysLat", 0);
m_sysLatOwnedSlot = sysLatClient.ownedSlot;
//the following should not be here because if RTSS isn't running when this is hit, the version is "0"
m_pOperatingSLD->m_RTSSVersion = "v" + to_string(sysLatClient.sharedMemoryVersion);
CUSBController usbController;
HANDLE hPort = usbController.OpenComPort(SysLatOpt.m_PortSpecifier.c_str());
while (!usbController.IsComPortOpened(hPort) && m_loopSize > 0)
{
hPort = usbController.OpenComPort(SysLatOpt.m_PortSpecifier.c_str());
AppendError("Failed to open COM port: ");
AppendError(SysLatOpt.m_PortSpecifier.c_str());
Sleep(1000);
}
DrawSquare(sysLatClient, m_strBlack);
LARGE_INTEGER StartingTime, EndingTime, ElapsedMicrosecondsDraw, ElapsedMicrosecondsExtra, Frequency;
QueryPerformanceFrequency(&Frequency);
vector<long long> timeVectorDraw, timeVectorExtra;
for (unsigned int loopCounter = 0; loopCounter < m_loopSize; loopCounter++)
{
QueryPerformanceCounter(&StartingTime);
time_t start = time(NULL);
while (serialReadData != 65 && time(NULL) - start < TIMEOUT) {
serialReadData = usbController.ReadByte(hPort);
}
DrawSquare(sysLatClient, m_strWhite);
while (serialReadData != 66 && time(NULL) - start < TIMEOUT) {
serialReadData = usbController.ReadByte(hPort);
}
DrawSquare(sysLatClient, m_strBlack);
sysLatResults = "";
while (serialReadData != 67 && time(NULL) - start < TIMEOUT) {
serialReadData = usbController.ReadByte(hPort);
if (serialReadData != 67 && serialReadData != 65 && serialReadData != 66) {
sysLatResults += (char)serialReadData;
}
}
QueryPerformanceCounter(&EndingTime);
ElapsedMicrosecondsDraw.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
ElapsedMicrosecondsDraw.QuadPart *= 1000000;
ElapsedMicrosecondsDraw.QuadPart /= Frequency.QuadPart;
timeVectorDraw.push_back(ElapsedMicrosecondsDraw.QuadPart);
//I think everything below(ESPECIALLY the "UpdateSLD" method) should be happening in a different thread so that the serial reads can continue uninterrupted - could the following be a coroutine?
// 1-3-2021 thinking on this more, I need the following work to be "queued" up for the main thread... Not sure what the best way to accomplish that is.
// 1-13-2021 - After some extensive testing, these functions are taking anywhere from 100-500 microseconds(half of a milllisecond) to complete, and should not be affecting the test accuracy by very much... still needs to be fixed though
//push_back() lots of this stuff to a vector and then have the Refresh(?) function handle it?
QueryPerformanceCounter(&StartingTime);
string processName = GetProcessNameFromPID(CRTSSClient::GetLastForegroundAppID());
string activeWindowTitle;
if (loopCounter < m_loopSize) { //this was for a really strange issue when trying to end the thread.
activeWindowTitle = GetActiveWindowTitle();
}
else {
activeWindowTitle = "";
}
ProcessNameTrim(processName, activeWindowTitle);
//This does the same as the block above, but uses PID instead of a bunch of unnecessary string editing.
//Both of the following work? I bet there's another(probably better way) to use the class name instead of the macro-definition(?) "_WINUSER_".
//HWND hWnd = ::GetForegroundWindow();
HWND hWnd = _WINUSER_::GetForegroundWindow();
DWORD PID;
GetWindowThreadProcessId(hWnd, &PID);
DWORD RTSS_Pid = CRTSSClient::GetLastForegroundAppID();
m_pOperatingSLD->UpdateSLD(loopCounter, sysLatResults, processName, activeWindowTitle, PID, RTSS_Pid);
QueryPerformanceCounter(&EndingTime);
ElapsedMicrosecondsExtra.QuadPart = EndingTime.QuadPart - StartingTime.QuadPart;
ElapsedMicrosecondsExtra.QuadPart *= 1000000;
ElapsedMicrosecondsExtra.QuadPart /= Frequency.QuadPart;
timeVectorExtra.push_back(ElapsedMicrosecondsExtra.QuadPart);
//DEBUG_PRINT("Draw: \t" + to_string(ElapsedMicrosecondsDraw.QuadPart) + "\tExtra: \t" + to_string(ElapsedMicrosecondsExtra.QuadPart))
}
long long totalMicroseconds = 0;
long long averageMicroseconds = 0;
for (size_t i = 0; i < timeVectorDraw.size(); i++) {
totalMicroseconds += timeVectorDraw[i];
}
double averageMilliseconds;
if (timeVectorDraw.size() != 0) {
averageMicroseconds = totalMicroseconds / timeVectorDraw.size();
averageMilliseconds = static_cast<double>(averageMicroseconds) / 1000;
//DEBUG_PRINT("Draw Total microseconds: " + to_string(totalMicroseconds))
//DEBUG_PRINT("Draw Average microseconds: " + to_string(averageMicroseconds))
//DEBUG_PRINT("Draw Average milliseconds: " + to_string(averageMilliseconds))
}
totalMicroseconds = 0;
averageMicroseconds = 0;
if (timeVectorExtra.size() != 0) {
for (size_t i = 0; i < timeVectorExtra.size(); i++) {
totalMicroseconds += timeVectorExtra[i];
}
averageMicroseconds = totalMicroseconds / timeVectorExtra.size();
averageMilliseconds = static_cast<double>(averageMicroseconds) / 1000;
}
//DEBUG_PRINT("Extra Total microseconds: " + to_string(totalMicroseconds))
//DEBUG_PRINT("Extra Average microseconds: " + to_string(averageMicroseconds))
//DEBUG_PRINT("Extra Average milliseconds: " + to_string(averageMilliseconds))
usbController.CloseComPort(hPort);
sysLatClient.ReleaseOSD();
return 0;
}
void CSysLat_SoftwareDlg::DrawSquare(CRTSSClient sysLatClient, CString& colorString)
{
m_updateString = "";
//The following conditional is FAR from perfect... In order for it to work properly I may need to count the number of rows and columns(in zoomed pixel units?) and use that value.
if (sysLatClient.ownedSlot == 0 && !RTSSOpt.m_bPositionManualOverride) {
if ((int)RTSSOpt.m_positionX < 0) {
RTSSOpt.m_internalX = 0;
}
if ((int)RTSSOpt.m_positionY < 0) {
//y = CRTSSClient::clientsNum * 20;
RTSSOpt.m_internalY = 20;
}
m_updateString.AppendFormat("<P=%d,%d>", RTSSOpt.m_internalX, RTSSOpt.m_internalY);
m_updateString += colorString;
m_updateString += "<P=0,0>";
}
else if (RTSSOpt.m_bPositionManualOverride) {
m_updateString.AppendFormat("<P=%d,%d>", RTSSOpt.m_internalX, RTSSOpt.m_internalY);
m_updateString += colorString;
m_updateString += "<P=0,0>";
}
else {
m_updateString += colorString;
}
sysLatClient.UpdateOSD(m_updateString);
}
//Dialog menu functions
//Tools
//The version without a parameter uses the other classes "ExportData" functions
void CSysLat_SoftwareDlg::ExportData()
{
if (m_vpPreviousSLD.size() > 0) {
for (unsigned int i = 0; i < m_vpPreviousSLD.size(); i++) {
//The following code is for testing file export changes - maybe I should make it run only in debugMode?
//Json::Value newJSON;
//const Json::Value* const sources[] = {
// &m_previousSLD[i]->m_JSONsld,
// &m_hardwareID.HardwareIDJSON,
// &m_machineInfo.MachineInfoJSON