Conditional joystick support

git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@2401 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
avolkov
2006-08-08 22:23:59 +00:00
parent d6c5443469
commit f5c15f10de
2 changed files with 62 additions and 2 deletions
+3 -1
View File
@@ -203,7 +203,8 @@ TFB_InitInput (int driver, int flags)
GamePaused = ExitRequested = FALSE; GamePaused = ExitRequested = FALSE;
SDL_EnableUNICODE(1); SDL_EnableUNICODE(1);
#ifdef HAVE_JOYSTICK
if ((SDL_InitSubSystem(SDL_INIT_JOYSTICK)) == -1) if ((SDL_InitSubSystem(SDL_INIT_JOYSTICK)) == -1)
{ {
log_add (log_Fatal, "Couldn't initialize joystick subsystem: %s", log_add (log_Fatal, "Couldn't initialize joystick subsystem: %s",
@@ -223,6 +224,7 @@ TFB_InitInput (int driver, int flags)
} }
SDL_JoystickEventState (SDL_ENABLE); SDL_JoystickEventState (SDL_ENABLE);
} }
#endif /* HAVE_JOYSTICK */
_in_character_mode = FALSE; _in_character_mode = FALSE;
resetKeyboardState (); resetKeyboardState ();
+59 -1
View File
@@ -23,6 +23,9 @@ typedef struct vcontrol_keypool {
struct vcontrol_keypool *next; struct vcontrol_keypool *next;
} keypool; } keypool;
#ifdef HAVE_JOYSTICK
typedef struct vcontrol_joystick_axis { typedef struct vcontrol_joystick_axis {
keybinding *neg, *pos; keybinding *neg, *pos;
int polarity; int polarity;
@@ -42,9 +45,12 @@ typedef struct vcontrol_joystick {
hat_type *hats; hat_type *hats;
} joystick; } joystick;
static keybinding *bindings[SDLK_LAST];
static joystick *joysticks; static joystick *joysticks;
#endif /* HAVE_JOYSTICK */
static int joycount; static int joycount;
static keybinding *bindings[SDLK_LAST];
static keypool *pool; static keypool *pool;
static VControl_NameBinding *nametable; static VControl_NameBinding *nametable;
@@ -91,6 +97,8 @@ free_key_pool (keypool *x)
} }
} }
#ifdef HAVE_JOYSTICK
static void static void
create_joystick (int index) create_joystick (int index)
{ {
@@ -162,6 +170,8 @@ destroy_joystick (int index)
} }
} }
#endif /* HAVE_JOYSTICK */
static void static void
key_init (void) key_init (void)
{ {
@@ -169,6 +179,8 @@ key_init (void)
pool = allocate_key_chunk (); pool = allocate_key_chunk ();
for (i = 0; i < SDLK_LAST; i++) for (i = 0; i < SDLK_LAST; i++)
bindings[i] = NULL; bindings[i] = NULL;
#ifdef HAVE_JOYSTICK
/* Prepare for possible joystick controls. We don't actually /* Prepare for possible joystick controls. We don't actually
GRAB joysticks unless we're asked to make a joystick GRAB joysticks unless we're asked to make a joystick
binding, though. */ binding, though. */
@@ -188,6 +200,9 @@ key_init (void)
{ {
joysticks = NULL; joysticks = NULL;
} }
#else
joycount = 0;
#endif /* HAVE_JOYSTICK */
} }
static void static void
@@ -198,9 +213,12 @@ key_uninit (void)
for (i = 0; i < SDLK_LAST; i++) for (i = 0; i < SDLK_LAST; i++)
bindings[i] = NULL; bindings[i] = NULL;
pool = NULL; pool = NULL;
#ifdef HAVE_JOYSTICK
for (i = 0; i < joycount; i++) for (i = 0; i < joycount; i++)
destroy_joystick (i); destroy_joystick (i);
vctrl_free (joysticks); vctrl_free (joysticks);
#endif /* HAVE_JOYSTICK */
} }
static void static void
@@ -232,12 +250,14 @@ VControl_Uninit (void)
int int
VControl_SetJoyThreshold (int port, int threshold) VControl_SetJoyThreshold (int port, int threshold)
{ {
#ifdef HAVE_JOYSTICK
if (port >= 0 && port < joycount) if (port >= 0 && port < joycount)
{ {
joysticks[port].threshold = threshold; joysticks[port].threshold = threshold;
return 0; return 0;
} }
else else
#endif /* HAVE_JOYSTICK */
{ {
// log_add (log_Warning, "VControl_SetJoyThreshold passed illegal port %d", port); // log_add (log_Warning, "VControl_SetJoyThreshold passed illegal port %d", port);
return -1; return -1;
@@ -369,6 +389,8 @@ VControl_AddBinding (SDL_Event *e, int *target)
case SDL_KEYDOWN: case SDL_KEYDOWN:
result = VControl_AddKeyBinding (e->key.keysym.sym, target); result = VControl_AddKeyBinding (e->key.keysym.sym, target);
break; break;
#ifdef HAVE_JOYSTICK
case SDL_JOYAXISMOTION: case SDL_JOYAXISMOTION:
result = VControl_AddJoyAxisBinding (e->jaxis.which, e->jaxis.axis, (e->jaxis.value < 0) ? -1 : 1, target); result = VControl_AddJoyAxisBinding (e->jaxis.which, e->jaxis.axis, (e->jaxis.value < 0) ? -1 : 1, target);
break; break;
@@ -378,6 +400,8 @@ VControl_AddBinding (SDL_Event *e, int *target)
case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONDOWN:
result = VControl_AddJoyButtonBinding (e->jbutton.which, e->jbutton.button, target); result = VControl_AddJoyButtonBinding (e->jbutton.which, e->jbutton.button, target);
break; break;
#endif /* HAVE_JOYSTICK */
default: default:
log_add (log_Warning, "VControl_AddBinding didn't understand argument event"); log_add (log_Warning, "VControl_AddBinding didn't understand argument event");
result = -1; result = -1;
@@ -394,6 +418,8 @@ VControl_RemoveBinding (SDL_Event *e, int *target)
case SDL_KEYDOWN: case SDL_KEYDOWN:
VControl_RemoveKeyBinding (e->key.keysym.sym, target); VControl_RemoveKeyBinding (e->key.keysym.sym, target);
break; break;
#ifdef HAVE_JOYSTICK
case SDL_JOYAXISMOTION: case SDL_JOYAXISMOTION:
VControl_RemoveJoyAxisBinding (e->jaxis.which, e->jaxis.axis, (e->jaxis.value < 0) ? -1 : 1, target); VControl_RemoveJoyAxisBinding (e->jaxis.which, e->jaxis.axis, (e->jaxis.value < 0) ? -1 : 1, target);
break; break;
@@ -403,6 +429,8 @@ VControl_RemoveBinding (SDL_Event *e, int *target)
case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONDOWN:
VControl_RemoveJoyButtonBinding (e->jbutton.which, e->jbutton.button, target); VControl_RemoveJoyButtonBinding (e->jbutton.which, e->jbutton.button, target);
break; break;
#endif /* HAVE_JOYSTICK */
default: default:
log_add (log_Warning, "VControl_RemoveBinding didn't understand argument event"); log_add (log_Warning, "VControl_RemoveBinding didn't understand argument event");
break; break;
@@ -433,6 +461,7 @@ VControl_RemoveKeyBinding (SDLKey symbol, int *target)
int int
VControl_AddJoyAxisBinding (int port, int axis, int polarity, int *target) VControl_AddJoyAxisBinding (int port, int axis, int polarity, int *target)
{ {
#ifdef HAVE_JOYSTICK
if (port >= 0 && port < joycount) if (port >= 0 && port < joycount)
{ {
joystick *j = &joysticks[port]; joystick *j = &joysticks[port];
@@ -461,6 +490,7 @@ VControl_AddJoyAxisBinding (int port, int axis, int polarity, int *target)
} }
} }
else else
#endif /* HAVE_JOYSTICK */
{ {
// log_add (log_Debug, "VControl: Attempted to bind to illegal port %d", port); // log_add (log_Debug, "VControl: Attempted to bind to illegal port %d", port);
return -1; return -1;
@@ -471,6 +501,7 @@ VControl_AddJoyAxisBinding (int port, int axis, int polarity, int *target)
void void
VControl_RemoveJoyAxisBinding (int port, int axis, int polarity, int *target) VControl_RemoveJoyAxisBinding (int port, int axis, int polarity, int *target)
{ {
#ifdef HAVE_JOYSTICK
if (port >= 0 && port < joycount) if (port >= 0 && port < joycount)
{ {
joystick *j = &joysticks[port]; joystick *j = &joysticks[port];
@@ -497,6 +528,7 @@ VControl_RemoveJoyAxisBinding (int port, int axis, int polarity, int *target)
} }
} }
else else
#endif /* HAVE_JOYSTICK */
{ {
log_add (log_Debug, "VControl: Attempted to unbind from illegal port %d", port); log_add (log_Debug, "VControl: Attempted to unbind from illegal port %d", port);
} }
@@ -505,6 +537,7 @@ VControl_RemoveJoyAxisBinding (int port, int axis, int polarity, int *target)
int int
VControl_AddJoyButtonBinding (int port, int button, int *target) VControl_AddJoyButtonBinding (int port, int button, int *target)
{ {
#ifdef HAVE_JOYSTICK
if (port >= 0 && port < joycount) if (port >= 0 && port < joycount)
{ {
joystick *j = &joysticks[port]; joystick *j = &joysticks[port];
@@ -522,6 +555,7 @@ VControl_AddJoyButtonBinding (int port, int button, int *target)
} }
} }
else else
#endif /* HAVE_JOYSTICK */
{ {
// log_add (log_Debug, "VControl: Attempted to bind to illegal port %d", port); // log_add (log_Debug, "VControl: Attempted to bind to illegal port %d", port);
return -1; return -1;
@@ -531,6 +565,7 @@ VControl_AddJoyButtonBinding (int port, int button, int *target)
void void
VControl_RemoveJoyButtonBinding (int port, int button, int *target) VControl_RemoveJoyButtonBinding (int port, int button, int *target)
{ {
#ifdef HAVE_JOYSTICK
if (port >= 0 && port < joycount) if (port >= 0 && port < joycount)
{ {
joystick *j = &joysticks[port]; joystick *j = &joysticks[port];
@@ -546,6 +581,7 @@ VControl_RemoveJoyButtonBinding (int port, int button, int *target)
} }
} }
else else
#endif /* HAVE_JOYSTICK */
{ {
log_add (log_Debug, "VControl: Attempted to unbind from illegal port %d", port); log_add (log_Debug, "VControl: Attempted to unbind from illegal port %d", port);
} }
@@ -554,6 +590,7 @@ VControl_RemoveJoyButtonBinding (int port, int button, int *target)
int int
VControl_AddJoyHatBinding (int port, int which, Uint8 dir, int *target) VControl_AddJoyHatBinding (int port, int which, Uint8 dir, int *target)
{ {
#ifdef HAVE_JOYSTICK
if (port >= 0 && port < joycount) if (port >= 0 && port < joycount)
{ {
joystick *j = &joysticks[port]; joystick *j = &joysticks[port];
@@ -591,6 +628,7 @@ VControl_AddJoyHatBinding (int port, int which, Uint8 dir, int *target)
} }
} }
else else
#endif /* HAVE_JOYSTICK */
{ {
// log_add (log_Debug, "VControl: Attempted to bind to illegal port %d", port); // log_add (log_Debug, "VControl: Attempted to bind to illegal port %d", port);
return -1; return -1;
@@ -600,6 +638,7 @@ VControl_AddJoyHatBinding (int port, int which, Uint8 dir, int *target)
void void
VControl_RemoveJoyHatBinding (int port, int which, Uint8 dir, int *target) VControl_RemoveJoyHatBinding (int port, int which, Uint8 dir, int *target)
{ {
#ifdef HAVE_JOYSTICK
if (port >= 0 && port < joycount) if (port >= 0 && port < joycount)
{ {
joystick *j = &joysticks[port]; joystick *j = &joysticks[port];
@@ -634,6 +673,7 @@ VControl_RemoveJoyHatBinding (int port, int which, Uint8 dir, int *target)
} }
} }
else else
#endif /* HAVE_JOYSTICK */
{ {
log_add (log_Debug, "VControl: Attempted to unbind from illegal port %d", port); log_add (log_Debug, "VControl: Attempted to unbind from illegal port %d", port);
} }
@@ -661,22 +701,27 @@ VControl_ProcessKeyUp (SDLKey symbol)
void void
VControl_ProcessJoyButtonDown (int port, int button) VControl_ProcessJoyButtonDown (int port, int button)
{ {
#ifdef HAVE_JOYSTICK
if (!joysticks[port].stick) if (!joysticks[port].stick)
return; return;
activate (joysticks[port].buttons[button]); activate (joysticks[port].buttons[button]);
#endif /* HAVE_JOYSTICK */
} }
void void
VControl_ProcessJoyButtonUp (int port, int button) VControl_ProcessJoyButtonUp (int port, int button)
{ {
#ifdef HAVE_JOYSTICK
if (!joysticks[port].stick) if (!joysticks[port].stick)
return; return;
deactivate (joysticks[port].buttons[button]); deactivate (joysticks[port].buttons[button]);
#endif /* HAVE_JOYSTICK */
} }
void void
VControl_ProcessJoyAxis (int port, int axis, int value) VControl_ProcessJoyAxis (int port, int axis, int value)
{ {
#ifdef HAVE_JOYSTICK
int t; int t;
if (!joysticks[port].stick) if (!joysticks[port].stick)
return; return;
@@ -717,11 +762,13 @@ VControl_ProcessJoyAxis (int port, int axis, int value)
} }
joysticks[port].axes[axis].polarity = 0; joysticks[port].axes[axis].polarity = 0;
} }
#endif /* HAVE_JOYSTICK */
} }
void void
VControl_ProcessJoyHat (int port, int which, Uint8 value) VControl_ProcessJoyHat (int port, int which, Uint8 value)
{ {
#ifdef HAVE_JOYSTICK
Uint8 old; Uint8 old;
if (!joysticks[port].stick) if (!joysticks[port].stick)
return; return;
@@ -743,6 +790,7 @@ VControl_ProcessJoyHat (int port, int which, Uint8 value)
if ((old & SDL_HAT_DOWN) && !(value & SDL_HAT_DOWN)) if ((old & SDL_HAT_DOWN) && !(value & SDL_HAT_DOWN))
deactivate (joysticks[port].hats[which].down); deactivate (joysticks[port].hats[which].down);
joysticks[port].hats[which].last = value; joysticks[port].hats[which].last = value;
#endif /* HAVE_JOYSTICK */
} }
void void
@@ -780,6 +828,8 @@ VControl_HandleEvent (const SDL_Event *e)
case SDL_KEYUP: case SDL_KEYUP:
VControl_ProcessKeyUp (e->key.keysym.sym); VControl_ProcessKeyUp (e->key.keysym.sym);
break; break;
#ifdef HAVE_JOYSTICK
case SDL_JOYAXISMOTION: case SDL_JOYAXISMOTION:
VControl_ProcessJoyAxis (e->jaxis.which, e->jaxis.axis, e->jaxis.value); VControl_ProcessJoyAxis (e->jaxis.which, e->jaxis.axis, e->jaxis.value);
break; break;
@@ -796,6 +846,8 @@ VControl_HandleEvent (const SDL_Event *e)
case SDL_JOYBUTTONUP: case SDL_JOYBUTTONUP:
VControl_ProcessJoyButtonUp (e->jbutton.which, e->jbutton.button); VControl_ProcessJoyButtonUp (e->jbutton.which, e->jbutton.button);
break; break;
#endif /* HAVE_JOYSTICK */
default: default:
break; break;
} }
@@ -865,6 +917,7 @@ VControl_Dump (FILE *out)
} }
} }
#ifdef HAVE_JOYSTICK
/* Print out joystick bindings */ /* Print out joystick bindings */
for (i = 0; i < joycount; i++) for (i = 0; i < joycount; i++)
{ {
@@ -902,6 +955,7 @@ VControl_Dump (FILE *out)
} }
} }
} }
#endif /* HAVE_JOYSTICK */
#ifdef VCONTROL_DEBUG #ifdef VCONTROL_DEBUG
/* Print out allocation data */ /* Print out allocation data */
@@ -970,6 +1024,7 @@ VControl_NextBinding (VCONTROL_GESTURE *gesture)
} }
if (done) return 1; if (done) return 1;
} }
#ifdef HAVE_JOYSTICK
else else
{ {
int i = iter_device / 3; int i = iter_device / 3;
@@ -1074,6 +1129,7 @@ VControl_NextBinding (VCONTROL_GESTURE *gesture)
if (done) return 1; if (done) return 1;
} }
} }
#endif /* HAVE_JOYSTICK */
} }
return 0; return 0;
} }
@@ -1321,6 +1377,7 @@ static Uint8
consume_dir (parse_state *state) consume_dir (parse_state *state)
{ {
Uint8 result = 0; Uint8 result = 0;
#ifdef HAVE_JOYSTICK
if (!stricmp (state->token, "left")) if (!stricmp (state->token, "left"))
{ {
result = SDL_HAT_LEFT; result = SDL_HAT_LEFT;
@@ -1341,6 +1398,7 @@ consume_dir (parse_state *state)
{ {
expected_error (state, "left', 'right', 'up' or 'down"); expected_error (state, "left', 'right', 'up' or 'down");
} }
#endif /* HAVE_JOYSTICK */
next_token (state); next_token (state);
return result; return result;
} }