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
#include <windows.h>
#include <stdio.h>
#include <string.h>
#include "../plugin.h"
#ifdef BUILD_PLUGIN
__declspec(dllexport) int sxhkd_plugin_abi(void) { return SXHKD_PLUGIN_ABI; }
__declspec(dllexport) int sxhkd_plugin_invoke(const char *action, const char *args) {
if (strcmp(action, "ping") != 0) return -1;
HANDLE event = OpenEventA(EVENT_MODIFY_STATE, FALSE, args);
if (!event) return -1;
int ok = SetEvent(event);
CloseHandle(event);
return ok ? 0 : -1;
}
#else
int main(void) {
char config[MAX_PATH], temp[MAX_PATH], command[MAX_PATH * 3], name[80], second[84], skipped[84];
if (!GetTempPathA(sizeof temp, temp) || !GetTempFileNameA(temp, "shk", 0, config)) return 1;
snprintf(name, sizeof name, "Local\\sxhkd-plugin-smoke-%lu-%lu",
GetCurrentProcessId(), GetTickCount());
snprintf(second, sizeof second, "%s-2", name);
snprintf(skipped, sizeof skipped, "%s-3", name);
HANDLE event = CreateEventA(NULL, TRUE, FALSE, name);
HANDLE next = CreateEventA(NULL, TRUE, FALSE, second);
HANDLE not_run = CreateEventA(NULL, TRUE, FALSE, skipped);
FILE *file = fopen(config, "w");
if (!event || !next || !not_run || !file) return 1;
fprintf(file, "alt + f24\n :smoke ping %s\n :smoke ping %s\n"
" :smoke unknown\n :smoke ping %s\n", name, second, skipped);
fclose(file);
snprintf(command, sizeof command, "\"%s\\dwhkd.exe\" -c \"%s\"", ".", config);
STARTUPINFOA si = {.cb = sizeof si};
PROCESS_INFORMATION pi;
if (!CreateProcessA(NULL, command, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi)) return 1;
HWND hwnd = NULL;
for (int tries = 0; tries < 40 && !hwnd; tries++) {
HWND candidate = NULL;
while ((candidate = FindWindowExA(HWND_MESSAGE, candidate, "dwhkd", NULL))) {
DWORD pid = 0;
GetWindowThreadProcessId(candidate, &pid);
if (pid == pi.dwProcessId) { hwnd = candidate; break; }
}
if (!hwnd) Sleep(50);
}
int ok = hwnd && PostMessageA(hwnd, WM_HOTKEY, 1, 0) &&
WaitForSingleObject(event, 2000) == WAIT_OBJECT_0 &&
WaitForSingleObject(next, 2000) == WAIT_OBJECT_0;
if (ok) {
SendMessageA(hwnd, WM_NULL, 0, 0); /* Wait until the hotkey handler has finished. */
ok = WaitForSingleObject(not_run, 0) == WAIT_TIMEOUT;
}
if (hwnd) PostMessageA(hwnd, WM_CLOSE, 0, 0);
if (WaitForSingleObject(pi.hProcess, 2000) != WAIT_OBJECT_0) TerminateProcess(pi.hProcess, 1);
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
CloseHandle(event);
CloseHandle(next);
CloseHandle(not_run);
DeleteFileA(config);
printf("plugin dispatch: %s\n", ok ? "ok" : "failed");
return ok ? 0 : 1;
}
#endif