diff --git a/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_OSX.cpp b/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_OSX.cpp index 5f7cead58c..33cee3c60c 100644 --- a/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_OSX.cpp +++ b/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_OSX.cpp @@ -8,6 +8,16 @@ #include #include +MemoryCardDriverThreaded_OSX::MemoryCardDriverThreaded_OSX() +{ + DarwinMCHelpers::Start(); +} + +MemoryCardDriverThreaded_OSX::~MemoryCardDriverThreaded_OSX() +{ + DarwinMCHelpers::Stop(); +} + void MemoryCardDriverThreaded_OSX::Flush( UsbStorageDevice *pDevice ) { if( pDevice->iRefNum == -1 ) @@ -23,6 +33,11 @@ void MemoryCardDriverThreaded_OSX::Flush( UsbStorageDevice *pDevice ) LOG->Warn( "Failed to flush the memory card." ); } +bool MemoryCardDriverThreaded_OSX::USBStorageDevicesChanged() +{ + return DarwinMCHelpers::DevicesChanged(); +} + void MemoryCardDriverThreaded_OSX::GetUSBStorageDevices( vector& vDevicesOut ) { LOG->Trace( "GetUSBStorageDevices." ); diff --git a/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_OSX.h b/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_OSX.h index 8b6991b812..16c88e3384 100644 --- a/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_OSX.h +++ b/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_OSX.h @@ -5,12 +5,15 @@ class MemoryCardDriverThreaded_OSX : public MemoryCardDriver { -public: +public: + MemoryCardDriverThreaded_OSX(); + ~MemoryCardDriverThreaded_OSX(); bool Mount( UsbStorageDevice *pDevice ) { return true; } void Unmount( UsbStorageDevice *pDevice ) { } void Flush( UsbStorageDevice *pDevice ); protected: + bool USBStorageDevicesChanged(); void GetUSBStorageDevices( vector& vStorageDevicesOut ); bool TestWrite( UsbStorageDevice *pDevice ); }; diff --git a/stepmania/src/archutils/Darwin/DarwinMCHelpers.h b/stepmania/src/archutils/Darwin/DarwinMCHelpers.h index fca7f103a4..9fdb0cb513 100644 --- a/stepmania/src/archutils/Darwin/DarwinMCHelpers.h +++ b/stepmania/src/archutils/Darwin/DarwinMCHelpers.h @@ -3,7 +3,10 @@ namespace DarwinMCHelpers { - void GetRemovableDevicePaths( vector vDevicePaths ); + void Start(); + void Stop(); + bool DevicesChanged(); + void GetRemovableDevicePaths( vector& vDevicePaths ); } #endif diff --git a/stepmania/src/archutils/Darwin/DarwinMCHelpers.mm b/stepmania/src/archutils/Darwin/DarwinMCHelpers.mm index 75e1aa1849..ed81331448 100644 --- a/stepmania/src/archutils/Darwin/DarwinMCHelpers.mm +++ b/stepmania/src/archutils/Darwin/DarwinMCHelpers.mm @@ -1,19 +1,72 @@ #include "global.h" #include "DarwinMCHelpers.h" +#include "RageThreads.h" #import -void DarwinMCHelpers::GetRemovableDevicePaths( vector vDevicePaths ) +static bool g_bChange; +static RageMutex g_Lock( "USB devices changed lock" ); + +@interface MemoryCardHelper : NSObject +- (void) devicesChanged:(NSNotification *)notification; +@end +@implementation MemoryCardHelper +- (void) devicesChanged:(NSNotification *)notification { + LockMut(g_Lock); + g_bChange = true; +} +@end + +static MemoryCardHelper *g_MCH = nil; + +void DarwinMCHelpers::Start() +{ + ASSERT( g_MCH == nil ); + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + NSNotificationCenter *nc = [[NSWorkspace sharedWorkspace] notificationCenter]; + + g_bChange = true; + g_MCH = [[MemoryCardHelper alloc] init]; + [nc addObserver:g_MCH selector:@selector(devicesChanged:) + name:@"NSWorkspaceDidMountNotification" object:nil]; + [nc addObserver:g_MCH selector:@selector(devicesChanged:) + name:@"NSWorkspaceDidUnmountNotification" object:nil]; + + [pool release]; +} + +void DarwinMCHelpers::Stop() +{ + NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; + NSNotificationCenter *nc = [[NSWorkspace sharedWorkspace] notificationCenter]; + + [nc removeObserver:g_MCH]; + [g_MCH release]; + + [pool release]; +} + +bool DarwinMCHelpers::DevicesChanged() +{ + LockMut( g_Lock ); + return g_bChange; +} + +void DarwinMCHelpers::GetRemovableDevicePaths( vector& vDevicePaths ) +{ + LockMut( g_Lock ); NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; NSWorkspace *ws = [NSWorkspace sharedWorkspace]; NSArray *paths = [ws mountedRemovableMedia]; NSEnumerator *i = [paths objectEnumerator]; NSString *path; + vDevicePaths.clear(); while( (path = [i nextObject]) ) vDevicePaths.push_back( [path UTF8String] ); [pool release]; + g_bChange = false; } /*