00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "Win32/Win32InputManager.h"
00024 #include "Win32/Win32Keyboard.h"
00025 #include "Win32/Win32Mouse.h"
00026 #include "Win32/Win32JoyStick.h"
00027 #include "OISException.h"
00028
00029 using namespace OIS;
00030
00031 const std::string Win32InputManager::iName = "Win32";
00032
00033
00034 Win32InputManager::Win32InputManager()
00035 {
00036 hWnd = 0;
00037 mDirectInput = 0;
00038
00039 kbSettings = 0;
00040 mouseSettings = 0;
00041 joySettings = 0;
00042
00043 joySticks = 0;
00044 }
00045
00046
00047 Win32InputManager::~Win32InputManager()
00048 {
00049 if( mDirectInput )
00050 {
00051 mDirectInput->Release();
00052 mDirectInput = 0;
00053 }
00054 }
00055
00056
00057 void Win32InputManager::_initialize( ParamList ¶mList )
00058 {
00059 HINSTANCE hInst = 0;
00060 HRESULT hr;
00061
00062
00063
00064 ParamList::iterator i = paramList.find("WINDOW");
00065 if( i == paramList.end() )
00066 OIS_EXCEPT( E_InvalidParam, "Win32InputManager::Win32InputManager >> No HWND found!" );
00067
00068 hWnd = (HWND)strtoul(i->second.c_str(), 0, 10);
00069
00070 if( IsWindow(hWnd) == 0 )
00071 OIS_EXCEPT( E_General, "Win32InputManager::Win32InputManager >> The sent HWND is not valid!");
00072
00073 hInst = GetModuleHandle(0);
00074
00075
00076 hr = DirectInput8Create( hInst, DIRECTINPUT_VERSION, IID_IDirectInput8, (VOID**)&mDirectInput, NULL );
00077 if (FAILED(hr))
00078 OIS_EXCEPT( E_General, "Win32InputManager::Win32InputManager >> Not able to init DirectX8 Input!");
00079
00080
00081 _parseConfigSettings( paramList );
00082 _enumerateDevices();
00083 }
00084
00085
00086 void Win32InputManager::_parseConfigSettings( ParamList ¶mList )
00087 {
00088
00089 std::map<std::string, DWORD> temp;
00090 temp["DISCL_BACKGROUND"] = DISCL_BACKGROUND;
00091 temp["DISCL_EXCLUSIVE"] = DISCL_EXCLUSIVE;
00092 temp["DISCL_FOREGROUND"] = DISCL_FOREGROUND;
00093 temp["DISCL_NONEXCLUSIVE"] = DISCL_NONEXCLUSIVE;
00094 temp["DISCL_NOWINKEY"] = DISCL_NOWINKEY;
00095
00096
00097 ParamList::iterator i = paramList.begin(), e = paramList.end();
00098 for( ; i != e; ++i )
00099 {
00100 if( i->first == "w32_keyboard" )
00101 kbSettings |= temp[i->second];
00102 else if( i->first == "w32_mouse" )
00103 mouseSettings |= temp[i->second];
00104 else if( i->first == "w32_joystick" )
00105 joySettings |= temp[i->second];
00106 }
00107 if( kbSettings == 0 ) kbSettings = DISCL_FOREGROUND | DISCL_NONEXCLUSIVE | DISCL_NOWINKEY;
00108 if( mouseSettings == 0 ) mouseSettings = DISCL_FOREGROUND | DISCL_EXCLUSIVE;
00109 if( joySettings == 0 ) joySettings = DISCL_FOREGROUND | DISCL_EXCLUSIVE;
00110 }
00111
00112
00113 void Win32InputManager::_enumerateDevices()
00114 {
00115
00116 mDirectInput->EnumDevices(NULL, _DIEnumKbdCallback, this, DIEDFL_ATTACHEDONLY);
00117 }
00118
00119
00120 BOOL CALLBACK Win32InputManager::_DIEnumKbdCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef)
00121 {
00122 Win32InputManager *_this_ = static_cast<Win32InputManager*>(pvRef);
00123 if( GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_JOYSTICK ||
00124 GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_GAMEPAD ||
00125 GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_1STPERSON ||
00126 GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_DRIVING ||
00127 GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_FLIGHT)
00128 {
00129 JoyStickInfo jsInfo;
00130 jsInfo.deviceID = lpddi->guidInstance;
00131 jsInfo.vendor = lpddi->tszInstanceName;
00132 jsInfo.devId = _this_->joySticks;
00133
00134 _this_->joySticks++;
00135
00136 _this_->unusedJoyStickList.push_back( jsInfo );
00137 }
00138
00139 return DIENUM_CONTINUE;
00140 }
00141
00142
00143 int Win32InputManager::numJoysticks()
00144 {
00145 return joySticks;
00146 }
00147
00148
00149 int Win32InputManager::numMice()
00150 {
00151 return 1;
00152 }
00153
00154
00155 int Win32InputManager::numKeyBoards()
00156 {
00157 return 1;
00158 }
00159
00160
00161 Object* Win32InputManager::createInputObject( Type iType, bool bufferMode )
00162 {
00163 Object* obj = 0;
00164
00165 switch( iType )
00166 {
00167 case OISKeyboard: obj = new Win32Keyboard( this, mDirectInput, bufferMode, kbSettings ); break;
00168 case OISMouse: obj = new Win32Mouse( this, mDirectInput, bufferMode, mouseSettings ); break;
00169 case OISJoyStick:
00170 {
00171
00172 JoyStickInfoList::iterator i = unusedJoyStickList.begin();
00173 if( i != unusedJoyStickList.end() )
00174 {
00175 obj = new Win32JoyStick( this, mDirectInput, bufferMode, joySettings, (*i) );
00176 unusedJoyStickList.erase(i);
00177 break;
00178 }
00179 OIS_EXCEPT(E_InputDeviceNonExistant, "No Unused JoyStick could be found!");
00180 }
00181 default: OIS_EXCEPT( E_InputDeviceNotSupported, "Type not implemented");
00182 }
00183
00184 try {
00185 obj->_initialize();
00186 }
00187 catch(...) {
00188 delete obj;
00189 throw;
00190 }
00191
00192 return obj;
00193 }
00194
00195
00196 void Win32InputManager::destroyInputObject( Object* obj )
00197 {
00198 if( obj == 0 )
00199 return;
00200
00201
00202 if( obj->type() == OISJoyStick )
00203 {
00204 unusedJoyStickList.push_back(((Win32JoyStick*)obj)->_getJoyInfo());
00205 }
00206
00207 delete obj;
00208 }