aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2018-10-20 21:07:37 +0200
committerTeddy Wing2018-10-20 21:07:37 +0200
commit1a7c0f0e45633424bef4872a0cc2a19877ba25f9 (patch)
treeb99f242a6a8d3d6ffc01ee2d423e261a0dcde989
parentdb8ad5e7c4c4dc027b242f00278daf661ad3ef22 (diff)
downloadDomeKey-1a7c0f0e45633424bef4872a0cc2a19877ba25f9.tar.bz2
Get timeout from config
Pass the config through the app, and pass the timeout it contains to `HeadphoneKey`. This enables us to have a user-configurable timeout through a config file.
-rw-r--r--DomeKey/AppDelegate.h3
-rw-r--r--DomeKey/AppDelegate.m11
-rw-r--r--DomeKey/HeadphoneKey.h4
-rw-r--r--DomeKey/HeadphoneKey.m16
-rw-r--r--DomeKey/main.m2
5 files changed, 32 insertions, 4 deletions
diff --git a/DomeKey/AppDelegate.h b/DomeKey/AppDelegate.h
index b343d6c..a8f2d01 100644
--- a/DomeKey/AppDelegate.h
+++ b/DomeKey/AppDelegate.h
@@ -8,12 +8,15 @@
#import <Cocoa/Cocoa.h>
#import "HeadphoneKey.h"
+#import "dome_key_map.h"
@interface AppDelegate : NSObject <NSApplicationDelegate> {
HeadphoneKey *_headphone_key;
MPRemoteCommandCenter *_blargh;
+ Config *_config;
}
+- (instancetype)initWithConfig:(Config *)config;
- (void)mpmediaplayerBS;
@end
diff --git a/DomeKey/AppDelegate.m b/DomeKey/AppDelegate.m
index 32bbd47..2146ef6 100644
--- a/DomeKey/AppDelegate.m
+++ b/DomeKey/AppDelegate.m
@@ -10,9 +10,18 @@
@implementation AppDelegate
+- (instancetype)initWithConfig:(Config *)config
+{
+ self = [super init];
+ if (self) {
+ _config = config;
+ }
+ return self;
+}
+
- (void)applicationDidFinishLaunching:(NSNotification *)notification
{
- _headphone_key = [[HeadphoneKey alloc] init];
+ _headphone_key = [[HeadphoneKey alloc] initWithTimeout:_config->timeout];
[_headphone_key startMonitoringBluetoothEvents];
_blargh = [MPRemoteCommandCenter sharedCommandCenter];
diff --git a/DomeKey/HeadphoneKey.h b/DomeKey/HeadphoneKey.h
index 4f048e0..f6533c3 100644
--- a/DomeKey/HeadphoneKey.h
+++ b/DomeKey/HeadphoneKey.h
@@ -18,7 +18,7 @@ typedef enum KeyPress : BOOL {
KeyPressUp = NO
} KeyPress;
-static const unsigned int TIMEOUT_MILLISECONDS = 1000;
+static const Milliseconds TIMEOUT_DEFAULT = 500;
@interface HeadphoneKey : NSObject {
NSArray *_mikeys;
@@ -26,8 +26,10 @@ static const unsigned int TIMEOUT_MILLISECONDS = 1000;
// const Trigger *_in_mode;
Trigger *_in_mode;
State *_state;
+ Milliseconds _timeout;
}
+- (instancetype)initWithTimeout:(Milliseconds)timeout;
- (void)handleDeadKey:(HeadphoneButton)button;
- (void)runAction;
- (void)startMonitoringBluetoothEvents;
diff --git a/DomeKey/HeadphoneKey.m b/DomeKey/HeadphoneKey.m
index e5ea7b0..5a83ad0 100644
--- a/DomeKey/HeadphoneKey.m
+++ b/DomeKey/HeadphoneKey.m
@@ -17,6 +17,11 @@
_key_buffer = [[NSMutableArray alloc] initWithCapacity:5];
_in_mode = NULL;
_state = state_new();
+
+ // Should never be used. We initialise it just in case, but the real
+ // default should always come from a `Config`, set in the Rust library.
+ _timeout = TIMEOUT_DEFAULT;
+
logger_init();
state_load_map_group(_state);
@@ -30,6 +35,15 @@
return self;
}
+- (instancetype)initWithTimeout:(Milliseconds)timeout
+{
+ self = [self init];
+ if (self) {
+ _timeout = timeout;
+ }
+ return self;
+}
+
- (void)dealloc
{
state_free(_state);
@@ -107,7 +121,7 @@
selector:@selector(runAction)
object:nil];
- NSTimeInterval timeout_seconds = TIMEOUT_MILLISECONDS / 1000.0;
+ NSTimeInterval timeout_seconds = _timeout / 1000.0;
[self performSelector:@selector(runAction)
withObject:nil
afterDelay:timeout_seconds];
diff --git a/DomeKey/main.m b/DomeKey/main.m
index 0f9d6ac..cabe180 100644
--- a/DomeKey/main.m
+++ b/DomeKey/main.m
@@ -27,7 +27,7 @@ int main(int argc, const char * argv[]) {
} else if (config->args.daemon) {
@autoreleasepool {
[NSApplication sharedApplication];
- AppDelegate *app = [[AppDelegate alloc] init];
+ AppDelegate *app = [[AppDelegate alloc] initWithConfig:config];
[NSApp setDelegate:app];
// insert code here...