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" #include "archutils/Win32/ddk/hidsdi.h"
} }
static CString GetUSBDevicePath (int num) static CString GetUSBDevicePath( int iNum )
{ {
GUID guid; GUID guid;
HidD_GetHidGuid(&guid); HidD_GetHidGuid( &guid );
HDEVINFO DeviceInfo = SetupDiGetClassDevs (&guid, HDEVINFO DeviceInfo = SetupDiGetClassDevs( &guid, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE) );
NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE));
SP_DEVICE_INTERFACE_DATA DeviceInterface; SP_DEVICE_INTERFACE_DATA DeviceInterface;
DeviceInterface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA); DeviceInterface.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
if (!SetupDiEnumDeviceInterfaces (DeviceInfo, if( !SetupDiEnumDeviceInterfaces (DeviceInfo,
NULL, &guid, num, &DeviceInterface)) NULL, &guid, iNum, &DeviceInterface) )
{ {
SetupDiDestroyDeviceInfoList (DeviceInfo); SetupDiDestroyDeviceInfoList( DeviceInfo );
return CString(); return CString();
} }
unsigned long size; unsigned long iSize;
SetupDiGetDeviceInterfaceDetail (DeviceInfo, &DeviceInterface, NULL, 0, &size, 0); 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); DeviceDetail->cbSize = sizeof(SP_INTERFACE_DEVICE_DETAIL_DATA);
CString ret; CString sRet;
if (SetupDiGetDeviceInterfaceDetail (DeviceInfo, &DeviceInterface, if( SetupDiGetDeviceInterfaceDetail(DeviceInfo, &DeviceInterface,
DeviceDetail, size, &size, NULL)) DeviceDetail, iSize, &iSize, NULL) )
ret = DeviceDetail->DevicePath; sRet = DeviceDetail->DevicePath;
free(DeviceDetail); free( DeviceDetail );
SetupDiDestroyDeviceInfoList (DeviceInfo); SetupDiDestroyDeviceInfoList( DeviceInfo );
return ret; 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; CString path;
while ((path = GetUSBDevicePath (index++)) != "") while( (path = GetUSBDevicePath(iIndex++)) != "" )
{ {
HANDLE h = CreateFile (path, GENERIC_READ, HANDLE h = CreateFile( path, GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL );
if(h == INVALID_HANDLE_VALUE) if( h == INVALID_HANDLE_VALUE )
continue; continue;
HIDD_ATTRIBUTES attr; HIDD_ATTRIBUTES attr;
if (!HidD_GetAttributes (h, &attr)) if( !HidD_GetAttributes(h, &attr) )
{ {
CloseHandle(h); CloseHandle( h );
continue; continue;
} }
CloseHandle(h); CloseHandle( h );
if ((VID != -1 && attr.VendorID != VID) && if( (iVID != -1 && attr.VendorID != iVID) &&
(PID != -1 && attr.ProductID != PID)) (iPID != -1 && attr.ProductID != iPID) )
continue; /* This isn't it. */ continue; /* This isn't it. */
/* The VID and PID match. */ /* The VID and PID match. */
if(num-- > 0) if( iNum-- > 0 )
continue; continue;
io.Open(path, blocksize); m_IO.Open( path, iBlockSize );
return true; return true;
} }
@@ -87,52 +86,52 @@ bool USBDevice::Open(int VID, int PID, int blocksize, int num)
bool USBDevice::IsOpen() const bool USBDevice::IsOpen() const
{ {
return io.IsOpen(); return m_IO.IsOpen();
} }
int USBDevice::GetPadEvent() int USBDevice::GetPadEvent()
{ {
if(!IsOpen()) if( !IsOpen() )
return -1; return -1;
long buf; long iBuf;
if( io.read(&buf) <= 0 ) if( m_IO.read(&iBuf) <= 0 )
return -1; return -1;
return buf; return iBuf;
} }
WindowsFileIO::WindowsFileIO() WindowsFileIO::WindowsFileIO()
{ {
ZeroMemory( &ov, sizeof(ov) ); ZeroMemory( &m_Overlapped, sizeof(m_Overlapped) );
h = INVALID_HANDLE_VALUE; m_Handle = INVALID_HANDLE_VALUE;
buf = NULL; m_pBuffer = NULL;
} }
WindowsFileIO::~WindowsFileIO() WindowsFileIO::~WindowsFileIO()
{ {
if(h != INVALID_HANDLE_VALUE) if( m_Handle != INVALID_HANDLE_VALUE )
CloseHandle(h); CloseHandle( m_Handle );
delete[] buf; 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() ); LOG->Trace( "WindowsFileIO::open(%s)", path.c_str() );
blocksize = blocksize_; m_iBlockSize = iBlockSize;
if(buf) if( m_pBuffer )
delete[] buf; delete[] m_pBuffer;
buf = new char[blocksize]; m_pBuffer = new char[m_iBlockSize];
if(h != INVALID_HANDLE_VALUE) if( m_Handle != INVALID_HANDLE_VALUE )
CloseHandle (h); CloseHandle( m_Handle );
h = CreateFile (path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, m_Handle = CreateFile( path, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL );
if(h == INVALID_HANDLE_VALUE) if( m_Handle == INVALID_HANDLE_VALUE )
return false; return false;
queue_read(); queue_read();
@@ -143,41 +142,41 @@ bool WindowsFileIO::Open(CString path, int blocksize_)
void WindowsFileIO::queue_read() void WindowsFileIO::queue_read()
{ {
/* Request feedback from the device. */ /* Request feedback from the device. */
unsigned long r; unsigned long iRead;
ReadFile(h, buf, blocksize, &r, &ov); 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); LOG->Trace( "this %p, %p", this, p );
/* We do; get the result. It'll go into the original buf /* We do; get the result. It'll go into the original m_pBuffer
* we supplied on the original call; that's why buf is a * we supplied on the original call; that's why m_pBuffer is a
* member instead of a local. */ * member instead of a local. */
unsigned long cnt; unsigned long iCnt;
int ret = GetOverlappedResult(h, &ov, &cnt, FALSE); 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; return -1;
queue_read(); 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; return -1;
} }
memcpy( p, buf, cnt ); memcpy( p, m_pBuffer, cnt );
return cnt; return cnt;
} }
int WindowsFileIO::read(void *p) int WindowsFileIO::read( void *p )
{ {
LOG->Trace( "WindowsFileIO::read()" ); LOG->Trace( "WindowsFileIO::read()" );
/* See if we have a response for our request (which we may /* See if we have a response for our request (which we may
* have made on a previous call): */ * have made on a previous call): */
if(WaitForSingleObjectEx(h, 0, TRUE) == WAIT_TIMEOUT) if( WaitForSingleObjectEx(m_Handle, 0, TRUE) == WAIT_TIMEOUT )
return -1; return -1;
return finish_read(p); return finish_read(p);
@@ -187,7 +186,7 @@ int WindowsFileIO::read_several(const vector<WindowsFileIO *> &sources, void *p,
{ {
HANDLE *Handles = new HANDLE[sources.size()]; HANDLE *Handles = new HANDLE[sources.size()];
for( unsigned i = 0; i < sources.size(); ++i ) 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); int ret = WaitForMultipleObjectsEx( sources.size(), Handles, false, int(timeout * 1000), true);
delete[] Handles; delete[] Handles;
@@ -209,12 +208,30 @@ int WindowsFileIO::read_several(const vector<WindowsFileIO *> &sources, void *p,
bool WindowsFileIO::IsOpen() const bool WindowsFileIO::IsOpen() const
{ {
return h != INVALID_HANDLE_VALUE; return m_Handle != INVALID_HANDLE_VALUE;
} }
/* /*
----------------------------------------------------------------------------- * (c) 2002-2005 Glenn Maynard
Copyright (c) 2002-2003 by the person(s) listed below. All rights reserved. * All rights reserved.
Glenn Maynard *
----------------------------------------------------------------------------- * 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.
*/
+13 -14
View File
@@ -4,30 +4,29 @@
#define WIN32_USB_H #define WIN32_USB_H
#include <vector> #include <vector>
#include "windows.h" #include <windows.h>
/* The API for Windows device I/O is obscenely horrible, so encapsulate it. */
class WindowsFileIO class WindowsFileIO
{ {
public: public:
WindowsFileIO(); WindowsFileIO();
~WindowsFileIO(); ~WindowsFileIO();
bool Open(CString path, int blocksize); bool Open( CString sPath, int iBlockSize );
bool IsOpen() const; bool IsOpen() const;
/* Nonblocking read. size must always be the same. Returns the number of bytes /* Nonblocking read. size must always be the same. Returns the number of bytes
* read, or 0. */ * read, or 0. */
int read(void *p); int read( void *p );
static int read_several(const vector<WindowsFileIO *> &sources, void *p, int &actual, float timeout); static int read_several( const vector<WindowsFileIO *> &sources, void *p, int &actual, float timeout );
private: private:
HANDLE h;
OVERLAPPED ov;
char *buf;
int blocksize;
void queue_read(); void queue_read();
int finish_read(void *p); int finish_read( void *p );
HANDLE m_Handle;
OVERLAPPED m_Overlapped;
char *m_pBuffer;
int m_iBlockSize;
}; };
/* WindowsFileIO - Windows USB I/O */ /* WindowsFileIO - Windows USB I/O */
@@ -35,16 +34,16 @@ class USBDevice
{ {
public: public:
int GetPadEvent(); int GetPadEvent();
bool Open(int VID, int PID, int blocksize, int num); bool Open( int VID, int PID, int blocksize, int num );
bool IsOpen() const; bool IsOpen() const;
WindowsFileIO io; WindowsFileIO m_IO;
}; };
#endif #endif
/* /*
* (c) 2002-2003 Glenn Maynard * (c) 2002-2005 Glenn Maynard
* All rights reserved. * All rights reserved.
* *
* Permission is hereby granted, free of charge, to any person obtaining a * Permission is hereby granted, free of charge, to any person obtaining a