Skip to content

Commit

Permalink
alternative approach without synthesize
Browse files Browse the repository at this point in the history
  • Loading branch information
jackielii committed Mar 27, 2023
1 parent f7ad393 commit 6e3c717
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 42 deletions.
32 changes: 28 additions & 4 deletions src/hotkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
5 changes: 4 additions & 1 deletion src/skhd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, &current_mode, &carbon);
END_TIMED_BLOCK();

Expand Down
34 changes: 0 additions & 34 deletions src/synthesize.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 0 additions & 3 deletions src/synthesize.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 6e3c717

Please sign in to comment.