forked from Skewjo/SysLat_Software
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRTSSClient.cpp
More file actions
513 lines (405 loc) · 14.5 KB
/
RTSSClient.cpp
File metadata and controls
513 lines (405 loc) · 14.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
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
#include "stdafx.h"
#include "RTSSClient.h"
#include "RTSSSharedMemory.h"
//static member initializations
DWORD CRTSSClient::sharedMemoryVersion = 0;
CRTSSProfileInterface CRTSSClient::m_profileInterface;
CString CRTSSClient::m_strInstallPath = "";
DWORD CRTSSClient::clientsNum = 0;
DWORD CRTSSClient::dwAppEntries = 0;
vector<std::pair<DWORD, string>> CRTSSClient::m_vszAppArr = {};
CRTSSClient::CRTSSClient(const char* setSlotOwner, int setClientPriority) {
slotOwnerOSD = setSlotOwner;
clientPriority = setClientPriority;
InitRTSSInterface();
}
void CRTSSClient::InitRTSSInterface() {
//init RivaTuner Statistics Server installation path - this needs to be occurring somewhere else...
if (m_strInstallPath.IsEmpty())
{
HKEY hKey;
if (ERROR_SUCCESS == RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Unwinder\\RTSS", &hKey))
{
char buf[MAX_PATH];
DWORD dwSize = MAX_PATH;
DWORD dwType;
if (ERROR_SUCCESS == RegQueryValueEx(hKey, "InstallPath", 0, &dwType, (LPBYTE)buf, &dwSize))
{
if (dwType == REG_SZ)
m_strInstallPath = buf;
}
RegCloseKey(hKey);
}
}
//validate RivaTuner Statistics Server installation path
if (_taccess(m_strInstallPath, 0))
m_strInstallPath = "";
if (!CRTSSClient::m_profileInterface.IsInitialized())
CRTSSClient::m_profileInterface.Init(m_strInstallPath);
//If the profile interface initialized, then initialize sharedMemoryVersion as well
if (m_profileInterface.IsInitialized()) {
DWORD dwResult = 0;
HANDLE hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, "RTSSSharedMemoryV2");
if (hMapFile)
{
LPVOID pMapAddr = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
LPRTSS_SHARED_MEMORY pMem = (LPRTSS_SHARED_MEMORY)pMapAddr;
if (pMem)
{
if ((pMem->dwSignature == 'RTSS') &&
(pMem->dwVersion >= 0x00020000))
dwResult = pMem->dwVersion;
UnmapViewOfFile(pMapAddr);
}
CloseHandle(hMapFile);
}
sharedMemoryVersion = dwResult;
}
}
DWORD CRTSSClient::GetProfileProperty(LPCSTR lpProfile, LPCSTR lpProfileProperty)
{
DWORD dwProperty = 0;
if (m_profileInterface.IsInitialized())
{
m_profileInterface.LoadProfile(lpProfile);
m_profileInterface.GetProfileProperty(lpProfileProperty, (LPBYTE)&dwProperty, sizeof(dwProperty));
}
return dwProperty;
}
void CRTSSClient::IncProfileProperty(LPCSTR lpProfile, LPCSTR lpProfileProperty, LONG dwIncrement)
{
if (m_profileInterface.IsInitialized())
{
m_profileInterface.LoadProfile(lpProfile);
LONG dwProperty = 0;
if (m_profileInterface.GetProfileProperty(lpProfileProperty, (LPBYTE)&dwProperty, sizeof(dwProperty)))
{
dwProperty += dwIncrement;
m_profileInterface.SetProfileProperty(lpProfileProperty, (LPBYTE)&dwProperty, sizeof(dwProperty));
m_profileInterface.SaveProfile(lpProfile);
m_profileInterface.UpdateProfiles();
}
}
}
void CRTSSClient::SetProfileProperty(LPCSTR lpProfile, LPCSTR lpProfileProperty, DWORD dwProperty)
{
if (m_profileInterface.IsInitialized())
{
m_profileInterface.LoadProfile(lpProfile);
m_profileInterface.SetProfileProperty(lpProfileProperty, (LPBYTE)&dwProperty, sizeof(dwProperty));
m_profileInterface.SaveProfile(lpProfile);
m_profileInterface.UpdateProfiles();
}
}
DWORD CRTSSClient::GetClientsNum() {
DWORD dwClients = 0;
HANDLE hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, "RTSSSharedMemoryV2");
if (hMapFile)
{
LPVOID pMapAddr = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
LPRTSS_SHARED_MEMORY pMem = (LPRTSS_SHARED_MEMORY)pMapAddr;
if (pMem)
{
if ((pMem->dwSignature == 'RTSS') &&
(pMem->dwVersion >= 0x00020000))
{
for (DWORD dwEntry = 0; dwEntry < pMem->dwOSDArrSize; dwEntry++)
{
RTSS_SHARED_MEMORY::LPRTSS_SHARED_MEMORY_OSD_ENTRY pEntry = (RTSS_SHARED_MEMORY::LPRTSS_SHARED_MEMORY_OSD_ENTRY)((LPBYTE)pMem + pMem->dwOSDArrOffset + dwEntry * pMem->dwOSDEntrySize);
if (strlen(pEntry->szOSDOwner))
dwClients++;
}
}
UnmapViewOfFile(pMapAddr);
}
CloseHandle(hMapFile);
}
return dwClients;
}
DWORD CRTSSClient::GetSharedMemoryVersion()
{
if (!sharedMemoryVersion) {
InitRTSSInterface();
}
return sharedMemoryVersion;
}
DWORD CRTSSClient::GetLastForegroundApp()
{
DWORD dwResult = 0;
HANDLE hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, "RTSSSharedMemoryV2");
if (hMapFile)
{
LPVOID pMapAddr = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
LPRTSS_SHARED_MEMORY pMem = (LPRTSS_SHARED_MEMORY)pMapAddr;
if (pMem)
{
if ((pMem->dwSignature == 'RTSS') &&
(pMem->dwVersion >= 0x00020000))
dwResult = pMem->dwLastForegroundApp;
UnmapViewOfFile(pMapAddr);
}
CloseHandle(hMapFile);
}
return dwResult;
}
DWORD CRTSSClient::GetLastForegroundAppID()
{
DWORD dwResult = 0;
HANDLE hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, "RTSSSharedMemoryV2");
if (hMapFile)
{
LPVOID pMapAddr = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
LPRTSS_SHARED_MEMORY pMem = (LPRTSS_SHARED_MEMORY)pMapAddr;
if (pMem)
{
if ((pMem->dwSignature == 'RTSS') &&
(pMem->dwVersion >= 0x00020000))
dwResult = pMem->dwLastForegroundAppProcessID;
UnmapViewOfFile(pMapAddr);
}
CloseHandle(hMapFile);
}
return dwResult;
}
DWORD CRTSSClient::GetAppArray() {
HANDLE hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, "RTSSSharedMemoryV2");
if (hMapFile)
{
LPVOID pMapAddr = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
LPRTSS_SHARED_MEMORY pMem = (LPRTSS_SHARED_MEMORY)pMapAddr;
if (pMem)
{
if ((pMem->dwSignature == 'RTSS') && (pMem->dwVersion >= 0x00020000)) {
//Check if RTSS detected or removed any programs....
int count = 0;
for (DWORD dwEntry = 0; dwEntry < pMem->dwAppArrSize; dwEntry++) {
RTSS_SHARED_MEMORY::LPRTSS_SHARED_MEMORY_APP_ENTRY pEntry = (RTSS_SHARED_MEMORY::LPRTSS_SHARED_MEMORY_APP_ENTRY)((LPBYTE)pMem + pMem->dwAppArrOffset + dwEntry * pMem->dwAppEntrySize);
if (strlen(pEntry->szName) && pEntry->dwProcessID != 0) {
count++;
}
else if(strlen(pEntry->szName) == 0){
break;
}
}
//If so, add them to the map we'll use to populate the menu
if (dwAppEntries != count) {
dwAppEntries = 0;
m_vszAppArr.clear();
for (DWORD dwEntry = 0; dwEntry < pMem->dwAppArrSize; dwEntry++)
{
RTSS_SHARED_MEMORY::LPRTSS_SHARED_MEMORY_APP_ENTRY pEntry = (RTSS_SHARED_MEMORY::LPRTSS_SHARED_MEMORY_APP_ENTRY)((LPBYTE)pMem + pMem->dwAppArrOffset + dwEntry * pMem->dwAppEntrySize);
DEBUG_PRINT("PID: " + to_string(pEntry->dwProcessID) + " Name: " + pEntry->szName)
if (strlen(pEntry->szName) && pEntry->dwProcessID != 0) {
dwAppEntries++;
string entryName = pEntry->szName;
SL::RemoveExtension(entryName);
SL::RemovePath(entryName);
m_vszAppArr.push_back(std::pair(pEntry->dwProcessID, entryName));
}
else if (strlen(pEntry->szName) == 0) {
break;
}
}
for (auto const& [pid, pName] : m_vszAppArr) {
DEBUG_PRINT("PID: " + to_string(pid) + " Name: " + pName)
}
}
}
//else return version error?
UnmapViewOfFile(pMapAddr);
}
CloseHandle(hMapFile);
}
return dwAppEntries;
}
BOOL CRTSSClient::UpdateOSD(LPCSTR lpText) {
BOOL bResult = FALSE;
//Doesn't it seem inefficient to open a handle to the shared memory every time? Can I not just leave it open?
HANDLE hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, "RTSSSharedMemoryV2");
if (hMapFile)
{
LPVOID pMapAddr = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
LPRTSS_SHARED_MEMORY pMem = (LPRTSS_SHARED_MEMORY)pMapAddr;
if (pMem)
{
if ((pMem->dwSignature == 'RTSS') &&
(pMem->dwVersion >= 0x00020000))
{
for (DWORD dwPass = 0; dwPass < 2; dwPass++)
//1st pass : find previously captured OSD slot
//2nd pass : otherwise find the first unused OSD slot and capture it
{
//If the caller is "SysLat" allow it to take over the first OSD slot
DWORD dwEntry;
if (clientPriority == 0) {
dwEntry = 0;
}
else {
dwEntry = 1;
}
for (dwEntry; dwEntry < pMem->dwOSDArrSize; dwEntry++)
//allow primary OSD clients (e.g. EVGA Precision / MSI Afterburner) to use the first slot exclusively, so third party
//applications start scanning the slots from the second one - CHANGED THIS TO 0 SO I CAN BE PRIMARY BECAUSE I NEED THE CORNERS
{
RTSS_SHARED_MEMORY::LPRTSS_SHARED_MEMORY_OSD_ENTRY pEntry = (RTSS_SHARED_MEMORY::LPRTSS_SHARED_MEMORY_OSD_ENTRY)((LPBYTE)pMem + pMem->dwOSDArrOffset + dwEntry * pMem->dwOSDEntrySize);
if (dwPass)
{
if (!strlen(pEntry->szOSDOwner))
strcpy_s(pEntry->szOSDOwner, sizeof(pEntry->szOSDOwner), slotOwnerOSD);
}
//remember that strcmp returns 0 if the strings match... so the following if statement basically says if the strings match
if (!strcmp(pEntry->szOSDOwner, slotOwnerOSD))
{
if (pMem->dwVersion >= 0x00020007)
//use extended text slot for v2.7 and higher shared memory, it allows displaying 4096 symbols
//instead of 256 for regular text slot
{
if (pMem->dwVersion >= 0x0002000e)
//OSD locking is supported on v2.14 and higher shared memory
{
DWORD dwBusy = _interlockedbittestandset(&pMem->dwBusy, 0);
//bit 0 of this variable will be set if OSD is locked by renderer and cannot be refreshed
//at the moment
if (!dwBusy)
{
strncpy_s(pEntry->szOSDEx, sizeof(pEntry->szOSDEx), lpText, sizeof(pEntry->szOSDEx) - 1);
pMem->dwBusy = 0;
}
}
else
strncpy_s(pEntry->szOSDEx, sizeof(pEntry->szOSDEx), lpText, sizeof(pEntry->szOSDEx) - 1);
}
else
strncpy_s(pEntry->szOSD, sizeof(pEntry->szOSD), lpText, sizeof(pEntry->szOSD) - 1);
pMem->dwOSDFrame++;
bResult = TRUE;
ownedSlot = dwEntry;
//I really don't like how I'm storing this... I feel like I should be using a different var
clientsNum = pMem->dwOSDArrSize;
break;
}
}
if (bResult)
break;
}
}
UnmapViewOfFile(pMapAddr);
}
CloseHandle(hMapFile);
}
return bResult;
}
void CRTSSClient::ReleaseOSD()
{
HANDLE hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, "RTSSSharedMemoryV2");
if (hMapFile)
{
LPVOID pMapAddr = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
LPRTSS_SHARED_MEMORY pMem = (LPRTSS_SHARED_MEMORY)pMapAddr;
if (pMem)
{
if ((pMem->dwSignature == 'RTSS') &&
(pMem->dwVersion >= 0x00020000))
{
for (DWORD dwEntry = 0; dwEntry < pMem->dwOSDArrSize; dwEntry++)
{
RTSS_SHARED_MEMORY::LPRTSS_SHARED_MEMORY_OSD_ENTRY pEntry = (RTSS_SHARED_MEMORY::LPRTSS_SHARED_MEMORY_OSD_ENTRY)((LPBYTE)pMem + pMem->dwOSDArrOffset + dwEntry * pMem->dwOSDEntrySize);
if (!strcmp(pEntry->szOSDOwner, slotOwnerOSD))
{
memset(pEntry, 0, pMem->dwOSDEntrySize);
pMem->dwOSDFrame++;
}
}
}
UnmapViewOfFile(pMapAddr);
}
CloseHandle(hMapFile);
}
}
DWORD CRTSSClient::EmbedGraph(DWORD dwOffset, FLOAT* lpBuffer, DWORD dwBufferPos, DWORD dwBufferSize, LONG dwWidth, LONG dwHeight, LONG dwMargin, FLOAT fltMin, FLOAT fltMax, DWORD dwFlags)
{
DWORD dwResult = 0;
HANDLE hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, "RTSSSharedMemoryV2");
if (hMapFile)
{
LPVOID pMapAddr = MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, 0);
LPRTSS_SHARED_MEMORY pMem = (LPRTSS_SHARED_MEMORY)pMapAddr;
if (pMem)
{
if ((pMem->dwSignature == 'RTSS') &&
(pMem->dwVersion >= 0x00020000))
{
for (DWORD dwPass = 0; dwPass < 2; dwPass++)
//1st pass : find previously captured OSD slot
//2nd pass : otherwise find the first unused OSD slot and capture it
{
for (DWORD dwEntry = 1; dwEntry < pMem->dwOSDArrSize; dwEntry++)
//allow primary OSD clients (i.e. EVGA Precision / MSI Afterburner) to use the first slot exclusively, so third party
//applications start scanning the slots from the second one
{
RTSS_SHARED_MEMORY::LPRTSS_SHARED_MEMORY_OSD_ENTRY pEntry = (RTSS_SHARED_MEMORY::LPRTSS_SHARED_MEMORY_OSD_ENTRY)((LPBYTE)pMem + pMem->dwOSDArrOffset + dwEntry * pMem->dwOSDEntrySize);
if (dwPass)
{
if (!strlen(pEntry->szOSDOwner))
strcpy_s(pEntry->szOSDOwner, sizeof(pEntry->szOSDOwner), "RTSSSharedMemorySample");
}
if (!strcmp(pEntry->szOSDOwner, "RTSSSharedMemorySample"))
{
if (pMem->dwVersion >= 0x0002000c)
//embedded graphs are supported for v2.12 and higher shared memory
{
if (dwOffset + sizeof(RTSS_EMBEDDED_OBJECT_GRAPH) + dwBufferSize * sizeof(FLOAT) > sizeof(pEntry->buffer))
//validate embedded object offset and size and ensure that we don't overrun the buffer
{
UnmapViewOfFile(pMapAddr);
CloseHandle(hMapFile);
return 0;
}
LPRTSS_EMBEDDED_OBJECT_GRAPH lpGraph = (LPRTSS_EMBEDDED_OBJECT_GRAPH)(pEntry->buffer + dwOffset);
//get pointer to object in buffer
lpGraph->header.dwSignature = RTSS_EMBEDDED_OBJECT_GRAPH_SIGNATURE;
lpGraph->header.dwSize = sizeof(RTSS_EMBEDDED_OBJECT_GRAPH) + dwBufferSize * sizeof(FLOAT);
lpGraph->header.dwWidth = dwWidth;
lpGraph->header.dwHeight = dwHeight;
lpGraph->header.dwMargin = dwMargin;
lpGraph->dwFlags = dwFlags;
lpGraph->fltMin = fltMin;
lpGraph->fltMax = fltMax;
lpGraph->dwDataCount = dwBufferSize;
if (lpBuffer && dwBufferSize)
{
for (DWORD dwPos = 0; dwPos < dwBufferSize; dwPos++)
{
FLOAT fltData = lpBuffer[dwBufferPos];
lpGraph->fltData[dwPos] = (fltData == FLT_MAX) ? 0 : fltData;
dwBufferPos = (dwBufferPos + 1) & (dwBufferSize - 1);
}
}
dwResult = lpGraph->header.dwSize;
}
break;
}
}
if (dwResult)
break;
}
}
UnmapViewOfFile(pMapAddr);
}
CloseHandle(hMapFile);
}
return dwResult;
}
void CRTSSClient::GetOSDText(CGroupedString& osd, BOOL bFormatTagsSupported, BOOL bObjTagsSupported)
{
if (bFormatTagsSupported && bObjTagsSupported)
{
//if (GetClientsNum() == 1)
//osd.Add("<P=0,10>", "Skewjo's stuff", "|");
//osd.Add("<P=0,10>", "", "|");
//move to position 0,10 (in zoomed pixel units)
//Note: take a note that position is specified in absolute coordinates so use this tag with caution because your text may
//overlap with text slots displayed by other applications, so in this demo we explicitly disable this tag usage if more than
//one client is currently rendering something in OSD
}
}