2 * freeglut_joystick_mswin.c
\r
4 * The Windows-specific mouse cursor related stuff.
\r
6 * Copyright (c) 2012 Stephen J. Baker. All Rights Reserved.
\r
7 * Written by John F. Fay, <fayjf@sourceforge.net>
\r
8 * Creation date: Sat Jan 28, 2012
\r
10 * Permission is hereby granted, free of charge, to any person obtaining a
\r
11 * copy of this software and associated documentation files (the "Software"),
\r
12 * to deal in the Software without restriction, including without limitation
\r
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
\r
14 * and/or sell copies of the Software, and to permit persons to whom the
\r
15 * Software is furnished to do so, subject to the following conditions:
\r
17 * The above copyright notice and this permission notice shall be included
\r
18 * in all copies or substantial portions of the Software.
\r
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
\r
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
\r
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
\r
23 * PAWEL W. OLSZTA BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
\r
24 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
\r
25 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\r
28 #include <GL/freeglut.h>
\r
29 #include "../Common/freeglut_internal.h"
\r
32 #if !defined(_WIN32_WCE)
\r
33 # include <windows.h>
\r
34 # include <mmsystem.h>
\r
35 # include <regstr.h>
\r
40 void fgPlatformJoystickRawRead( SFG_Joystick* joy, int* buttons, float* axes )
\r
44 status = joyGetPosEx( joy->pJoystick.js_id, &joy->pJoystick.js );
\r
46 if ( status != JOYERR_NOERROR )
\r
48 joy->error = GL_TRUE;
\r
53 *buttons = joy->pJoystick.js.dwButtons;
\r
58 * WARNING - Fall through case clauses!!
\r
60 switch ( joy->num_axes )
\r
63 /* Generate two POV axes from the POV hat angle.
\r
64 * Low 16 bits of js.dwPOV gives heading (clockwise from ahead) in
\r
65 * hundredths of a degree, or 0xFFFF when idle.
\r
67 if ( ( joy->pJoystick.js.dwPOV & 0xFFFF ) == 0xFFFF )
\r
74 /* This is the contentious bit: how to convert angle to X/Y.
\r
75 * wk: I know of no define for PI that we could use here:
\r
76 * SG_PI would pull in sg, M_PI is undefined for MSVC
\r
77 * But the accuracy of the value of PI is very unimportant at
\r
80 float s = (float) sin ( ( joy->pJoystick.js.dwPOV & 0xFFFF ) * ( 0.01 * 3.1415926535f / 180.0f ) );
\r
81 float c = (float) cos ( ( joy->pJoystick.js.dwPOV & 0xFFFF ) * ( 0.01 * 3.1415926535f / 180.0f ) );
\r
83 /* Convert to coordinates on a square so that North-East
\r
84 * is (1,1) not (.7,.7), etc.
\r
85 * s and c cannot both be zero so we won't divide by zero.
\r
87 if ( fabs ( s ) < fabs ( c ) )
\r
89 axes [ 6 ] = ( c < 0.0 ) ? -s/c : s/c ;
\r
90 axes [ 7 ] = ( c < 0.0 ) ? -1.0f : 1.0f;
\r
94 axes [ 6 ] = ( s < 0.0 ) ? -1.0f : 1.0f;
\r
95 axes [ 7 ] = ( s < 0.0 ) ? -c/s : c/s ;
\r
99 case 6: axes[5] = (float) joy->pJoystick.js.dwVpos;
\r
100 case 5: axes[4] = (float) joy->pJoystick.js.dwUpos;
\r
101 case 4: axes[3] = (float) joy->pJoystick.js.dwRpos;
\r
102 case 3: axes[2] = (float) joy->pJoystick.js.dwZpos;
\r
103 case 2: axes[1] = (float) joy->pJoystick.js.dwYpos;
\r
104 case 1: axes[0] = (float) joy->pJoystick.js.dwXpos;
\r
112 http://msdn.microsoft.com/archive/en-us/dnargame/html/msdn_sidewind3d.asp
\r
114 # if FREEGLUT_LIB_PRAGMAS
\r
115 # pragma comment (lib, "advapi32.lib")
\r
118 static int fghJoystickGetOEMProductName ( SFG_Joystick* joy, char *buf, int buf_sz )
\r
120 char buffer [ 256 ];
\r
122 char OEMKey [ 256 ];
\r
131 /* Open .. MediaResources\CurrentJoystickSettings */
\r
132 _snprintf ( buffer, sizeof(buffer), "%s\\%s\\%s",
\r
133 REGSTR_PATH_JOYCONFIG, joy->pJoystick.jsCaps.szRegKey,
\r
134 REGSTR_KEY_JOYCURR );
\r
136 lr = RegOpenKeyEx ( HKEY_LOCAL_MACHINE, buffer, 0, KEY_QUERY_VALUE, &hKey);
\r
138 if ( lr != ERROR_SUCCESS ) return 0;
\r
140 /* Get OEM Key name */
\r
141 dwcb = sizeof(OEMKey);
\r
143 /* JOYSTICKID1-16 is zero-based; registry entries for VJOYD are 1-based. */
\r
144 _snprintf ( buffer, sizeof(buffer), "Joystick%d%s", joy->pJoystick.js_id + 1, REGSTR_VAL_JOYOEMNAME );
\r
146 lr = RegQueryValueEx ( hKey, buffer, 0, 0, (LPBYTE) OEMKey, &dwcb);
\r
147 RegCloseKey ( hKey );
\r
149 if ( lr != ERROR_SUCCESS ) return 0;
\r
151 /* Open OEM Key from ...MediaProperties */
\r
152 _snprintf ( buffer, sizeof(buffer), "%s\\%s", REGSTR_PATH_JOYOEM, OEMKey );
\r
154 lr = RegOpenKeyEx ( HKEY_LOCAL_MACHINE, buffer, 0, KEY_QUERY_VALUE, &hKey );
\r
156 if ( lr != ERROR_SUCCESS ) return 0;
\r
161 lr = RegQueryValueEx ( hKey, REGSTR_VAL_JOYOEMNAME, 0, 0, (LPBYTE) buf,
\r
163 RegCloseKey ( hKey );
\r
165 if ( lr != ERROR_SUCCESS ) return 0;
\r
171 void fgPlatformJoystickOpen( SFG_Joystick* joy )
\r
175 joy->pJoystick.js.dwFlags = JOY_RETURNALL;
\r
176 joy->pJoystick.js.dwSize = sizeof( joy->pJoystick.js );
\r
178 memset( &joy->pJoystick.jsCaps, 0, sizeof( joy->pJoystick.jsCaps ) );
\r
181 ( joyGetDevCaps( joy->pJoystick.js_id, &joy->pJoystick.jsCaps, sizeof( joy->pJoystick.jsCaps ) ) !=
\r
184 if( joy->pJoystick.jsCaps.wNumAxes == 0 )
\r
187 joy->error = GL_TRUE;
\r
191 /* Device name from jsCaps is often "Microsoft PC-joystick driver",
\r
192 * at least for USB. Try to get the real name from the registry.
\r
194 if ( ! fghJoystickGetOEMProductName( joy, joy->name,
\r
195 sizeof( joy->name ) ) )
\r
197 fgWarning( "JS: Failed to read joystick name from registry" );
\r
198 strncpy( joy->name, joy->pJoystick.jsCaps.szPname, sizeof( joy->name ) );
\r
201 /* Windows joystick drivers may provide any combination of
\r
202 * X,Y,Z,R,U,V,POV - not necessarily the first n of these.
\r
204 if( joy->pJoystick.jsCaps.wCaps & JOYCAPS_HASPOV )
\r
206 joy->num_axes = _JS_MAX_AXES;
\r
207 joy->min[ 7 ] = -1.0; joy->max[ 7 ] = 1.0; /* POV Y */
\r
208 joy->min[ 6 ] = -1.0; joy->max[ 6 ] = 1.0; /* POV X */
\r
213 joy->min[ 5 ] = ( float )joy->pJoystick.jsCaps.wVmin;
\r
214 joy->max[ 5 ] = ( float )joy->pJoystick.jsCaps.wVmax;
\r
215 joy->min[ 4 ] = ( float )joy->pJoystick.jsCaps.wUmin;
\r
216 joy->max[ 4 ] = ( float )joy->pJoystick.jsCaps.wUmax;
\r
217 joy->min[ 3 ] = ( float )joy->pJoystick.jsCaps.wRmin;
\r
218 joy->max[ 3 ] = ( float )joy->pJoystick.jsCaps.wRmax;
\r
219 joy->min[ 2 ] = ( float )joy->pJoystick.jsCaps.wZmin;
\r
220 joy->max[ 2 ] = ( float )joy->pJoystick.jsCaps.wZmax;
\r
221 joy->min[ 1 ] = ( float )joy->pJoystick.jsCaps.wYmin;
\r
222 joy->max[ 1 ] = ( float )joy->pJoystick.jsCaps.wYmax;
\r
223 joy->min[ 0 ] = ( float )joy->pJoystick.jsCaps.wXmin;
\r
224 joy->max[ 0 ] = ( float )joy->pJoystick.jsCaps.wXmax;
\r
227 /* Guess all the rest judging on the axes extremals */
\r
228 for( i = 0; i < joy->num_axes; i++ )
\r
230 joy->center [ i ] = ( joy->max[ i ] + joy->min[ i ] ) * 0.5f;
\r
231 joy->dead_band[ i ] = 0.0f;
\r
232 joy->saturate [ i ] = 1.0f;
\r
238 void fgPlatformJoystickInit( SFG_Joystick *fgJoystick[], int ident )
\r
243 fgJoystick[ ident ]->pJoystick.js_id = JOYSTICKID1;
\r
244 fgJoystick[ ident ]->error = GL_FALSE;
\r
247 fgJoystick[ ident ]->pJoystick.js_id = JOYSTICKID2;
\r
248 fgJoystick[ ident ]->error = GL_FALSE;
\r
251 fgJoystick[ ident ]->num_axes = 0;
\r
252 fgJoystick[ ident ]->error = GL_TRUE;
\r
259 void fgPlatformJoystickClose ( int ident )
\r
261 /* Do nothing special */
\r