diff options
author | koekeishiya <aasvi93@hotmail.com> | 2018-08-28 19:05:40 +0200 |
---|---|---|
committer | koekeishiya <aasvi93@hotmail.com> | 2018-08-28 19:05:40 +0200 |
commit | 822c4603547aa927b62efb6f77dd0c6b56ec7f33 (patch) | |
tree | b4ac83c244578b7946f487cc85fb0d5d3c36b604 /src/carbon.c | |
parent | e616840f72bc0c2c18c1011a3d333fc15adabbfd (diff) | |
download | skhd-822c4603547aa927b62efb6f77dd0c6b56ec7f33.tar.gz skhd-822c4603547aa927b62efb6f77dd0c6b56ec7f33.zip |
initial code required for #47
Diffstat (limited to 'src/carbon.c')
-rw-r--r-- | src/carbon.c | 56 |
1 files changed, 56 insertions, 0 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; +} |