Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/netp3/src/ois/win32/Win32InputManager.cpp @ 2989

Last change on this file since 2989 was 1505, checked in by rgrieder, 17 years ago

f* svn: It doesn't even inform you if you attempt to set a non existing property. It is svn:eol-style and not eol-style when using the command by the way…

  • Property svn:eol-style set to native
File size: 7.6 KB
Line 
1/*
2The zlib/libpng License
3
4Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com)
5
6This software is provided 'as-is', without any express or implied warranty. In no event will
7the authors be held liable for any damages arising from the use of this software.
8
9Permission is granted to anyone to use this software for any purpose, including commercial
10applications, and to alter it and redistribute it freely, subject to the following
11restrictions:
12
13    1. The origin of this software must not be misrepresented; you must not claim that
14                you wrote the original software. If you use this software in a product,
15                an acknowledgment in the product documentation would be appreciated but is
16                not required.
17
18    2. Altered source versions must be plainly marked as such, and must not be
19                misrepresented as being the original software.
20
21    3. This notice may not be removed or altered from any source distribution.
22*/
23#include "Win32/Win32InputManager.h"
24#include "Win32/Win32Keyboard.h"
25#include "Win32/Win32Mouse.h"
26#include "Win32/Win32JoyStick.h"
27#include "OISException.h"
28
29using namespace OIS;
30
31//--------------------------------------------------------------------------------//
32Win32InputManager::Win32InputManager() : InputManager("Win32InputManager")
33{
34        hWnd = 0;
35        mDirectInput = 0;
36
37        kbSettings    = 0;
38        mouseSettings = 0;
39        joySettings   = 0;
40
41        joySticks = 0;
42        keyboardUsed = mouseUsed = false;
43
44        //Setup our internal factories
45        mFactories.push_back(this);
46}
47
48//--------------------------------------------------------------------------------//
49Win32InputManager::~Win32InputManager()
50{
51        if( mDirectInput )
52        {
53                mDirectInput->Release();
54                mDirectInput = 0;
55        }
56}
57
58//--------------------------------------------------------------------------------//
59void Win32InputManager::_initialize( ParamList &paramList )
60{
61        HINSTANCE hInst = 0;
62        HRESULT hr;
63
64        //TODO 64 bit proof this little conversion xxx wip
65        //First of all, get the Windows Handle and Instance
66        ParamList::iterator i = paramList.find("WINDOW");
67        if( i == paramList.end() ) 
68                OIS_EXCEPT( E_InvalidParam, "Win32InputManager::Win32InputManager >> No HWND found!" );
69
70        hWnd  = (HWND)strtoul(i->second.c_str(), 0, 10);
71
72        if( IsWindow(hWnd) == 0 )
73                OIS_EXCEPT( E_General, "Win32InputManager::Win32InputManager >> The sent HWND is not valid!");
74
75        hInst = GetModuleHandle(0);
76
77        //Create the device
78        hr = DirectInput8Create( hInst, DIRECTINPUT_VERSION, IID_IDirectInput8, (VOID**)&mDirectInput, NULL );
79    if (FAILED(hr))     
80                OIS_EXCEPT( E_General, "Win32InputManager::Win32InputManager >> Not able to init DirectX8 Input!");
81
82        //Ok, now we have DirectInput, parse whatever extra settings were sent to us
83        _parseConfigSettings( paramList );
84        _enumerateDevices();
85}
86
87//--------------------------------------------------------------------------------//
88void Win32InputManager::_parseConfigSettings( ParamList &paramList )
89{
90        //Here we pick up settings such as a device's cooperation mode
91        std::map<std::string, DWORD> temp;
92        temp["DISCL_BACKGROUND"]        = DISCL_BACKGROUND;
93        temp["DISCL_EXCLUSIVE"]         = DISCL_EXCLUSIVE;
94        temp["DISCL_FOREGROUND"]        = DISCL_FOREGROUND;
95        temp["DISCL_NONEXCLUSIVE"]      = DISCL_NONEXCLUSIVE;
96        temp["DISCL_NOWINKEY"]          = DISCL_NOWINKEY;
97
98        //Check for pairs: ie. ("w32_keyboard","DISCL_NOWINKEY")("w32_keyboard","DISCL_FOREGROUND")
99        ParamList::iterator i = paramList.begin(), e = paramList.end();
100        for( ; i != e; ++i ) 
101        {
102                if( i->first == "w32_keyboard" )
103                                kbSettings |= temp[i->second];
104                else if( i->first == "w32_mouse" )
105                                mouseSettings |= temp[i->second];
106                else if( i->first == "w32_joystick" )
107                                joySettings |= temp[i->second];
108        }
109        if( kbSettings == 0 ) kbSettings = DISCL_FOREGROUND | DISCL_NONEXCLUSIVE | DISCL_NOWINKEY;
110        if( mouseSettings == 0 ) mouseSettings = DISCL_FOREGROUND | DISCL_EXCLUSIVE;
111        if( joySettings == 0 ) joySettings = DISCL_FOREGROUND | DISCL_EXCLUSIVE;
112}
113
114//--------------------------------------------------------------------------------//
115void Win32InputManager::_enumerateDevices()
116{
117        //Enumerate all attached devices
118        mDirectInput->EnumDevices(NULL, _DIEnumKbdCallback, this, DIEDFL_ATTACHEDONLY); 
119}
120
121//--------------------------------------------------------------------------------//
122BOOL CALLBACK Win32InputManager::_DIEnumKbdCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef)
123{
124        Win32InputManager *_this_ = static_cast<Win32InputManager*>(pvRef);
125        if( GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_JOYSTICK ||
126                GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_GAMEPAD ||
127                GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_1STPERSON ||
128                GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_DRIVING ||
129                GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_FLIGHT)
130        {
131                JoyStickInfo jsInfo;
132                jsInfo.deviceID = lpddi->guidInstance;
133                jsInfo.vendor = lpddi->tszInstanceName;
134                jsInfo.devId = _this_->joySticks;
135
136                _this_->joySticks++;
137               
138                _this_->unusedJoyStickList.push_back( jsInfo );
139        }
140
141        return DIENUM_CONTINUE;
142}
143
144//----------------------------------------------------------------------------//
145void Win32InputManager::_returnJoyStick(const JoyStickInfo& joystick)
146{
147        unusedJoyStickList.push_back(joystick);
148}
149
150//----------------------------------------------------------------------------//
151DeviceList Win32InputManager::freeDeviceList()
152{
153        DeviceList ret;
154
155        if( keyboardUsed == false )
156                ret.insert(std::make_pair(OISKeyboard, mInputSystemName));
157
158        if( mouseUsed == false )
159                ret.insert(std::make_pair(OISMouse, mInputSystemName));
160
161        for(JoyStickInfoList::iterator i = unusedJoyStickList.begin(); i != unusedJoyStickList.end(); ++i)
162                ret.insert(std::make_pair(OISJoyStick, i->vendor));
163
164        return ret;
165}
166
167//----------------------------------------------------------------------------//
168int Win32InputManager::totalDevices(Type iType)
169{
170        switch(iType)
171        {
172        case OISKeyboard: return 1;
173        case OISMouse: return 1;
174        case OISJoyStick: return joySticks;
175        default: return 0;
176        }
177}
178
179//----------------------------------------------------------------------------//
180int Win32InputManager::freeDevices(Type iType)
181{
182        switch(iType)
183        {
184        case OISKeyboard: return keyboardUsed ? 0 : 1;
185        case OISMouse: return mouseUsed ? 0 : 1;
186        case OISJoyStick: return (int)unusedJoyStickList.size();
187        default: return 0;
188        }
189}
190
191//----------------------------------------------------------------------------//
192bool Win32InputManager::vendorExist(Type iType, const std::string & vendor)
193{
194        if( (iType == OISKeyboard || iType == OISMouse) && vendor == mInputSystemName )
195        {
196                return true;
197        }
198        else if( iType == OISJoyStick )
199        {
200                for(JoyStickInfoList::iterator i = unusedJoyStickList.begin(); i != unusedJoyStickList.end(); ++i)
201                        if(i->vendor == vendor)
202                                return true;
203        }
204
205        return false;
206}
207
208//----------------------------------------------------------------------------//
209Object* Win32InputManager::createObject(InputManager* creator, Type iType, bool bufferMode, const std::string & vendor)
210{
211        Object *obj = 0;
212
213        switch(iType)
214        {
215        case OISKeyboard: 
216        {
217                if( keyboardUsed == false )
218                        obj = new Win32Keyboard(this, mDirectInput, bufferMode, kbSettings);
219                break;
220        }
221        case OISMouse:
222        {
223                if( mouseUsed == false )
224                        obj = new Win32Mouse(this, mDirectInput, bufferMode, mouseSettings);
225                break;
226        }
227        case OISJoyStick:
228        {
229                for(JoyStickInfoList::iterator i = unusedJoyStickList.begin(); i != unusedJoyStickList.end(); ++i)
230                {
231                        if(vendor == "" || i->vendor == vendor)
232                        {
233                                obj = new Win32JoyStick(this, mDirectInput, bufferMode, joySettings, *i);
234                                unusedJoyStickList.erase(i);
235                                break;
236                        }
237                }
238                break;
239        }
240        default:
241                break;
242        }
243
244        if( obj == 0 )
245                OIS_EXCEPT(E_InputDeviceNonExistant, "No devices match requested type.");
246
247        return obj;
248}
249
250//----------------------------------------------------------------------------//
251void Win32InputManager::destroyObject(Object* obj)
252{
253        delete obj;
254}
Note: See TracBrowser for help on using the repository browser.