diff options
-rw-r--r-- | src/carbon.c | 56 | ||||
-rw-r--r-- | src/skhd.c | 14 |
2 files changed, 68 insertions, 2 deletions
diff --git a/src/carbon.c b/src/carbon.c new file mode 100644 index 0000000..98e2793 --- /dev/null +++ b/src/carbon.c @@ -0,0 +1,56 @@ +#include <Carbon/Carbon.h> + +struct carbon_event +{ + EventTargetRef target; + EventHandlerUPP handler; + EventTypeSpec type; + EventHandlerRef handler_ref; + char * volatile process_name; +}; + +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wdeprecated" +static OSStatus +carbon_event_handler(EventHandlerCallRef ref, EventRef event, void *context) +{ + struct carbon_event *carbon = (struct carbon_event *) context; + + ProcessSerialNumber psn; + if (GetEventParameter(event, + kEventParamProcessID, + typeProcessSerialNumber, + NULL, + sizeof(psn), + NULL, + &psn) != noErr) { + return -1; + } + + CFStringRef process_name_ref; + if (CopyProcessName(&psn, &process_name_ref) == noErr) { + if (carbon->process_name) free(carbon->process_name); + carbon->process_name = copy_cfstring(process_name_ref); + printf("front app changed: %s\n", carbon->process_name); + CFRelease(process_name_ref); + } + + return noErr; +} +#pragma clang diagnostic pop + +bool carbon_event_init(struct carbon_event *carbon) +{ + carbon->target = GetApplicationEventTarget(); + carbon->handler = NewEventHandlerUPP(carbon_event_handler); + carbon->type.eventClass = kEventClassApplication; + carbon->type.eventKind = kEventAppFrontSwitched; + carbon->process_name = NULL; + + return InstallEventHandler(carbon->target, + carbon->handler, + 1, + &carbon->type, + carbon, + &carbon->handler_ref) == noErr; +} @@ -27,6 +27,7 @@ #include "parse.c" #include "hotkey.c" #include "synthesize.c" +// #include "carbon.c" #define internal static extern bool CGSIsSecureEventInputSet(); @@ -62,6 +63,9 @@ internal unsigned major_version = 0; internal unsigned minor_version = 2; internal unsigned patch_version = 5; +// internal struct carbon_event carbon; +internal struct event_tap event_tap; +internal struct hotloader hotloader; internal struct mode *current_mode; internal struct table mode_map; internal char *config_file; @@ -219,6 +223,14 @@ int main(int argc, char **argv) error("skhd: could not initialize keycode map! abort..\n"); } + /* + * NOTE(koekeishiya: hooks up event for tracking name of focused process/application + * + * if (!carbon_event_init(&carbon)) { + * error("skhd: could not initialize carbon events! abort..\n"); + * } + */ + if (!config_file) { use_default_config_path(); } @@ -234,13 +246,11 @@ int main(int argc, char **argv) END_SCOPED_TIMED_BLOCK(); BEGIN_SCOPED_TIMED_BLOCK("begin_eventtap"); - struct event_tap event_tap; event_tap.mask = (1 << kCGEventKeyDown) | (1 << NX_SYSDEFINED); event_tap_begin(&event_tap, key_handler); END_SCOPED_TIMED_BLOCK(); BEGIN_SCOPED_TIMED_BLOCK("begin_hotloader"); - struct hotloader hotloader = {}; if (hotloader_add_file(&hotloader, config_file) && hotloader_begin(&hotloader, config_handler)) { debug("skhd: watching '%s' for changes\n", config_file); |