1 | /* |
---|
2 | The zlib/libpng License |
---|
3 | |
---|
4 | Copyright (c) 2005-2007 Phillip Castaneda (pjcast -- www.wreckedgames.com) |
---|
5 | |
---|
6 | This software is provided 'as-is', without any express or implied warranty. In no event will |
---|
7 | the authors be held liable for any damages arising from the use of this software. |
---|
8 | |
---|
9 | Permission is granted to anyone to use this software for any purpose, including commercial |
---|
10 | applications, and to alter it and redistribute it freely, subject to the following |
---|
11 | restrictions: |
---|
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 | |
---|
29 | using namespace OIS; |
---|
30 | |
---|
31 | //--------------------------------------------------------------------------------// |
---|
32 | Win32InputManager::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 | //--------------------------------------------------------------------------------// |
---|
49 | Win32InputManager::~Win32InputManager() |
---|
50 | { |
---|
51 | if( mDirectInput ) |
---|
52 | { |
---|
53 | mDirectInput->Release(); |
---|
54 | mDirectInput = 0; |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | //--------------------------------------------------------------------------------// |
---|
59 | void Win32InputManager::_initialize( ParamList ¶mList ) |
---|
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 | |
---|
85 | // Enumerate devices ... |
---|
86 | _enumerateDevices(); |
---|
87 | } |
---|
88 | |
---|
89 | //--------------------------------------------------------------------------------// |
---|
90 | void Win32InputManager::_parseConfigSettings( ParamList ¶mList ) |
---|
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 | //--------------------------------------------------------------------------------// |
---|
117 | void Win32InputManager::_enumerateDevices() |
---|
118 | { |
---|
119 | //Enumerate all attached devices |
---|
120 | mDirectInput->EnumDevices(NULL, _DIEnumDevCallback, this, DIEDFL_ATTACHEDONLY); |
---|
121 | |
---|
122 | #ifdef OIS_WIN32_XINPUT_SUPPORT |
---|
123 | //let's check how many possible XInput devices we may have (max 4)... |
---|
124 | for(int i = 0; i < 3; ++i) |
---|
125 | { |
---|
126 | XINPUT_STATE state; |
---|
127 | if(XInputGetState(i, &state) != ERROR_DEVICE_NOT_CONNECTED) |
---|
128 | { //Once we found 1, just check our whole list against devices |
---|
129 | Win32JoyStick::CheckXInputDevices(unusedJoyStickList); |
---|
130 | break; |
---|
131 | } |
---|
132 | } |
---|
133 | #endif |
---|
134 | } |
---|
135 | |
---|
136 | //--------------------------------------------------------------------------------// |
---|
137 | BOOL CALLBACK Win32InputManager::_DIEnumDevCallback(LPCDIDEVICEINSTANCE lpddi, LPVOID pvRef) |
---|
138 | { |
---|
139 | Win32InputManager *_this_ = static_cast<Win32InputManager*>(pvRef); |
---|
140 | |
---|
141 | // Register only game devices (keyboard and mouse are managed differently). |
---|
142 | if( GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_JOYSTICK || |
---|
143 | GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_GAMEPAD || |
---|
144 | GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_1STPERSON || |
---|
145 | GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_DRIVING || |
---|
146 | GET_DIDEVICE_TYPE(lpddi->dwDevType) == DI8DEVTYPE_FLIGHT) |
---|
147 | { |
---|
148 | JoyStickInfo jsInfo; |
---|
149 | jsInfo.isXInput = false; |
---|
150 | jsInfo.productGuid = lpddi->guidProduct; |
---|
151 | jsInfo.deviceID = lpddi->guidInstance; |
---|
152 | jsInfo.vendor = lpddi->tszInstanceName; |
---|
153 | jsInfo.devId = _this_->joySticks; |
---|
154 | |
---|
155 | _this_->joySticks++; |
---|
156 | |
---|
157 | _this_->unusedJoyStickList.push_back( jsInfo ); |
---|
158 | } |
---|
159 | |
---|
160 | return DIENUM_CONTINUE; |
---|
161 | } |
---|
162 | |
---|
163 | //----------------------------------------------------------------------------// |
---|
164 | void Win32InputManager::_returnJoyStick(const JoyStickInfo& joystick) |
---|
165 | { |
---|
166 | unusedJoyStickList.push_back(joystick); |
---|
167 | } |
---|
168 | |
---|
169 | //----------------------------------------------------------------------------// |
---|
170 | DeviceList Win32InputManager::freeDeviceList() |
---|
171 | { |
---|
172 | DeviceList ret; |
---|
173 | |
---|
174 | if( keyboardUsed == false ) |
---|
175 | ret.insert(std::make_pair(OISKeyboard, mInputSystemName)); |
---|
176 | |
---|
177 | if( mouseUsed == false ) |
---|
178 | ret.insert(std::make_pair(OISMouse, mInputSystemName)); |
---|
179 | |
---|
180 | for(JoyStickInfoList::iterator i = unusedJoyStickList.begin(); i != unusedJoyStickList.end(); ++i) |
---|
181 | ret.insert(std::make_pair(OISJoyStick, i->vendor)); |
---|
182 | |
---|
183 | return ret; |
---|
184 | } |
---|
185 | |
---|
186 | //----------------------------------------------------------------------------// |
---|
187 | int Win32InputManager::totalDevices(Type iType) |
---|
188 | { |
---|
189 | switch(iType) |
---|
190 | { |
---|
191 | case OISKeyboard: return 1; |
---|
192 | case OISMouse: return 1; |
---|
193 | case OISJoyStick: return joySticks; |
---|
194 | default: return 0; |
---|
195 | } |
---|
196 | } |
---|
197 | |
---|
198 | //----------------------------------------------------------------------------// |
---|
199 | int Win32InputManager::freeDevices(Type iType) |
---|
200 | { |
---|
201 | switch(iType) |
---|
202 | { |
---|
203 | case OISKeyboard: return keyboardUsed ? 0 : 1; |
---|
204 | case OISMouse: return mouseUsed ? 0 : 1; |
---|
205 | case OISJoyStick: return (int)unusedJoyStickList.size(); |
---|
206 | default: return 0; |
---|
207 | } |
---|
208 | } |
---|
209 | |
---|
210 | //----------------------------------------------------------------------------// |
---|
211 | bool Win32InputManager::vendorExist(Type iType, const std::string & vendor) |
---|
212 | { |
---|
213 | if( (iType == OISKeyboard || iType == OISMouse) && vendor == mInputSystemName ) |
---|
214 | { |
---|
215 | return true; |
---|
216 | } |
---|
217 | else if( iType == OISJoyStick ) |
---|
218 | { |
---|
219 | for(JoyStickInfoList::iterator i = unusedJoyStickList.begin(); i != unusedJoyStickList.end(); ++i) |
---|
220 | if(i->vendor == vendor) |
---|
221 | return true; |
---|
222 | } |
---|
223 | |
---|
224 | return false; |
---|
225 | } |
---|
226 | |
---|
227 | //----------------------------------------------------------------------------// |
---|
228 | Object* Win32InputManager::createObject(InputManager* creator, Type iType, bool bufferMode, const std::string & vendor) |
---|
229 | { |
---|
230 | Object *obj = 0; |
---|
231 | |
---|
232 | switch(iType) |
---|
233 | { |
---|
234 | case OISKeyboard: |
---|
235 | { |
---|
236 | if( keyboardUsed == false ) |
---|
237 | obj = new Win32Keyboard(this, mDirectInput, bufferMode, kbSettings); |
---|
238 | break; |
---|
239 | } |
---|
240 | case OISMouse: |
---|
241 | { |
---|
242 | if( mouseUsed == false ) |
---|
243 | obj = new Win32Mouse(this, mDirectInput, bufferMode, mouseSettings); |
---|
244 | break; |
---|
245 | } |
---|
246 | case OISJoyStick: |
---|
247 | { |
---|
248 | for(JoyStickInfoList::iterator i = unusedJoyStickList.begin(); i != unusedJoyStickList.end(); ++i) |
---|
249 | { |
---|
250 | if(vendor == "" || i->vendor == vendor) |
---|
251 | { |
---|
252 | obj = new Win32JoyStick(this, mDirectInput, bufferMode, joySettings, *i); |
---|
253 | unusedJoyStickList.erase(i); |
---|
254 | break; |
---|
255 | } |
---|
256 | } |
---|
257 | break; |
---|
258 | } |
---|
259 | default: |
---|
260 | break; |
---|
261 | } |
---|
262 | |
---|
263 | if( obj == 0 ) |
---|
264 | OIS_EXCEPT(E_InputDeviceNonExistant, "No devices match requested type."); |
---|
265 | |
---|
266 | return obj; |
---|
267 | } |
---|
268 | |
---|
269 | //----------------------------------------------------------------------------// |
---|
270 | void Win32InputManager::destroyObject(Object* obj) |
---|
271 | { |
---|
272 | delete obj; |
---|
273 | } |
---|