| 1 | /* |
| 2 | PLIB - A Suite of Portable Game Libraries |
| 3 | Copyright (C) 1998,2002 Steve Baker |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Library General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Library General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Library General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | |
| 19 | For further information visit http://plib.sourceforge.net |
| 20 | |
| 21 | $Id: jsWindows.cxx 2164 2011-01-22 22:47:30Z fayjf $ |
| 22 | */ |
| 23 | |
| 24 | #include <string> |
| 25 | |
| 26 | #include "FlightGear_js.h" |
| 27 | |
| 28 | #include <Windows.h> |
| 29 | |
| 30 | #include <cstring> |
| 31 | #include <RegStr.h> // for REGSTR_PATH_JOYCONFIG, etc |
| 32 | |
| 33 | #include <simgear/debug/logstream.hxx> |
| 34 | #include <simgear/structure/exception.hxx> |
| 35 | |
| 36 | #define _JS_MAX_AXES_WIN 8 /* X,Y,Z,R,U,V,POV_X,POV_Y */ |
| 37 | |
| 38 | struct os_specific_s { |
| 39 | JOYCAPS jsCaps ; |
| 40 | JOYINFOEX js ; |
| 41 | UINT js_id ; |
| 42 | static bool getOEMProductName ( jsJoystick* joy, char *buf, int buf_sz ) ; |
| 43 | }; |
| 44 | |
| 45 | // Give a human-readable interpretation of joyGetDevCaps()'s return value |
| 46 | static std::string joyGetDevCaps_errorString(MMRESULT errorCode) |
| 47 | { |
| 48 | switch (errorCode) { |
| 49 | case MMSYSERR_NODRIVER: |
| 50 | return "joystick driver not present, or specified joystick identifier is " |
| 51 | "invalid"; |
| 52 | case MMSYSERR_INVALPARAM: |
| 53 | return "invalid parameter passed to joyGetDevCaps()"; |
| 54 | // joyGetDevCaps() appears to return undocumented values, see |
| 55 | // https://sourceforge.net/p/flightgear/mailman/message/36657149/ |
| 56 | case MMSYSERR_BADDEVICEID: // fallthrough |
| 57 | case JOYERR_PARMS: |
| 58 | return "invalid joystick identifier"; |
| 59 | case JOYERR_UNPLUGGED: |
| 60 | return "joystick not connected to the system"; |
| 61 | case JOYERR_NOERROR: |
| 62 | return "no error"; |
| 63 | default: |
| 64 | // Don't throw an exception, since joyGetDevCaps()'s documentation isn't |
| 65 | // correct (we can't be sure to have covered all possible return values). |
| 66 | return "unexpected value passed to joyGetDevCaps_errorString(): " |
| 67 | + std::to_string(errorCode); |
| 68 | } |
| 69 | |
| 70 | throw sg_exception("This code path should be unreachable; value " |
| 71 | "passed to joyGetDevCaps_errorString(): " |
| 72 | + std::to_string(errorCode)); |
| 73 | } |
| 74 | |
| 75 | // Inspired by |
| 76 | // http://msdn.microsoft.com/archive/en-us/dnargame/html/msdn_sidewind3d.asp |
| 77 | |
| 78 | bool os_specific_s::getOEMProductName ( jsJoystick* joy, char *buf, int buf_sz ) |
| 79 | { |
| 80 | if ( joy->error ) return false ; |
| 81 | |
| 82 | union |
| 83 | { |
| 84 | char key [ 256 ] ; |
| 85 | char value [ 256 ] ; |
| 86 | } ; |
| 87 | char OEMKey [ 256 ] ; |
| 88 | |
| 89 | HKEY hKey ; |
| 90 | DWORD dwcb ; |
| 91 | LONG lr ; |
| 92 | int hkcu = 0; |
| 93 | |
| 94 | // Open .. MediaResources\CurrentJoystickSettings |
| 95 | sprintf ( key, "%s\\%s\\%s", |
| 96 | REGSTR_PATH_JOYCONFIG, joy->os->jsCaps.szRegKey, |
| 97 | REGSTR_KEY_JOYCURR ) ; |
| 98 | |
| 99 | lr = RegOpenKeyEx ( HKEY_LOCAL_MACHINE, key, 0, KEY_QUERY_VALUE, &hKey) ; |
| 100 | |
| 101 | if ( lr != ERROR_SUCCESS ) |
| 102 | { |
| 103 | hkcu = 1; |
| 104 | // XP/Vista/7 seem to have moved it to "current user" |
| 105 | lr = RegOpenKeyEx ( HKEY_CURRENT_USER, key, 0, KEY_QUERY_VALUE, &hKey) ; |
| 106 | if ( lr != ERROR_SUCCESS ) return false ; |
| 107 | } |
| 108 | |
| 109 | // Get OEM Key name |
| 110 | dwcb = sizeof(OEMKey) ; |
| 111 | |
| 112 | // JOYSTICKID1-16 is zero-based; registry entries for VJOYD are 1-based. |
| 113 | sprintf ( value, "Joystick%d%s", joy->os->js_id + 1, REGSTR_VAL_JOYOEMNAME ) ; |
| 114 | |
| 115 | lr = RegQueryValueEx ( hKey, value, 0, 0, (LPBYTE) OEMKey, &dwcb); |
| 116 | RegCloseKey ( hKey ) ; |
| 117 | |
| 118 | if ( lr != ERROR_SUCCESS ) return false ; |
| 119 | |
| 120 | // Open OEM Key from ...MediaProperties |
| 121 | sprintf ( key, "%s\\%s", REGSTR_PATH_JOYOEM, OEMKey ) ; |
| 122 | |
| 123 | if (!hkcu) |
| 124 | lr = RegOpenKeyEx ( HKEY_LOCAL_MACHINE, key, 0, KEY_QUERY_VALUE, &hKey) ; |
| 125 | else |
| 126 | lr = RegOpenKeyEx ( HKEY_CURRENT_USER, key, 0, KEY_QUERY_VALUE, &hKey) ; |
| 127 | if ( lr != ERROR_SUCCESS ) |
| 128 | { |
| 129 | return false ; |
| 130 | } |
| 131 | |
| 132 | // Get OEM Name |
| 133 | dwcb = buf_sz ; |
| 134 | |
| 135 | lr = RegQueryValueEx ( hKey, REGSTR_VAL_JOYOEMNAME, 0, 0, (LPBYTE) buf, |
| 136 | &dwcb ) ; |
| 137 | RegCloseKey ( hKey ) ; |
| 138 | |
| 139 | if ( lr != ERROR_SUCCESS ) return false ; |
| 140 | |
| 141 | return true ; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | void jsJoystick::open () |
| 146 | { |
| 147 | name [0] = '\0' ; |
| 148 | |
| 149 | os->js . dwFlags = JOY_RETURNALL ; |
| 150 | os->js . dwSize = sizeof ( os->js ) ; |
| 151 | |
| 152 | memset ( &(os->jsCaps), 0, sizeof(os->jsCaps) ) ; |
| 153 | |
| 154 | const auto joyGetDevCaps_res = joyGetDevCaps(os->js_id, &(os->jsCaps), |
| 155 | sizeof(os->jsCaps)); |
| 156 | error = (joyGetDevCaps_res != JOYERR_NOERROR); |
| 157 | |
| 158 | if (error) { |
| 159 | SG_LOG(SG_INPUT, SG_DEBUG, |
| 160 | "joyGetDevCaps reported: " |
| 161 | << joyGetDevCaps_errorString(joyGetDevCaps_res)); |
| 162 | } |
| 163 | |
| 164 | num_buttons = os->jsCaps.wNumButtons ; |
| 165 | if ( os->jsCaps.wNumAxes == 0 ) |
| 166 | { |
| 167 | SG_LOG(SG_INPUT, SG_DEBUG, |
| 168 | "Joystick reported zero axes currently in use (JOYCAPS.wNumAxes)"); |
| 169 | num_axes = 0 ; |
| 170 | setError () ; |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | // Device name from jsCaps is often "Microsoft PC-joystick driver", |
| 175 | // at least for USB. Try to get the real name from the registry. |
| 176 | if ( ! os->getOEMProductName ( this, name, sizeof(name) ) ) |
| 177 | { |
| 178 | jsSetError ( SG_WARN, |
| 179 | "JS: Failed to read joystick name from registry" ) ; |
| 180 | |
| 181 | strncpy ( name, os->jsCaps.szPname, sizeof(name) ) ; |
| 182 | } |
| 183 | |
| 184 | // Windows joystick drivers may provide any combination of |
| 185 | // X,Y,Z,R,U,V,POV - not necessarily the first n of these. |
| 186 | if ( os->jsCaps.wCaps & JOYCAPS_HASPOV ) |
| 187 | { |
| 188 | num_axes = _JS_MAX_AXES_WIN ; |
| 189 | min [ 7 ] = -1.0 ; max [ 7 ] = 1.0 ; // POV Y |
| 190 | min [ 6 ] = -1.0 ; max [ 6 ] = 1.0 ; // POV X |
| 191 | } |
| 192 | else |
| 193 | num_axes = 6 ; |
| 194 | |
| 195 | min [ 5 ] = (float) os->jsCaps.wVmin ; max [ 5 ] = (float) os->jsCaps.wVmax ; |
| 196 | min [ 4 ] = (float) os->jsCaps.wUmin ; max [ 4 ] = (float) os->jsCaps.wUmax ; |
| 197 | min [ 3 ] = (float) os->jsCaps.wRmin ; max [ 3 ] = (float) os->jsCaps.wRmax ; |
| 198 | min [ 2 ] = (float) os->jsCaps.wZmin ; max [ 2 ] = (float) os->jsCaps.wZmax ; |
| 199 | min [ 1 ] = (float) os->jsCaps.wYmin ; max [ 1 ] = (float) os->jsCaps.wYmax ; |
| 200 | min [ 0 ] = (float) os->jsCaps.wXmin ; max [ 0 ] = (float) os->jsCaps.wXmax ; |
| 201 | } |
| 202 | |
| 203 | for ( int i = 0 ; i < num_axes ; i++ ) |
| 204 | { |
| 205 | center [ i ] = ( max[i] + min[i] ) / 2.0f ; |
| 206 | dead_band [ i ] = 0.0f ; |
| 207 | saturate [ i ] = 1.0f ; |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | |
| 212 | void jsJoystick::close () |
| 213 | { |
| 214 | delete os; |
| 215 | } |
| 216 | |
| 217 | |
| 218 | |
| 219 | jsJoystick::jsJoystick ( int ident ) |
| 220 | { |
| 221 | id = ident ; |
| 222 | os = new struct os_specific_s; |
| 223 | |
| 224 | if (ident >= 0 && static_cast<unsigned int>(ident) < joyGetNumDevs()) { |
| 225 | os->js_id = JOYSTICKID1 + ident; |
| 226 | open(); |
| 227 | } |
| 228 | else { |
| 229 | SG_LOG(SG_INPUT, SG_DEBUG, |
| 230 | "Joystick identifier not in the range of valid ids"); |
| 231 | num_axes = 0; |
| 232 | setError(); |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | |
| 237 | |
| 238 | void jsJoystick::rawRead ( int *buttons, float *axes ) |
| 239 | { |
| 240 | if ( error ) |
| 241 | { |
| 242 | if ( buttons ) |
| 243 | *buttons = 0 ; |
| 244 | |
| 245 | if ( axes ) |
| 246 | for ( int i = 0 ; i < num_axes ; i++ ) |
| 247 | axes[i] = 1500.0f ; |
| 248 | |
| 249 | return ; |
| 250 | } |
| 251 | |
| 252 | MMRESULT status = joyGetPosEx ( os->js_id, &(os->js) ) ; |
| 253 | |
| 254 | if ( status != JOYERR_NOERROR ) |
| 255 | { |
| 256 | setError() ; |
| 257 | return ; |
| 258 | } |
| 259 | |
| 260 | if ( buttons != NULL ) |
| 261 | *buttons = (int) os->js.dwButtons ; |
| 262 | |
| 263 | if ( axes != NULL ) |
| 264 | { |
| 265 | /* WARNING - Fall through case clauses!! */ |
| 266 | |
| 267 | switch ( num_axes ) |
| 268 | { |
| 269 | case 8: |
| 270 | // Generate two POV axes from the POV hat angle. |
| 271 | // Low 16 bits of js.dwPOV gives heading (clockwise from ahead) in |
| 272 | // hundredths of a degree, or 0xFFFF when idle. |
| 273 | |
| 274 | if ( ( os->js.dwPOV & 0xFFFF ) == 0xFFFF ) |
| 275 | { |
| 276 | axes [ 6 ] = 0.0 ; |
| 277 | axes [ 7 ] = 0.0 ; |
| 278 | } |
| 279 | else |
| 280 | { |
| 281 | // This is the contentious bit: how to convert angle to X/Y. |
| 282 | // wk: I know of no define for PI that we could use here: |
| 283 | // SG_PI would pull in sg, M_PI is undefined for MSVC |
| 284 | // But the accuracy of the value of PI is very unimportant at |
| 285 | // this point. |
| 286 | |
| 287 | float s = (float) sin ( ( os->js.dwPOV & 0xFFFF ) * ( 0.01 * 3.1415926535f / 180 ) ) ; |
| 288 | float c = (float) cos ( ( os->js.dwPOV & 0xFFFF ) * ( 0.01 * 3.1415926535f / 180 ) ) ; |
| 289 | |
| 290 | // Convert to coordinates on a square so that North-East |
| 291 | // is (1,1) not (.7,.7), etc. |
| 292 | // s and c cannot both be zero so we won't divide by zero. |
| 293 | if ( fabs ( s ) < fabs ( c ) ) |
| 294 | { |
| 295 | axes [ 6 ] = ( c < 0.0 ) ? -s/c : s/c ; |
| 296 | axes [ 7 ] = ( c < 0.0 ) ? -1.0f : 1.0f ; |
| 297 | } |
| 298 | else |
| 299 | { |
| 300 | axes [ 6 ] = ( s < 0.0 ) ? -1.0f : 1.0f ; |
| 301 | axes [ 7 ] = ( s < 0.0 ) ? -c/s : c/s ; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | case 6: axes[5] = (float) os->js . dwVpos ; |
| 306 | case 5: axes[4] = (float) os->js . dwUpos ; |
| 307 | case 4: axes[3] = (float) os->js . dwRpos ; |
| 308 | case 3: axes[2] = (float) os->js . dwZpos ; |
| 309 | case 2: axes[1] = (float) os->js . dwYpos ; |
| 310 | case 1: axes[0] = (float) os->js . dwXpos ; |
| 311 | break; |
| 312 | |
| 313 | default: |
| 314 | jsSetError ( SG_WARN, "PLIB_JS: Wrong num_axes. Joystick input is now invalid" ) ; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | void jsInit() {} |