Skip to content

Commit 987fec0

Browse files
committed
Windows Refactor
1 parent fec4617 commit 987fec0

4 files changed

Lines changed: 108 additions & 127 deletions

File tree

src/windows/dispatch_event.c

Lines changed: 9 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -44,24 +44,6 @@ UIOHOOK_API void hook_set_dispatch_proc(dispatcher_t dispatch_proc, void *user_d
4444
dispatch_data = user_data;
4545
}
4646

47-
#ifdef USE_EPOCH_TIME
48-
static uint64_t get_unix_timestamp() {
49-
FILETIME system_time;
50-
51-
// Get the local system time in UTC.
52-
GetSystemTimeAsFileTime(&system_time);
53-
54-
// Convert the local system time to a Unix epoch in MS.
55-
// milliseconds = 100-nanoseconds / 10000
56-
uint64_t timestamp = (((uint64_t) system_time.dwHighDateTime << 32) | system_time.dwLowDateTime) / 10000;
57-
58-
// Convert Windows epoch to Unix epoch. (1970 - 1601 in milliseconds)
59-
timestamp -= 11644473600000;
60-
61-
return timestamp;
62-
}
63-
#endif
64-
6547
// Send out an event if a dispatcher was set.
6648
static void dispatch_event(uiohook_event *const event) {
6749
if (dispatch != NULL) {
@@ -75,8 +57,9 @@ static void dispatch_event(uiohook_event *const event) {
7557
}
7658
}
7759

78-
bool dispatch_hook_enable() {
60+
bool dispatch_hook_enable(uint64_t timestamp) {
7961
bool consumed = false;
62+
8063
// Initialize native input helper functions.
8164
load_input_helper();
8265

@@ -99,14 +82,8 @@ bool dispatch_hook_enable() {
9982
return consumed;
10083
}
10184

102-
bool dispatch_hook_disable() {
85+
bool dispatch_hook_disable(uint64_t timestamp) {
10386
bool consumed = false;
104-
// Get the local system time in UNIX epoch form.
105-
#ifdef USE_EPOCH_TIME
106-
uint64_t timestamp = get_unix_timestamp();
107-
#else
108-
uint64_t timestamp = GetMessageTime();
109-
#endif
11087

11188
// Populate the hook stop event.
11289
uio_event.time = timestamp;
@@ -123,13 +100,8 @@ bool dispatch_hook_disable() {
123100
return consumed;
124101
}
125102

126-
bool dispatch_key_press(KBDLLHOOKSTRUCT *kbhook) {
103+
bool dispatch_key_press(uint64_t timestamp, KBDLLHOOKSTRUCT *kbhook) {
127104
bool consumed = false;
128-
#ifdef USE_EPOCH_TIME
129-
uint64_t timestamp = get_unix_timestamp();
130-
#else
131-
uint64_t timestamp = kbhook->time;
132-
#endif
133105

134106
// Check and setup modifiers.
135107
if (kbhook->vkCode == VK_LSHIFT) { set_modifier_mask(MASK_SHIFT_L); }
@@ -197,13 +169,8 @@ bool dispatch_key_press(KBDLLHOOKSTRUCT *kbhook) {
197169
return consumed;
198170
}
199171

200-
bool dispatch_key_release(KBDLLHOOKSTRUCT *kbhook) {
172+
bool dispatch_key_release(uint64_t timestamp, KBDLLHOOKSTRUCT *kbhook) {
201173
bool consumed = false;
202-
#ifdef USE_EPOCH_TIME
203-
uint64_t timestamp = get_unix_timestamp();
204-
#else
205-
uint64_t timestamp = kbhook->time;
206-
#endif
207174

208175
// Check and setup modifiers.
209176
if (kbhook->vkCode == VK_LSHIFT) { unset_modifier_mask(MASK_SHIFT_L); }
@@ -241,13 +208,8 @@ bool dispatch_key_release(KBDLLHOOKSTRUCT *kbhook) {
241208
return consumed;
242209
}
243210

244-
bool dispatch_button_press(MSLLHOOKSTRUCT *mshook, uint16_t button) {
211+
bool dispatch_button_press(uint64_t timestamp, MSLLHOOKSTRUCT *mshook, uint16_t button) {
245212
bool consumed = false;
246-
#ifdef USE_EPOCH_TIME
247-
uint64_t timestamp = get_unix_timestamp();
248-
#else
249-
uint64_t timestamp = mshook->time;
250-
#endif
251213

252214
// Track the number of clicks, the button must match the previous button.
253215
if (button == click_button && (long int) (timestamp - click_time) <= hook_get_multi_click_time()) {
@@ -298,13 +260,8 @@ bool dispatch_button_press(MSLLHOOKSTRUCT *mshook, uint16_t button) {
298260
return consumed;
299261
}
300262

301-
bool dispatch_button_release(MSLLHOOKSTRUCT *mshook, uint16_t button) {
263+
bool dispatch_button_release(uint64_t timestamp, MSLLHOOKSTRUCT *mshook, uint16_t button) {
302264
bool consumed = false;
303-
#ifdef USE_EPOCH_TIME
304-
uint64_t timestamp = get_unix_timestamp();
305-
#else
306-
uint64_t timestamp = mshook->time;
307-
#endif
308265

309266
// Populate mouse released event.
310267
uio_event.time = timestamp;
@@ -364,13 +321,8 @@ bool dispatch_button_release(MSLLHOOKSTRUCT *mshook, uint16_t button) {
364321
}
365322

366323

367-
bool dispatch_mouse_move(MSLLHOOKSTRUCT *mshook) {
324+
bool dispatch_mouse_move(uint64_t timestamp, MSLLHOOKSTRUCT *mshook) {
368325
bool consumed = false;
369-
#ifdef USE_EPOCH_TIME
370-
uint64_t timestamp = get_unix_timestamp();
371-
#else
372-
uint64_t timestamp = mshook->time;
373-
#endif
374326

375327
// We received a mouse move event with the mouse actually moving.
376328
// This verifies that the mouse was moved after being depressed.
@@ -415,13 +367,8 @@ bool dispatch_mouse_move(MSLLHOOKSTRUCT *mshook) {
415367
return consumed;
416368
}
417369

418-
bool dispatch_mouse_wheel(MSLLHOOKSTRUCT *mshook, uint8_t direction) {
370+
bool dispatch_mouse_wheel(uint64_t timestamp, MSLLHOOKSTRUCT *mshook, uint8_t direction) {
419371
bool consumed = false;
420-
#ifdef USE_EPOCH_TIME
421-
uint64_t timestamp = get_unix_timestamp();
422-
#else
423-
uint64_t timestamp = mshook->time;
424-
#endif
425372

426373
// Track the number of clicks.
427374
// Reset the click count and previous button.

src/windows/dispatch_event.h

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@
1919
#include <stdbool.h>
2020
#include <windows.h>
2121

22-
extern bool dispatch_hook_enable();
22+
extern bool dispatch_hook_enable(uint64_t timestamp);
2323

24-
extern bool dispatch_hook_disable();
24+
extern bool dispatch_hook_disable(uint64_t timestamp);
2525

26-
extern bool dispatch_key_press(KBDLLHOOKSTRUCT *kbhook);
26+
extern bool dispatch_key_press(uint64_t timestamp, KBDLLHOOKSTRUCT *kbhook);
2727

28-
extern bool dispatch_key_release(KBDLLHOOKSTRUCT *kbhook);
28+
extern bool dispatch_key_release(uint64_t timestamp, KBDLLHOOKSTRUCT *kbhook);
2929

30-
extern bool dispatch_button_press(MSLLHOOKSTRUCT *mshook, uint16_t button);
30+
extern bool dispatch_button_press(uint64_t timestamp, MSLLHOOKSTRUCT *mshook, uint16_t button);
3131

32-
extern bool dispatch_button_release(MSLLHOOKSTRUCT *mshook, uint16_t button);
32+
extern bool dispatch_button_release(uint64_t timestamp, MSLLHOOKSTRUCT *mshook, uint16_t button);
3333

34-
extern bool dispatch_mouse_move(MSLLHOOKSTRUCT *mshook);
34+
extern bool dispatch_mouse_move(uint64_t timestamp, MSLLHOOKSTRUCT *mshook);
3535

36-
extern bool dispatch_mouse_wheel(MSLLHOOKSTRUCT *mshook, uint8_t direction);
36+
extern bool dispatch_mouse_wheel(uint64_t timestamp, MSLLHOOKSTRUCT *mshook, uint8_t direction);

src/windows/input_helper.c

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,28 @@ uint16_t get_modifiers() {
364364
return modifier_mask;
365365
}
366366

367+
static void initialize_modifiers() {
368+
// NOTE We are checking the high order bit, so it will be < 0 for a singed short.
369+
if (GetKeyState(VK_LSHIFT) < 0) { set_modifier_mask(MASK_SHIFT_L); }
370+
if (GetKeyState(VK_RSHIFT) < 0) { set_modifier_mask(MASK_SHIFT_R); }
371+
if (GetKeyState(VK_LCONTROL) < 0) { set_modifier_mask(MASK_CTRL_L); }
372+
if (GetKeyState(VK_RCONTROL) < 0) { set_modifier_mask(MASK_CTRL_R); }
373+
if (GetKeyState(VK_LMENU) < 0) { set_modifier_mask(MASK_ALT_L); }
374+
if (GetKeyState(VK_RMENU) < 0) { set_modifier_mask(MASK_ALT_R); }
375+
if (GetKeyState(VK_LWIN) < 0) { set_modifier_mask(MASK_META_L); }
376+
if (GetKeyState(VK_RWIN) < 0) { set_modifier_mask(MASK_META_R); }
377+
378+
if (GetKeyState(VK_LBUTTON) < 0) { set_modifier_mask(MASK_BUTTON1); }
379+
if (GetKeyState(VK_RBUTTON) < 0) { set_modifier_mask(MASK_BUTTON2); }
380+
if (GetKeyState(VK_MBUTTON) < 0) { set_modifier_mask(MASK_BUTTON3); }
381+
if (GetKeyState(VK_XBUTTON1) < 0) { set_modifier_mask(MASK_BUTTON4); }
382+
if (GetKeyState(VK_XBUTTON2) < 0) { set_modifier_mask(MASK_BUTTON5); }
383+
384+
if (GetKeyState(VK_NUMLOCK) < 0) { set_modifier_mask(MASK_NUM_LOCK); }
385+
if (GetKeyState(VK_CAPITAL) < 0) { set_modifier_mask(MASK_CAPS_LOCK); }
386+
if (GetKeyState(VK_SCROLL) < 0) { set_modifier_mask(MASK_SCROLL_LOCK); }
387+
}
388+
367389

368390
/***********************************************************************
369391
* The following code is based on code provided by Marc-André Moreau
@@ -852,6 +874,8 @@ SIZE_T keycode_to_unicode(DWORD keycode, PWCHAR buffer, SIZE_T size) {
852874

853875
// Returns the number of locales that were loaded.
854876
int load_input_helper() {
877+
initialize_modifiers();
878+
855879
#if defined(_WIN32) && !defined(_WIN64)
856880
if (is_wow64()) {
857881
ptr_padding = sizeof(void *);
@@ -885,5 +909,8 @@ int unload_input_helper() {
885909
// Reset the current local.
886910
locale_current = NULL;
887911

912+
// Reset the modifier mask.
913+
modifier_mask = 0x0;
914+
888915
return count;
889916
}

0 commit comments

Comments
 (0)