diff --git a/Apple2Mac/Apple2Mac.xcodeproj/project.pbxproj b/Apple2Mac/Apple2Mac.xcodeproj/project.pbxproj index 7560a619..b9b106d7 100644 --- a/Apple2Mac/Apple2Mac.xcodeproj/project.pbxproj +++ b/Apple2Mac/Apple2Mac.xcodeproj/project.pbxproj @@ -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)", diff --git a/Apple2Mac/Apple2Mac/Classes/OSX/EmulatorGLView.m b/Apple2Mac/Apple2Mac/Classes/OSX/EmulatorGLView.m index 89f67bb3..0d024044 100644 --- a/Apple2Mac/Apple2Mac/Classes/OSX/EmulatorGLView.m +++ b/Apple2Mac/Apple2Mac/Classes/OSX/EmulatorGLView.m @@ -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 diff --git a/Apple2Mac/Apple2Mac/Classes/OSX/EmulatorJoystickController.h b/Apple2Mac/Apple2Mac/Classes/OSX/EmulatorJoystickController.h index 307bf9f7..5b441ea2 100644 --- a/Apple2Mac/Apple2Mac/Classes/OSX/EmulatorJoystickController.h +++ b/Apple2Mac/Apple2Mac/Classes/OSX/EmulatorJoystickController.h @@ -11,5 +11,7 @@ @interface EmulatorJoystickController : NSObject + (EmulatorJoystickController *)sharedInstance; +- (void)connectivityPoll; + @end diff --git a/Apple2Mac/Apple2Mac/Classes/OSX/EmulatorJoystickController.m b/Apple2Mac/Apple2Mac/Classes/OSX/EmulatorJoystickController.m index 5430efda..3e38cdb7 100644 --- a/Apple2Mac/Apple2Mac/Classes/OSX/EmulatorJoystickController.m +++ b/Apple2Mac/Apple2Mac/Classes/OSX/EmulatorJoystickController.m @@ -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]) { - [joystick setDelegate:nil]; - [joystick stopListening]; + @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); - [joystick setDelegate:self]; - [joystick startListening]; + @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)