From a46a80ba3c3c5faba0824301d407634cba820432 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sat, 10 Dec 2005 01:36:15 +0000 Subject: [PATCH] refactor to look more like the Linux driver --- .../MemoryCardDriverThreaded_Windows.cpp | 116 ++++++++++++++---- .../MemoryCardDriverThreaded_Windows.h | 2 + 2 files changed, 93 insertions(+), 25 deletions(-) diff --git a/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_Windows.cpp b/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_Windows.cpp index 7a5389f39f..6dafbffee6 100644 --- a/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_Windows.cpp +++ b/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_Windows.cpp @@ -5,6 +5,7 @@ #include "RageLog.h" #include "ProfileManager.h" #include "PrefsManager.h" +#include "Foreach.h" const CString TEMP_MOUNT_POINT_INTERNAL = "/@mctemp/"; const CString TEMP_MOUNT_POINT = "/@mctemptimeout/"; @@ -78,7 +79,6 @@ static bool IsFloppyDrive( char c ) void MemoryCardDriverThreaded_Windows::GetUSBStorageDevices( vector& vDevicesOut ) { const int MAX_DRIVES = 26; - CString sDrives; for( int i=0; iMount( &usbd ); - FILEMAN->Mount( "dir", usbd.sOsMountDir, TEMP_MOUNT_POINT_INTERNAL ); - FILEMAN->Mount( "timeout", TEMP_MOUNT_POINT_INTERNAL, TEMP_MOUNT_POINT ); - - usbd.bIsNameAvailable = PROFILEMAN->FastLoadProfileNameFromMemoryCard( TEMP_MOUNT_POINT, usbd.sName ); - - FILEMAN->Unmount( "timeout", TEMP_MOUNT_POINT_INTERNAL, TEMP_MOUNT_POINT ); - FILEMAN->Unmount( "dir", usbd.sOsMountDir, TEMP_MOUNT_POINT_INTERNAL ); +bool MemoryCardDriverThreaded_Windows::NeedUpdate( bool bMount ) +{ + if( bMount ) + { + /* Check if any devices need a write test. */ + for( unsigned i=0; iTrace( "Found drives: %s", sDrives.c_str() ); + /* Nothing needs a write test (or we can't do it right now). If no devices + * have changed, either, we have nothing to do. */ + DWORD dwNewLogicalDrives = ::GetLogicalDrives(); + if( dwNewLogicalDrives != m_dwLastLogicalDrives ) + { + m_dwLastLogicalDrives = dwNewLogicalDrives; + return true; + } + + /* Nothing to do. */ + return false; } bool MemoryCardDriverThreaded_Windows::DoOneUpdate( bool bMount, vector& vStorageDevicesOut ) { - DWORD dwNewLogicalDrives = ::GetLogicalDrives(); - if( dwNewLogicalDrives == m_dwLastLogicalDrives ) - { - // no change from last update + if( !NeedUpdate(bMount) ) return false; + + vector vOld = m_vDevicesLastSeen; // copy + GetUSBStorageDevices( vStorageDevicesOut ); + + // log connects + FOREACH( UsbStorageDevice, vStorageDevicesOut, newd ) + { + vector::iterator iter = find( vOld.begin(), vOld.end(), *newd ); + if( iter == vOld.end() ) // didn't find + LOG->Trace( "New device connected: %s", newd->sDevice.c_str() ); } - m_dwLastLogicalDrives = dwNewLogicalDrives; + /* When we first see a device, regardless of bMount, just return it as CHECKING, + * so the main thread knows about the device. On the next call where bMount is + * true, check it. */ + for( unsigned i=0; i::iterator iter = find( vOld.begin(), vOld.end(), d ); + if( iter == vOld.end() ) // didn't find + { + LOG->Trace( "New device entering CHECKING: %s", d.sDevice.c_str() ); + d.m_State = UsbStorageDevice::STATE_CHECKING; + continue; + } + + /* Preserve the state of the device, and any data loaded from previous checks. */ + d.m_State = iter->m_State; + d.bIsNameAvailable = iter->bIsNameAvailable; + d.sName = iter->sName; + + /* The device was here last time. If CHECKING, check the device now, if + * we're allowed to. */ + if( d.m_State == UsbStorageDevice::STATE_CHECKING ) + { + if( !bMount ) + { + /* We can't check it now. Keep STATE_CHECKING, and check it when we can. */ + d.m_State = UsbStorageDevice::STATE_CHECKING; + continue; + } + + if( !TestWrite(d.sOsMountDir) ) + { + d.SetError( "TestFailed" ); + } + else + { + /* We've successfully mounted and tested the device. Read the + * profile name (by mounting a temporary, private mountpoint), + * and then unmount it until Mount() is called. */ + d.m_State = UsbStorageDevice::STATE_READY; + + FILEMAN->Mount( "dir", d.sOsMountDir, TEMP_MOUNT_POINT ); + d.bIsNameAvailable = PROFILEMAN->FastLoadProfileNameFromMemoryCard( TEMP_MOUNT_POINT, d.sName ); + FILEMAN->Unmount( "dir", d.sOsMountDir, TEMP_MOUNT_POINT ); + } + + LOG->Trace( "WriteTest: %s, Name: %s", d.m_State == UsbStorageDevice::STATE_ERROR? "failed":"succeeded", d.sName.c_str() ); + } + } + + m_vDevicesLastSeen = vStorageDevicesOut; return true; } diff --git a/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_Windows.h b/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_Windows.h index 9124f53ae2..6e020e27a1 100644 --- a/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_Windows.h +++ b/stepmania/src/arch/MemoryCard/MemoryCardDriverThreaded_Windows.h @@ -18,8 +18,10 @@ public: private: void GetUSBStorageDevices( vector& vDevicesOut ); + bool NeedUpdate( bool bMount ); DWORD m_dwLastLogicalDrives; + vector m_vDevicesLastSeen; }; #ifdef ARCH_MEMORY_CARD_DRIVER