libs-gui/Source/NSSpeechRecognizer.m
2020-02-08 02:45:40 -05:00

202 lines
5.2 KiB
Objective-C

/* Implementation of class NSSpeechRecognizer
Copyright (C) 2019 Free Software Foundation, Inc.
By: Gregory John Casamento
Date: Fri Dec 6 04:55:59 EST 2019
This file is part of the GNUstep Library.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110 USA.
*/
#import <AppKit/NSSpeechRecognizer.h>
#import <Foundation/NSDistantObject.h>
#import <Foundation/NSString.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSArray.h>
#import <Foundation/NSThread.h>
#import <Foundation/NSError.h>
#import <Foundation/NSConnection.h>
#import <Foundation/NSDistributedNotificationCenter.h>
#import "GSFastEnumeration.h"
#import "AppKit/NSWorkspace.h"
id _speechRecognitionServer = nil;
BOOL _serverLaunchTested = NO;
static int clients;
#define SPEECH_RECOGNITION_SERVER @"GSSpeechRecognitionServer"
@implementation NSSpeechRecognizer
/**
* If the remote end exits before freeing the GSSpeechRecognizer then we need
* to send it a -release message to make sure it dies.
*/
+ (void)connectionDied: (NSNotification*)aNotification
{
NSEnumerator *e = [[[aNotification object] localObjects] objectEnumerator];
NSObject *o = nil;
for (o = [e nextObject] ; nil != o ; o = [e nextObject])
{
if ([o isKindOfClass: self])
{
[o release];
}
}
}
+ (void) initialize
{
if (self == [NSSpeechRecognizer class])
{
if (nil == _speechRecognitionServer)
{
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
[ws launchApplication: SPEECH_RECOGNITION_SERVER
showIcon: NO
autolaunch: NO];
}
[[NSNotificationCenter defaultCenter]
addObserver: self
selector: @selector(connectionDied:)
name: NSConnectionDidDieNotification
object: nil];
}
}
- (void) processNotification: (NSNotification *)note
{
NSString *word = (NSString *)[note object];
word = [word lowercaseString];
FOR_IN(NSString*, obj, _commands)
{
if ([[obj lowercaseString] isEqualToString: word])
{
[_delegate speechRecognizer: self
didRecognizeCommand: word];
}
}
END_FOR_IN(_commands);
}
// Initialize
- (instancetype) init
{
self = [super init];
if (self != nil)
{
[[NSDistributedNotificationCenter defaultCenter]
addObserver: self
selector: @selector(processNotification:)
name: GSSpeechRecognizerDidRecognizeWordNotification
object: nil];
if (nil == _speechRecognitionServer && !_serverLaunchTested)
{
unsigned int i = 0;
// Wait for up to five seconds for the server to launch, then give up.
for (i=0 ; i < 50 ; i++)
{
_speechRecognitionServer = [NSConnection
rootProxyForConnectionWithRegisteredName: SPEECH_RECOGNITION_SERVER
host: nil];
RETAIN(_speechRecognitionServer);
if (nil != _speechRecognitionServer)
{
NSLog(@"Server found!!!");
clients++;
break;
}
[NSThread sleepForTimeInterval: 0.1];
}
// Set a flag so we don't bother waiting for the speech recognition server to
// launch the next time if it didn't work this time.
_serverLaunchTested = YES;
}
}
return self;
}
// Delegate
- (id<NSSpeechRecognizerDelegate>) delegate
{
return _delegate;
}
- (void) setDelegate: (id<NSSpeechRecognizerDelegate>)delegate
{
_delegate = delegate;
}
// Configuring...
- (NSArray *) commands
{
return _commands;
}
- (void) setCommands: (NSArray *)commands
{
ASSIGNCOPY(_commands, commands);
}
- (NSString *) displayCommandsTitle
{
return _displayCommandsTitle;
}
- (void) setDisplayCommandsTitle: (NSString *)displayCommandsTitle
{
ASSIGNCOPY(_displayCommandsTitle, displayCommandsTitle);
}
- (BOOL) listensInForegroundOnly
{
return _listensInForegroundOnly;
}
- (void) setListensInForegroundOnly: (BOOL)listensInForegroundOnly
{
_listensInForegroundOnly = listensInForegroundOnly;
}
- (BOOL) blocksOtherRecognizers
{
return _blocksOtherRecognizers;
}
- (void) setBlocksOtherRecognizers: (BOOL)blocksOtherRecognizers
{
_blocksOtherRecognizers = blocksOtherRecognizers;
}
// Listening
- (void) startListening
{
[_speechRecognitionServer startListening];
}
- (void) stopListening
{
[_speechRecognitionServer stopListening];
}
@end