From 6e3c717b71c9d8b78f402c40f4b233bc4301eefd Mon Sep 17 00:00:00 2001 From: Jackie Li Date: Mon, 27 Mar 2023 23:55:34 +0100 Subject: [PATCH] alternative approach without synthesize --- src/hotkey.c | 32 ++++++++++++++++++++++++++++---- src/skhd.c | 5 ++++- src/synthesize.c | 34 ---------------------------------- src/synthesize.h | 3 --- 4 files changed, 32 insertions(+), 42 deletions(-) diff --git a/src/hotkey.c b/src/hotkey.c index 75e069b..36f9f60 100644 --- a/src/hotkey.c +++ b/src/hotkey.c @@ -164,14 +164,38 @@ find_process_command_mapping(struct hotkey *hotkey, uint32_t *capture, struct ca return result; } +bool find_and_forward_hotkey(struct hotkey *k, struct mode *m, CGEventRef event) +{ + struct hotkey *found = table_find(&m->hotkey_map, k); + if (!found || !found->forwarded_hotkey) return false; + debug("forwarding hotkey\n"); + struct hotkey *forwarded = found->forwarded_hotkey; + + int flags = 0; + if (has_flags(forwarded, Hotkey_Flag_Alt)) { + flags |= kCGEventFlagMaskAlternate; + } + if (has_flags(forwarded, Hotkey_Flag_Shift)) { + flags |= kCGEventFlagMaskShift; + } + if (has_flags(forwarded, Hotkey_Flag_Cmd)) { + flags |= kCGEventFlagMaskCommand; + } + if (has_flags(forwarded, Hotkey_Flag_Control)) { + flags |= kCGEventFlagMaskControl; + } + if (has_flags(forwarded, Hotkey_Flag_Fn)) { + flags |= kCGEventFlagMaskSecondaryFn; + } + CGEventSetFlags(event, flags); + CGEventSetIntegerValueField(event, kCGKeyboardEventKeycode, forwarded->key); + return true; +} + bool find_and_exec_hotkey(struct hotkey *k, struct table *t, struct mode **m, struct carbon_event *carbon) { uint32_t c = MODE_CAPTURE((int)(*m)->capture); for (struct hotkey *h = find_hotkey(*m, k, &c); h; passthrough(h, &c), h = 0) { - if (h->forwarded_hotkey) { - synthesize_forward_hotkey(h->forwarded_hotkey); - continue; - } char *cmd = h->command[0]; if (has_flags(h, Hotkey_Flag_Activate)) { *m = table_find(t, cmd); diff --git a/src/skhd.c b/src/skhd.c index be19110..3feccec 100644 --- a/src/skhd.c +++ b/src/skhd.c @@ -177,8 +177,11 @@ internal EVENT_TAP_CALLBACK(key_handler) return event; } - BEGIN_TIMED_BLOCK("handle_keypress"); struct hotkey eventkey = create_eventkey(event); + if (find_and_forward_hotkey(&eventkey, current_mode, event)) { + return event; + } + BEGIN_TIMED_BLOCK("handle_keypress"); bool result = find_and_exec_hotkey(&eventkey, &mode_map, ¤t_mode, &carbon); END_TIMED_BLOCK(); diff --git a/src/synthesize.c b/src/synthesize.c index c8359f5..6eddd6f 100644 --- a/src/synthesize.c +++ b/src/synthesize.c @@ -63,40 +63,6 @@ void synthesize_key(char *key_string) synthesize_modifiers(hotkey, false); } -void synthesize_forward_hotkey(struct hotkey *hotkey) -{ - CGEventSourceRef source = CGEventSourceCreate(kCGEventSourceStateHIDSystemState); - CGEventSourceSetUserData(source, SHKD_CGEVENT_USER_DATA); - - CGEventRef de = CGEventCreateKeyboardEvent(source, hotkey->key, true); - CGEventRef ue = CGEventCreateKeyboardEvent(source, hotkey->key, false); - - int flags = 0; - if (has_flags(hotkey, Hotkey_Flag_Alt)) { - flags |= kCGEventFlagMaskAlternate; - } - if (has_flags(hotkey, Hotkey_Flag_Shift)) { - flags |= kCGEventFlagMaskShift; - } - if (has_flags(hotkey, Hotkey_Flag_Cmd)) { - flags |= kCGEventFlagMaskCommand; - } - if (has_flags(hotkey, Hotkey_Flag_Control)) { - flags |= kCGEventFlagMaskControl; - } - if (has_flags(hotkey, Hotkey_Flag_Fn)) { - flags |= kCGEventFlagMaskSecondaryFn; - } - CGEventSetFlags(de, flags); - - CGEventPost(kCGSessionEventTap, de); - CGEventPost(kCGSessionEventTap, ue); - - CFRelease(ue); - CFRelease(de); - CFRelease(source); -} - void synthesize_text(char *text) { CFStringRef text_ref = CFStringCreateWithCString(NULL, text, kCFStringEncodingUTF8); diff --git a/src/synthesize.h b/src/synthesize.h index be7d3dc..4a2b262 100644 --- a/src/synthesize.h +++ b/src/synthesize.h @@ -4,7 +4,4 @@ void synthesize_key(char *key_string); void synthesize_text(char *text); -void synthesize_forward_hotkey(struct hotkey *hotkey); -#define SHKD_CGEVENT_USER_DATA 0x12345 - #endif