Skip to content

Commit

Permalink
Doing some more cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kwhat committed May 2, 2024
1 parent 9a0079a commit c7b3a0d
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 84 deletions.
4 changes: 2 additions & 2 deletions src/darwin/dispatch_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;

Expand Down
26 changes: 13 additions & 13 deletions src/darwin/input_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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];
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/darwin/input_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/darwin/post_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/windows/dispatch_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
34 changes: 17 additions & 17 deletions src/windows/input_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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];
}
}

Expand Down Expand Up @@ -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);
Expand Down
14 changes: 8 additions & 6 deletions src/windows/input_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
***********************************************************************/
Expand Down Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/windows/post_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/x11/dispatch_event.c
Original file line number Diff line number Diff line change
Expand Up @@ -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); }
Expand Down Expand Up @@ -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); }
Expand Down
45 changes: 14 additions & 31 deletions src/x11/input_helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
12 changes: 6 additions & 6 deletions src/x11/input_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();

Expand Down

0 comments on commit c7b3a0d

Please sign in to comment.