Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/gamestates3/src/external/ois/win32/Win32InputManager.cpp @ 8869

Last change on this file since 8869 was 5781, checked in by rgrieder, 15 years ago

Reverted trunk again. We might want to find a way to delete these revisions again (x3n's changes are still available as diff in the commit mails).

  • Property svn:eol-style set to native
File size: 7.7 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 input system
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
85        // Enumerate devices ...
86        _enumerateDevices();
87}
88
89//--------------------------------------------------------------------------------//
90void Win32InputManager::_parseConfigSettings( ParamList &paramList )
91{
92        //Here we pick up settings such as a device's cooperation mode
93        std::map<std::string, DWORD> temp;
94        temp["DISCL_BACKGROUND"]        = DISCL_BACKGROUND;
95        temp["DISCL_EXCLUSIVE"]         = DISCL_EXCLUSIVE;
96        temp["DISCL_FOREGROUND"]        = DISCL_FOREGROUND;
97        temp["DISCL_NONEXCLUSIVE"]      = DISCL_NONEXCLUSIVE;
98        temp["DISCL_NOWINKEY"]          = DISCL_NOWINKEY;
99
100        //Check for pairs: ie. ("w32_keyboard","DISCL_NOWINKEY")("w32_keyboard","DISCL_FOREGROUND")
101        ParamList::iterator i = paramList.begin(), e = paramList.end();
102        for( ; i != e; ++i ) 
103        {
104                if( i->first == "w32_keyboard" )
105                                kbSettings |= temp[i->second];
106                else if( i->first == "w32_mouse" )
107                                mouseSettings |= temp[i->second];
108                else if( i->first == "w32_joystick" )
109                                joySettings |= temp[i->second];
110        }
111        if( kbSettings == 0 ) kbSettings = DISCL_FOREGROUND | DISCL_NONEXCLUSIVE | DISCL_NOWINKEY;
112        if( mouseSettings == 0 ) mouseSettings = DISCL_FOREGROUND | DISCL_EXCLUSIVE;
113        if( joySettings == 0 ) joySettings = DISCL_FOREGROUND | DISCL_EXCLUSIVE;
114}
115
116//--------------------------------------------------------------------------------//
117void Win32InputManager::_enumerateDevices()
118{
119        //Enumerate all attached devices
120        mDirectInput->EnumDevices(NULL , _DIEnumDevCallback, this, DIEDFL_ATTACHEDONLY); 
121}
122
123//--------------------------------------------------------------------------------//
124BOOL CALLBACK Win32InputManager::_DIEnumDevCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef)
125{
126        Win32InputManager *_this_ = static_cast<Win32InputManager*>(pvRef);
127
128        // Register only game devices (keyboard and mouse are managed differently).
129        if( GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_JOYSTICK ||
130                GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_GAMEPAD ||
131                GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_1STPERSON ||
132                GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_DRIVING ||
133                GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_FLIGHT)
134        {
135                JoyStickInfo jsInfo;
136                jsInfo.deviceID = lpddi->guidInstance;
137                jsInfo.vendor = lpddi->tszInstanceName;
138                jsInfo.devId = _this_->joySticks;
139
140                _this_->joySticks++;
141               
142                _this_->unusedJoyStickList.push_back( jsInfo );
143        }
144
145        return DIENUM_CONTINUE;
146}
147
148//----------------------------------------------------------------------------//
149void Win32InputManager::_returnJoyStick(const JoyStickInfo& joystick)
150{
151        unusedJoyStickList.push_back(joystick);
152}
153
154//----------------------------------------------------------------------------//
155DeviceList Win32InputManager::freeDeviceList()
156{
157        DeviceList ret;
158
159        if( keyboardUsed == false )
160                ret.insert(std::make_pair(OISKeyboard, mInputSystemName));
161
162        if( mouseUsed == false )
163                ret.insert(std::make_pair(OISMouse, mInputSystemName));
164
165        for(JoyStickInfoList::iterator i = unusedJoyStickList.begin(); i != unusedJoyStickList.end(); ++i)
166                ret.insert(std::make_pair(OISJoyStick, i->vendor));
167
168        return ret;
169}
170
171//----------------------------------------------------------------------------//
172int Win32InputManager::totalDevices(Type iType)
173{
174        switch(iType)
175        {
176        case OISKeyboard: return 1;
177        case OISMouse: return 1;
178        case OISJoyStick: return joySticks;
179        default: return 0;
180        }
181}
182
183//----------------------------------------------------------------------------//
184int Win32InputManager::freeDevices(Type iType)
185{
186        switch(iType)
187        {
188        case OISKeyboard: return keyboardUsed ? 0 : 1;
189        case OISMouse: return mouseUsed ? 0 : 1;
190        case OISJoyStick: return (int)unusedJoyStickList.size();
191        default: return 0;
192        }
193}
194
195//----------------------------------------------------------------------------//
196bool Win32InputManager::vendorExist(Type iType, const std::string & vendor)
197{
198        if( (iType == OISKeyboard || iType == OISMouse) && vendor == mInputSystemName )
199        {
200                return true;
201        }
202        else if( iType == OISJoyStick )
203        {
204                for(JoyStickInfoList::iterator i = unusedJoyStickList.begin(); i != unusedJoyStickList.end(); ++i)
205                        if(i->vendor == vendor)
206                                return true;
207        }
208
209        return false;
210}
211
212//----------------------------------------------------------------------------//
213Object* Win32InputManager::createObject(InputManager* creator, Type iType, bool bufferMode, const std::string & vendor)
214{
215        Object *obj = 0;
216
217        switch(iType)
218        {
219        case OISKeyboard: 
220        {
221                if( keyboardUsed == false )
222                        obj = new Win32Keyboard(this, mDirectInput, bufferMode, kbSettings);
223                break;
224        }
225        case OISMouse:
226        {
227                if( mouseUsed == false )
228                        obj = new Win32Mouse(this, mDirectInput, bufferMode, mouseSettings);
229                break;
230        }
231        case OISJoyStick:
232        {
233                for(JoyStickInfoList::iterator i = unusedJoyStickList.begin(); i != unusedJoyStickList.end(); ++i)
234                {
235                        if(vendor == "" || i->vendor == vendor)
236                        {
237                                obj = new Win32JoyStick(this, mDirectInput, bufferMode, joySettings, *i);
238                                unusedJoyStickList.erase(i);
239                                break;
240                        }
241                }
242                break;
243        }
244        default:
245                break;
246        }
247
248        if( obj == 0 )
249                OIS_EXCEPT(E_InputDeviceNonExistant, "No devices match requested type.");
250
251        return obj;
252}
253
254//----------------------------------------------------------------------------//
255void Win32InputManager::destroyObject(Object* obj)
256{
257        delete obj;
258}
Note: See TracBrowser for help on using the repository browser.