00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "CATInternal.h"
00014 #include "CATDirectInput.h"
00015 #include "CATDIJoystick.h"
00016
00017
00018 CATDirectInput::CATDirectInput()
00019 {
00020 fDirectInput = 0;
00021 fInitialized = false;
00022 fUserCallback = 0;
00023 fUserParam = 0;
00024 }
00025
00026
00027 bool CATDirectInput::Init()
00028 {
00029 if (DI_OK == DirectInput8Create((HINSTANCE)::GetModuleHandle(0) ,DIRECTINPUT_VERSION,IID_IDirectInput8W, (void**)&fDirectInput, NULL))
00030 {
00031 fInitialized = true;
00032 }
00033 return fInitialized;
00034 }
00035
00036 CATDirectInput::~CATDirectInput()
00037 {
00038 if (fDirectInput)
00039 fDirectInput->Release();
00040
00041 fDirectInput = 0;
00042
00043 while (fJoystickList.size())
00044 {
00045 DIDEVICEINSTANCE* inputDevInfo = fJoystickList[0];
00046 delete inputDevInfo;
00047 fJoystickList.erase(fJoystickList.begin());
00048 }
00049
00050 }
00051
00052
00053 void CATDirectInput::DoControlPanel(HWND parent)
00054 {
00055 if (fDirectInput)
00056 {
00057 fDirectInput->RunControlPanel(parent, 0);
00058 }
00059 }
00060
00061
00062 void CATDirectInput::EnumJoysticks(EnumJoysticksCallback callback, void *userParam)
00063 {
00064 fUserCallback = callback;
00065 fUserParam = userParam;
00066
00067 this->fDirectInput->EnumDevices(
00068 DI8DEVCLASS_GAMECTRL,
00069 DIJoysticksCB,
00070 this,
00071 DIEDFL_ATTACHEDONLY);
00072 }
00073
00074
00075
00076 BOOL CALLBACK CATDirectInput::DIJoysticksCB( LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef )
00077 {
00078 CATDirectInput* cdi = (CATDirectInput*)pvRef;
00079
00080
00081 DIDEVICEINSTANCE* inputDevInfo = new DIDEVICEINSTANCE;
00082 memcpy(inputDevInfo,lpddi,sizeof(DIDEVICEINSTANCE));
00083 cdi->fJoystickList.push_back(inputDevInfo);
00084
00085
00086 if (cdi->fUserCallback)
00087 {
00088 cdi->fUserCallback(inputDevInfo->tszInstanceName, cdi->fUserParam);
00089 }
00090
00091 return TRUE;
00092 }
00093
00094
00095
00096 CATJoystick* CATDirectInput::CreateJoystick(const CATWChar* name)
00097 {
00098 for (CATUInt32 i = 0; i < fJoystickList.size(); i++)
00099 {
00100 DIDEVICEINSTANCE* inputDevInfo;
00101 if (inputDevInfo = fJoystickList[i])
00102 {
00103 if (0 == _wcsicmp(inputDevInfo->tszInstanceName,name))
00104 {
00105 CATDIJoystick* joystick = 0;
00106 joystick = new CATDIJoystick(this->fDirectInput);
00107 if (joystick->Init(inputDevInfo))
00108 {
00109 return joystick;
00110 }
00111 delete joystick;
00112 joystick = 0;
00113 }
00114 }
00115 }
00116
00117 return 0;
00118 }