-
Notifications
You must be signed in to change notification settings - Fork 0
/
Functions.cpp
323 lines (274 loc) · 8.46 KB
/
Functions.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
// This is a personal academic project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++, C#, and Java: http://www.viva64.com
#include "Functions.h"
#include <assert.h>
#include <dxgi1_6.h>
#pragma comment (lib, "dxgi.lib")
#if (SIMPLE_MODE == 1)
//do it via right-click toggling the option
void DoProcessWork(bool toggle, const std::pair<float, float>& scale_desktop, const POINT& saved_mousePos)
{
std::pair<LONG, LONG> idxs;
HWND focus_wnd = GetForegroundWindow();
INPUT Inputs[3] = { 0 };
Sleep(SLEEP_INTERVAL_STEP4);
//right-click at pos [x,y]
idxs = std::pair<LONG, LONG>(DISPCAL_ICON_LOC);
Inputs[0].type = INPUT_MOUSE;
Inputs[0].mi.dx = LONG(scale_desktop.first * idxs.first); // desired X coordinate
Inputs[0].mi.dy = LONG(scale_desktop.second * idxs.second); // desired Y coordinate
Inputs[0].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_VIRTUALDESK | MOUSEEVENTF_MOVE;
Inputs[1].type = INPUT_MOUSE;
Inputs[1].mi.dwFlags = MOUSEEVENTF_RIGHTDOWN;
Inputs[2].type = INPUT_MOUSE;
Inputs[2].mi.dwFlags = MOUSEEVENTF_RIGHTUP;
SendInput(3, Inputs, sizeof(INPUT));
Sleep(SLEEP_INTERVAL_STEP4);
if (toggle)
{
WriteErrorMessage("DisplayCal Enabled");
//click at top option
idxs = std::pair<LONG, LONG>(DISPCAL_LOAD_CAL_LOC);
Inputs[0].type = INPUT_MOUSE;
Inputs[0].mi.dx = LONG(scale_desktop.first * idxs.first); // desired X coordinate
Inputs[0].mi.dy = LONG(scale_desktop.second * idxs.second); // desired Y coordinate
Inputs[0].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_VIRTUALDESK | MOUSEEVENTF_MOVE;
Inputs[1].type = INPUT_MOUSE;
Inputs[1].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
Inputs[2].type = INPUT_MOUSE;
Inputs[2].mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(3, Inputs, sizeof(INPUT));
}
else
{
WriteErrorMessage("DisplayCal Disabled");
//click at second option
idxs = std::pair<LONG, LONG>(DISPCAL_RESET_TABLE_LOC);
Inputs[0].type = INPUT_MOUSE;
Inputs[0].mi.dx = LONG(scale_desktop.first * idxs.first); // desired X coordinate
Inputs[0].mi.dy = LONG(scale_desktop.second * idxs.second); // desired Y coordinate
Inputs[0].mi.dwFlags = MOUSEEVENTF_ABSOLUTE | MOUSEEVENTF_VIRTUALDESK | MOUSEEVENTF_MOVE;
Inputs[1].type = INPUT_MOUSE;
Inputs[1].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
Inputs[2].type = INPUT_MOUSE;
Inputs[2].mi.dwFlags = MOUSEEVENTF_LEFTUP;
SendInput(3, Inputs, sizeof(INPUT));
}
//reset back to where user was before we interfered
SetCursorPos(saved_mousePos.x, saved_mousePos.y);
SetForegroundWindow(focus_wnd);
}
#else
#include <TlHelp32.h>
#include <algorithm>
//do it via scripting
void DoProcessWork(const wchar_t* str, const std::pair<float, float>&, const POINT&)
{
std::wstring wide(str);
std::string str1;
std::transform(wide.begin(), wide.end(), std::back_inserter(str1), [](wchar_t c) {
return (char)c;
});
WriteErrorMessage(str1);
KillProcess(APP_NAME_EXE);
Sleep(10);
remove(path_to_lock_file.c_str());
startup(SCRIPT_PATH);
Sleep(SLEEP_INTERVAL_STEP1);
SendKeystrokesToNotepad(str);
Sleep(SLEEP_INTERVAL_STEP2);
KillProcess(APP_NAME_EXE);
}
VOID startup(const char* lpApplicationName)
{
// additional information
STARTUPINFOA si;
PROCESS_INFORMATION pi;
// set the size of the structures
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
// start the program up
CreateProcessA(lpApplicationName, // the path
NULL, // Command line
NULL, // Process handle not inheritable
NULL, // Thread handle not inheritable
FALSE, // Set handle inheritance to FALSE
0, // No creation flags
NULL, // Use parent's environment block
NULL, // Use parent's starting directory
&si, // Pointer to STARTUPINFO structure
&pi // Pointer to PROCESS_INFORMATION structure (removed extra parentheses)
);
// Close process and thread handles.
CloseHandle(pi.hProcess);
CloseHandle(pi.hThread);
}
DWORD GetProcessIdFromName(LPCTSTR szProcessName)
{
PROCESSENTRY32 entry;
entry.dwSize = sizeof(PROCESSENTRY32);
HANDLE snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (Process32First(snapshot, &entry) != FALSE)
while (Process32Next(snapshot, &entry) != FALSE)
if (!_wcsicmp(entry.szExeFile, szProcessName))
{
CloseHandle(snapshot);
return entry.th32ProcessID;
}
CloseHandle(snapshot);
return 0;
}
HWND GetWindowHandle()
{
HWND handle = FindWindow(NULL, APP_NAME);
uint32_t waited_ms = 0;
while (handle == NULL && waited_ms < MAX_WAIT_TIMER)
{
Sleep(SLEEP_INTERVAL_STEP3);
waited_ms += SLEEP_INTERVAL_STEP3;
handle = FindWindow(NULL, APP_NAME);
}
return handle;
}
bool SetHWNDasForeground(HWND wnd)
{
bool has_been_set = SetForegroundWindow(wnd);
uint32_t waited_ms = 0;
while (has_been_set == false && waited_ms < MAX_WAIT_TIMER)
{
Sleep(SLEEP_INTERVAL_STEP3);
waited_ms += SLEEP_INTERVAL_STEP3;
has_been_set = SetForegroundWindow(wnd);
}
return has_been_set;
}
void SendKeystrokesToNotepad(const TCHAR* const text)
{
INPUT* keystroke;
size_t i, character_count, keystrokes_to_send;
HWND notepad;
assert(text != NULL);
//Fill in the array of keystrokes to send.
character_count = _tcslen(text);
keystrokes_to_send = (character_count + 1) * 2 + 1;
keystroke = new INPUT[keystrokes_to_send]{};
for (i = 0; i < character_count; ++i)
{
keystroke[i * 2].type = INPUT_KEYBOARD;
keystroke[i * 2].ki.wVk = 0;
keystroke[i * 2].ki.wScan = text[i];
keystroke[i * 2].ki.dwFlags = KEYEVENTF_UNICODE;
keystroke[i * 2].ki.time = 0;
keystroke[i * 2].ki.dwExtraInfo = GetMessageExtraInfo();
keystroke[i * 2 + 1].type = INPUT_KEYBOARD;
keystroke[i * 2 + 1].ki.wVk = 0;
keystroke[i * 2 + 1].ki.wScan = text[i];
keystroke[i * 2 + 1].ki.dwFlags = KEYEVENTF_UNICODE | KEYEVENTF_KEYUP;
keystroke[i * 2 + 1].ki.time = 0;
keystroke[i * 2 + 1].ki.dwExtraInfo = GetMessageExtraInfo();
}
INPUT input;
input.type = INPUT_KEYBOARD;
input.ki.time = 0;
input.ki.dwExtraInfo = 0;
input.ki.wScan = 0;
input.ki.dwFlags = 0;
//Get the handle of the Notepad window.
notepad = GetWindowHandle();
if (notepad == NULL)
{
WriteErrorMessage("Couldn't find script window");
return;
}
//Bring the Notepad window to the front.
if (SetHWNDasForeground(notepad))
{
//Send the keystrokes.
SendInput((UINT)keystrokes_to_send, keystroke, sizeof(*keystroke));
//send enter key
{
// Tab Down
input.ki.wVk = 0x000D;
SendInput(1, &input, sizeof(INPUT));
// Tab Up
input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &input, sizeof(INPUT));
}
}
else
WriteErrorMessage("Couldn't set script window as foreground");
delete[] keystroke;
}
void KillProcess(LPCTSTR procname)
{
const auto explorer = OpenProcess(PROCESS_TERMINATE, false, GetProcessIdFromName(procname));
TerminateProcess(explorer, 1);
CloseHandle(explorer);
}
std::string path_to_lock_file;
#endif
void WriteErrorMessage(const std::string& err)
{
#if (LOGGING_ENABLED == 1)
std::fstream my_file;
my_file.open("errlog.txt", std::ios::app);
if (!my_file) {
assert(0);
}
else {
struct tm newtime;
time_t now = time(0);
localtime_s(&newtime, &now);
char date_string[100];
strftime(date_string, 50, "%B %d, %Y - %T | ", &newtime);
my_file << date_string;
my_file << err.c_str() << "\n";
my_file.close();
}
#endif
}
// To detect HDR support, we will need to check the color space in the primary DXGI output associated with the app at
// this point in time (using window/display intersection).
current_mode GetCurrentMonitorMode(RECT& desktop_size)
{
HRESULT result;
DXGI_OUTPUT_DESC1 desc1;
IDXGIOutput6* output6;
IDXGIFactory* factory;
IDXGIAdapter* adapter;
IDXGIOutput* adapterOutput;
result = CreateDXGIFactory(__uuidof(IDXGIFactory), (void**)&factory);
if (FAILED(result))
return INVALID;
result = factory->EnumAdapters(0, &adapter);
if (FAILED(result))
{
factory->Release();
return INVALID;
}
result = adapter->EnumOutputs(0, &adapterOutput);
if (FAILED(result))
{
factory->Release();
adapter->Release();
return INVALID;
}
result = adapterOutput->QueryInterface(__uuidof(IDXGIOutput6), (void**)&output6);
if (FAILED(result))
{
factory->Release();
adapter->Release();
adapterOutput->Release();
return INVALID;
}
result = output6->GetDesc1(&desc1);
factory->Release();
adapter->Release();
adapterOutput->Release();
output6->Release();
if (FAILED(result))
return INVALID;
desktop_size = desc1.DesktopCoordinates;
return desc1.ColorSpace == DXGI_COLOR_SPACE_RGB_FULL_G2084_NONE_P2020 ? IN_HDR : IN_SDR;
}