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