Joystick fixes for Mac

- Prevent spurious NSExceptions from harshing our mellow
    - Allow joystick hot-plugging by polling connectivity during link/timer callback
This commit is contained in:
Aaron Culliney 2014-11-29 13:33:57 -08:00
parent b6e329a3c6
commit 9c320c4de6
4 changed files with 52 additions and 8 deletions

View File

@ -1361,6 +1361,7 @@
"DEBUGGER=1",
"KEYPAD_JOYSTICK=1",
"AUDIO_ENABLED=1",
"VIDEO_OPENGL=1",
);
HEADER_SEARCH_PATHS = (
"$(inherited)",
@ -1388,6 +1389,7 @@
"DEBUGGER=1",
"KEYPAD_JOYSTICK=1",
"AUDIO_ENABLED=1",
"VIDEO_OPENGL=1",
);
HEADER_SEARCH_PATHS = (
"$(inherited)",

View File

@ -9,6 +9,7 @@
// Based on sample code from https://developer.apple.com/library/mac/samplecode/GLEssentials/Introduction/Intro.html
#import "EmulatorGLView.h"
#import "EmulatorJoystickController.h"
// Apple //e common routines
#import "common.h"
@ -279,6 +280,7 @@ static CVReturn displayLinkCallback(CVDisplayLinkRef displayLink, const CVTimeSt
video_driver_render();
CGLFlushDrawable([[self openGLContext] CGLContextObj]);
CGLUnlockContext([[self openGLContext] CGLContextObj]);
[[EmulatorJoystickController sharedInstance] connectivityPoll];
}
- (void)dealloc

View File

@ -11,5 +11,7 @@
@interface EmulatorJoystickController : NSObject
+ (EmulatorJoystickController *)sharedInstance;
- (void)connectivityPoll;
@end

View File

@ -11,7 +11,7 @@
#import "common.h"
@interface EmulatorJoystickController()
@property (nonatomic, retain) NSArray *allJoysticks;
@property (nonatomic, retain) NSDictionary *allJoysticks;
- (void)resetJoysticks;
@end
@ -52,22 +52,60 @@ void gldriver_joystick_reset(void) {
- (void)resetJoysticks
{
for (DDHidJoystick *joystick in self.allJoysticks)
for (DDHidJoystick *joystick in [self.allJoysticks allValues])
{
@try {
[joystick setDelegate:nil];
[joystick stopListening];
} @catch (NSException *e) {
// hot-plugging joysticks can cause glitches
NSLog(@"Joystick device library raised exception on close : %@", e);
}
self.allJoysticks = [DDHidJoystick allJoysticks];
}
NSArray *joysticks = [DDHidJoystick allJoysticks];
NSMutableDictionary *allJoysticks = [NSMutableDictionary dictionary];
for (DDHidJoystick *joystick in joysticks) {
NSString *key =[NSString stringWithFormat:@"%@-%@-%@", [joystick manufacturer], [joystick serialNumber], [joystick transport]];
[allJoysticks setObject:joystick forKey:key];
}
self.allJoysticks = allJoysticks;
dispatch_async(dispatch_get_main_queue(), ^ {
for (DDHidJoystick *joystick in self.allJoysticks)
for (DDHidJoystick *joystick in [self.allJoysticks allValues])
{
NSLog(@"found joystick : '%@' '%@' '%@' %@", [joystick manufacturer], [joystick serialNumber], [joystick transport], joystick);
@try {
[joystick setDelegate:self];
[joystick startListening];
} @catch (NSException *e) {
// hot-plugging joystick can cause glitches
NSLog(@"Joystick device library raised exception on start : %@", e);
}
}
});
}
#pragma mark -
#pragma mark Joystick connectivity polling
- (void)connectivityPoll
{
NSArray *joysticks = [DDHidJoystick allJoysticks];
BOOL changed = ([joysticks count] != [self.allJoysticks count]);
for (DDHidJoystick *joystick in joysticks)
{
NSString *key =[NSString stringWithFormat:@"%@-%@-%@", [joystick manufacturer], [joystick serialNumber], [joystick transport]];
if (![self.allJoysticks objectForKey:key])
{
changed = YES;
break;
}
}
if (changed)
{
[self resetJoysticks];
}
}
#pragma mark -
#pragma mark NSObject(DDHidJoystickDelegate)