clean up old code

This commit is contained in:
Glenn Maynard
2005-11-23 20:32:32 +00:00
parent 11333f8d32
commit d9f3f093f3
2 changed files with 102 additions and 86 deletions
+89 -72
View File
@@ -14,71 +14,70 @@ extern "C" {
#include "archutils/Win32/ddk/hidsdi.h"
}
static CString GetUSBDevicePath (int num)
static CString GetUSBDevicePath( int iNum )
{
GUID guid;
HidD_GetHidGuid(&guid);
HidD_GetHidGuid( &guid );
HDEVINFO DeviceInfo = SetupDiGetClassDevs (&guid,
NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE));
HDEVINFO DeviceInfo = SetupDiGetClassDevs( &guid, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE) );
SP_DEVICE_INTERFACE_DATA DeviceInterface;
DeviceInterface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
if (!SetupDiEnumDeviceInterfaces (DeviceInfo,
NULL, &guid, num, &DeviceInterface))
if( !SetupDiEnumDeviceInterfaces (DeviceInfo,
NULL, &guid, iNum, &DeviceInterface) )
{
SetupDiDestroyDeviceInfoList (DeviceInfo);
SetupDiDestroyDeviceInfoList( DeviceInfo );
return CString();
}
unsigned long size;
SetupDiGetDeviceInterfaceDetail (DeviceInfo, &DeviceInterface, NULL, 0, &size, 0);
unsigned long iSize;
SetupDiGetDeviceInterfaceDetail( DeviceInfo, &DeviceInterface, NULL, 0, &iSize, 0 );
PSP_INTERFACE_DEVICE_DETAIL_DATA DeviceDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA) malloc(size);
PSP_INTERFACE_DEVICE_DETAIL_DATA DeviceDetail = (PSP_INTERFACE_DEVICE_DETAIL_DATA) malloc( iSize );
DeviceDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
CString ret;
if (SetupDiGetDeviceInterfaceDetail (DeviceInfo, &DeviceInterface,
DeviceDetail, size, &size, NULL))
ret = DeviceDetail->DevicePath;
free(DeviceDetail);
CString sRet;
if( SetupDiGetDeviceInterfaceDetail(DeviceInfo, &DeviceInterface,
DeviceDetail, iSize, &iSize, NULL) )
sRet = DeviceDetail->DevicePath;
free( DeviceDetail );
SetupDiDestroyDeviceInfoList (DeviceInfo);
return ret;
SetupDiDestroyDeviceInfoList( DeviceInfo );
return sRet;
}
bool USBDevice::Open(int VID, int PID, int blocksize, int num)
bool USBDevice::Open( int iVID, int iPID, int iBlockSize, int iNum )
{
DWORD index = 0;
DWORD iIndex = 0;
CString path;
while ((path = GetUSBDevicePath (index++)) != "")
while( (path = GetUSBDevicePath(iIndex++)) != "" )
{
HANDLE h = CreateFile (path, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
HANDLE h = CreateFile( path, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );
if(h == INVALID_HANDLE_VALUE)
if( h == INVALID_HANDLE_VALUE )
continue;
HIDD_ATTRIBUTES attr;
if (!HidD_GetAttributes (h, &attr))
if( !HidD_GetAttributes(h, &attr) )
{
CloseHandle(h);
CloseHandle( h );
continue;
}
CloseHandle(h);
CloseHandle( h );
if ((VID != -1 && attr.VendorID != VID) &&
(PID != -1 && attr.ProductID != PID))
if( (iVID != -1 && attr.VendorID != iVID) &&
(iPID != -1 && attr.ProductID != iPID) )
continue; /* This isn't it. */
/* The VID and PID match. */
if(num-- > 0)
if( iNum-- > 0 )
continue;
io.Open(path, blocksize);
m_IO.Open( path, iBlockSize );
return true;
}
@@ -87,52 +86,52 @@ bool USBDevice::Open(int VID, int PID, int blocksize, int num)
bool USBDevice::IsOpen() const
{
return io.IsOpen();
return m_IO.IsOpen();
}
int USBDevice::GetPadEvent()
{
if(!IsOpen())
if( !IsOpen() )
return -1;
long buf;
if( io.read(&buf) <= 0 )
long iBuf;
if( m_IO.read(&iBuf) <= 0 )
return -1;
return buf;
return iBuf;
}
WindowsFileIO::WindowsFileIO()
{
ZeroMemory( &ov, sizeof(ov) );
h = INVALID_HANDLE_VALUE;
buf = NULL;
ZeroMemory( &m_Overlapped, sizeof(m_Overlapped) );
m_Handle = INVALID_HANDLE_VALUE;
m_pBuffer = NULL;
}
WindowsFileIO::~WindowsFileIO()
{
if(h != INVALID_HANDLE_VALUE)
CloseHandle(h);
delete[] buf;
if( m_Handle != INVALID_HANDLE_VALUE )
CloseHandle( m_Handle );
delete[] m_pBuffer;
}
bool WindowsFileIO::Open(CString path, int blocksize_)
bool WindowsFileIO::Open( CString path, int iBlockSize )
{
LOG->Trace( "WindowsFileIO::open(%s)", path.c_str() );
blocksize = blocksize_;
m_iBlockSize = iBlockSize;
if(buf)
delete[] buf;
buf = new char[blocksize];
if( m_pBuffer )
delete[] m_pBuffer;
m_pBuffer = new char[m_iBlockSize];
if(h != INVALID_HANDLE_VALUE)
CloseHandle (h);
if( m_Handle != INVALID_HANDLE_VALUE )
CloseHandle( m_Handle );
h = CreateFile (path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
m_Handle = CreateFile( path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL );
if(h == INVALID_HANDLE_VALUE)
if( m_Handle == INVALID_HANDLE_VALUE )
return false;
queue_read();
@@ -143,41 +142,41 @@ bool WindowsFileIO::Open(CString path, int blocksize_)
void WindowsFileIO::queue_read()
{
/* Request feedback from the device. */
unsigned long r;
ReadFile(h, buf, blocksize, &r, &ov);
unsigned long iRead;
ReadFile( m_Handle, m_pBuffer, m_iBlockSize, &iRead, &m_Overlapped );
}
int WindowsFileIO::finish_read(void *p)
int WindowsFileIO::finish_read( void *p )
{
LOG->Trace("this %p, %p", this, p);
/* We do; get the result. It'll go into the original buf
* we supplied on the original call; that's why buf is a
LOG->Trace( "this %p, %p", this, p );
/* We do; get the result. It'll go into the original m_pBuffer
* we supplied on the original call; that's why m_pBuffer is a
* member instead of a local. */
unsigned long cnt;
int ret = GetOverlappedResult(h, &ov, &cnt, FALSE);
unsigned long iCnt;
int iRet = GetOverlappedResult( m_Handle, &m_Overlapped, &iCnt, FALSE );
if(ret == 0 && (GetLastError() == ERROR_IO_PENDING || GetLastError() == ERROR_IO_INCOMPLETE))
if( iRet == 0 && (GetLastError() == ERROR_IO_PENDING || GetLastError() == ERROR_IO_INCOMPLETE) )
return -1;
queue_read();
if(ret == 0)
if( iRet == 0 )
{
LOG->Warn(werr_ssprintf(GetLastError(), "Error reading Pump pad"));
LOG->Warn( werr_ssprintf(GetLastError(), "Error reading USB device") );
return -1;
}
memcpy( p, buf, cnt );
memcpy( p, m_pBuffer, cnt );
return cnt;
}
int WindowsFileIO::read(void *p)
int WindowsFileIO::read( void *p )
{
LOG->Trace( "WindowsFileIO::read()" );
/* See if we have a response for our request (which we may
* have made on a previous call): */
if(WaitForSingleObjectEx(h, 0, TRUE) == WAIT_TIMEOUT)
if( WaitForSingleObjectEx(m_Handle, 0, TRUE) == WAIT_TIMEOUT )
return -1;
return finish_read(p);
@@ -187,7 +186,7 @@ int WindowsFileIO::read_several(const vector<WindowsFileIO *> &sources, void *p,
{
HANDLE *Handles = new HANDLE[sources.size()];
for( unsigned i = 0; i < sources.size(); ++i )
Handles[i] = sources[i]->h;
Handles[i] = sources[i]->m_Handle;
int ret = WaitForMultipleObjectsEx( sources.size(), Handles, false, int(timeout * 1000), true);
delete[] Handles;
@@ -209,12 +208,30 @@ int WindowsFileIO::read_several(const vector<WindowsFileIO *> &sources, void *p,
bool WindowsFileIO::IsOpen() const
{
return h != INVALID_HANDLE_VALUE;
return m_Handle != INVALID_HANDLE_VALUE;
}
/*
-----------------------------------------------------------------------------
Copyright (c) 2002-2003 by the person(s) listed below. All rights reserved.
Glenn Maynard
-----------------------------------------------------------------------------
*/
* (c) 2002-2005 Glenn Maynard
* All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, and/or sell copies of the Software, and to permit persons to
* whom the Software is furnished to do so, provided that the above
* copyright notice(s) and this permission notice appear in all copies of
* the Software and that both the above copyright notice(s) and this
* permission notice appear in supporting documentation.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
* THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS
* INCLUDED IN THIS NOTICE BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT
* OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/