| 1 |
/* |
| 2 |
Copyright (C) 2009-2011,2015 wxLauncher Team |
| 3 |
|
| 4 |
This program is free software; you can redistribute it and/or |
| 5 |
modify it under the terms of the GNU General Public License |
| 6 |
as published by the Free Software Foundation; either version 2 |
| 7 |
of the License, or (at your option) any later version. |
| 8 |
|
| 9 |
This program is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
GNU General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU General Public License |
| 15 |
along with this program; if not, write to the Free Software |
| 16 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 |
*/ |
| 18 |
|
| 19 |
#include <wx/wx.h> |
| 20 |
|
| 21 |
#include "apis/JoystickManager.h" |
| 22 |
#include "generated/configure_launcher.h" |
| 23 |
|
| 24 |
#if USE_JOYSTICK && HAS_SDL |
| 25 |
#include "SDL.h" |
| 26 |
#endif |
| 27 |
|
| 28 |
#include "global/BasicDefaults.h" |
| 29 |
#include "global/MemoryDebugging.h" |
| 30 |
|
| 31 |
#include <vector> |
| 32 |
|
| 33 |
namespace JoyMan { |
| 34 |
#if USE_JOYSTICK |
| 35 |
ApiType currentApi = API_NATIVE; |
| 36 |
#if HAS_SDL |
| 37 |
bool isSdlInitialized = false; |
| 38 |
std::vector<SDL_Joystick*> sdlJoysticks; |
| 39 |
|
| 40 |
void clearSDLJoystickList() |
| 41 |
{ |
| 42 |
for (std::vector<SDL_Joystick*>::iterator iter = sdlJoysticks.begin(); iter != sdlJoysticks.end(); ++iter) |
| 43 |
{ |
| 44 |
SDL_JoystickClose(*iter); |
| 45 |
} |
| 46 |
sdlJoysticks.clear(); |
| 47 |
} |
| 48 |
#endif |
| 49 |
#if IS_WIN32 |
| 50 |
bool isWinInitialized = false; |
| 51 |
wxArrayString winJoysticks; |
| 52 |
#endif |
| 53 |
#endif |
| 54 |
}; |
| 55 |
|
| 56 |
/** \namespace JoyMan |
| 57 |
The JoyMan namespace contains helper functions for working with joysticks on |
| 58 |
the compiled platform. The interface of this namespace is always available |
| 59 |
but will only work if preprocessor symbol USE_JOYSTICK is set to 1, and of |
| 60 |
course if the platform supports it. |
| 61 |
\sa JoyMan::WasCompiledIn(); |
| 62 |
*/ |
| 63 |
|
| 64 |
using namespace JoyMan; |
| 65 |
|
| 66 |
/** \return true when JoyMan is ready to accept queries about joysticks. |
| 67 |
\note Always returns false when JoyMan is not compiled in. |
| 68 |
\sa JoyMan::WasCompiledIn() |
| 69 |
*/ |
| 70 |
bool JoyMan::IsInitialized() { |
| 71 |
#if USE_JOYSTICK |
| 72 |
#if HAS_SDL |
| 73 |
if (currentApi == API_SDL) |
| 74 |
{ |
| 75 |
return isSdlInitialized; |
| 76 |
} |
| 77 |
#endif |
| 78 |
#if IS_WIN32 |
| 79 |
if (currentApi == API_NATIVE) |
| 80 |
{ |
| 81 |
return isWinInitialized; |
| 82 |
} |
| 83 |
#endif |
| 84 |
|
| 85 |
return false; |
| 86 |
#else |
| 87 |
return false; |
| 88 |
#endif |
| 89 |
} |
| 90 |
|
| 91 |
/** Gets JoyMan ready to manage joysticks. |
| 92 |
\return true if successful, false otherwise. |
| 93 |
\note Will also return false if JoyMan is not compiled in. |
| 94 |
\sa JoyMan::WasCompiledIn() |
| 95 |
*/ |
| 96 |
bool JoyMan::Initialize(ApiType apiType) { |
| 97 |
currentApi = apiType; |
| 98 |
|
| 99 |
#if USE_JOYSTICK |
| 100 |
if ( JoyMan::IsInitialized() ) { |
| 101 |
wxLogDebug(_T("JoyMan already initialized")); |
| 102 |
return true; |
| 103 |
} |
| 104 |
#else |
| 105 |
wxLogDebug(wxT_2("JoyMan was disabled at compile time")); |
| 106 |
return false; |
| 107 |
#endif |
| 108 |
|
| 109 |
#if USE_JOYSTICK |
| 110 |
#if IS_WIN32 |
| 111 |
if (currentApi == API_NATIVE) |
| 112 |
{ |
| 113 |
UINT num = joyGetNumDevs(); // get the number of joys supported by windows. |
| 114 |
if (num > 16) { |
| 115 |
/* greater than 16 is cause for a warning because according to MSDN, |
| 116 |
Windows 2000 and later support -1 thru 16 in the MM api (and windows NT |
| 117 |
only supports 1 or 2). MSDN also notes if you want more than 16 use |
| 118 |
DirectInput. */ |
| 119 |
wxLogWarning(_T("Windows reports that the joystick driver ") |
| 120 |
_T("supports more than 16 joysticks (reports %d)!"), num); |
| 121 |
} |
| 122 |
|
| 123 |
MMRESULT result = JOYERR_NOERROR; |
| 124 |
JOYINFO joyinfo; |
| 125 |
JOYCAPS joycaps; |
| 126 |
JoyMan::winJoysticks.clear(); |
| 127 |
int totalNumberOfJoysticks = 0; |
| 128 |
|
| 129 |
for (UINT counter = 0; counter < num; counter++) { |
| 130 |
memset(reinterpret_cast<void*>(&joyinfo), 0, sizeof(JOYINFO)); |
| 131 |
|
| 132 |
result = joyGetPos(counter, &joyinfo); |
| 133 |
wxString joystickName; |
| 134 |
|
| 135 |
if (result == JOYERR_NOERROR) { |
| 136 |
// joystick plugged in |
| 137 |
totalNumberOfJoysticks++; |
| 138 |
memset(reinterpret_cast<void*>(&joycaps), 0, sizeof(JOYCAPS)); |
| 139 |
|
| 140 |
result = joyGetDevCaps(counter, &joycaps, sizeof(JOYCAPS)); |
| 141 |
if (result == JOYERR_NOERROR) { |
| 142 |
joystickName = wxString(joycaps.szPname, wxMBConvUTF16()); |
| 143 |
winJoysticks.Add(joystickName); |
| 144 |
} |
| 145 |
else { |
| 146 |
wxLogError(_T("Error in retrieving joystick caps")); |
| 147 |
continue; |
| 148 |
} |
| 149 |
} |
| 150 |
else if (result == JOYERR_UNPLUGGED) { |
| 151 |
// unplugged |
| 152 |
totalNumberOfJoysticks++; |
| 153 |
} |
| 154 |
else { |
| 155 |
// Joystick doesn't exist, do nothing |
| 156 |
} |
| 157 |
} |
| 158 |
wxLogInfo(_T("Windows reports %d joysticks, %d seem to be plugged in."), |
| 159 |
totalNumberOfJoysticks, static_cast<int>(winJoysticks.size())); |
| 160 |
return true; |
| 161 |
} |
| 162 |
#endif |
| 163 |
|
| 164 |
#if HAS_SDL |
| 165 |
if (currentApi == API_SDL) |
| 166 |
{ |
| 167 |
if (SDL_InitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC) < 0) { |
| 168 |
wxLogError(wxT_2("SDL Joystick failed to initialize.")); |
| 169 |
return false; |
| 170 |
} |
| 171 |
|
| 172 |
JoyMan::clearSDLJoystickList(); |
| 173 |
|
| 174 |
for (int i = 0; i < SDL_NumJoysticks(); i++) { |
| 175 |
SDL_Joystick* joy = SDL_JoystickOpen(i); |
| 176 |
if (joy != NULL) { |
| 177 |
sdlJoysticks.push_back(joy); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
return true; |
| 182 |
} |
| 183 |
#endif |
| 184 |
|
| 185 |
return false; |
| 186 |
#else |
| 187 |
return false; |
| 188 |
#endif |
| 189 |
} |
| 190 |
|
| 191 |
/** Dismantles JoyMan and frees any memory used. |
| 192 |
\note Will always return true when JoyMan is not compiled in. |
| 193 |
\return Return true if DeInitializtion was successful. |
| 194 |
*/ |
| 195 |
bool JoyMan::DeInitialize() { |
| 196 |
#if USE_JOYSTICK |
| 197 |
#if HAS_SDL |
| 198 |
if ( isSdlInitialized ) { |
| 199 |
JoyMan::isSdlInitialized = false; |
| 200 |
JoyMan::sdlJoysticks.clear(); |
| 201 |
JoyMan::clearSDLJoystickList(); |
| 202 |
|
| 203 |
SDL_QuitSubSystem(SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC); |
| 204 |
} |
| 205 |
#endif |
| 206 |
#if IS_WIN32 |
| 207 |
if (isWinInitialized) |
| 208 |
{ |
| 209 |
JoyMan::isWinInitialized = false; |
| 210 |
JoyMan::winJoysticks.clear(); |
| 211 |
} |
| 212 |
#endif |
| 213 |
#endif |
| 214 |
return true; |
| 215 |
} |
| 216 |
/** \return true if the internals of JoyMan were compiled into the launcher. */ |
| 217 |
bool JoyMan::WasCompiledIn() { |
| 218 |
#if USE_JOYSTICK |
| 219 |
return true; |
| 220 |
#else |
| 221 |
return false; |
| 222 |
#endif |
| 223 |
} |
| 224 |
|
| 225 |
/** \return number of joysticks that the system reports as existing. |
| 226 |
\sa JoyMan::IsPluggedIn() */ |
| 227 |
unsigned int JoyMan::NumberOfJoysticks() { |
| 228 |
#if USE_JOYSTICK |
| 229 |
#if HAS_SDL |
| 230 |
if (currentApi == API_SDL) |
| 231 |
{ |
| 232 |
return sdlJoysticks.size(); |
| 233 |
} |
| 234 |
#endif |
| 235 |
#if IS_WIN32 |
| 236 |
if (currentApi == API_NATIVE) |
| 237 |
{ |
| 238 |
return winJoysticks.size(); |
| 239 |
} |
| 240 |
#endif |
| 241 |
return 0; |
| 242 |
#else |
| 243 |
return 0; |
| 244 |
#endif |
| 245 |
} |
| 246 |
|
| 247 |
/** \return number of joystsicks that are plugged in. |
| 248 |
\sa JoyMan::NumberOfJoysticks() |
| 249 |
\sa JoyMan::IsPluggedIn() */ |
| 250 |
unsigned int JoyMan::NumberOfPluggedInJoysticks() { |
| 251 |
unsigned int total = JoyMan::NumberOfJoysticks(); |
| 252 |
unsigned int pluggedIn = 0; |
| 253 |
for ( unsigned int i = 0; i < total; i++) { |
| 254 |
if ( JoyMan::IsJoystickPluggedIn(i) ) { |
| 255 |
pluggedIn++; |
| 256 |
} |
| 257 |
} |
| 258 |
return pluggedIn; |
| 259 |
} |
| 260 |
|
| 261 |
/** \return @c true when the joystick supports force feedback, @c false otherwise */ |
| 262 |
#if USE_JOYSTICK |
| 263 |
bool JoyMan::SupportsForceFeedback(unsigned int i) { |
| 264 |
if ( i == static_cast<unsigned int>(DEFAULT_JOYSTICK_ID) ) { |
| 265 |
return false; |
| 266 |
} else { |
| 267 |
#if HAS_SDL |
| 268 |
if (currentApi == API_SDL) |
| 269 |
{ |
| 270 |
if (sdlJoysticks.size() <= i) { |
| 271 |
return false; |
| 272 |
} |
| 273 |
else { |
| 274 |
SDL_Haptic* haptic = SDL_HapticOpenFromJoystick(sdlJoysticks[i]); |
| 275 |
bool hasForceFeedback = haptic != NULL; |
| 276 |
|
| 277 |
if (hasForceFeedback) |
| 278 |
{ |
| 279 |
SDL_HapticClose(haptic); |
| 280 |
} |
| 281 |
|
| 282 |
return hasForceFeedback; |
| 283 |
} |
| 284 |
} |
| 285 |
#endif |
| 286 |
return true; |
| 287 |
} |
| 288 |
#else |
| 289 |
bool JoyMan::SupportsForceFeedback(unsigned int) { |
| 290 |
return false; |
| 291 |
#endif |
| 292 |
} |
| 293 |
|
| 294 |
/** \bug Assumes all joysticks have a calibrate tool. */ |
| 295 |
#if USE_JOYSTICK |
| 296 |
bool JoyMan::HasCalibrateTool(unsigned int i) { |
| 297 |
if ( i == static_cast<unsigned int>(DEFAULT_JOYSTICK_ID) ) { |
| 298 |
return false; |
| 299 |
} else { |
| 300 |
return true; |
| 301 |
} |
| 302 |
#else |
| 303 |
bool JoyMan::HasCalibrateTool(unsigned int) { |
| 304 |
return false; |
| 305 |
#endif |
| 306 |
} |
| 307 |
|
| 308 |
/** Launch the windows joystick calibration tool. */ |
| 309 |
void JoyMan::LaunchCalibrateTool(unsigned int WXUNUSED(i)) { |
| 310 |
#if IS_WIN32 |
| 311 |
// the same call that the current window launcher uses to open the |
| 312 |
// calibration tool. Obviously it only works on windows :) |
| 313 |
WinExec("rundll32.exe shell32.dll,Control_RunDLL joy.cpl", SW_SHOWNORMAL); |
| 314 |
#endif |
| 315 |
} |
| 316 |
|
| 317 |
/** Returns the name of the joystick for display. */ |
| 318 |
#if USE_JOYSTICK |
| 319 |
wxString JoyMan::JoystickName(unsigned int i) { |
| 320 |
#if HAS_SDL |
| 321 |
if (currentApi == API_SDL) |
| 322 |
{ |
| 323 |
if (sdlJoysticks.size() <= i) { |
| 324 |
return wxEmptyString; |
| 325 |
} |
| 326 |
else { |
| 327 |
const char* name = SDL_JoystickName(sdlJoysticks[i]); |
| 328 |
if (name != NULL) |
| 329 |
{ |
| 330 |
return wxString::FromUTF8(name); |
| 331 |
} else |
| 332 |
{ |
| 333 |
return wxEmptyString; |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
#endif |
| 338 |
#if IS_WIN32 |
| 339 |
if (currentApi == API_NATIVE) |
| 340 |
{ |
| 341 |
return winJoysticks[i]; |
| 342 |
} |
| 343 |
#endif |
| 344 |
return wxEmptyString; |
| 345 |
#else |
| 346 |
wxString JoyMan::JoystickName(unsigned int) { |
| 347 |
return wxEmptyString; |
| 348 |
#endif |
| 349 |
} |
| 350 |
|
| 351 |
wxString JoyMan::JoystickGUID(unsigned int i) |
| 352 |
{ |
| 353 |
#if USE_JOYSTICK |
| 354 |
#if HAS_SDL |
| 355 |
if (currentApi == API_SDL) |
| 356 |
{ |
| 357 |
if (sdlJoysticks.size() <= i) { |
| 358 |
return wxEmptyString; |
| 359 |
} else { |
| 360 |
SDL_JoystickGUID guid = SDL_JoystickGetGUID(sdlJoysticks[i]); |
| 361 |
|
| 362 |
char guidStr[33]; |
| 363 |
SDL_JoystickGetGUIDString(guid, guidStr, 33); |
| 364 |
return wxString::FromAscii(guidStr); |
| 365 |
} |
| 366 |
} |
| 367 |
#endif |
| 368 |
#if IS_WIN32 |
| 369 |
if (currentApi == API_NATIVE) |
| 370 |
{ |
| 371 |
// No GUIDs on windows |
| 372 |
return wxEmptyString; |
| 373 |
} |
| 374 |
#endif |
| 375 |
return wxEmptyString; |
| 376 |
#else |
| 377 |
return wxEmptyString; |
| 378 |
#endif |
| 379 |
} |
| 380 |
|
| 381 |
/** Returns true when the joystick is plugged in. */ |
| 382 |
#if USE_JOYSTICK |
| 383 |
bool JoyMan::IsJoystickPluggedIn(unsigned int i) { |
| 384 |
if ( i == static_cast<unsigned int>(DEFAULT_JOYSTICK_ID) ) { |
| 385 |
return false; |
| 386 |
} else { |
| 387 |
// FIXME because we're using indexes to represent joysticks, there's no guarantee |
| 388 |
// that an index value always refers to the same joystick |
| 389 |
return i >= JoyMan::NumberOfJoysticks() ? false : !JoyMan::JoystickName(i).IsEmpty(); |
| 390 |
} |
| 391 |
#else |
| 392 |
bool JoyMan::IsJoystickPluggedIn(unsigned int) { |
| 393 |
return false; |
| 394 |
#endif |
| 395 |
} |