libs-gui/Source/NSSpeechRecognizer.m

164 lines
4.2 KiB
Mathematica
Raw Normal View History

2019-12-06 10:01:51 +00:00
/* 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.
2019-12-06 10:01:51 +00:00
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.
2019-12-06 10:01:51 +00:00
*/
#import <AppKit/NSSpeechRecognizer.h>
2020-02-01 09:30:36 +00:00
#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 "AppKit/NSWorkspace.h"
id _speechRecognitionServer = nil;
Class _speechRecognitionClass = nil;
BOOL _serverLaunchTested = NO;
2020-02-01 09:33:25 +00:00
#define SPEECH_RECOGNITION_SERVER @"GSSpeechRecognitionServer"
2020-02-01 09:30:36 +00:00
@interface NSObject (GSSpeechRecognitionServer)
- (NSSpeechRecognizer *)newRecognizer;
@end
2019-12-06 10:01:51 +00:00
@implementation NSSpeechRecognizer
2020-02-01 09:16:48 +00:00
+ (void) initialize
{
2020-02-01 09:30:36 +00:00
_speechRecognitionClass = [NSSpeechRecognizer class];
2020-02-01 09:33:25 +00:00
_speechRecognitionServer = [[NSConnection rootProxyForConnectionWithRegisteredName: SPEECH_RECOGNITION_SERVER
2020-02-01 09:30:36 +00:00
host: nil] retain];
if (nil == _speechRecognitionServer)
{
NSWorkspace *ws = [NSWorkspace sharedWorkspace];
2020-02-01 09:33:25 +00:00
[ws launchApplication: SPEECH_RECOGNITION_SERVER
2020-02-01 09:30:36 +00:00
showIcon: NO
autolaunch: NO];
}
2020-02-01 09:16:48 +00:00
}
// Initialize
- (instancetype) init
{
self = [super init];
if (self != nil)
{
}
return self;
}
2020-02-01 09:30:36 +00:00
+ (id) allocWithZone: (NSZone *)aZone
{
if (self == _speechRecognitionClass)
{
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++)
{
2020-02-01 09:33:25 +00:00
_speechRecognitionServer = [[NSConnection rootProxyForConnectionWithRegisteredName: SPEECH_RECOGNITION_SERVER
2020-02-01 09:30:36 +00:00
host: nil] retain];
if (nil != _speechRecognitionServer)
{
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;
}
// If there is no server, this will return nil
return [_speechRecognitionServer newRecognizer];
}
return [super allocWithZone: aZone];
}
// Delegate
2020-02-01 09:16:48 +00:00
- (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
{
// Do nothing...
}
- (void) stopListening
{
// Do nothing...
}
2019-12-06 10:01:51 +00:00
@end