From 73b27de4dcb823dbb978f154522fc0a383903dce Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Mon, 29 Oct 2018 23:23:35 +0100 Subject: HeadphoneKey: Play audio only with the `--audio` command line flag Allow users to decide whether interface audio (mode activated & deactivated) should play. The flag value is in a `Config`, so to make it easier to pass both the audio flag and the timeout config value to `HeadphoneKey`, rename `initWithTimeout:` to `initWithConfig:`. Only allocate a `Sounds` instance if `--audio` is passed, because there's no reason to allocate memory for it otherwise. Add a `_play_audio` flag as a static variable to make it accessible to the `on_mode_change()` function, just like we did for `sounds_inst`. In `on_mode_change()`, don't play any audio if `_play_audio` is on. --- DomeKey/AppDelegate.m | 2 +- DomeKey/HeadphoneKey.h | 2 +- DomeKey/HeadphoneKey.m | 16 +++++++++++++--- 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/DomeKey/AppDelegate.m b/DomeKey/AppDelegate.m index bcd4dd0..30bd898 100644 --- a/DomeKey/AppDelegate.m +++ b/DomeKey/AppDelegate.m @@ -21,7 +21,7 @@ - (void)applicationDidFinishLaunching:(NSNotification *)notification { - _headphone_key = [[HeadphoneKey alloc] initWithTimeout:_config->timeout]; + _headphone_key = [[HeadphoneKey alloc] initWithConfig:_config]; } @end diff --git a/DomeKey/HeadphoneKey.h b/DomeKey/HeadphoneKey.h index fc500cd..1d12e10 100644 --- a/DomeKey/HeadphoneKey.h +++ b/DomeKey/HeadphoneKey.h @@ -27,7 +27,7 @@ static const Milliseconds TIMEOUT_DEFAULT = 500; Milliseconds _timeout; } -- (instancetype)initWithTimeout:(Milliseconds)timeout; +- (instancetype)initWithConfig:(Config *)config; - (void)handleDeadKey:(HeadphoneButton)button; - (void)runAction; diff --git a/DomeKey/HeadphoneKey.m b/DomeKey/HeadphoneKey.m index a176cbf..11046e7 100644 --- a/DomeKey/HeadphoneKey.m +++ b/DomeKey/HeadphoneKey.m @@ -9,6 +9,7 @@ #import "HeadphoneKey.h" static const Sounds *sounds_inst; +static BOOL _play_audio; @implementation HeadphoneKey @@ -23,7 +24,7 @@ static const Sounds *sounds_inst; // default should always come from a `Config`, set in the Rust library. _timeout = TIMEOUT_DEFAULT; - sounds_inst = [[Sounds alloc] init]; + _play_audio = NO; // TODO: Think about moving this logger higher up dome_key_logger_init(); @@ -39,11 +40,16 @@ static const Sounds *sounds_inst; return self; } -- (instancetype)initWithTimeout:(Milliseconds)timeout +- (instancetype)initWithConfig:(Config *)config { self = [self init]; if (self) { - _timeout = timeout; + _timeout = config->timeout; + _play_audio = config->args.audio; + + if (_play_audio) { + sounds_inst = [[Sounds alloc] init]; + } } return self; } @@ -112,6 +118,10 @@ static const Sounds *sounds_inst; } void on_mode_change(ModeChange mode_change) { + if (!_play_audio) { + return; + } + switch (mode_change) { case ModeChange_Activated: [sounds_inst playModeActivated]; -- cgit v1.2.3