-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathRTSS_Crosshair.cpp
335 lines (275 loc) · 8.99 KB
/
RTSS_Crosshair.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
#include <windows.h>
#include "RTSSSharedMemory.h"
#pragma comment(linker, "/SUBSYSTEM:WINDOWS")
#pragma comment(linker, "/NODEFAULTLIB")
#pragma comment(linker, "/INCREMENTAL:NO")
#define Map1 "RTSS_Crosshair_Overlay"
#define Map2 "RTSS_Crosshair_Placeholder"
typedef struct {
HANDLE hMapFile;
LPRTSS_SHARED_MEMORY pMem;
} THREAD_PARAM;
THREAD_PARAM param;
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
DWORD WINAPI ThreadProc(LPVOID param);
BOOL UpdateOSD(LPCSTR lpText, LPCSTR mapName);
void ReleaseOSD(LPCSTR mapName);
void WinMainCRTStartup()
{
LPCTSTR pClassName = TEXT("RTSS_Crosshair");
HINSTANCE hinst;
MSG msg;
HWND hwnd;
WNDCLASSEX wc;
hinst = GetModuleHandle(NULL);
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WindowProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hinst;
wc.hIcon = NULL;
wc.hIconSm = NULL;
wc.hCursor = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = pClassName;
if (!RegisterClassEx(&wc)) return;
hwnd = CreateWindow(
pClassName,
TEXT("RTSS Crosshair"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
425,
GetSystemMetrics(SM_CYMIN) + 126,
NULL,
NULL,
hinst,
NULL );
if (!hwnd) return;
ShowWindow(hwnd, SW_SHOW);
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return;
}
DWORD WINAPI ThreadProc(LPVOID param)
{
int crossX = 0, crossY = 0, crossSize = 100;
bool changeState = false;
TCHAR crossFormat[256];
DWORD tempVar;
DWORD tempSize = sizeof(tempVar);
if (ERROR_SUCCESS == RegGetValueA(HKEY_CURRENT_USER, "Software\\RTSS_Crosshair", "x_coord", RRF_RT_REG_DWORD, NULL, &tempVar, &tempSize)) {
crossX = tempVar;
changeState = true;
tempSize = sizeof(tempVar);
}
if (ERROR_SUCCESS == RegGetValueA(HKEY_CURRENT_USER, "Software\\RTSS_Crosshair", "y_coord", RRF_RT_REG_DWORD, NULL, &tempVar, &tempSize)) {
crossY = tempVar;
changeState = true;
tempSize = sizeof(tempVar);
}
if (ERROR_SUCCESS == RegGetValueA(HKEY_CURRENT_USER, "Software\\RTSS_Crosshair", "size", RRF_RT_REG_DWORD, NULL, &tempVar, &tempSize)) {
crossSize = tempVar;
changeState = true;
}
while (TRUE) {
if (GetAsyncKeyState(VK_RCONTROL) && GetAsyncKeyState(VK_DECIMAL)) {
RegSetKeyValueA(HKEY_CURRENT_USER, "Software\\RTSS_Crosshair", "x_coord", REG_DWORD, &crossX, (DWORD)sizeof(crossX));
RegSetKeyValueA(HKEY_CURRENT_USER, "Software\\RTSS_Crosshair", "y_coord", REG_DWORD, &crossY, (DWORD)sizeof(crossY));
RegSetKeyValueA(HKEY_CURRENT_USER, "Software\\RTSS_Crosshair", "size", REG_DWORD, &crossSize, (DWORD)sizeof(crossSize));
}
if (GetAsyncKeyState(VK_RCONTROL) && GetAsyncKeyState(VK_NUMPAD5)) {
RECT rect;
if (GetClientRect(GetForegroundWindow(), &rect)) //GetClientRect GetWindowRect
{
crossX = (rect.right - rect.left) / 2;
crossY = (rect.bottom - rect.top) / 2;
changeState = true;
}
}
if (GetAsyncKeyState(VK_RCONTROL) && GetAsyncKeyState(VK_NUMPAD0)) {
crossX = 0;
crossY = 0;
crossSize = 100;
changeState = true;
}
if (GetAsyncKeyState(VK_RSHIFT) && GetAsyncKeyState(VK_NUMPAD4)) {
crossX -= 50;
changeState = true;
}
if (GetAsyncKeyState(VK_RSHIFT) && GetAsyncKeyState(VK_NUMPAD6)) {
crossX += 50;
changeState = true;
}
if (GetAsyncKeyState(VK_RSHIFT) && GetAsyncKeyState(VK_NUMPAD8)) {
crossY -= 50;
changeState = true;
}
if (GetAsyncKeyState(VK_RSHIFT) && GetAsyncKeyState(VK_NUMPAD2)) {
crossY += 50;
changeState = true;
}
if (GetAsyncKeyState(VK_RCONTROL) && GetAsyncKeyState(VK_NUMPAD4)) {
crossX--;
changeState = true;
}
if (GetAsyncKeyState(VK_RCONTROL) && GetAsyncKeyState(VK_NUMPAD6)) {
crossX++;
changeState = true;
}
if (GetAsyncKeyState(VK_RCONTROL) && GetAsyncKeyState(VK_NUMPAD8)) {
crossY--;
changeState = true;
}
if (GetAsyncKeyState(VK_RCONTROL) && GetAsyncKeyState(VK_NUMPAD2)) {
crossY++;
changeState = true;
}
if (GetAsyncKeyState(VK_RCONTROL) && GetAsyncKeyState(VK_ADD)) {
crossSize += 5;
changeState = true;
}
if (GetAsyncKeyState(VK_RCONTROL) && GetAsyncKeyState(VK_SUBTRACT)) {
crossSize -= 5;
changeState = true;
}
if (changeState) {
changeState = false;
wsprintf(crossFormat, TEXT("<P=%d,%d><S=%d>+"), crossX, crossY, crossSize);
UpdateOSD(crossFormat, Map1);
UpdateOSD("<P=0,0><S=100>", Map2);
}
Sleep(100);
}
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
{
switch (msg) {
case WM_DESTROY:
ReleaseOSD(Map1);
ReleaseOSD(Map2);
ExitProcess(0);
break;
case WM_CREATE:
CreateWindow(
TEXT("EDIT"),
"RightCtrl + Num0 - Default settings \r\n"
"RightCtrl + Num5 - Center \r\n"
"RightCtrl + Num1, Num4, Num6, Num8 - move by 1 pixel \r\n"
"Num1, Num4, Num6, Num8 + RightShift - move by 50 pixels \r\n"
"RightCtrl + Num+ - increase size by 10% \r\n"
"RightCtrl + Num- - decrease size by 10% \r\n"
"RightCtrl + Num. - save current position in registry",
WS_CHILD | WS_VISIBLE | WS_BORDER | ES_LEFT | ES_MULTILINE | ES_READONLY, //
2, 2, 400, 122,
hwnd, (HMENU)1,
((LPCREATESTRUCT)(lp))->hInstance, NULL
);
UpdateOSD("+", Map1);
UpdateOSD("", Map2);
DWORD dwThreadId;
CreateThread(NULL, 0, ThreadProc, NULL, 0, &dwThreadId);
break;
default:
return DefWindowProc(hwnd, msg, wp, lp);
}
return 0;
}
/////////////////////////////////////////////////////////////////////////////
BOOL UpdateOSD(LPCSTR lpText, LPCSTR mapName)
{
BOOL bResult = FALSE;
HANDLE hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, TEXT("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 (!lstrlen(pEntry->szOSDOwner))
lstrcpy(pEntry->szOSDOwner, mapName);
}
if (!lstrcmp(pEntry->szOSDOwner, mapName))
{
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)
{
lstrcpyn(pEntry->szOSDEx, lpText, sizeof(pEntry->szOSDEx) - 1);
pMem->dwBusy = 0;
}
}
else
lstrcpyn(pEntry->szOSDEx, lpText, sizeof(pEntry->szOSDEx) - 1);
}
else
lstrcpyn(pEntry->szOSD, lpText, sizeof(pEntry->szOSD) - 1);
pMem->dwOSDFrame++;
bResult = TRUE;
break;
}
}
if (bResult)
break;
}
}
UnmapViewOfFile(pMapAddr);
}
CloseHandle(hMapFile);
}
return bResult;
}
/////////////////////////////////////////////////////////////////////////////
void ReleaseOSD(LPCSTR mapName)
{
HANDLE hMapFile = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, TEXT("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 (!lstrcmp(pEntry->szOSDOwner, mapName))
{
SecureZeroMemory(pEntry, pMem->dwOSDEntrySize);
pMem->dwOSDFrame++;
}
}
}
UnmapViewOfFile(pMapAddr);
}
CloseHandle(hMapFile);
}
}