aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2018-11-04 00:28:36 +0100
committerTeddy Wing2018-11-04 00:28:36 +0100
commit5ab4bcac5b4a62cca615185580094b6388f41fc4 (patch)
treefc8e89350ff93c0cc30bc0e51a863c1cf910dca0
parentd3ac5363b4f657284dd15ced308abd1c67b6ab9a (diff)
downloadDomeKey-5ab4bcac5b4a62cca615185580094b6388f41fc4.tar.bz2
Get mapping reloader to reload mappings for real
Previously I got the inter-process communication set up, but left the work of actually reloading the mappings into memory for later. That later is now. Make `Mappings` solely responsible for loading and reloading mappings. What used to be in `HeadphoneKey` for this has now been moved. HeadphoneKey: * Store a reference to a `Mappings` instance * Load mappings using the `Mappings` API instead of calling `dome_key_state_load_map_group()` directly. * Move the call to `observeReloadNotification` here from `main()`. Made more sense to me to have it with the other related mappings setup method calls. * Move the `_state` variable into `Mappings`, since that's where it's used most. Get it from an accessor in the one spot we need it. Mappings: * Make `observeReloadNotification` an instance method. While not necessary, since I decided to call it in `HeadphoneKey` after initialising `_mappings` and calling `reloadMappings`, I figured it made sense to call the method on the instance. * Make `reloadMappings` both an instance method and a class method. Made sense to call it on the instance since we instantiate `Mappings` just before calling it in `HeadphoneKey`. But we also need to be able to call it from `reload_mappings`, the C function, which has no knowledge of the `Mappings` instance. That's why we make it a class method also. * Add a `_state` static variable. Not ideal. Would rather have an instance variable, but this value needs to be accessible from the `reload_mappings` C function, which doesn't have access to instance variables.
-rw-r--r--DomeKey/HeadphoneKey.h3
-rw-r--r--DomeKey/HeadphoneKey.m14
-rw-r--r--DomeKey/Mappings.h9
-rw-r--r--DomeKey/Mappings.m32
-rw-r--r--DomeKey/main.m2
5 files changed, 44 insertions, 16 deletions
diff --git a/DomeKey/HeadphoneKey.h b/DomeKey/HeadphoneKey.h
index 1d12e10..4496785 100644
--- a/DomeKey/HeadphoneKey.h
+++ b/DomeKey/HeadphoneKey.h
@@ -9,6 +9,7 @@
#import <Foundation/Foundation.h>
#import <DDHidLib/DDHidAppleMikey.h>
+#import "Mappings.h"
#import "Sounds.h"
#import "dome_key_map.h"
#import "log.h"
@@ -23,7 +24,7 @@ static const Milliseconds TIMEOUT_DEFAULT = 500;
@interface HeadphoneKey : NSObject {
NSArray *_mikeys;
NSMutableArray *_key_buffer;
- State *_state;
+ Mappings *_mappings;
Milliseconds _timeout;
}
diff --git a/DomeKey/HeadphoneKey.m b/DomeKey/HeadphoneKey.m
index aec9598..a16dbc0 100644
--- a/DomeKey/HeadphoneKey.m
+++ b/DomeKey/HeadphoneKey.m
@@ -18,7 +18,10 @@ static BOOL _play_audio;
self = [super init];
if (self) {
_key_buffer = [[NSMutableArray alloc] initWithCapacity:5];
- _state = dome_key_state_new();
+
+ _mappings = [[Mappings alloc] init];
+ [_mappings reloadMappings];
+ [_mappings observeReloadNotification];
// 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.
@@ -26,8 +29,6 @@ static BOOL _play_audio;
_play_audio = NO;
- dome_key_state_load_map_group(_state);
-
_mikeys = [DDHidAppleMikey allMikeys];
[_mikeys makeObjectsPerformSelector:@selector(setDelegate:)
withObject:self];
@@ -52,11 +53,6 @@ static BOOL _play_audio;
return self;
}
-- (void)dealloc
-{
- dome_key_state_free(_state);
-}
-
- (void)ddhidAppleMikey:(DDHidAppleMikey *)mikey
press:(unsigned)usageId
upOrDown:(BOOL)upOrDown
@@ -110,7 +106,7 @@ static BOOL _play_audio;
.length = count
};
- dome_key_run_key_action(_state, trigger, on_mode_change);
+ dome_key_run_key_action([_mappings state], trigger, on_mode_change);
[_key_buffer removeAllObjects];
}
diff --git a/DomeKey/Mappings.h b/DomeKey/Mappings.h
index 136dc87..acd3546 100644
--- a/DomeKey/Mappings.h
+++ b/DomeKey/Mappings.h
@@ -9,9 +9,16 @@
#import <Foundation/Foundation.h>
#include <notify.h>
+#import "dome_key_map.h"
+
@interface Mappings : NSObject
-+ (void)observeReloadNotification;
+- (State *)state;
+
+- (void)observeReloadNotification;
+
+- (void)reloadMappings;
++ (void)reloadMappings;
+ (uint32_t)dispatchReload;
@end
diff --git a/DomeKey/Mappings.m b/DomeKey/Mappings.m
index 62098fe..f130ba6 100644
--- a/DomeKey/Mappings.m
+++ b/DomeKey/Mappings.m
@@ -12,9 +12,21 @@
static const CFStringRef CFNOTIFICATION_NAME_RELOAD = CFSTR(NOTIFICATION_NAME_RELOAD);
+static State *_state;
+
@implementation Mappings
-+ (void)observeReloadNotification
+- (void)dealloc
+{
+ dome_key_state_free(_state);
+}
+
+- (State *)state
+{
+ return _state;
+}
+
+- (void)observeReloadNotification
{
CFNotificationCenterRef center = CFNotificationCenterGetDarwinNotifyCenter();
@@ -39,15 +51,29 @@ void reload_mappings(
) {
if (CFStringCompare(name, CFNOTIFICATION_NAME_RELOAD, 0) ==
kCFCompareEqualTo) {
- // Reload mappings
- NSLog(@"TODO: reload mappings");
+ // TODO: Mappings reloaded message
+
+ [Mappings reloadMappings];
}
}
+- (void)reloadMappings
+{
+ [Mappings reloadMappings];
+}
+
++ (void)reloadMappings
+{
+ dome_key_state_free(_state);
+ _state = dome_key_state_new();
+ dome_key_state_load_map_group(_state);
+}
+
+ (uint32_t)dispatchReload
{
uint32_t status = notify_post(NOTIFICATION_NAME_RELOAD);
if (status != 0) {
+ // TODO: print to stderr
// Notification failed
NSLog(@"Reload notification failed");
}
diff --git a/DomeKey/main.m b/DomeKey/main.m
index 0b33a55..7c2f1c7 100644
--- a/DomeKey/main.m
+++ b/DomeKey/main.m
@@ -50,8 +50,6 @@ int main(int argc, const char * argv[]) {
// insert code here...
NSLog(@"Hello, World!");
- [Mappings observeReloadNotification];
-
[NSApp run];
}
} else if (config->args.version) {