diff --git a/stepmania/src/arch/InputHandler/InputHandler_Win32_Para.cpp b/stepmania/src/arch/InputHandler/InputHandler_Win32_Para.cpp index 7bbfafda81..a1b13ac1d5 100644 --- a/stepmania/src/arch/InputHandler/InputHandler_Win32_Para.cpp +++ b/stepmania/src/arch/InputHandler/InputHandler_Win32_Para.cpp @@ -7,114 +7,40 @@ #include "RageInputDevice.h" #include "archutils/Win32/USB.h" +// TODO: Abstract this windows-specific stuff into USBDevice. +extern "C" { +#include "archutils/Win32/ddk/setupapi.h" +/* Quiet header warning: */ +#include "archutils/Win32/ddk/hidsdi.h" +} + +static void InitHack( HANDLE h ) +{ + UCHAR hack[] = {0, 1}; + + if( HidD_SetFeature(h, (PVOID) hack, 2) == TRUE ) + LOG->Info( "Para controller powered on successfully" ); + else + LOG->Warn( "Para controller power-on failed" ); +} + InputHandler_Win32_Para::InputHandler_Win32_Para() { - shutdown = false; - const int para_usb_vid = 0x0507, para_usb_pid = 0x0409; + const int para_usb_vid = 0x0507; + const int para_usb_pid = 0x0011; - dev = new USBDevice; + USBDevice *dev = new USBDevice; - bool FoundOnePad = false; - if( dev->Open(para_usb_vid, para_usb_pid, sizeof(long), 0) ) + if( dev->Open(para_usb_vid, para_usb_pid, sizeof(long), 0, InitHack) ) { - FoundOnePad = true; - LOG->Info("Found Para controller"); - } - - /* Don't start a thread if we have no pads. */ - if( FoundOnePad && PREFSMAN->m_bThreadedInput ) - { - InputThread.SetName("Para thread"); - InputThread.Create( InputThread_Start, this ); - } -} - -InputHandler_Win32_Para::~InputHandler_Win32_Para() -{ - if( InputThread.IsCreated() ) - { - shutdown = true; - LOG->Trace("Shutting down Para thread ..."); - InputThread.Wait(); - LOG->Trace("Para thread shut down."); - } - - delete dev; -} - -void InputHandler_Win32_Para::HandleInput( int devno, int event ) -{ - static const int bits[NUM_PARA_PAD_BUTTONS] = { - /* sensors */ (1<<9), (1<<12), (1<<13), (1<<11), (1<<10), - /* buttons */ (1<<16),(1<<17), (1<<20), (1<<21), - }; - - InputDevice id = DEVICE_PARA1; - - for (int butno = 0 ; butno < NUM_PARA_PAD_BUTTONS ; butno++) - { - DeviceInput di(id, butno); - - /* If we're in a thread, our timestamp is accurate. */ - if( InputThread.IsCreated() ) - di.ts.Touch(); - - ButtonPressed(di, !(event & bits[butno])); + LOG->Info("Para controller initialized"); } + SAFE_DELETE( dev ); } void InputHandler_Win32_Para::GetDevicesAndDescriptions(vector& vDevicesOut, vector& vDescriptionsOut) { - if( dev->IsOpen() ) - { - vDevicesOut.push_back( DEVICE_PARA1 ); - vDescriptionsOut.push_back( "Para USB" ); - } -} - -int InputHandler_Win32_Para::InputThread_Start( void *p ) -{ - ((InputHandler_Win32_Para *) p)->InputThreadMain(); - return 0; -} - -void InputHandler_Win32_Para::InputThreadMain() -{ - if(!SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST)) - LOG->Warn(werr_ssprintf(GetLastError(), "Failed to set Para thread priority")); - - vector sources; - if( dev->m_IO.IsOpen() ) - sources.push_back( &dev->m_IO ); - - while(!shutdown) - { - CHECKPOINT; - int actual = 0, val = 0; - int ret = WindowsFileIO::read_several(sources, &val, actual, 0.100f); - - CHECKPOINT; - if(ret <= 0) - continue; /* no event */ - - HandleInput( actual, val ); - InputHandler::UpdateTimer(); - } - CHECKPOINT; -} - -void InputHandler_Win32_Para::Update(float fDeltaTime) -{ - if( !InputThread.IsCreated() ) - { - int ret = dev->GetPadEvent(); - - if(ret == -1) - return; - - HandleInput( 0, ret ); - InputHandler::UpdateTimer(); - } + // The device appears as a HID joystick } /* diff --git a/stepmania/src/arch/InputHandler/InputHandler_Win32_Para.h b/stepmania/src/arch/InputHandler/InputHandler_Win32_Para.h index 28074dc8ff..6e1cb04ba3 100644 --- a/stepmania/src/arch/InputHandler/InputHandler_Win32_Para.h +++ b/stepmania/src/arch/InputHandler/InputHandler_Win32_Para.h @@ -1,26 +1,15 @@ +/* Initializes a USB Para controller so that it will function as a regular HID joystick. */ + #ifndef INPUT_HANDLER_WIN32_PARA_H #define INPUT_HANDLER_WIN32_PARA_H #include "InputHandler.h" -#include "RageThreads.h" -class USBDevice; class InputHandler_Win32_Para: public InputHandler { public: - void Update( float fDeltaTime ); InputHandler_Win32_Para(); - ~InputHandler_Win32_Para(); void GetDevicesAndDescriptions( vector& vDevicesOut, vector& vDescriptionsOut ); - -private: - USBDevice *dev; - RageThread InputThread; - bool shutdown; - - static int InputThread_Start( void *p ); - void InputThreadMain(); - void HandleInput( int devno, int event ); }; #define USE_INPUT_HANDLER_WIN32_PARA diff --git a/stepmania/src/arch/InputHandler/InputHandler_Win32_Pump.cpp b/stepmania/src/arch/InputHandler/InputHandler_Win32_Pump.cpp index da47cfd76d..7767cfddb8 100644 --- a/stepmania/src/arch/InputHandler/InputHandler_Win32_Pump.cpp +++ b/stepmania/src/arch/InputHandler/InputHandler_Win32_Pump.cpp @@ -17,7 +17,7 @@ InputHandler_Win32_Pump::InputHandler_Win32_Pump() bool bFoundOnePad = false; for( int i = 0; i < NUM_PUMPS; ++i ) { - if( m_pDevice[i].Open(pump_usb_vid, pump_usb_pid, sizeof(long), i) ) + if( m_pDevice[i].Open(pump_usb_vid, pump_usb_pid, sizeof(long), i, NULL) ) { bFoundOnePad = true; LOG->Info( "Found Pump pad %i", i ); diff --git a/stepmania/src/archutils/Win32/USB.cpp b/stepmania/src/archutils/Win32/USB.cpp index 90abfac65e..df792548c3 100644 --- a/stepmania/src/archutils/Win32/USB.cpp +++ b/stepmania/src/archutils/Win32/USB.cpp @@ -47,8 +47,7 @@ static CString GetUSBDevicePath( int iNum ) return sRet; } - -bool USBDevice::Open( int iVID, int iPID, int iBlockSize, int iNum ) +bool USBDevice::Open( int iVID, int iPID, int iBlockSize, int iNum, void (*pfnInit)(HANDLE) ) { DWORD iIndex = 0; @@ -67,15 +66,24 @@ bool USBDevice::Open( int iVID, int iPID, int iBlockSize, int iNum ) CloseHandle( h ); continue; } - CloseHandle( h ); if( (iVID != -1 && attr.VendorID != iVID) || (iPID != -1 && attr.ProductID != iPID) ) + { + CloseHandle( h ); continue; /* This isn't it. */ + } /* The VID and PID match. */ if( iNum-- > 0 ) + { + CloseHandle( h ); continue; + } + + if( pfnInit ) + pfnInit( h ); + CloseHandle(h); m_IO.Open( path, iBlockSize ); return true; diff --git a/stepmania/src/archutils/Win32/USB.h b/stepmania/src/archutils/Win32/USB.h index 6dcec7578d..a51e557497 100644 --- a/stepmania/src/archutils/Win32/USB.h +++ b/stepmania/src/archutils/Win32/USB.h @@ -34,7 +34,7 @@ class USBDevice { public: int GetPadEvent(); - bool Open( int VID, int PID, int blocksize, int num ); + bool Open( int iVID, int iPID, int iBlockSize, int iNum, void (*pfnInit)(HANDLE) ); bool IsOpen() const; WindowsFileIO m_IO;