Vegastrike 0.5.1 rc1  1.0
Original sources for Vegastrike Evolved
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
in_joystick.h File Reference
#include "config.h"
#include "in_kb_data.h"
#include "vegastrike.h"
#include "in_kb.h"

Go to the source code of this file.

Classes

class  JoyStick
 

Macros

#define NUMJBUTTONS   32
 
#define MAX_AXES   32
 

Typedefs

typedef void(* JoyHandler )(KBSTATE, float x, float y, int mod)
 

Enumerations

enum  {
  VS_HAT_CENTERED =0, VS_HAT_LEFT, VS_HAT_RIGHT, VS_HAT_DOWN,
  VS_HAT_UP, VS_HAT_RIGHTUP, VS_HAT_RIGHTDOWN, VS_HAT_LEFTUP,
  VS_HAT_LEFTDOWN
}
 

Functions

void ProcessJoystick (int whichjoystick)
 
void InitJoystick ()
 
void DeInitJoystick ()
 
void BindJoyKey (int key, int joystick, KBHandler handler, const KBData &data)
 
void UnbindJoyKey (int joystick, int key)
 
void UnbindHatswitchKey (int hatswitch, int val_index)
 
void BindHatswitchKey (int hatswitch, int val_index, KBHandler handler, const KBData &data)
 
void BindDigitalHatswitchKey (int joystick, int hatswitch, int dir_index, KBHandler handler, const KBData &data)
 
void UnbindDigitalHatswitchKey (int joystick, int hatswitch, int dir_index)
 

Variables

const int MAX_JOYSTICKS = 16
 
const int MOUSE_JOYSTICK = MAX_JOYSTICKS-1
 
const int MAX_BUTTONS = 48
 
const int MAX_DIGITAL_HATSWITCHES = 4
 
const int MAX_DIGITAL_VALUES = 9
 
JoyStickjoystick [MAX_JOYSTICKS]
 

Macro Definition Documentation

#define MAX_AXES   32
#define NUMJBUTTONS   32

Definition at line 38 of file in_joystick.h.

Referenced by GenBindJoyKey(), GenUnbindJoyKey(), InitJoystick(), and ProcessJoystick().

Typedef Documentation

typedef void(* JoyHandler)(KBSTATE, float x, float y, int mod)

Definition at line 106 of file in_joystick.h.

Enumeration Type Documentation

anonymous enum
Enumerator
VS_HAT_CENTERED 
VS_HAT_LEFT 
VS_HAT_RIGHT 
VS_HAT_DOWN 
VS_HAT_UP 
VS_HAT_RIGHTUP 
VS_HAT_RIGHTDOWN 
VS_HAT_LEFTUP 
VS_HAT_LEFTDOWN 

Definition at line 54 of file in_joystick.h.

Function Documentation

void BindDigitalHatswitchKey ( int  joystick,
int  hatswitch,
int  dir_index,
KBHandler  handler,
const KBData data 
)

Definition at line 82 of file in_sdl.cpp.

References DIGHATSWITCH, GenBindJoyKey(), and MAX_DIGITAL_VALUES.

83 {
84  GenBindJoyKey( DIGHATSWITCH, joystick, key*MAX_DIGITAL_VALUES+dir, handler, data );
85 }
void BindHatswitchKey ( int  hatswitch,
int  val_index,
KBHandler  handler,
const KBData data 
)

Definition at line 72 of file in_sdl.cpp.

References GenBindJoyKey(), and HATSWITCH.

73 {
74  GenBindJoyKey( HATSWITCH, joystick, key, handler, data );
75 }
void BindJoyKey ( int  key,
int  joystick,
KBHandler  handler,
const KBData data 
)

Definition at line 62 of file in_sdl.cpp.

References GenBindJoyKey(), and JOYSTICK_SWITCH.

63 {
64  GenBindJoyKey( JOYSTICK_SWITCH, joystick, key, handler, data );
65 }
void DeInitJoystick ( )

Definition at line 172 of file in_joystick.cpp.

References i, and MAX_JOYSTICKS.

Referenced by DeInitInput().

173 {
174  for (int i = 0; i < MAX_JOYSTICKS; i++)
175  delete joystick[i];
176 }
void InitJoystick ( )

Definition at line 129 of file in_joystick.cpp.

References vs_options::force_use_of_joystick, h, i, j, JoystickPollingRate(), MAX_DIGITAL_HATSWITCHES, MAX_DIGITAL_VALUES, MAX_HATSWITCHES, MAX_JOYSTICKS, MAX_VALUES, myGlutJoystickCallback(), num_joysticks, NUMJBUTTONS, UnbindDigitalHatswitchKey(), UnbindHatswitchKey(), UnbindJoyKey(), and v.

Referenced by InitInput().

130 {
131  int i;
132  for (i = 0; i < NUMJBUTTONS; i++)
133  for (int j = 0; j < MAX_JOYSTICKS; j++)
134  UnbindJoyKey( j, i );
135  for (int h = 0; h < MAX_HATSWITCHES; h++)
136  for (int v = 0; v < MAX_VALUES; v++)
137  UnbindHatswitchKey( h, v );
138  for (int j = 0; j < MAX_JOYSTICKS; j++)
139  for (int h = 0; h < MAX_DIGITAL_HATSWITCHES; h++)
140  for (int v = 0; v < MAX_DIGITAL_VALUES; v++)
141  UnbindDigitalHatswitchKey( j, h, v );
142 #ifndef NO_SDL_JOYSTICK
143 #ifdef HAVE_SDL
144  num_joysticks = SDL_NumJoysticks();
145  printf( "%i joysticks were found.\n\n", num_joysticks );
146  printf( "The names of the joysticks are:\n" );
147 #else
148  //use glut
149  if (glutDeviceGet( GLUT_HAS_JOYSTICK ) || game_options.force_use_of_joystick) {
150  printf( "setting joystick functionality:: joystick online" );
151  glutJoystickFunc( myGlutJoystickCallback, JoystickPollingRate() );
152  num_joysticks = 1;
153  }
154 #endif
155 #endif
156  for (i = 0; i < MAX_JOYSTICKS; i++) {
157 #ifndef NO_SDL_JOYSTICK
158 #ifdef HAVE_SDL
159  if (i < num_joysticks)
160  printf( " %s\n", SDL_JoystickName( i ) );
161 #else
162  if (i < num_joysticks)
163  //SDL_EventState (SDL_JOYBUTTONDOWN,SDL_ENABLE);
164  //SDL_EventState (SDL_JOYBUTTONUP,SDL_ENABLE);
165  printf( "Glut detects %d joystick", i+1 );
166 #endif
167 #endif
168  joystick[i] = new JoyStick( i ); //SDL_Init is done in main.cpp
169  }
170 }
void ProcessJoystick ( int  whichjoystick)

Definition at line 87 of file in_sdl.cpp.

References JSHandlerCall::data, DIGHATSWITCH, JoyStick::digital_hat, DOWN, JSHandlerCall::function, JoyStick::GetJoyStick(), h, HATSWITCH, VegaConfig::hatswitch, VegaConfig::hatswitch_axis, VegaConfig::hatswitch_joystick, VegaConfig::hatswitch_margin, i, j, JoyStick::joy_axis, joystick, JOYSTICK_SWITCH, MAX_DIGITAL_VALUES, MAX_HATSWITCHES, MAX_JOYSTICKS, MAX_VALUES, JoyStick::nr_of_hats, NUMJBUTTONS, PRESS, RELEASE, UP, v, vs_config, VS_HAT_CENTERED, VS_HAT_DOWN, VS_HAT_LEFT, VS_HAT_LEFTDOWN, VS_HAT_LEFTUP, VS_HAT_RIGHT, VS_HAT_RIGHTDOWN, VS_HAT_RIGHTUP, VS_HAT_UP, x, y, and z.

Referenced by ProcessInput().

88 {
89  float x, y, z;
90  int buttons;
91 #ifdef HAVE_SDL
92 #ifndef NO_SDL_JOYSTICK
93  SDL_JoystickUpdate(); //FIXME isn't this supposed to be called already by SDL?
94 #endif
95 #endif
96  for (int i = whichplayer; i < whichplayer+1 && i < MAX_JOYSTICKS; i++) {
97  buttons = 0;
98  if ( joystick[i]->isAvailable() ) {
99  joystick[i]->GetJoyStick( x, y, z, buttons );
100  for (int h = 0; h < joystick[i]->nr_of_hats; h++) {
101 #ifdef HAVE_SDL
102  Uint8
103 #else
104  unsigned char
105 #endif
106  hsw = joystick[i]->digital_hat[h];
107  if (joystick[i]->debug_digital_hatswitch) {
108  char buf[100];
109  sprintf( buf, "hsw: %d", hsw );
110  std::cout<<buf<<std::endl;
111  }
112  for (int dir_index = 0; dir_index < MAX_DIGITAL_VALUES; dir_index++) {
113  bool press = false;
114 #ifdef HAVE_SDL
115 #ifndef NO_SDL_JOYSTICK
116  //CENTERED is an exact position.
117  if ( dir_index == VS_HAT_CENTERED && (hsw == SDL_HAT_CENTERED) ) {
118  if (joystick[i]->debug_digital_hatswitch)
119  std::cout<<"center"<<std::endl;
120  press = true;
121  }
122  if ( dir_index == VS_HAT_LEFT && (hsw&SDL_HAT_LEFT) )
123  press = true;
124  if ( dir_index == VS_HAT_RIGHT && (hsw&SDL_HAT_RIGHT) )
125  press = true;
126  if ( dir_index == VS_HAT_DOWN && (hsw&SDL_HAT_DOWN) )
127  press = true;
128  if ( dir_index == VS_HAT_UP && (hsw&SDL_HAT_UP) )
129  press = true;
130  if ( dir_index == VS_HAT_RIGHTUP && (hsw&SDL_HAT_RIGHTUP) )
131  press = true;
132  if ( dir_index == VS_HAT_RIGHTDOWN && (hsw&SDL_HAT_RIGHTDOWN) )
133  press = true;
134  if ( dir_index == VS_HAT_LEFTUP && (hsw&SDL_HAT_LEFTUP) )
135  press = true;
136  if ( dir_index == VS_HAT_LEFTDOWN && (hsw&SDL_HAT_LEFTDOWN) )
137  press = true;
138 #endif
139 #endif
140  KBSTATE *state =
141  &JoystickState[DIGHATSWITCH][i][h*MAX_DIGITAL_VALUES+dir_index];
142  JSHandlerCall *handler =
143  &JoystickBindings[DIGHATSWITCH][i][h*MAX_DIGITAL_VALUES+dir_index];
144  if (press == true) {
145  if (*state == UP) {
146  (*handler->function)
147  ( handler->data, PRESS );
148  *state = DOWN;
149  }
150  } else {
151  if (*state == DOWN)
152  (*handler->function)
153  ( handler->data, RELEASE );
154  *state = UP;
155  }
156  (*handler->function)( handler->data, *state );
157  }
158  } //digital_hatswitch
159  for (int j = 0; j < NUMJBUTTONS; j++) {
160  KBSTATE *state = &JoystickState[JOYSTICK_SWITCH][i][j];
162  if ( ( buttons&(1<<j) ) ) {
163  if (*state == UP) {
164  (*handler->function)
165  ( handler->data, PRESS );
166  *state = DOWN;
167  }
168  } else {
169  if (*state == DOWN)
170  (*handler->function)( handler->data, RELEASE );
171  *state = UP;
172  }
173  (*handler->function)( handler->data, *state );
174  }
175  } //is available
176  } //for nr joysticks
177  for (int h = 0; h < MAX_HATSWITCHES; h++) {
178  float margin = fabs( vs_config->hatswitch_margin[h] );
179  if (margin < 1.0) {
180  //we have hatswitch nr. h
181  int hs_axis = vs_config->hatswitch_axis[h];
182  int hs_joy = vs_config->hatswitch_joystick[h];
183  if ( joystick[hs_joy]->isAvailable() ) {
184  float axevalue = joystick[hs_joy]->joy_axis[hs_axis];
185  for (int v = 0; v < MAX_VALUES; v++) {
186  float hs_val = vs_config->hatswitch[h][v];
187  if (fabs( hs_val ) <= 1.0) {
188  //this is set
189  JSHandlerCall *handler = &JoystickBindings[HATSWITCH][h][v];
190  KBSTATE *state = &JoystickState[HATSWITCH][h][v];
191  if (hs_val-margin <= axevalue && axevalue <= hs_val+margin) {
192  //hatswitch pressed
193  if (*state == UP) {
194  (*handler->function)( handler->data, PRESS );
195  *state = DOWN;
196  }
197  } else {
198  //not pressed
199  if (*state == DOWN)
200  (*handler->function)( handler->data, RELEASE );
201  *state = UP;
202  }
203  (*handler->function)( handler->data, *state );
204  }
205  } //for all values
206  } //is available
207  }
208  }
209 }
void UnbindDigitalHatswitchKey ( int  joystick,
int  hatswitch,
int  dir_index 
)

Definition at line 77 of file in_sdl.cpp.

References DIGHATSWITCH, GenUnbindJoyKey(), and MAX_DIGITAL_VALUES.

Referenced by InitJoystick().

78 {
80 }
void UnbindHatswitchKey ( int  hatswitch,
int  val_index 
)

Definition at line 67 of file in_sdl.cpp.

References GenUnbindJoyKey(), and HATSWITCH.

Referenced by InitJoystick().

68 {
70 }
void UnbindJoyKey ( int  joystick,
int  key 
)

Definition at line 57 of file in_sdl.cpp.

References GenUnbindJoyKey(), and JOYSTICK_SWITCH.

Referenced by InitJoystick().

58 {
60 }

Variable Documentation

const int MAX_BUTTONS = 48

Definition at line 50 of file in_joystick.h.

const int MAX_DIGITAL_HATSWITCHES = 4

Definition at line 51 of file in_joystick.h.

Referenced by GenUnbindJoyKey(), and InitJoystick().

const int MAX_DIGITAL_VALUES = 9
const int MOUSE_JOYSTICK = MAX_JOYSTICKS-1

Definition at line 49 of file in_joystick.h.

Referenced by DealWithWarp(), and JoyStick::JoyStick().