Implement both ignoring all recognizers, but the ones specified and ignoring when app is not active.

This commit is contained in:
Gregory John Casamento 2020-02-08 15:01:31 -05:00
parent efc901b79d
commit 4b1d3c679f
4 changed files with 107 additions and 2 deletions

View file

@ -36,13 +36,17 @@ extern "C" {
@protocol NSSpeechRecognizerDelegate;
@class NSArray, NSString, NSUUID;
@interface NSSpeechRecognizer : NSObject
{
id<NSSpeechRecognizerDelegate> _delegate;
NSArray *_commands;
NSString *_displayCommandsTitle;
NSUUID *_uuid;
BOOL _blocksOtherRecognizers;
BOOL _listensInForegroundOnly;
BOOL _appInForeground; // private
}
// Initialize

View file

@ -23,6 +23,7 @@
*/
#import <AppKit/NSSpeechRecognizer.h>
#import <AppKit/NSApplication.h>
#import <Foundation/NSDistantObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSDictionary.h>
@ -32,6 +33,7 @@
#import <Foundation/NSConnection.h>
#import <Foundation/NSDistributedNotificationCenter.h>
#import <Foundation/NSDebug.h>
#import <Foundation/NSUUID.h>
#import "GSFastEnumeration.h"
#import "AppKit/NSWorkspace.h"
@ -40,6 +42,12 @@ BOOL _serverLaunchTested = NO;
#define SPEECH_RECOGNITION_SERVER @"GSSpeechRecognitionServer"
@interface NSObject (SpeechRecognitionServerPrivate)
- (void) addToBlockingRecognizers: (NSString *)s;
- (void) removeFromBlockingRecognizers: (NSString *)s;
- (BOOL) isBlocking: (NSString *)s;
@end
@implementation NSSpeechRecognizer
+ (void) initialize
@ -60,6 +68,23 @@ BOOL _serverLaunchTested = NO;
{
NSString *word = (NSString *)[note object];
if (_listensInForegroundOnly)
{
if (_appInForeground == NO)
{
return;
}
}
if (_blocksOtherRecognizers)
{
if ([_speechRecognitionServer isBlocking: [_uuid UUIDString]] == NO)
{
// If we are not a blocking recognizer, then we are blocked...
return;
}
}
word = [word lowercaseString];
FOR_IN(NSString*, obj, _commands)
{
@ -72,6 +97,20 @@ BOOL _serverLaunchTested = NO;
END_FOR_IN(_commands);
}
- (void) processAppStatusNotification: (NSNotification *)note
{
NSString *name = [note name];
if ([name isEqualToString: NSApplicationDidBecomeActiveNotification])
{
_appInForeground = YES;
}
else
{
_appInForeground = NO;
}
}
// Initialize
- (instancetype) init
{
@ -84,7 +123,23 @@ BOOL _serverLaunchTested = NO;
name: GSSpeechRecognizerDidRecognizeWordNotification
object: nil];
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(processAppStatusNotification:)
name: NSApplicationDidBecomeActiveNotification
object: nil];
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(processAppStatusNotification:)
name: NSApplicationDidResignActiveNotification
object: nil];
_delegate = nil;
_blocksOtherRecognizers = NO;
_listensInForegroundOnly = YES;
_uuid = [NSUUID UUID];
if (nil == _speechRecognitionServer && !_serverLaunchTested)
{
unsigned int i = 0;
@ -112,6 +167,14 @@ BOOL _serverLaunchTested = NO;
return self;
}
- (void) dealloc
{
[[NSDistributedNotificationCenter defaultCenter] removeObserver: self];
[[NSNotificationCenter defaultCenter] removeObserver: self];
_delegate = nil;
[super dealloc];
}
// Delegate
- (id<NSSpeechRecognizerDelegate>) delegate
{
@ -161,6 +224,14 @@ BOOL _serverLaunchTested = NO;
- (void) setBlocksOtherRecognizers: (BOOL)blocksOtherRecognizers
{
if (blocksOtherRecognizers == YES)
{
[_speechRecognitionServer addToBlockingRecognizers: [_uuid UUIDString]];
}
else
{
[_speechRecognitionServer removeFromBlockingRecognizers: [_uuid UUIDString]];
}
_blocksOtherRecognizers = blocksOtherRecognizers;
}

View file

@ -35,6 +35,7 @@
@interface GSSpeechRecognitionServer : NSObject
{
GSSpeechRecognitionEngine *_engine;
NSMutableArray *_blocking;
}
/**
@ -42,7 +43,13 @@
*/
+ (id)sharedServer;
// Start and stop processing....
- (void) startListening;
- (void) stopListening;
// Add or remove from blocking list...
- (void) addToBlockingRecognizers: (NSString *)s;
- (void) removeFromBlockingRecognizers: (NSString *)s;
- (BOOL) isBlocking: (NSString *)s;
@end

View file

@ -100,7 +100,6 @@ static int _clients = 0;
}
_engine = [GSSpeechRecognitionEngine defaultSpeechRecognitionEngine];
if (nil == _engine)
{
[self release];
@ -110,10 +109,19 @@ static int _clients = 0;
{
NSLog(@"Got engine %@", _engine);
}
_blocking = [[NSMutableArray alloc] initWithCapacity: 10]; // 10 seems reasonable...
return self;
}
- (void) dealloc
{
RELEASE(_engine);
RELEASE(_blocking);
[super dealloc];
}
- (void) startListening
{
[_engine startListening];
@ -124,4 +132,19 @@ static int _clients = 0;
[_engine stopListening];
}
- (void) addToBlockingRecognizers: (NSString *)s
{
[_blocking addObject: s];
}
- (void) removeFromBlockingRecognizers: (NSString *)s
{
[_blocking removeObject: s];
}
- (BOOL) isBlocking: (NSString *)s
{
return [_blocking containsObject: s];
}
@end