E:/Download/ois-1.0RC1/src/win32/Win32InputManager.cpp

Go to the documentation of this file.
00001 /*
00002 The zlib/libpng License
00003 
00004 Copyright (c) 2006 Phillip Castaneda (pjcast -- www.wreckedgames.com)
00005 
00006 This software is provided 'as-is', without any express or implied warranty. In no event will
00007 the authors be held liable for any damages arising from the use of this software.
00008 
00009 Permission is granted to anyone to use this software for any purpose, including commercial 
00010 applications, and to alter it and redistribute it freely, subject to the following
00011 restrictions:
00012 
00013     1. The origin of this software must not be misrepresented; you must not claim that 
00014                 you wrote the original software. If you use this software in a product, 
00015                 an acknowledgment in the product documentation would be appreciated but is 
00016                 not required.
00017 
00018     2. Altered source versions must be plainly marked as such, and must not be 
00019                 misrepresented as being the original software.
00020 
00021     3. This notice may not be removed or altered from any source distribution.
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 &paramList )
00058 {
00059         HINSTANCE hInst = 0;
00060         HRESULT hr;
00061 
00062         //TODO 64 bit proof this little conversion xxx wip
00063         //First of all, get the Windows Handle and Instance
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         //Create the device
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         //Ok, now we have DirectInput, parse whatever extra settings were sent to us
00081         _parseConfigSettings( paramList );
00082         _enumerateDevices();
00083 }
00084 
00085 //--------------------------------------------------------------------------------//
00086 void Win32InputManager::_parseConfigSettings( ParamList &paramList )
00087 {
00088         //Here we pick up settings such as a device's cooperation mode
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         //Check for pairs: ie. ("w32_keyboard","DISCL_NOWINKEY")("w32_keyboard","DISCL_FOREGROUND")
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         //Enumerate all attached devices
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                         //Find a JoyStick not in use
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; //rethrow
00190         }
00191 
00192         return obj;
00193 }
00194 
00195 //----------------------------------------------------------------------------//
00196 void Win32InputManager::destroyInputObject( Object* obj )
00197 {
00198         if( obj == 0 )
00199                 return;
00200 
00201         //If it was a numbered device... add it back to availiable list
00202         if( obj->type() == OISJoyStick )
00203         {
00204                 unusedJoyStickList.push_back(((Win32JoyStick*)obj)->_getJoyInfo());
00205         }
00206 
00207         delete obj;
00208 }

Generated on Sat Dec 1 20:13:52 2007 for OIS by  doxygen 1.5.4