Skip to content

Commit

Permalink
add context to frontmost_application_observer
Browse files Browse the repository at this point in the history
  • Loading branch information
tekezo committed Jul 3, 2017
1 parent 5f4667b commit 1806348
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
8 changes: 5 additions & 3 deletions appendix/frontmost_application_observer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
#include "thread_utility.hpp"
#include <Carbon/Carbon.h>

static void callback(const char* bundle_identifier, const char* file_path) {
namespace {
void callback(const std::string& bundle_identifier, const std::string& file_path) {
krbn::logger::get_logger().info("callback");
krbn::logger::get_logger().info(" bundle_identifier:{0}", bundle_identifier ? bundle_identifier : "(nullptr)");
krbn::logger::get_logger().info(" file_path:{0}", file_path ? file_path : "(nullptr)");
krbn::logger::get_logger().info(" bundle_identifier:{0}", bundle_identifier);
krbn::logger::get_logger().info(" file_path:{0}", file_path);
}
} // namespace

int main(int argc, char** argv) {
krbn::logger::get_logger().set_level(spdlog::level::off);
Expand Down
28 changes: 26 additions & 2 deletions src/share/frontmost_application_observer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,39 @@
namespace krbn {
class frontmost_application_observer final {
public:
frontmost_application_observer(krbn_frontmost_application_observer_callback callback) : observer_(nullptr) {
krbn_frontmost_application_observer_initialize(&observer_, callback);
typedef std::function<void(const std::string& bundle_identifier,
const std::string& file_path)>
callback;

frontmost_application_observer(const callback& callback) : callback_(callback),
observer_(nullptr) {
krbn_frontmost_application_observer_initialize(&observer_,
static_cpp_callback,
this);
}

~frontmost_application_observer(void) {
krbn_frontmost_application_observer_terminate(&observer_);
}

private:
static void static_cpp_callback(const char* bundle_identifier,
const char* file_path,
void* context) {
auto observer = reinterpret_cast<frontmost_application_observer*>(context);
if (observer) {
observer->cpp_callback(bundle_identifier, file_path);
}
}

void cpp_callback(const std::string& bundle_identifier,
const std::string& file_path) {
if (callback_) {
callback_(bundle_identifier, file_path);
}
}

callback callback_;
krbn_frontmost_application_observer_objc* observer_;
};
} // namespace krbn
6 changes: 4 additions & 2 deletions src/share/frontmost_application_observer_objc.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ extern "C" {

typedef void krbn_frontmost_application_observer_objc;
typedef void (*krbn_frontmost_application_observer_callback)(const char* bundle_identifier,
const char* file_path);
const char* file_path,
void* context);
void krbn_frontmost_application_observer_initialize(krbn_frontmost_application_observer_objc** observer,
krbn_frontmost_application_observer_callback callback);
krbn_frontmost_application_observer_callback callback,
void* context);
void krbn_frontmost_application_observer_terminate(krbn_frontmost_application_observer_objc** observer);

#ifdef __cplusplus
Expand Down
16 changes: 12 additions & 4 deletions src/share/frontmost_application_observer_objc.m
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,19 @@
@interface KrbnFrontmostApplicationObserver : NSObject

@property krbn_frontmost_application_observer_callback callback;
@property void* context;

@end

@implementation KrbnFrontmostApplicationObserver

- (instancetype)initWithCallback:(krbn_frontmost_application_observer_callback)callback {
- (instancetype)initWithCallback:(krbn_frontmost_application_observer_callback)callback
context:(void*)context {
self = [super init];

if (self) {
_callback = callback;
_context = context;

[NSWorkspace.sharedWorkspace.notificationCenter addObserver:self
selector:@selector(handleNotification:)
Expand All @@ -33,7 +36,11 @@ - (void)runCallback:(NSRunningApplication*)runningApplication {
@try {
NSString* bundleIdentifier = runningApplication.bundleIdentifier;
NSString* path = [[runningApplication.executableURL path] stringByStandardizingPath];
self.callback([bundleIdentifier UTF8String], [path UTF8String]);

const char* b = [bundleIdentifier UTF8String];
const char* p = [path UTF8String];

self.callback(b ? b : "", p ? p : "", self.context);
} @catch (NSException* exception) {
NSLog(@"runCallback error");
}
Expand All @@ -56,7 +63,8 @@ - (void)runCallbackWithFrontmostApplication {
@end

void krbn_frontmost_application_observer_initialize(krbn_frontmost_application_observer_objc** observer,
krbn_frontmost_application_observer_callback callback) {
krbn_frontmost_application_observer_callback callback,
void* context) {
if (!observer) {
NSLog(@"krbn_frontmost_application_observer_initialize invalid arguments");
return;
Expand All @@ -66,7 +74,7 @@ void krbn_frontmost_application_observer_initialize(krbn_frontmost_application_o
return;
}

KrbnFrontmostApplicationObserver* o = [[KrbnFrontmostApplicationObserver alloc] initWithCallback:callback];
KrbnFrontmostApplicationObserver* o = [[KrbnFrontmostApplicationObserver alloc] initWithCallback:callback context:context];
[o runCallbackWithFrontmostApplication];

*observer = (__bridge_retained void*)(o);
Expand Down

0 comments on commit 1806348

Please sign in to comment.