From c7b3a0d08301d66fb398a250f19053029568db06 Mon Sep 17 00:00:00 2001 From: Alexander Barker Date: Wed, 1 May 2024 21:46:53 -0700 Subject: [PATCH] Doing some more cleanup --- src/darwin/dispatch_event.c | 4 ++-- src/darwin/input_helper.c | 26 ++++++++++----------- src/darwin/input_helper.h | 4 ++-- src/darwin/post_event.c | 2 +- src/windows/dispatch_event.c | 6 ++--- src/windows/input_helper.c | 34 +++++++++++++-------------- src/windows/input_helper.h | 14 ++++++----- src/windows/post_event.c | 2 +- src/x11/dispatch_event.c | 4 ++-- src/x11/input_helper.c | 45 +++++++++++------------------------- src/x11/input_helper.h | 12 +++++----- 11 files changed, 69 insertions(+), 84 deletions(-) diff --git a/src/darwin/dispatch_event.c b/src/darwin/dispatch_event.c index f7f5c015..19ab95e7 100644 --- a/src/darwin/dispatch_event.c +++ b/src/darwin/dispatch_event.c @@ -115,7 +115,7 @@ bool dispatch_key_press(uint64_t timestamp, CGEventRef event_ref) { uio_event.mask |= MASK_EMULATED; } - uio_event.data.keyboard.keycode = keycode_to_scancode(keycode); + uio_event.data.keyboard.keycode = keycode_to_uiocode(keycode); uio_event.data.keyboard.rawcode = keycode; uio_event.data.keyboard.keychar = CHAR_UNDEFINED; @@ -172,7 +172,7 @@ bool dispatch_key_release(uint64_t timestamp, CGEventRef event_ref) { uio_event.mask |= MASK_EMULATED; } - uio_event.data.keyboard.keycode = keycode_to_scancode(keycode); + uio_event.data.keyboard.keycode = keycode_to_uiocode(keycode); uio_event.data.keyboard.rawcode = keycode; uio_event.data.keyboard.keychar = CHAR_UNDEFINED; diff --git a/src/darwin/input_helper.c b/src/darwin/input_helper.c index c1d3444c..d2b0ad5a 100644 --- a/src/darwin/input_helper.c +++ b/src/darwin/input_helper.c @@ -83,8 +83,8 @@ static pthread_mutex_t main_runloop_mutex = PTHREAD_MUTEX_INITIALIZER; #endif // Scancode loopup table -static const uint16_t keycode_scancode_table[][2] = { - /* idx { keycode, scancode }, */ +static const uint16_t uiocode_kvkcode_table[][2] = { + /* idx { uiocode, kvkcode }, */ /* 0 */ { VC_A, kVK_Undefined }, // 0x00 /* 1 */ { VC_S, kVK_Escape }, // 0x01 /* 2 */ { VC_D, kVK_ANSI_1 }, // 0x02 @@ -486,29 +486,29 @@ static void initialize_modifiers() { } -uint16_t keycode_to_scancode(UInt64 keycode) { - uint16_t scancode = VC_UNDEFINED; +uint16_t keycode_to_uiocode(UInt64 keycode) { + uint16_t uiocode = VC_UNDEFINED; // Bound check 0 <= keycode < 256 - if (keycode < sizeof(keycode_scancode_table) / sizeof(keycode_scancode_table[0])) { - scancode = keycode_scancode_table[keycode][0]; + if (keycode < sizeof(uiocode_kvkcode_table) / sizeof(uiocode_kvkcode_table[0])) { + uiocode = uiocode_kvkcode_table[keycode][0]; } - return scancode; + return uiocode; } -UInt64 scancode_to_keycode(uint16_t scancode) { +UInt64 uiocode_to_keycode(uint16_t uiocode) { UInt64 keycode = kVK_Undefined; // Bound check 0 <= keycode < 128 - if (scancode < 128) { - keycode = keycode_scancode_table[scancode][1]; + if (uiocode < 128) { + keycode = uiocode_kvkcode_table[scancode][1]; } else { // Calculate the upper offset. - unsigned short i = (scancode & 0x007F) | 0x80; + unsigned short i = (uiocode & 0x007F) | 0x80; - if (i < sizeof(keycode_scancode_table) / sizeof(keycode_scancode_table[1])) { - keycode = keycode_scancode_table[i][1]; + if (i < sizeof(uiocode_kvkcode_table) / sizeof(uiocode_kvkcode_table[1])) { + keycode = uiocode_kvkcode_table[i][1]; } } diff --git a/src/darwin/input_helper.h b/src/darwin/input_helper.h index 8ae40a23..2dd1512c 100644 --- a/src/darwin/input_helper.h +++ b/src/darwin/input_helper.h @@ -172,10 +172,10 @@ extern bool is_accessibility_enabled(); /* Converts an OSX keycode to the appropriate UIOHook scancode constant. */ -extern uint16_t keycode_to_scancode(UInt64 keycode); +extern uint16_t keycode_to_uiocode(UInt64 keycode); /* Converts a UIOHook scancode constant to the appropriate OSX keycode. */ -extern UInt64 scancode_to_keycode(uint16_t keycode); +extern UInt64 uiocode_to_keycode(uint16_t uiocode); /* Fill the buffer with unicode chars produced by the event_ref, returns how many chars where written */ extern UniCharCount event_to_unicode(CGEventRef event_ref, UniChar *buffer, UniCharCount size); diff --git a/src/darwin/post_event.c b/src/darwin/post_event.c index 65f02b2f..30ff1746 100644 --- a/src/darwin/post_event.c +++ b/src/darwin/post_event.c @@ -110,7 +110,7 @@ static int post_key_event(uiohook_event * const event, CGEventSourceRef src) { break; } - CGKeyCode keycode = (CGKeyCode) scancode_to_keycode(event->data.keyboard.keycode); + CGKeyCode keycode = (CGKeyCode) uiocode_to_keycode(event->data.keyboard.keycode); if (keycode == kVK_Undefined) { logger(LOG_LEVEL_WARN, "%s [%u]: Unable to lookup scancode: %li\n", __FUNCTION__, __LINE__, event->data.keyboard.keycode); diff --git a/src/windows/dispatch_event.c b/src/windows/dispatch_event.c index 399dfecc..a41c24d7 100644 --- a/src/windows/dispatch_event.c +++ b/src/windows/dispatch_event.c @@ -129,7 +129,7 @@ bool dispatch_key_press(uint64_t timestamp, KBDLLHOOKSTRUCT *kbhook) { uio_event.mask |= MASK_EMULATED; } - uio_event.data.keyboard.keycode = keycode_to_scancode(kbhook->vkCode, kbhook->flags); + uio_event.data.keyboard.keycode = vkcode_to_uiocode(kbhook->vkCode, kbhook->flags); uio_event.data.keyboard.rawcode = (uint16_t) kbhook->vkCode; uio_event.data.keyboard.keychar = CHAR_UNDEFINED; @@ -147,7 +147,7 @@ bool dispatch_key_press(uint64_t timestamp, KBDLLHOOKSTRUCT *kbhook) { WCHAR buffer[2]; // = { WCH_NONE }; // If the pressed event was not consumed and a unicode char exists... - SIZE_T count = keycode_to_unicode(kbhook->vkCode, buffer, sizeof(buffer)); + SIZE_T count = vkcode_to_unicode(kbhook->vkCode, buffer, sizeof(buffer)); for (unsigned int i = 0; i < count; i++) { // Populate key typed event. uio_event.time = timestamp; @@ -198,7 +198,7 @@ bool dispatch_key_release(uint64_t timestamp, KBDLLHOOKSTRUCT *kbhook) { uio_event.mask |= MASK_EMULATED; } - uio_event.data.keyboard.keycode = keycode_to_scancode(kbhook->vkCode, kbhook->flags); + uio_event.data.keyboard.keycode = vkcode_to_uiocode(kbhook->vkCode, kbhook->flags); uio_event.data.keyboard.rawcode = (uint16_t) kbhook->vkCode; uio_event.data.keyboard.keychar = CHAR_UNDEFINED; diff --git a/src/windows/input_helper.c b/src/windows/input_helper.c index db1be8a3..fed6796a 100644 --- a/src/windows/input_helper.c +++ b/src/windows/input_helper.c @@ -30,8 +30,8 @@ static uint16_t modifier_mask; -static const uint16_t keycode_scancode_table[][2] = { - /* idx { vk_code, scancode }, */ +static const uint16_t uiocode_vkcode_table[][2] = { + /* idx { uiocode, vkcode }, */ /* 0 */ { VC_UNDEFINED, 0x0000 }, // 0x00 /* 1 */ { MOUSE_BUTTON1, VK_ESCAPE }, // 0x01 /* 2 */ { MOUSE_BUTTON2, 0x0031 }, // 0x02 @@ -293,13 +293,13 @@ static const uint16_t keycode_scancode_table[][2] = { /* 255 */ { VC_UNDEFINED, 0x0000 } // 0xFE Unassigned }; -unsigned short keycode_to_scancode(DWORD vk_code, DWORD flags) { - unsigned short scancode = VC_UNDEFINED; +uint16_t vkcode_to_uiocode(DWORD vk_code, DWORD flags) { + uint16_t uiocode = VC_UNDEFINED; // Check the vk_code is in range. // NOTE vk_code >= 0 is assumed because DWORD is unsigned. - if (vk_code < sizeof(keycode_scancode_table) / sizeof(keycode_scancode_table[0])) { - scancode = keycode_scancode_table[vk_code][0]; + if (vk_code < sizeof(uiocode_vkcode_table) / sizeof(uiocode_vkcode_table[0])) { + uiocode = uiocode_vkcode_table[vk_code][0]; // Check the extended key flag as defined at: // https://learn.microsoft.com/en-us/windows/win32/inputdev/about-keyboard-input#extended-key-flag @@ -309,7 +309,7 @@ unsigned short keycode_to_scancode(DWORD vk_code, DWORD flags) { switch (vk_code) { case VK_RETURN: - scancode = VC_KP_ENTER; + uiocode = VC_KP_ENTER; break; } } else { @@ -328,28 +328,28 @@ unsigned short keycode_to_scancode(DWORD vk_code, DWORD flags) { case VK_INSERT: case VK_DELETE: - scancode |= 0xEE00; + uiocode |= 0xEE00; break; } } } - return scancode; + return uiocode; } -DWORD scancode_to_keycode(unsigned short scancode) { - unsigned short keycode = 0x0000; +DWORD uiocode_to_vkcode(uint16_t uiocode) { + DWORD keycode = 0x0000; // Check the vk_code is in range. // NOTE vk_code >= 0 is assumed because the scancode is unsigned. - if (scancode < 128) { - keycode = keycode_scancode_table[scancode][1]; + if (uiocode < 128) { + keycode = uiocode_vkcode_table[uiocode][1]; } else { // Calculate the upper offset based on the lower half of the scancode + 128. - unsigned short int i = (scancode & 0x007F) | 0x80; + unsigned short int i = (uiocode & 0x007F) | 0x80; - if (i < sizeof(keycode_scancode_table) / sizeof(keycode_scancode_table[1])) { - keycode = keycode_scancode_table[i][1]; + if (i < sizeof(uiocode_vkcode_table) / sizeof(uiocode_vkcode_table[1])) { + keycode = uiocode_vkcode_table[i][1]; } } @@ -685,7 +685,7 @@ static int refresh_locale_list() { } // Returns the number of chars written to the buffer. -SIZE_T keycode_to_unicode(DWORD keycode, PWCHAR buffer, SIZE_T size) { +SIZE_T vkcode_to_unicode(DWORD keycode, PWCHAR buffer, SIZE_T size) { // Get the thread id that currently has focus and ask for its current locale. DWORD focus_pid = GetWindowThreadProcessId(GetForegroundWindow(), NULL); HKL locale_id = GetKeyboardLayout(focus_pid); diff --git a/src/windows/input_helper.h b/src/windows/input_helper.h index c52f3e4c..2da771a0 100644 --- a/src/windows/input_helper.h +++ b/src/windows/input_helper.h @@ -36,7 +36,8 @@ * following functions under the terms of the Public Domain. * * For further reading and the original code, please visit: - * http://legacy.docdroppers.org/wiki/index.php?title=Writing_Keyloggers + * https://rayolback.files.wordpress.com/2010/04/keystrokes.doc + * http://www.docdroppers.org/wiki/index.php?title=Writing_Keyloggers * http://www.techmantras.com/content/writing-keyloggers-full-length-tutorial * ***********************************************************************/ @@ -126,13 +127,14 @@ typedef struct tagKbdLayer { } KBDTABLES, *PKBDTABLES; // __ptr64 -extern SIZE_T keycode_to_unicode(DWORD keycode, PWCHAR buffer, SIZE_T size); +/* Converts a uiohook virtual key code to the appropriate Windows VK key code. */ +extern DWORD uiocode_to_vkcode(uint16_t uiocode); -//extern DWORD unicode_to_keycode(wchar_t unicode); +/* Converts a Windows VK key code to the appropriate uiohook virtual key code. */ +extern uint16_t vkcode_to_uiocode(DWORD vk_code, DWORD flags); -extern unsigned short keycode_to_scancode(DWORD vk_code, DWORD flags); - -extern DWORD scancode_to_keycode(unsigned short scancode); +/* Converts a Windows vk code to it's unicode representation */ +extern SIZE_T vkcode_to_unicode(DWORD keycode, PWCHAR buffer, SIZE_T size); /* Set the native modifier mask for future events. */ extern void set_modifier_mask(uint16_t mask); diff --git a/src/windows/post_event.c b/src/windows/post_event.c index 437d9b72..0be5a031 100644 --- a/src/windows/post_event.c +++ b/src/windows/post_event.c @@ -75,7 +75,7 @@ static int map_keyboard_event(uiohook_event * const event, INPUT * const input) return UIOHOOK_FAILURE; } - input->ki.wVk = (WORD) scancode_to_keycode(event->data.keyboard.keycode); + input->ki.wVk = (WORD) uiocode_to_vkcode(event->data.keyboard.keycode); if (input->ki.wVk == 0x0000) { logger(LOG_LEVEL_WARN, "%s [%u]: Unable to lookup scancode: %li\n", __FUNCTION__, __LINE__, event->data.keyboard.keycode); diff --git a/src/x11/dispatch_event.c b/src/x11/dispatch_event.c index 7ff49313..5c829cad 100644 --- a/src/x11/dispatch_event.c +++ b/src/x11/dispatch_event.c @@ -102,7 +102,7 @@ bool dispatch_key_press(uint64_t timestamp, XKeyPressedEvent * const x_event) { wchar_t surrogate[2] = {}; size_t count = x_key_event_lookup(x_event, surrogate, sizeof(surrogate) - 1, &keysym); - uint16_t uiocode = keysym_to_vcode(keysym); + uint16_t uiocode = keysym_to_uiocode(keysym); // TODO VC_ALT_GRAPH MASK? if (uiocode == VC_SHIFT_L) { set_modifier_mask(MASK_SHIFT_L); } @@ -173,7 +173,7 @@ bool dispatch_key_release(uint64_t timestamp, XKeyReleasedEvent * const x_event) x_key_event_lookup(x_event, NULL, 0, &keysym); - uint16_t uiocode = keysym_to_vcode(keysym); + uint16_t uiocode = keysym_to_uiocode(keysym); // TODO VC_ALT_GRAPH MASK? if (uiocode == VC_SHIFT_L) { unset_modifier_mask(MASK_SHIFT_L); } diff --git a/src/x11/input_helper.c b/src/x11/input_helper.c index 59046ccb..826084a6 100644 --- a/src/x11/input_helper.c +++ b/src/x11/input_helper.c @@ -50,7 +50,7 @@ Display *helper_disp; // Where do we open this display? FIXME Use the ctrl dis static uint16_t modifier_mask; -static const uint32_t keysym_vcode_table[][2] = { +static const uint32_t uiocode_keysym_table[][2] = { { VC_ESCAPE, XK_Escape, }, { VC_ESCAPE, osfXK_Escape }, // HP OSF KeySym in HPkeysym.h @@ -429,51 +429,34 @@ static const uint32_t keysym_vcode_table[][2] = { }; -uint16_t keysym_to_vcode(KeySym keysym) { +uint16_t keysym_to_uiocode(KeySym keysym) { uint16_t uiocode = VC_UNDEFINED; - for (unsigned int i = 0; i < sizeof(keysym_vcode_table) / sizeof(keysym_vcode_table[0]); i++) { - if (keysym == keysym_vcode_table[i][1]) { - uiocode = keysym_vcode_table[i][0]; + for (unsigned int i = 0; i < sizeof(uiocode_keysym_table) / sizeof(uiocode_keysym_table[0]); i++) { + if (keysym == uiocode_keysym_table[i][1]) { + uiocode = uiocode_keysym_table[i][0]; break; } } - if ((get_modifiers() & MASK_NUM_LOCK) == 0) { - switch (uiocode) { - case VC_KP_SEPARATOR: - case VC_KP_1: - case VC_KP_2: - case VC_KP_3: - case VC_KP_4: - case VC_KP_5: - case VC_KP_6: - case VC_KP_7: - case VC_KP_8: - case VC_KP_0: - case VC_KP_9: - uiocode |= 0xEE00; - break; - } - } - return uiocode; } -KeyCode vcode_to_keycode(uint16_t vcode) { +KeyCode uiocode_to_keycode(uint16_t uiocode) { KeyCode keycode = 0x0000; KeyCode keysym = NoSymbol; - for (unsigned int i = 0; i < sizeof(keysym_vcode_table) / sizeof(keysym_vcode_table[0]); i++) { - if (vcode == keysym_vcode_table[i][0]) { - keycode = keysym_vcode_table[i][1]; - if (keysym = XKeysymToKeycode(helper_disp, keycode)) { - break; - } + for (unsigned int i = 0; i < sizeof(uiocode_keysym_table) / sizeof(uiocode_keysym_table[0]); i++) { + if (uiocode == uiocode_keysym_table[i][0]) { + keysym = uiocode_keysym_table[i][1]; + + // It doesn't matter if this succeeds, we won't find another match in the table so always exist. + keycode = XKeysymToKeycode(helper_disp, keysym) + break; } } - return keysym; + return keycode; } // Set the native modifier mask for future events. diff --git a/src/x11/input_helper.h b/src/x11/input_helper.h index 1a676096..6eca1e10 100644 --- a/src/x11/input_helper.h +++ b/src/x11/input_helper.h @@ -49,11 +49,14 @@ typedef union { extern Display *helper_disp; +/* Converts a uiohook virtual key code to the appropriate X11 key code. */ +extern KeyCode uiocode_to_keycode(uint16_t uiocode); + /* Converts a X11 key symbol to the appropriate uiohook virtual key code. */ -extern uint16_t keysym_to_vcode(KeySym keycode); +extern uint16_t keysym_to_uiocode(KeySym keysym); -/* Converts a uiohook virtual key code to the appropriate X11 key code. */ -extern KeyCode vcode_to_keycode(uint16_t uiocod); +/* Converts a X11 key event to a key symbol and retrieves it's appropriate unicode representation. */ +extern size_t event_to_unicode(XKeyEvent *x_event, wchar_t *surrogate, size_t length, KeySym *keysym); /* Set the native modifier mask for future events. */ extern void set_modifier_mask(uint16_t mask); @@ -70,9 +73,6 @@ extern void wire_data_to_event(XRecordInterceptData *recorded_data, XEvent *x_ev /* Lookup a X11 buttons possible remapping and return that value. */ extern uint8_t button_map_lookup(uint8_t button); -/* Lookup a KeySym and get it's string representation. */ -extern size_t x_key_event_lookup(XKeyEvent *x_event, wchar_t *surrogate, size_t length, KeySym *keysym); - /* Enable detectable auto-repeat for keys */ extern bool enable_key_repeat();