diff options
author | Teddy Wing | 2019-03-23 14:13:09 +0100 |
---|---|---|
committer | Teddy Wing | 2019-03-23 14:13:09 +0100 |
commit | 8aba8891e027dc1aa5d352fb60240e704e5628d8 (patch) | |
tree | bbc3e4873f0a14b59e23dc6f09f257d29afb0424 | |
parent | f0ca208cd4d347a5273480a21ee05230b855c3e5 (diff) | |
download | DomeKey-8aba8891e027dc1aa5d352fb60240e704e5628d8.tar.bz2 |
Extract headphone key listener to `HeadphoneKeyEventWired`
Move the DDHidLib headphone button event listener from `HeadphoneKey` to
`HeadphoneKeyEventWired`. This creates a common interface for both wired
and wireless Bluetooth headphones.
The DDHidLib listener now lives in its own class dedicated to wired
headphones. A new `HeadphoneKeyEventBluetooth` will set up the event
listener for Bluetooth headphones.
Both `HeadphoneKeyEvent_` listeners call a delegate method set up on
`HeadphoneKey` that relays the pressed headphone button.
-rw-r--r-- | DomeKey/HeadphoneKey.h | 12 | ||||
-rw-r--r-- | DomeKey/HeadphoneKey.m | 31 | ||||
-rw-r--r-- | DomeKey/HeadphoneKeyEventDelegate.h | 7 | ||||
-rw-r--r-- | DomeKey/HeadphoneKeyEventWired.h | 16 | ||||
-rw-r--r-- | DomeKey/HeadphoneKeyEventWired.m | 38 |
5 files changed, 69 insertions, 35 deletions
diff --git a/DomeKey/HeadphoneKey.h b/DomeKey/HeadphoneKey.h index 472f100..6670ff5 100644 --- a/DomeKey/HeadphoneKey.h +++ b/DomeKey/HeadphoneKey.h @@ -48,22 +48,18 @@ // #import <Foundation/Foundation.h> -#import "DDHidAppleMikey.h" +#import "HeadphoneKeyEventDelegate.h" +#import "HeadphoneKeyEventWired.h" #import "Mappings.h" #import "Sounds.h" #import "dome_key_map.h" #import "log.h" -typedef enum KeyPress : BOOL { - KeyPressDown = YES, - KeyPressUp = NO -} KeyPress; - static const Milliseconds TIMEOUT_DEFAULT = 500; -@interface HeadphoneKey : NSObject { - NSArray *_mikeys; +@interface HeadphoneKey : NSObject <HeadphoneKeyEventDelegate> { + HeadphoneKeyEventWired *_wired_headphone_event; NSMutableArray *_key_buffer; Mappings *_mappings; Milliseconds _timeout; diff --git a/DomeKey/HeadphoneKey.m b/DomeKey/HeadphoneKey.m index e5665d1..3aa79ce 100644 --- a/DomeKey/HeadphoneKey.m +++ b/DomeKey/HeadphoneKey.m @@ -58,6 +58,9 @@ static BOOL _play_audio; { self = [super init]; if (self) { + _wired_headphone_event = [[HeadphoneKeyEventWired alloc] + initWithDelegate:self]; + _key_buffer = [[NSMutableArray alloc] initWithCapacity:5]; _mappings = [[Mappings alloc] init]; @@ -69,13 +72,6 @@ static BOOL _play_audio; _timeout = TIMEOUT_DEFAULT; _play_audio = NO; - - _mikeys = [DDHidAppleMikey allMikeys]; - [_mikeys makeObjectsPerformSelector:@selector(setDelegate:) - withObject:self]; - [_mikeys makeObjectsPerformSelector:@selector(setListenInExclusiveMode:) - withObject:(id)kCFBooleanTrue]; - [_mikeys makeObjectsPerformSelector:@selector(startListening)]; } return self; } @@ -94,26 +90,9 @@ static BOOL _play_audio; return self; } -- (void)ddhidAppleMikey:(DDHidAppleMikey *)mikey - press:(unsigned)usageId - upOrDown:(BOOL)upOrDown +- (void)headphoneButtonWasPressed:(HeadphoneButton)button { - if (upOrDown == KeyPressUp) { - switch (usageId) { - case kHIDUsage_Csmr_PlayOrPause: - LogDebug(@"Middle"); - [self handleDeadKey:HeadphoneButton_Play]; - break; - case kHIDUsage_Csmr_VolumeIncrement: - LogDebug(@"Top"); - [self handleDeadKey:HeadphoneButton_Up]; - break; - case kHIDUsage_Csmr_VolumeDecrement: - LogDebug(@"Bottom"); - [self handleDeadKey:HeadphoneButton_Down]; - break; - } - } + [self handleDeadKey:button]; } - (void)handleDeadKey:(HeadphoneButton)button diff --git a/DomeKey/HeadphoneKeyEventDelegate.h b/DomeKey/HeadphoneKeyEventDelegate.h index 8fd8eba..444919a 100644 --- a/DomeKey/HeadphoneKeyEventDelegate.h +++ b/DomeKey/HeadphoneKeyEventDelegate.h @@ -9,5 +9,12 @@ #ifndef HeadphoneKeyEventDelegate_h #define HeadphoneKeyEventDelegate_h +#include "dome_key_map.h" + +@protocol HeadphoneKeyEventDelegate + +- (void)headphoneButtonWasPressed:(HeadphoneButton)button; + +@end #endif /* HeadphoneKeyEventDelegate_h */ diff --git a/DomeKey/HeadphoneKeyEventWired.h b/DomeKey/HeadphoneKeyEventWired.h index f3dd630..b24bb56 100644 --- a/DomeKey/HeadphoneKeyEventWired.h +++ b/DomeKey/HeadphoneKeyEventWired.h @@ -7,7 +7,21 @@ // #import <Foundation/Foundation.h> +#import "DDHidAppleMikey.h" -@interface HeadphoneKeyEventWired : NSObject +#import "HeadphoneKeyEventDelegate.h" +#import "log.h" + +typedef enum KeyPress : BOOL { + KeyPressDown = YES, + KeyPressUp = NO +} KeyPress; + +@interface HeadphoneKeyEventWired : NSObject { + NSArray *_mikeys; + id <HeadphoneKeyEventDelegate> _delegate; +} + +- (instancetype)initWithDelegate:(id <HeadphoneKeyEventDelegate>)delegate; @end diff --git a/DomeKey/HeadphoneKeyEventWired.m b/DomeKey/HeadphoneKeyEventWired.m index 7245146..8364d2a 100644 --- a/DomeKey/HeadphoneKeyEventWired.m +++ b/DomeKey/HeadphoneKeyEventWired.m @@ -10,4 +10,42 @@ @implementation HeadphoneKeyEventWired +- (instancetype)initWithDelegate:(id <HeadphoneKeyEventDelegate>)delegate +{ + self = [self init]; + if (self) { + _delegate = delegate; + + _mikeys = [DDHidAppleMikey allMikeys]; + [_mikeys makeObjectsPerformSelector:@selector(setDelegate:) + withObject:self]; + [_mikeys makeObjectsPerformSelector:@selector(setListenInExclusiveMode:) + withObject:(id)kCFBooleanTrue]; + [_mikeys makeObjectsPerformSelector:@selector(startListening)]; + } + return self; +} + +- (void)ddhidAppleMikey:(DDHidAppleMikey *)mikey + press:(unsigned)usageId + upOrDown:(BOOL)upOrDown +{ + if (upOrDown == KeyPressUp) { + switch (usageId) { + case kHIDUsage_Csmr_PlayOrPause: + LogDebug(@"Middle"); + [_delegate headphoneButtonWasPressed:HeadphoneButton_Play]; + break; + case kHIDUsage_Csmr_VolumeIncrement: + LogDebug(@"Top"); + [_delegate headphoneButtonWasPressed:HeadphoneButton_Up]; + break; + case kHIDUsage_Csmr_VolumeDecrement: + LogDebug(@"Bottom"); + [_delegate headphoneButtonWasPressed:HeadphoneButton_Down]; + break; + } + } +} + @end |