-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathkeycap.cpp
More file actions
261 lines (236 loc) · 8.75 KB
/
keycap.cpp
File metadata and controls
261 lines (236 loc) · 8.75 KB
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
////////////////////////////////////////////////////////////////////////////////
// The MIT License (MIT)
//
// Copyright (c) 2023 Tim Stair
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
////////////////////////////////////////////////////////////////////////////////
// keycap.cpp : Defines the entry point for key capture and re-map
//
#include "keycap.h"
#include "keycaputil.h"
#include "inputprockeyboard.h"
#include "inputprocmouse.h"
#include "configfile.h"
// === prototypes
// avoid mangling the function names (necessary for export and usage in C#)
extern "C"
{
__declspec(dllexport) int LoadAndCaptureFromFile(HINSTANCE hInstance, char* sFile);
__declspec(dllexport) void ShutdownCapture();
}
// non extern functions
void InitiallizeEntryContainerListItem(RemapEntryContainerListItem* pKeyItem, RemapEntry* pEntry);
// sweet globals
HHOOK g_hookKeyboard = nullptr, g_hookMouse = nullptr;
RemapEntryContainerListItem* g_KeyTranslationTable[WIN_KEY_COUNT];
RemapEntry* g_KeyTranslationHead = nullptr;
void* g_KeyTranslationEnd = nullptr; // pointer indicating the end of the input file data
/*
Performs the load file operation and initiates the keyboard capture process
hInstance: The HINSTANCE of the KeyCapture application
sFile: The file to load
returns: A HOOK_RESULT value depending on the outcome of the file load and keyboard hook intiailization
*/
__declspec(dllexport) int LoadAndCaptureFromFile(HINSTANCE hInstance, char* sFile)
{
ValidateStructs();
int nLoadResult = LoadFile(sFile, &g_KeyTranslationHead, &g_KeyTranslationEnd);
switch (nLoadResult)
{
case INPUT_MISSING:
case INPUT_ZERO:
case INPUT_BAD:
ShutdownCapture();
return nLoadResult;
}
// validate file (and compile the "hash" table g_KeyTranslationTable)
memset(g_KeyTranslationTable, 0, WIN_KEY_COUNT * sizeof(RemapEntryContainerListItem*));
RemapEntry* pEntry = g_KeyTranslationHead;
bool bValidTranslationSet = false;
// The translation table contains linked lists of all the output sets for a given key due to the flag keys (shift/alt/ctrl)
// Example: input 'a' will be in the same list as 'shift+a' 'alt+a' 'ctrl+a' (or any combos like 'alt+shift+a')
while(nullptr != pEntry)
{
if(0 == pEntry->outputCount)
{
ShutdownCapture();
return INPUT_BAD;
}
// TODO: just get a pointer to g_KeyTranslationTable[pKey->inputConfig.virtualKey] ?
#ifdef _DEBUG
char* pInputConfigDescription = GetInputConfigDescription(pEntry->inputConfig);
LogDebugMessage("Loading %s Outputs: %d", pInputConfigDescription, pEntry->outputCount);
free(pInputConfigDescription);
#endif
// if the entry doesn't exist yet for the given input vkey create a new one with a null next pointer
if(nullptr == g_KeyTranslationTable[pEntry->inputConfig.virtualKey])
{
g_KeyTranslationTable[pEntry->inputConfig.virtualKey] = static_cast<RemapEntryContainerListItem*>(malloc(sizeof(RemapEntryContainerListItem)));
InitiallizeEntryContainerListItem(g_KeyTranslationTable[pEntry->inputConfig.virtualKey], pEntry);
g_KeyTranslationTable[pEntry->inputConfig.virtualKey]->pNext = nullptr;
}
// if the entry does exist create a new entry and append it to the existing linked list
else
{
RemapEntryContainerListItem* pKeyItem = g_KeyTranslationTable[pEntry->inputConfig.virtualKey];
while(nullptr != pKeyItem->pNext)
{
pKeyItem = pKeyItem->pNext;
}
pKeyItem->pNext = (RemapEntryContainerListItem*)malloc(sizeof(RemapEntryContainerListItem));
pKeyItem = pKeyItem->pNext;
InitiallizeEntryContainerListItem(pKeyItem, pEntry);
pKeyItem->pNext = nullptr;
}
// jump to the next entry
pEntry = (RemapEntry*)(&pEntry->outputCount + (sizeof(unsigned int) + (pEntry->outputCount * sizeof(OutputConfig))));
if(pEntry > g_KeyTranslationEnd)
{
LogDebugMessage("Load Failed. Attempted to read bytes beyond file boundary.");
ShutdownCapture();
return INPUT_BAD;
}
else if(pEntry == g_KeyTranslationEnd)
{
bValidTranslationSet = true;
break;
}
}
if(bValidTranslationSet)
{
// Note: This fails in VisualStudio if managed debugging is NOT enabled in the project(!)
g_hookKeyboard = SetWindowsHookEx( WH_KEYBOARD_LL, LowLevelKeyboardProc, hInstance, NULL);
g_hookMouse = SetWindowsHookEx(WH_MOUSE_LL, LowLevelMouseProc, hInstance, NULL);
if(g_hookKeyboard && g_hookMouse)
{
return HOOK_CREATION_SUCCESS;
}
ShutdownCapture();
return HOOK_CREATION_FAILURE;
}
ShutdownCapture();
return INPUT_BAD;
}
void InitiallizeEntryContainerListItem(RemapEntryContainerListItem* pKeyItem, RemapEntry* pEntry)
{
pKeyItem->pEntryContainer = static_cast<RemapEntryContainer*>(malloc(sizeof(RemapEntryContainer)));
pKeyItem->pEntryContainer->pEntryState = static_cast<RemapEntryState*>(calloc(1, sizeof(RemapEntryState)));
pKeyItem->pEntryContainer->pEntry = pEntry;
}
void ShutdownInputThreads(bool forceShutdown)
{
// signal shutdown for all entries with a thread handle
for (int nIdx = 0; nIdx < WIN_KEY_COUNT; nIdx++)
{
if (nullptr != g_KeyTranslationTable[nIdx])
{
RemapEntryContainerListItem* pListItem = g_KeyTranslationTable[nIdx];
RemapEntryContainerListItem* pNextItem = nullptr;
while (nullptr != pListItem)
{
pNextItem = pListItem->pNext;
if (nullptr != pListItem->pEntryContainer->pEntryState->threadHandle)
{
pListItem->pEntryContainer->pEntryState->bShutdown = true;
}
pListItem = pNextItem;
}
}
}
// a forced shutdown will kill threads (mostly for application exit)
if(!forceShutdown) return;
// monitor and terminate threads for all entries with a thread handle
for (int nIdx = 0; nIdx < WIN_KEY_COUNT; nIdx++)
{
if (nullptr != g_KeyTranslationTable[nIdx])
{
RemapEntryContainerListItem* pListItem = g_KeyTranslationTable[nIdx];
RemapEntryContainerListItem* pNextItem = nullptr;
while (nullptr != pListItem)
{
pNextItem = pListItem->pNext;
if (nullptr != pListItem->pEntryContainer->pEntryState->threadHandle)
{
DWORD dwExitCode = WAIT_TIMEOUT;
for (int shutdownIteration = 0; shutdownIteration < THREAD_SHUTDOWN_MAX_ATTEMPTS && dwExitCode != WAIT_OBJECT_0; shutdownIteration++)
{
// check on the state of the thread (for 100ms) (MS says not to use GetExitCodeThread unless the thread is known to be exited)
dwExitCode = WaitForSingleObject(pListItem->pEntryContainer->pEntryState->threadHandle, THREAD_SHUTDOWN_ATTEMPT_DELAY_MS);
}
if (dwExitCode != WAIT_OBJECT_0)
{
LogDebugMessage("Force terminating thread!");
TerminateThread(pListItem->pEntryContainer->pEntryState->threadHandle, 1);
}
}
pListItem = pNextItem;
}
}
}
}
/*
Shuts down the key capture hook and frees any allocated memory
*/
__declspec(dllexport) void ShutdownCapture()
{
// stop any active threads
ShutdownInputThreads(true);
// disable the hooks
if(g_hookKeyboard)
{
LogDebugMessage("Unhooked keyboard");
UnhookWindowsHookEx(g_hookKeyboard);
g_hookKeyboard = nullptr;
}
if (g_hookMouse)
{
LogDebugMessage("Unhooked mouse");
UnhookWindowsHookEx(g_hookMouse);
g_hookMouse = nullptr;
}
// memory clean up
if(nullptr != g_KeyTranslationHead)
{
LogDebugMessage("Cleared Memory!");
free(g_KeyTranslationHead);
g_KeyTranslationHead = nullptr;
}
// clean up "hash" table
for(int nIdx = 0; nIdx < WIN_KEY_COUNT; nIdx++)
{
if(nullptr != g_KeyTranslationTable[nIdx])
{
RemapEntryContainerListItem* pListItem = g_KeyTranslationTable[nIdx];
RemapEntryContainerListItem* pNextItem = nullptr;
while(nullptr != pListItem)
{
pNextItem = pListItem->pNext;
free(pListItem->pEntryContainer->pEntryState);
free(pListItem->pEntryContainer);
// NOTE: the entry itself was freed above
free(pListItem);
pListItem = pNextItem;
}
g_KeyTranslationTable[nIdx] = nullptr;
}
}
g_KeyTranslationEnd = nullptr;
LogDebugMessage("Capture Shutdown");
}