Skip to content

Commit

Permalink
Merge pull request OpenEmu#565 from dethbunny/DeviceFix
Browse files Browse the repository at this point in the history
Notify UI for device disconnect; resolves OpenEmu#511
  • Loading branch information
PsychoH13 committed Mar 30, 2013
2 parents e50ac56 + 3ea5e86 commit c5d80a3
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 14 deletions.
4 changes: 2 additions & 2 deletions OpenEmu/OEDeviceHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,6 @@

- (BOOL)isKeyboardDevice;

extern NSString *const OEInputDeviceLowBatteryNotification;

@end

extern NSString *const OEDeviceHandlerDidReceiveLowBatteryWarningNotification;
2 changes: 1 addition & 1 deletion OpenEmu/OEDeviceHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#define NO __objc_no
#endif

NSString *const OEInputDeviceLowBatteryNotification = @"OEInputDeviceLowBatteryNotification";
NSString *const OEDeviceHandlerDidReceiveLowBatteryWarningNotification = @"OEDeviceHandlerDidReceiveLowBatteryWarningNotification";

@interface OEHIDEvent ()
- (BOOL)OE_setupEventWithDeviceHandler:(OEHIDDeviceHandler *)aDeviceHandler value:(IOHIDValueRef)aValue;
Expand Down
1 change: 0 additions & 1 deletion OpenEmu/OEGameViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
*/

#import <Cocoa/Cocoa.h>
#import "OEDeviceHandler.h"

extern NSString *const OEGameVolumeKey;
extern NSString *const OEGameDefaultVideoFilterKey;
Expand Down
45 changes: 36 additions & 9 deletions OpenEmu/OEGameViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@
#import "OEGameDocument.h"
#import "OEAudioDeviceManager.h"

#import "OEDeviceHandler.h"
#import "OEDeviceDescription.h"
#import "OEDeviceManager.h"

#import "OEHUDAlert+DefaultAlertsAdditions.h"

#import "NSString+UUID.h"
Expand Down Expand Up @@ -244,7 +248,8 @@ - (void)dealloc
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self name:NSViewFrameDidChangeNotification object:gameView];
[nc removeObserver:self name:OEInputDeviceLowBatteryNotification object:nil];
[nc removeObserver:self name:OEDeviceHandlerDidReceiveLowBatteryWarningNotification object:nil];
[nc removeObserver:self name:OEDeviceManagerDidRemoveDeviceHandlerNotification object:nil];

[controlsWindow close];
controlsWindow = nil;
Expand Down Expand Up @@ -403,7 +408,8 @@ - (void)OE_terminateEmulationWithoutNotification
{
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc removeObserver:self name:NSViewFrameDidChangeNotification object:gameView];
[nc removeObserver:self name:OEInputDeviceLowBatteryNotification object:nil];
[nc removeObserver:self name:OEDeviceHandlerDidReceiveLowBatteryWarningNotification object:nil];
[nc removeObserver:self name:OEDeviceManagerDidRemoveDeviceHandlerNotification object:nil];

_emulationStatus = OEGameViewControllerEmulationStatusNotStarted;

Expand Down Expand Up @@ -471,7 +477,8 @@ - (void)OE_startEmulation

NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self selector:@selector(viewDidChangeFrame:) name:NSViewFrameDidChangeNotification object:gameView];
[nc addObserver:self selector:@selector(OE_lowDeviceBattery:) name:OEInputDeviceLowBatteryNotification object:nil];
[nc addObserver:self selector:@selector(OE_lowDeviceBattery:) name:OEDeviceHandlerDidReceiveLowBatteryWarningNotification object:nil];
[nc addObserver:self selector:@selector(OE_deviceDidDisconnect:) name:OEDeviceManagerDidRemoveDeviceHandlerNotification object:nil];

NSWindow *window = [self OE_rootWindow];
[window makeFirstResponder:gameView];
Expand Down Expand Up @@ -1100,12 +1107,32 @@ - (void)windowDidChangeScreen:(NSNotification *)notification

- (void)OE_lowDeviceBattery:(NSNotification *)notification
{
OEDeviceHandler *deviceHandler = [notification object];
NSUInteger deviceNumber = [deviceHandler deviceNumber];
NSString *lowBatteryString = [NSString stringWithFormat:NSLocalizedString(@"The battery in device number %lu is low. Please charge or replace the battery.", @"Low battery alert detail message; %lu is placeholder for controller number."), deviceNumber];
OEHUDAlert *alert = [OEHUDAlert alertWithMessageText:lowBatteryString defaultButton:NSLocalizedString(@"Resume", @"Continue emulation.") alternateButton:nil];
[alert setHeadlineText:[NSString stringWithFormat:NSLocalizedString(@"Low Controller Battery", @"Device battery level is low.")]];
[alert runModal];
if(_emulationStatus == OEGameViewControllerEmulationStatusPlaying) {
[self setPauseEmulation:YES];
OEDeviceHandler *devHandler = [notification object];
NSString *lowBatteryString = [NSString stringWithFormat:NSLocalizedString(@"The battery in device number %lu, %@, is low. Please charge or replace the battery.", @"Low battery alert detail message."), [devHandler deviceNumber], [[devHandler deviceDescription] name]];
OEHUDAlert *alert = [OEHUDAlert alertWithMessageText:lowBatteryString
defaultButton:NSLocalizedString(@"Resume", nil)
alternateButton:nil];
[alert setHeadlineText:[NSString stringWithFormat:NSLocalizedString(@"Low Controller Battery", @"Device battery level is low.")]];
[alert runModal];
[self setPauseEmulation:NO];
}
}

- (void)OE_deviceDidDisconnect:(NSNotification *)notification
{
if(_emulationStatus == OEGameViewControllerEmulationStatusPlaying) {
[self setPauseEmulation:YES];
OEDeviceHandler *devHandler = [[notification userInfo] objectForKey:OEDeviceManagerDeviceHandlerUserInfoKey];
NSString *lowBatteryString = [NSString stringWithFormat:NSLocalizedString(@"Device number %lu, %@, has disconnected.", @"Device disconnection detail message."), [devHandler deviceNumber], [[devHandler deviceDescription] name]];
OEHUDAlert *alert = [OEHUDAlert alertWithMessageText:lowBatteryString
defaultButton:NSLocalizedString(@"Resume", nil)
alternateButton:nil];
[alert setHeadlineText:[NSString stringWithFormat:NSLocalizedString(@"Device Disconnected", @"A controller device has disconnected.")]];
[alert runModal];
[self setPauseEmulation:NO];
}
}

#pragma mark - Plugin discovery
Expand Down
2 changes: 1 addition & 1 deletion OpenEmu/OEWiimoteHIDDeviceHandler.m
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ - (void)OE_checkBatteryLevel
if(!_lowBatteryWarning)
{
_lowBatteryWarning = YES;
[[NSNotificationCenter defaultCenter] postNotificationName:OEInputDeviceLowBatteryNotification object:self];
[[NSNotificationCenter defaultCenter] postNotificationName:OEDeviceHandlerDidReceiveLowBatteryWarningNotification object:self];
}
}
else if(_charging) _lowBatteryWarning = NO;
Expand Down

0 comments on commit c5d80a3

Please sign in to comment.