Skip to content

Commit 3d0debe

Browse files
committed
Windows Refactor
1 parent fec4617 commit 3d0debe

File tree

4 files changed

+113
-203
lines changed

4 files changed

+113
-203
lines changed

src/windows/dispatch_event.c

Lines changed: 11 additions & 71 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,17 +57,11 @@ 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;
80-
// Initialize native input helper functions.
81-
load_input_helper();
8262

83-
// Get the local system time in UNIX epoch form.
84-
#ifdef USE_EPOCH_TIME
85-
uint64_t timestamp = get_unix_timestamp();
86-
#else
87-
uint64_t timestamp = GetMessageTime();
88-
#endif
63+
// Initialize native input helper.
64+
load_input_helper();
8965

9066
// Populate the hook start event.
9167
uio_event.time = timestamp;
@@ -99,14 +75,8 @@ bool dispatch_hook_enable() {
9975
return consumed;
10076
}
10177

102-
bool dispatch_hook_disable() {
78+
bool dispatch_hook_disable(uint64_t timestamp) {
10379
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
11080

11181
// Populate the hook stop event.
11282
uio_event.time = timestamp;
@@ -117,19 +87,14 @@ bool dispatch_hook_disable() {
11787
dispatch_event(&uio_event);
11888
consumed = uio_event.mask & MASK_CONSUME;
11989

120-
// Deinitialize native input helper functions.
90+
// Uninitialize the native input helper.
12191
unload_input_helper();
12292

12393
return consumed;
12494
}
12595

126-
bool dispatch_key_press(KBDLLHOOKSTRUCT *kbhook) {
96+
bool dispatch_key_press(uint64_t timestamp, KBDLLHOOKSTRUCT *kbhook) {
12797
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
13398

13499
// Check and setup modifiers.
135100
if (kbhook->vkCode == VK_LSHIFT) { set_modifier_mask(MASK_SHIFT_L); }
@@ -197,13 +162,8 @@ bool dispatch_key_press(KBDLLHOOKSTRUCT *kbhook) {
197162
return consumed;
198163
}
199164

200-
bool dispatch_key_release(KBDLLHOOKSTRUCT *kbhook) {
165+
bool dispatch_key_release(uint64_t timestamp, KBDLLHOOKSTRUCT *kbhook) {
201166
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
207167

208168
// Check and setup modifiers.
209169
if (kbhook->vkCode == VK_LSHIFT) { unset_modifier_mask(MASK_SHIFT_L); }
@@ -241,13 +201,8 @@ bool dispatch_key_release(KBDLLHOOKSTRUCT *kbhook) {
241201
return consumed;
242202
}
243203

244-
bool dispatch_button_press(MSLLHOOKSTRUCT *mshook, uint16_t button) {
204+
bool dispatch_button_press(uint64_t timestamp, MSLLHOOKSTRUCT *mshook, uint16_t button) {
245205
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
251206

252207
// Track the number of clicks, the button must match the previous button.
253208
if (button == click_button && (long int) (timestamp - click_time) <= hook_get_multi_click_time()) {
@@ -298,13 +253,8 @@ bool dispatch_button_press(MSLLHOOKSTRUCT *mshook, uint16_t button) {
298253
return consumed;
299254
}
300255

301-
bool dispatch_button_release(MSLLHOOKSTRUCT *mshook, uint16_t button) {
256+
bool dispatch_button_release(uint64_t timestamp, MSLLHOOKSTRUCT *mshook, uint16_t button) {
302257
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
308258

309259
// Populate mouse released event.
310260
uio_event.time = timestamp;
@@ -364,13 +314,8 @@ bool dispatch_button_release(MSLLHOOKSTRUCT *mshook, uint16_t button) {
364314
}
365315

366316

367-
bool dispatch_mouse_move(MSLLHOOKSTRUCT *mshook) {
317+
bool dispatch_mouse_move(uint64_t timestamp, MSLLHOOKSTRUCT *mshook) {
368318
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
374319

375320
// We received a mouse move event with the mouse actually moving.
376321
// This verifies that the mouse was moved after being depressed.
@@ -415,13 +360,8 @@ bool dispatch_mouse_move(MSLLHOOKSTRUCT *mshook) {
415360
return consumed;
416361
}
417362

418-
bool dispatch_mouse_wheel(MSLLHOOKSTRUCT *mshook, uint8_t direction) {
363+
bool dispatch_mouse_wheel(uint64_t timestamp, MSLLHOOKSTRUCT *mshook, uint8_t direction) {
419364
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
425365

426366
// Track the number of clicks.
427367
// 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)