Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/carbon.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,58 @@ find_active_process_name(void)
GetFrontProcess(&psn);
return find_process_name_for_psn(&psn);
}

static inline char *
find_process_bundle_id_for_psn(ProcessSerialNumber *psn)
{
CFDictionaryRef process_info_ref = ProcessInformationCopyDictionary(psn, kProcessDictionaryIncludeAllInformationMask);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we assume this call will never return NULL?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not according to my tests and I can't find any documentation since it's long deprecated. But I'll add checks just to be sure.

if (!process_info_ref) {
return NULL;
}
CFStringRef process_bundle_id_ref = CFDictionaryGetValue(process_info_ref, kCFBundleIdentifierKey);
CFRelease(process_info_ref);
if (!process_bundle_id_ref) {
return NULL;
}
char *process_bundle_id = copy_cfstring(process_bundle_id_ref);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy_cfstring_to_cstring will cause memory leak if it fails.

How are we ensuring it will not return NULL and dereference?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. Added checks for null value.

CFRelease(process_bundle_id_ref);
for (char *s = process_bundle_id; *s; ++s) *s = tolower(*s);
return process_bundle_id;
}

static inline char *
find_active_process_bundle_id(void)
{
ProcessSerialNumber psn;
GetFrontProcess(&psn);
return find_process_bundle_id_for_psn(&psn);
}

static inline char *
find_process_bundle_name_for_psn(ProcessSerialNumber *psn)
{
CFDictionaryRef process_info_ref = ProcessInformationCopyDictionary(psn, kProcessDictionaryIncludeAllInformationMask);
if (!process_info_ref) {
return NULL;
}
CFStringRef process_bundle_name_ref = CFDictionaryGetValue(process_info_ref, kCFBundleNameKey);
CFRelease(process_info_ref);
if (process_bundle_name_ref) {
return NULL;
}
char *process_bundle_name = copy_cfstring(process_bundle_name_ref);
CFRelease(process_bundle_name_ref);
for (char *s = process_bundle_name; *s; ++s) *s = tolower(*s);
return process_bundle_name;
}

static inline char *
find_active_process_bundle_name(void)
{
ProcessSerialNumber psn;
GetFrontProcess(&psn);
return find_process_bundle_name_for_psn(&psn);
}
#pragma clang diagnostic pop

static OSStatus
Expand All @@ -55,6 +107,20 @@ carbon_event_handler(EventHandlerCallRef ref, EventRef event, void *context)

carbon->process_name = find_process_name_for_psn(&psn);

if (carbon->process_bundle_id) {
free(carbon->process_bundle_id);
carbon->process_bundle_id = NULL;
}

carbon->process_bundle_id = find_process_bundle_id_for_psn(&psn);

if (carbon->process_bundle_name) {
free(carbon->process_bundle_name);
carbon->process_bundle_name = NULL;
}

carbon->process_bundle_name = find_process_bundle_name_for_psn(&psn);

return noErr;
}

Expand All @@ -65,6 +131,8 @@ bool carbon_event_init(struct carbon_event *carbon)
carbon->type.eventClass = kEventClassApplication;
carbon->type.eventKind = kEventAppFrontSwitched;
carbon->process_name = find_active_process_name();
carbon->process_bundle_id = find_active_process_bundle_id();
carbon->process_bundle_name = find_active_process_bundle_name();

return InstallEventHandler(carbon->target,
carbon->handler,
Expand Down
2 changes: 2 additions & 0 deletions src/carbon.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ struct carbon_event
EventTypeSpec type;
EventHandlerRef handler_ref;
char * volatile process_name;
char * volatile process_bundle_id;
char * volatile process_bundle_name;
};

char *find_process_name_for_pid(pid_t pid);
Expand Down
4 changes: 3 additions & 1 deletion src/hotkey.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ find_process_command_mapping(struct hotkey *hotkey, uint32_t *capture, struct ca
bool found = false;

for (int i = 0; i < buf_len(hotkey->process_name); ++i) {
if (same_string(carbon->process_name, hotkey->process_name[i])) {
if (same_string(carbon->process_name, hotkey->process_name[i])
|| same_string(carbon->process_bundle_id, hotkey->process_name[i])
|| same_string(carbon->process_bundle_name, hotkey->process_name[i])) {
result = hotkey->command[i];
found = true;
break;
Expand Down
4 changes: 4 additions & 0 deletions src/skhd.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ static EVENT_TAP_CALLBACK(key_handler)
} break;
case kCGEventKeyDown: {
if (table_find(&blacklst, carbon.process_name)) return event;
if (table_find(&blacklst, carbon.process_bundle_id)) return event;
if (table_find(&blacklst, carbon.process_bundle_name)) return event;
if (!current_mode) return event;

BEGIN_TIMED_BLOCK("handle_keypress");
Expand All @@ -181,6 +183,8 @@ static EVENT_TAP_CALLBACK(key_handler)
} break;
case NX_SYSDEFINED: {
if (table_find(&blacklst, carbon.process_name)) return event;
if (table_find(&blacklst, carbon.process_bundle_id)) return event;
if (table_find(&blacklst, carbon.process_bundle_name)) return event;
if (!current_mode) return event;

struct hotkey eventkey;
Expand Down