-
Notifications
You must be signed in to change notification settings - Fork 0
/
win_functions.c
430 lines (385 loc) · 12.3 KB
/
win_functions.c
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
#include "win_functions.h"
#include "_cgo_export.h"
#include <tlhelp32.h>
boolean UpdateLoading = FALSE;
// Shuts down the program
void Exit()
{
//if (!ExitCalled){
Shell_NotifyIcon(NIM_DELETE, &nIconData);
FreeMenuIfNecessary();
UnhookWindowsHookEx(KeyboardHook);
UnhookWindowsHookEx(MouseHook);
//}
//ExitCalled = TRUE;
PostQuitMessage(0);
}
// Uses SendInput to emulate a keyboard press
void PressKeyboardKey(char key){
INPUT ip;
ip.type = INPUT_KEYBOARD;
ip.ki.wVk = key;
ip.ki.wScan = 0;
ip.ki.dwFlags = 0;
ip.ki.time = 0;
ip.ki.dwExtraInfo = 0;
SendInput(1, &ip, sizeof(INPUT));
ip.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &ip, sizeof(INPUT));
}
// Copys an item to clipboard.
// Used instead of SendInput ctrl+c, due to it being faster in this case.
void ItemToClipboard(){
HWND foreground = GetForegroundWindow();
if (foreground){
SendMessage(foreground, WM_KEYDOWN, 67, 0);
SendMessage(foreground, WM_KEYUP, 67, 0);
}
}
// Checks whether the Current active window is called "Path of Exile"
boolean IsPoEActive(){
HWND foreground = GetForegroundWindow();
if (foreground){
char window_title[18];
GetWindowText(foreground, window_title, 18);
if (!strcmp(window_title , "Path of Exile")){
return TRUE;
}
}
return FALSE;
}
// Called whenever user presses a keyboard key.
// Checks whether poe is running, if so checks item on hotkey pressed and evaluates it.
LRESULT CALLBACK LowLevelKeyboardProc( int nCode, WPARAM wParam, LPARAM lParam ){
char pressedKey;
// Declare a pointer to the KBDLLHOOKSTRUCTdsad
KBDLLHOOKSTRUCT *pKeyBoard = (KBDLLHOOKSTRUCT *)lParam;
switch( wParam )
{
case WM_KEYDOWN: // When the key has been pressed down
{
//get the key code
pressedKey = (char)pKeyBoard->vkCode;
if ((pressedKey == -94) || (pressedKey == -93)){ //Ctrl
CtrlPressed = TRUE;
}
}
break;
case WM_KEYUP:
{
//get the key code
pressedKey = (char)pKeyBoard->vkCode;
if ((pressedKey == -94) || (pressedKey == -93)){ //Ctrl
CtrlPressed = FALSE;
}else if (pressedKey == 68){ //d){
if (CtrlPressed && IsPoEActive()){
POINT p;
GetCursorPos(&p);
//Copy item to clipboard
ItemToClipboard();
//Read from Clipboard
Sleep(5);
HANDLE h;
if (!OpenClipboard(NULL)){
return CallNextHookEx( NULL, nCode, wParam, lParam);
}
h = GetClipboardData(CF_TEXT);
const char* output = evaluateItem((char*)h);
const size_t len = strlen(output) + 1;
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, len);
memcpy(GlobalLock(hMem), output, len);
GlobalUnlock(hMem);
EmptyClipboard();
SetClipboardData(CF_TEXT, hMem);
CloseClipboard();
if (len > 1) {
PressKeyboardKey(VK_RETURN);
PressKeyboardKey(65); //ctrl+a
PressKeyboardKey(86); //ctrl+v
PressKeyboardKey(VK_RETURN);
}else if (UseDoubleclick){
mouse_event(MOUSEEVENTF_LEFTDOWN, p.x, p.y, 0, 0);
mouse_event(MOUSEEVENTF_LEFTUP, p.x, p.y, 0, 0);
}
}
}
}
break;
}
//
return CallNextHookEx( NULL, nCode, wParam, lParam);
}
// Starts the listening for userinput.
void InstallHooks(){
KeyboardHook = SetWindowsHookEx( WH_KEYBOARD_LL, LowLevelKeyboardProc, hInstance,0);
//MouseHook = SetWindowsHookEx( WH_MOUSE_LL, LowLevelMouseProc, hInstance,0);
}
void ShowMenu(){
POINT p;
GetCursorPos(&p);
if (go_NeedsReload()){
CreateCustomMenu();
}
SetForegroundWindow(hwnd); // Win32 bug work-around
TrackPopupMenu(hMenu, TPM_BOTTOMALIGN | TPM_LEFTALIGN, p.x, p.y, 0, hwnd, NULL);
}
void FreeMenuIfNecessary(){
if (hMenu){
DestroyMenu(hMenu);
hMenu = NULL;
}
}
void CreateCustomMenu(){
FreeMenuIfNecessary();
hMenu = CreatePopupMenu();
HMENU hFilterSubMenu = CreatePopupMenu();
HMENU hOptionsSubMenu = CreatePopupMenu();
char *currFilter = go_GetCurrentFilterName();
//Main Menu
AppendMenu(hMenu, MF_STRING | MF_GRAYED, 0, VERSION);
//AppendMenuW(hSubMenu, MF_SEPARATOR, 0, NULL);
//AppendMenuW(hSubMenu, MF_STRING, CMD_ABOUT, L"About");
AppendMenuW(hMenu, MF_STRING | MF_POPUP, (UINT_PTR)hFilterSubMenu, L"Filer");
AppendMenuW(hMenu, MF_STRING | MF_POPUP, (UINT_PTR)hOptionsSubMenu, L"Options");
AppendMenuW(hMenu, MF_SEPARATOR, 0, NULL);
AppendMenuW(hMenu, MF_STRING, CMD_EXIT, L"Exit");
//Sub Menu of Filters
AppendMenuW(hFilterSubMenu, MF_STRING, CMD_FILTER_RELOAD, L"Reload current filter");
AppendMenuW(hFilterSubMenu, MF_SEPARATOR, 0, NULL);
int count = 0;
char* name = go_GetNextFilterName();
while ((strlen(name) > 0) && (count < 50)){
if (strcmp(name , currFilter) == 0){
AppendMenu(hFilterSubMenu, MF_STRING | MF_CHECKED, (UINT_PTR)hFilterSubMenu, name);
}else{
AppendMenu(hFilterSubMenu, MF_STRING, CMD_FILTERSELECT_START+count, name);
}
free(name);
name = go_GetNextFilterName();
count++;
}
free(name);
AppendMenuW(hFilterSubMenu, MF_SEPARATOR, 0, NULL);
if (strlen(currFilter) > 0){
AppendMenuW(hFilterSubMenu, MF_STRING, CMD_FILTER_NONE, L"None");
}else{
AppendMenuW(hFilterSubMenu, MF_STRING | MF_CHECKED, CMD_FILTER_NONE, L"None");
}
free(currFilter);
//Sub Menu of Options
if (UseDoubleclick){
AppendMenuW(hOptionsSubMenu, MF_STRING | MF_CHECKED, CMD_OPTIONS_USEDOUBLECLICK, L"Send Doubleclick");
}else{
AppendMenuW(hOptionsSubMenu, MF_STRING, CMD_OPTIONS_USEDOUBLECLICK, L"Send Doubleclick");
}
if (CheckForUpdates){
AppendMenuW(hOptionsSubMenu, MF_STRING | MF_CHECKED, CMD_OPTIONS_CHECKUPDATES, L"Check for updates");
}else{
AppendMenuW(hOptionsSubMenu, MF_STRING, CMD_OPTIONS_CHECKUPDATES, L"Check for updates");
}
if (LaunchPoE){
AppendMenuW(hOptionsSubMenu, MF_STRING | MF_CHECKED, CMD_OPTIONS_LAUNCHPOE, L"Launch PoE on start");
}else{
AppendMenuW(hOptionsSubMenu, MF_STRING, CMD_OPTIONS_LAUNCHPOE, L"Launch PoE on start");
}
// SetForegroundWindow(hWnd); // Win32 bug work-around
// TrackPopupMenu(hMenu, TPM_BOTTOMALIGN | TPM_LEFTALIGN, p.x, p.y, 0, hWnd, NULL);
}
// Creates a Window and sets global hwnd to it.
void InitializeWindow(){
// Register the window class.
LPCTSTR CLASS_NAME = "PoEPricer Window Class";
WNDCLASS wc = { };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
RegisterClass(&wc);
// Create the window.
hwnd = CreateWindowEx(
0, // Optional window styles.
CLASS_NAME, // Window class
"PoEPricer", // Window text
WS_OVERLAPPEDWINDOW, // Window style
// Size and position
//CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
0,0,0,0,
NULL, // Parent window
NULL, // Menu
hInstance, // Instance handle
NULL // Additional application data
);
if (hwnd == NULL)
{
Exit();
}
ShowWindow(hwnd, FALSE);
}
// Set in Initialize Window, receives WindowMessages (like when to show popup menu)
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
switch (uMsg)
{
case WM_DESTROY:
Exit();
return 0;
case WM_COMMAND:
if( wParam == CMD_EXIT){
Exit();
return 0;
}else if( wParam == CMD_FILTER_NONE){
go_LoadFilterByIndex(-1);
}else if( wParam == CMD_FILTER_RELOAD){
go_ReloadFilter();
}else if( wParam == CMD_OPTIONS_USEDOUBLECLICK){
UseDoubleclick = !UseDoubleclick;
go_SetUseDoubleclick(UseDoubleclick);
}else if( wParam == CMD_OPTIONS_CHECKUPDATES){
CheckForUpdates = !CheckForUpdates;
go_SetCheckForUpdates(CheckForUpdates);
}else if( wParam == CMD_OPTIONS_LAUNCHPOE){
LaunchPoE = !LaunchPoE;
go_SetLaunchPoE(LaunchPoE);
}else if(wParam >= CMD_FILTERSELECT_START && wParam < CMD_FILTERSELECT_START+50){
go_LoadFilterByIndex(wParam - CMD_FILTERSELECT_START);
}
/*switch (LOWORD(wParam))
{
case CMD_EXIT:
}*/
case WM_POEPRICERMESSAGE:
if (lParam == WM_RBUTTONUP){
ShowMenu();
return 0;
}
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
// Creates the window needed for popup menu, then registers a tray icon, as well as
// making sure to receive WM_POEPRICERMESSAGE whenever icon is rightclicked.
void ShowTrayIcon(){
//Get a window for our tray
InitializeWindow(); // Exits if it fails
//Load icon image
HICON hIcon;
hIcon = LoadImage(NULL, "data/icon.ico", IMAGE_ICON, 64, 64, LR_LOADFROMFILE);
//hIcon = LoadImage(NULL, IDI_INFORMATION, IMAGE_ICON, 16, 16, LR_SHARED);
//Initialize Icon
nIconData.cbSize = sizeof(NOTIFYICONDATA);
nIconData.hWnd = hwnd;
nIconData.uID = 100;
nIconData.uCallbackMessage = WM_POEPRICERMESSAGE;
nIconData.hIcon = hIcon;
strcpy(nIconData.szTip, "PoEPricer");
nIconData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
Shell_NotifyIcon(NIM_ADD, &nIconData);
}
void c_AskForUpdate(char* msg){
int answer = MessageBox(NULL, msg, "Update available", MB_YESNO|MB_ICONINFORMATION);
free(msg);
if (answer == IDYES){
UpdateLoading = TRUE;
ShellExecute(NULL, "open", "http://poe.melanite.net", NULL, NULL, SW_SHOWNORMAL);
}
}
void launchPoE(){
DWORD dwType = REG_SZ;
HKEY hKey = 0;
char directory[1024];
DWORD value_length = 1024;
const char* subkey = "Software\\GrindingGearGames\\Path of Exile";
RegOpenKey(HKEY_CURRENT_USER,subkey,&hKey);
if (RegQueryValueEx(hKey, "InstallLocation", NULL, &dwType, (LPBYTE)&directory, &value_length) != ERROR_SUCCESS){
MessageBox(NULL, "Could not determine Path of Exile's install location.\nDisabling autostart.", "Error", MB_OK|MB_ICONERROR);
LaunchPoE = FALSE;
go_SetLaunchPoE(LaunchPoE);
return;
}
char executable[1024];
strcpy(executable, directory);
strcat(executable, "\\PathOfExile.exe");
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory( &si, sizeof(si) );
ZeroMemory( &pi, sizeof(pi) );
si.cb = sizeof(si);
if (!CreateProcess( executable, // No module name (use command line)
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
directory, // Use given starting directory
&si, // Pointer to STARTUPINFO structure
&pi ) // Pointer to PROCESS_INFORMATION structure
){
MessageBox(NULL, "Could not start Path of Exile.\nDisabling autostart.", "Error", MB_OK|MB_ICONERROR);
LaunchPoE = FALSE;
go_SetLaunchPoE(LaunchPoE);
return;
}
PoEHandle = pi.hProcess;
}
DWORD FindProcessId(char* processName){
// strip path
char* p = strrchr(processName, '\\');
if(p)
processName = p+1;
PROCESSENTRY32 processInfo;
processInfo.dwSize = sizeof(processInfo);
HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if ( processesSnapshot == INVALID_HANDLE_VALUE )
return 0;
Process32First(processesSnapshot, &processInfo);
if ( !strcmp(processName, processInfo.szExeFile) )
{
CloseHandle(processesSnapshot);
return processInfo.th32ProcessID;
}
while ( Process32Next(processesSnapshot, &processInfo) )
{
if ( !strcmp(processName, processInfo.szExeFile) )
{
CloseHandle(processesSnapshot);
return processInfo.th32ProcessID;
}
}
CloseHandle(processesSnapshot);
return 0;
}
void c_Loop(char* version, boolean useDoubleclick, boolean checkUpdates, boolean launchPoEBool){
VERSION = version;
UseDoubleclick = useDoubleclick;
CheckForUpdates = checkUpdates;
LaunchPoE = launchPoEBool;
PoEHandle = NULL;
if(LaunchPoE && FindProcessId("PathOfExile.exe") == 0 && !UpdateLoading){
launchPoE();
}
//Initialize Variables
//ExitCalled = FALSE;
hInstance = GetModuleHandle(NULL);
CtrlPressed = FALSE;
//Install Hooks
InstallHooks();
//c_LoadFilters();
//CreateCustomMenu(); //Created automatically when needed
ShowTrayIcon();
MSG msg = {0};
if(PoEHandle != NULL){
SetTimer(NULL, 1, 1000, NULL);
}
while (GetMessage(&msg, NULL, 0, 0) != 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
if (PoEHandle != NULL){
DWORD dwExitCode;
GetExitCodeProcess(PoEHandle,&dwExitCode);
if(dwExitCode != STILL_ACTIVE) {
Exit();
}
}
}
Exit();
}