-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRTSSSharedMemoryInterface.cpp
More file actions
226 lines (186 loc) · 7.03 KB
/
RTSSSharedMemoryInterface.cpp
File metadata and controls
226 lines (186 loc) · 7.03 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
// RTSSSharedMemoryInterface.cpp: implementation of the CRTSSSharedMemoryInterface class.
//
// created by Unwinder
//////////////////////////////////////////////////////////////////////
#include "pch.h"
#include "RTSSSharedMemoryInterface.h"
#include <RTSSSharedMemory.h>
#include <float.h>
//////////////////////////////////////////////////////////////////////
CRTSSSharedMemoryInterface::CRTSSSharedMemoryInterface()
{
}
//////////////////////////////////////////////////////////////////////
CRTSSSharedMemoryInterface::~CRTSSSharedMemoryInterface()
{
}
//////////////////////////////////////////////////////////////////////
BOOL CRTSSSharedMemoryInterface::UpdateOSD(LPCSTR lpOSDOwner, LPCSTR lpText)
{
BOOL bResult = FALSE;
HANDLE hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, L"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), lpOSDOwner);
}
if (!strcmp(pEntry->szOSDOwner, lpOSDOwner))
{
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;
break;
}
}
if (bResult)
break;
}
}
UnmapViewOfFile(pMapAddr);
}
CloseHandle(hMapFile);
}
return bResult;
}
//////////////////////////////////////////////////////////////////////
void CRTSSSharedMemoryInterface::ReleaseOSD(LPCSTR lpOSDOwner)
{
HANDLE hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, L"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=1; 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, lpOSDOwner))
{
memset(pEntry, 0, pMem->dwOSDEntrySize);
pMem->dwOSDFrame++;
}
}
}
UnmapViewOfFile(pMapAddr);
}
CloseHandle(hMapFile);
}
}
//////////////////////////////////////////////////////////////////////
DWORD CRTSSSharedMemoryInterface::EmbedGraph(LPCSTR lpOSDOwner, 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, L"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), lpOSDOwner);
}
if (!strcmp(pEntry->szOSDOwner, lpOSDOwner))
{
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;
}
//////////////////////////////////////////////////////////////////////