aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkoekeishiya <aasvi93@hotmail.com>2018-08-28 19:05:40 +0200
committerkoekeishiya <aasvi93@hotmail.com>2018-08-28 19:05:40 +0200
commit822c4603547aa927b62efb6f77dd0c6b56ec7f33 (patch)
treeb4ac83c244578b7946f487cc85fb0d5d3c36b604 /src
parente616840f72bc0c2c18c1011a3d333fc15adabbfd (diff)
downloadskhd-822c4603547aa927b62efb6f77dd0c6b56ec7f33.tar.gz
skhd-822c4603547aa927b62efb6f77dd0c6b56ec7f33.zip
initial code required for #47
Diffstat (limited to 'src')
-rw-r--r--src/carbon.c56
-rw-r--r--src/skhd.c14
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;
+}
diff --git a/src/skhd.c b/src/skhd.c
index b33b6c7..1391125 100644
--- a/src/skhd.c
+++ b/src/skhd.c
@@ -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);