-
Notifications
You must be signed in to change notification settings - Fork 36
/
dual-key-remap.c
173 lines (146 loc) · 4.68 KB
/
dual-key-remap.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
#define VERSION "0.8"
#define AUTHOR "ililim"
#include <windows.h>
#include <stdio.h>
#include <ctype.h>
#include "input.h"
#include "keys.c"
#include "remap.c"
// Globals
// ----------------
// A semi random value used to identify inputs generated
// by Dual Key Remap. Ideally high to minimize chances of a collision
// with a real pointer used by another application.
// Note: This approach is what AHK used, we should a different key id
// from them to avoid collisions.
#define INJECTED_KEY_ID 0xFFC3CED7
struct Remap * g_remap_list;
HHOOK g_keyboard_hook;
HHOOK g_mouse_hook;
void send_input(int scan_code, int virt_code, enum Direction direction)
{
INPUT input = {0};
input.type = INPUT_KEYBOARD;
input.ki.time = 0;
input.ki.dwExtraInfo = (ULONG_PTR)INJECTED_KEY_ID;
input.ki.wScan = scan_code;
input.ki.wVk = virt_code;
// Per MS Docs: https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-keybd_even
// we need to flag whether "the scan code was preceded by a prefix byte having the value 0xE0 (224)"
int is_extended_key = scan_code>>8 == 0xE0;
input.ki.dwFlags = (direction == UP ? KEYEVENTF_KEYUP : 0) |
(is_extended_key ? KEYEVENTF_EXTENDEDKEY : 0);
SendInput(1, &input, sizeof(INPUT));
}
LRESULT CALLBACK mouse_callback(int msg_code, WPARAM w_param, LPARAM l_param) {
int block_input = 0;
// Per MS docs we should only act for HC_ACTION's
if (msg_code == HC_ACTION) {
switch (w_param) {
case WM_MOUSEWHEEL:
case WM_LBUTTONDOWN:
case WM_RBUTTONDOWN:
case WM_MBUTTONDOWN:
case WM_NCXBUTTONDOWN:
case WM_XBUTTONDOWN:
// Since no key corresponds to the mouse inputs; use a dummy input
block_input = handle_input(0, MOUSE_DUMMY_VK, 0, 0);
}
}
return (block_input) ? 1 : CallNextHookEx(g_mouse_hook, msg_code, w_param, l_param);
}
LRESULT CALLBACK keyboard_callback(int msg_code, WPARAM w_param, LPARAM l_param)
{
int block_input = 0;
// Per MS docs we should only act for HC_ACTION's
if (msg_code == HC_ACTION) {
KBDLLHOOKSTRUCT * data = (KBDLLHOOKSTRUCT *)l_param;
enum Direction direction = (w_param == WM_KEYDOWN || w_param == WM_SYSKEYDOWN)
? DOWN
: UP;
int is_injected = data->dwExtraInfo == INJECTED_KEY_ID;
block_input = handle_input(
data->scanCode,
data->vkCode,
direction,
is_injected
);
}
return (block_input) ? 1 : CallNextHookEx(g_mouse_hook, msg_code, w_param, l_param);
}
void create_console()
{
if (AllocConsole()) {
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
printf("== dual-key-remap (version: %s, author: %s) ==\n\n", VERSION, AUTHOR);
}
}
void destroy_console()
{
fclose(stdout);
fclose(stderr);
FreeConsole();
}
int load_config_file(wchar_t * path)
{
FILE * file;
char line[255];
if (_wfopen_s(&file, path, L"r") > 0) {
printf("Cannot open configuration file '%ws'. Make sure it is in the same directory as 'dual-key-remap.exe'.\n",
path);
return 1;
}
int linenum = 1;
while (fgets(line, 255, file)) {
if (load_config_line((char *)&line, linenum++)) {
fclose(file);
return 1;
}
};
fclose(file);
return 0;
}
void put_config_path(wchar_t * path)
{
HMODULE module = GetModuleHandleW(NULL);
GetModuleFileNameW(module, path, MAX_PATH);
path[wcslen(path) - strlen("dual-key-remap.exe")] = '\0';
wcscat(path, L"config.txt");
}
int main()
{
// Initialization may print errors to stdout, create a console to show that output.
create_console();
HANDLE mutex = CreateMutex(NULL, TRUE, "dual-key-remap.single-instance");
if (GetLastError() == ERROR_ALREADY_EXISTS)
{
printf("dual-key-remap.exe is already running!\n");
goto end;
}
wchar_t config_path[MAX_PATH];
put_config_path(config_path);
int err = load_config_file(config_path);
if (err) {
goto end;
}
g_debug = g_debug || getenv("DEBUG") != NULL;
g_mouse_hook = SetWindowsHookEx(WH_MOUSE_LL, mouse_callback, NULL, 0);
g_keyboard_hook = SetWindowsHookEx(WH_KEYBOARD_LL, keyboard_callback, NULL, 0);
// We're all good if we got this far. Hide the console window unless we're debugging.
if (g_debug) {
printf("-- DEBUG MODE --\n");
} else {
destroy_console();
}
MSG msg;
while (GetMessage(&msg, NULL, 0, 0) > 0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
end:
printf("\nPress any key to exit...\n");
getch();
return 1;
}