Skip to content

Commit

Permalink
Windows Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhat committed Apr 13, 2024
1 parent fec4617 commit 3d0debe
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 203 deletions.
82 changes: 11 additions & 71 deletions src/windows/dispatch_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,6 @@ UIOHOOK_API void hook_set_dispatch_proc(dispatcher_t dispatch_proc, void *user_d
dispatch_data = user_data;
}

#ifdef USE_EPOCH_TIME
static uint64_t get_unix_timestamp() {
FILETIME system_time;

// Get the local system time in UTC.
GetSystemTimeAsFileTime(&system_time);

// Convert the local system time to a Unix epoch in MS.
// milliseconds = 100-nanoseconds / 10000
uint64_t timestamp = (((uint64_t) system_time.dwHighDateTime << 32) | system_time.dwLowDateTime) / 10000;

// Convert Windows epoch to Unix epoch. (1970 - 1601 in milliseconds)
timestamp -= 11644473600000;

return timestamp;
}
#endif

// Send out an event if a dispatcher was set.
static void dispatch_event(uiohook_event *const event) {
if (dispatch != NULL) {
Expand All @@ -75,17 +57,11 @@ static void dispatch_event(uiohook_event *const event) {
}
}

bool dispatch_hook_enable() {
bool dispatch_hook_enable(uint64_t timestamp) {
bool consumed = false;
// Initialize native input helper functions.
load_input_helper();

// Get the local system time in UNIX epoch form.
#ifdef USE_EPOCH_TIME
uint64_t timestamp = get_unix_timestamp();
#else
uint64_t timestamp = GetMessageTime();
#endif
// Initialize native input helper.
load_input_helper();

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

bool dispatch_hook_disable() {
bool dispatch_hook_disable(uint64_t timestamp) {
bool consumed = false;
// Get the local system time in UNIX epoch form.
#ifdef USE_EPOCH_TIME
uint64_t timestamp = get_unix_timestamp();
#else
uint64_t timestamp = GetMessageTime();
#endif

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

// Deinitialize native input helper functions.
// Uninitialize the native input helper.
unload_input_helper();

return consumed;
}

bool dispatch_key_press(KBDLLHOOKSTRUCT *kbhook) {
bool dispatch_key_press(uint64_t timestamp, KBDLLHOOKSTRUCT *kbhook) {
bool consumed = false;
#ifdef USE_EPOCH_TIME
uint64_t timestamp = get_unix_timestamp();
#else
uint64_t timestamp = kbhook->time;
#endif

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

bool dispatch_key_release(KBDLLHOOKSTRUCT *kbhook) {
bool dispatch_key_release(uint64_t timestamp, KBDLLHOOKSTRUCT *kbhook) {
bool consumed = false;
#ifdef USE_EPOCH_TIME
uint64_t timestamp = get_unix_timestamp();
#else
uint64_t timestamp = kbhook->time;
#endif

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

bool dispatch_button_press(MSLLHOOKSTRUCT *mshook, uint16_t button) {
bool dispatch_button_press(uint64_t timestamp, MSLLHOOKSTRUCT *mshook, uint16_t button) {
bool consumed = false;
#ifdef USE_EPOCH_TIME
uint64_t timestamp = get_unix_timestamp();
#else
uint64_t timestamp = mshook->time;
#endif

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

bool dispatch_button_release(MSLLHOOKSTRUCT *mshook, uint16_t button) {
bool dispatch_button_release(uint64_t timestamp, MSLLHOOKSTRUCT *mshook, uint16_t button) {
bool consumed = false;
#ifdef USE_EPOCH_TIME
uint64_t timestamp = get_unix_timestamp();
#else
uint64_t timestamp = mshook->time;
#endif

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


bool dispatch_mouse_move(MSLLHOOKSTRUCT *mshook) {
bool dispatch_mouse_move(uint64_t timestamp, MSLLHOOKSTRUCT *mshook) {
bool consumed = false;
#ifdef USE_EPOCH_TIME
uint64_t timestamp = get_unix_timestamp();
#else
uint64_t timestamp = mshook->time;
#endif

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

bool dispatch_mouse_wheel(MSLLHOOKSTRUCT *mshook, uint8_t direction) {
bool dispatch_mouse_wheel(uint64_t timestamp, MSLLHOOKSTRUCT *mshook, uint8_t direction) {
bool consumed = false;
#ifdef USE_EPOCH_TIME
uint64_t timestamp = get_unix_timestamp();
#else
uint64_t timestamp = mshook->time;
#endif

// Track the number of clicks.
// Reset the click count and previous button.
Expand Down
16 changes: 8 additions & 8 deletions src/windows/dispatch_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@
#include <stdbool.h>
#include <windows.h>

extern bool dispatch_hook_enable();
extern bool dispatch_hook_enable(uint64_t timestamp);

extern bool dispatch_hook_disable();
extern bool dispatch_hook_disable(uint64_t timestamp);

extern bool dispatch_key_press(KBDLLHOOKSTRUCT *kbhook);
extern bool dispatch_key_press(uint64_t timestamp, KBDLLHOOKSTRUCT *kbhook);

extern bool dispatch_key_release(KBDLLHOOKSTRUCT *kbhook);
extern bool dispatch_key_release(uint64_t timestamp, KBDLLHOOKSTRUCT *kbhook);

extern bool dispatch_button_press(MSLLHOOKSTRUCT *mshook, uint16_t button);
extern bool dispatch_button_press(uint64_t timestamp, MSLLHOOKSTRUCT *mshook, uint16_t button);

extern bool dispatch_button_release(MSLLHOOKSTRUCT *mshook, uint16_t button);
extern bool dispatch_button_release(uint64_t timestamp, MSLLHOOKSTRUCT *mshook, uint16_t button);

extern bool dispatch_mouse_move(MSLLHOOKSTRUCT *mshook);
extern bool dispatch_mouse_move(uint64_t timestamp, MSLLHOOKSTRUCT *mshook);

extern bool dispatch_mouse_wheel(MSLLHOOKSTRUCT *mshook, uint8_t direction);
extern bool dispatch_mouse_wheel(uint64_t timestamp, MSLLHOOKSTRUCT *mshook, uint8_t direction);
27 changes: 27 additions & 0 deletions src/windows/input_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,28 @@ uint16_t get_modifiers() {
return modifier_mask;
}

static void initialize_modifiers() {
// NOTE We are checking the high order bit, so it will be < 0 for a singed short.
if (GetKeyState(VK_LSHIFT) < 0) { set_modifier_mask(MASK_SHIFT_L); }
if (GetKeyState(VK_RSHIFT) < 0) { set_modifier_mask(MASK_SHIFT_R); }
if (GetKeyState(VK_LCONTROL) < 0) { set_modifier_mask(MASK_CTRL_L); }
if (GetKeyState(VK_RCONTROL) < 0) { set_modifier_mask(MASK_CTRL_R); }
if (GetKeyState(VK_LMENU) < 0) { set_modifier_mask(MASK_ALT_L); }
if (GetKeyState(VK_RMENU) < 0) { set_modifier_mask(MASK_ALT_R); }
if (GetKeyState(VK_LWIN) < 0) { set_modifier_mask(MASK_META_L); }
if (GetKeyState(VK_RWIN) < 0) { set_modifier_mask(MASK_META_R); }

if (GetKeyState(VK_LBUTTON) < 0) { set_modifier_mask(MASK_BUTTON1); }
if (GetKeyState(VK_RBUTTON) < 0) { set_modifier_mask(MASK_BUTTON2); }
if (GetKeyState(VK_MBUTTON) < 0) { set_modifier_mask(MASK_BUTTON3); }
if (GetKeyState(VK_XBUTTON1) < 0) { set_modifier_mask(MASK_BUTTON4); }
if (GetKeyState(VK_XBUTTON2) < 0) { set_modifier_mask(MASK_BUTTON5); }

if (GetKeyState(VK_NUMLOCK) < 0) { set_modifier_mask(MASK_NUM_LOCK); }
if (GetKeyState(VK_CAPITAL) < 0) { set_modifier_mask(MASK_CAPS_LOCK); }
if (GetKeyState(VK_SCROLL) < 0) { set_modifier_mask(MASK_SCROLL_LOCK); }
}


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

// Returns the number of locales that were loaded.
int load_input_helper() {
initialize_modifiers();

#if defined(_WIN32) && !defined(_WIN64)
if (is_wow64()) {
ptr_padding = sizeof(void *);
Expand Down Expand Up @@ -885,5 +909,8 @@ int unload_input_helper() {
// Reset the current local.
locale_current = NULL;

// Reset the modifier mask.
modifier_mask = 0x0;

return count;
}
Loading

0 comments on commit 3d0debe

Please sign in to comment.