commit b1df5e29e466d9121fad035bdebe73ab8cbfd671 Author: Jordan Orelli Date: Sun Dec 4 01:28:41 2016 -0800 fuck yes diff --git a/procmon.go b/procmon.go new file mode 100644 index 0000000..f863e26 --- /dev/null +++ b/procmon.go @@ -0,0 +1,23 @@ +package main + +import ( + "fmt" + "runtime" +) + +/* +#cgo CFLAGS: -x objective-c +#cgo LDFLAGS: -framework AppKit +#include "procmon.h" +*/ +import "C" + +//export TheGoFunc +func TheGoFunc(name string) { + fmt.Printf("hi from c: %s\n", name) +} + +func main() { + C.TheCFunc() + runtime.Goexit() +} diff --git a/procmon.h b/procmon.h new file mode 100644 index 0000000..b91a0e2 --- /dev/null +++ b/procmon.h @@ -0,0 +1,3 @@ +#import + +void TheCFunc(); diff --git a/procmon.m b/procmon.m new file mode 100644 index 0000000..3abaee6 --- /dev/null +++ b/procmon.m @@ -0,0 +1,66 @@ +#include "procmon.h" +#import "_cgo_export.h" +#import + +@interface ProcWatcher : NSObject ++ (instancetype) shared; +- (void) applicationDidLaunch:(NSNotification*) notification; +@end + +@implementation ProcWatcher + ++ (instancetype) shared { + static id sharedInstance; + static dispatch_once_t predicate; + + NSLog(@"ProcWatcher shared instance accessed"); + + dispatch_once(&predicate, ^{ + TheGoFunc((GoString){"dispatch", 8}); + sharedInstance = [ProcWatcher new]; + }); + return sharedInstance; +} + +- (void) applicationDidLaunch:(NSNotification*) notification { + NSDictionary* info = notification.userInfo; + NSRunningApplication* app = info[NSWorkspaceApplicationKey]; + NSString* bundleId = app.bundleIdentifier; + + NSLog(@"application launched: %@", bundleId); + + TheGoFunc((GoString){bundleId.UTF8String, bundleId.length}); +} + +@end + +void TheCFunc() { + NSLog(@"current run loop: %@", [NSRunLoop currentRunLoop]); + NSLog(@"current run loop mode: %@", [[NSRunLoop currentRunLoop] currentMode]); + NSLog(@"current run loop mode: %@", [[NSRunLoop currentRunLoop] currentMode]); + + if ([NSThread isMainThread]) { + NSLog(@"TheCFunc is in the main thread"); + } else { + NSLog(@"TheCFunc is NOT in the main thread"); + } + + NSLog(@"hi from nslog"); + TheGoFunc((GoString){"hi", 2}); + + NSArray* running = [[NSWorkspace sharedWorkspace] runningApplications]; + NSLog(@"%@", running); + + ProcWatcher* pw = [ProcWatcher shared]; + + NSNotificationCenter* notifications = [[NSWorkspace sharedWorkspace] notificationCenter]; + + [notifications + addObserver: pw + selector: @selector(applicationDidLaunch:) + name: NSWorkspaceDidLaunchApplicationNotification object: nil]; + + NSLog(@"thing has finished"); + + [[NSRunLoop currentRunLoop] run]; +}