* Better SDL joystick support (from Erik Auerswald)

This commit is contained in:
Tim Angus 2006-06-17 20:30:38 +00:00
parent 7d657ba62f
commit ce0100138c
2 changed files with 123 additions and 21 deletions

3
README
View file

@ -123,8 +123,9 @@ New cvars
com_altivec - enable use of altivec on PowerPC systems com_altivec - enable use of altivec on PowerPC systems
s_backend - read only, indicates the current sound s_backend - read only, indicates the current sound
backend backend
in_shiftedKeys - non-SDL Linux only. Enables binding to in_shiftedKeys - non-SDL Linux only; enables binding to
shifted keys shifted keys
in_joystickNo - SDL only; select which joystick to use
cl_consoleHistory - read only, stores the console history cl_consoleHistory - read only, stores the console history
cl_platformSensitivity - read only, indicates the mouse input cl_platformSensitivity - read only, indicates the mouse input
scaling scaling

View file

@ -1416,17 +1416,32 @@ static int joy_keys[16] = {
K_JOY26, K_JOY27 K_JOY26, K_JOY27
}; };
// translate hat events into keypresses
// the 4 highest buttons are used for the first hat ...
static int hat_keys[16] = {
K_JOY29, K_JOY30,
K_JOY31, K_JOY32,
K_JOY25, K_JOY26,
K_JOY27, K_JOY28,
K_JOY21, K_JOY22,
K_JOY23, K_JOY24,
K_JOY17, K_JOY18,
K_JOY19, K_JOY20
};
// bk001130 - from linux_glimp.c // bk001130 - from linux_glimp.c
extern cvar_t * in_joystick; extern cvar_t * in_joystick;
extern cvar_t * in_joystickDebug; extern cvar_t * in_joystickDebug;
extern cvar_t * joy_threshold; extern cvar_t * joy_threshold;
cvar_t *in_joystickNo;
#define ARRAYLEN(x) (sizeof (x) / sizeof (x[0])) #define ARRAYLEN(x) (sizeof (x) / sizeof (x[0]))
struct struct
{ {
qboolean buttons[16]; // !!! FIXME: these might be too many. qboolean buttons[16]; // !!! FIXME: these might be too many.
unsigned int oldaxes; unsigned int oldaxes;
unsigned int oldhats;
} stick_state; } stick_state;
@ -1466,14 +1481,19 @@ void IN_StartupJoystick( void )
for (i = 0; i < total; i++) for (i = 0; i < total; i++)
Com_Printf("[%d] %s\n", i, SDL_JoystickName(i)); Com_Printf("[%d] %s\n", i, SDL_JoystickName(i));
// !!! FIXME: someone should add a way to select a specific stick. in_joystickNo = Cvar_Get( "in_joystickNo", "0", CVAR_ARCHIVE );
for( i = 0; i < total; i++ ) { if( in_joystickNo->integer < 0 || in_joystickNo->integer >= total )
stick = SDL_JoystickOpen(i); Cvar_Set( "in_joystickNo", "0" );
if (stick == NULL)
continue;
Com_Printf( "Joystick %d opened\n", i ); stick = SDL_JoystickOpen( in_joystickNo->integer );
Com_Printf( "Name: %s\n", SDL_JoystickName(i) );
if (stick == NULL) {
Com_Printf( "No joystick opened.\n" );
return;
}
Com_Printf( "Joystick %d opened\n", in_joystickNo->integer );
Com_Printf( "Name: %s\n", SDL_JoystickName(in_joystickNo->integer) );
Com_Printf( "Axes: %d\n", SDL_JoystickNumAxes(stick) ); Com_Printf( "Axes: %d\n", SDL_JoystickNumAxes(stick) );
Com_Printf( "Hats: %d\n", SDL_JoystickNumHats(stick) ); Com_Printf( "Hats: %d\n", SDL_JoystickNumHats(stick) );
Com_Printf( "Buttons: %d\n", SDL_JoystickNumButtons(stick) ); Com_Printf( "Buttons: %d\n", SDL_JoystickNumButtons(stick) );
@ -1483,19 +1503,13 @@ void IN_StartupJoystick( void )
/* Our work here is done. */ /* Our work here is done. */
return; return;
}
/* No soup for you. */
if( stick == NULL ) {
Com_Printf( "No joystick opened.\n" );
return;
}
} }
void IN_JoyMove( void ) void IN_JoyMove( void )
{ {
qboolean joy_pressed[ARRAYLEN(joy_keys)]; qboolean joy_pressed[ARRAYLEN(joy_keys)];
unsigned int axes = 0; unsigned int axes = 0;
unsigned int hats = 0;
int total = 0; int total = 0;
int i = 0; int i = 0;
@ -1549,7 +1563,94 @@ void IN_JoyMove( void )
} }
} }
// !!! FIXME: look at the hats... // look at the hats...
total = SDL_JoystickNumHats(stick);
if (total > 0)
{
if (total > 4) total = 4;
for (i = 0; i < total; i++)
{
((Uint8 *)&hats)[i] = SDL_JoystickGetHat(stick, i);
}
}
// update hat state
if (hats != stick_state.oldhats)
{
for( i = 0; i < 4; i++ ) {
if( ((Uint8 *)&hats)[i] != ((Uint8 *)&stick_state.oldhats)[i] ) {
// release event
switch( ((Uint8 *)&stick_state.oldhats)[i] ) {
case SDL_HAT_UP:
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 0], qfalse, 0, NULL );
break;
case SDL_HAT_RIGHT:
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 1], qfalse, 0, NULL );
break;
case SDL_HAT_DOWN:
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 2], qfalse, 0, NULL );
break;
case SDL_HAT_LEFT:
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 3], qfalse, 0, NULL );
break;
case SDL_HAT_RIGHTUP:
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 0], qfalse, 0, NULL );
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 1], qfalse, 0, NULL );
break;
case SDL_HAT_RIGHTDOWN:
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 2], qfalse, 0, NULL );
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 1], qfalse, 0, NULL );
break;
case SDL_HAT_LEFTUP:
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 0], qfalse, 0, NULL );
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 3], qfalse, 0, NULL );
break;
case SDL_HAT_LEFTDOWN:
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 2], qfalse, 0, NULL );
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 3], qfalse, 0, NULL );
break;
default:
break;
}
// press event
switch( ((Uint8 *)&hats)[i] ) {
case SDL_HAT_UP:
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 0], qtrue, 0, NULL );
break;
case SDL_HAT_RIGHT:
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 1], qtrue, 0, NULL );
break;
case SDL_HAT_DOWN:
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 2], qtrue, 0, NULL );
break;
case SDL_HAT_LEFT:
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 3], qtrue, 0, NULL );
break;
case SDL_HAT_RIGHTUP:
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 0], qtrue, 0, NULL );
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 1], qtrue, 0, NULL );
break;
case SDL_HAT_RIGHTDOWN:
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 2], qtrue, 0, NULL );
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 1], qtrue, 0, NULL );
break;
case SDL_HAT_LEFTUP:
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 0], qtrue, 0, NULL );
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 3], qtrue, 0, NULL );
break;
case SDL_HAT_LEFTDOWN:
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 2], qtrue, 0, NULL );
Sys_QueEvent( 0, SE_KEY, hat_keys[4*i + 3], qtrue, 0, NULL );
break;
default:
break;
}
}
}
}
// save hat state
stick_state.oldhats = hats;
// finally, look at the axes... // finally, look at the axes...
total = SDL_JoystickNumAxes(stick); total = SDL_JoystickNumAxes(stick);