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_sdl.cpp
Go to the documentation of this file.
1 #include "in_joystick.h"
2 #include "vs_globals.h"
3 #include "config_xml.h"
4 #include "in_kb_data.h"
5 #include <assert.h>
6 void DefaultJoyHandler( const KBData&, KBSTATE newState ) // DELETE ME
7 {
8 }
9 
11 {
12  KBHandler function;
15  {
16  function = DefaultJoyHandler;
17  }
18  JSHandlerCall( KBHandler function, const KBData &data )
19  {
20  this->function = function;
21  this->data = data;
22  }
23 };
25 {
30 };
31 #define MAXOR( A, B ) ( ( (A) < (B) ) ? (B) : (A) )
39 
40 static void GenUnbindJoyKey( JSSwitches whichswitch, int joystick, int key )
41 {
42  assert( key
43  < MAXOR( NUMJBUTTONS,
46  JoystickBindings[whichswitch][joystick][key] = JSHandlerCall();
47  JoystickState[whichswitch][joystick][key] = UP;
48 }
49 
50 static void GenBindJoyKey( JSSwitches whichswitch, int joystick, int key, KBHandler handler, const KBData &data )
51 {
52  assert( key < NUMJBUTTONS && joystick < MAX_JOYSTICKS );
53  JoystickBindings[whichswitch][joystick][key] = JSHandlerCall( handler, data );
54  handler( KBData(), RESET );
55 }
56 
57 void UnbindJoyKey( int joystick, int key )
58 {
59  GenUnbindJoyKey( JOYSTICK_SWITCH, joystick, key );
60 }
61 
62 void BindJoyKey( int joystick, int key, KBHandler handler, const KBData &data )
63 {
64  GenBindJoyKey( JOYSTICK_SWITCH, joystick, key, handler, data );
65 }
66 
67 void UnbindHatswitchKey( int joystick, int key )
68 {
69  GenUnbindJoyKey( HATSWITCH, joystick, key );
70 }
71 
72 void BindHatswitchKey( int joystick, int key, KBHandler handler, const KBData &data )
73 {
74  GenBindJoyKey( HATSWITCH, joystick, key, handler, data );
75 }
76 
77 void UnbindDigitalHatswitchKey( int joystick, int key, int dir )
78 {
79  GenUnbindJoyKey( DIGHATSWITCH, joystick, key*MAX_DIGITAL_VALUES+dir );
80 }
81 
82 void BindDigitalHatswitchKey( int joystick, int key, int dir, KBHandler handler, const KBData &data )
83 {
84  GenBindJoyKey( DIGHATSWITCH, joystick, key*MAX_DIGITAL_VALUES+dir, handler, data );
85 }
86 
87 void ProcessJoystick( int whichplayer )
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];
161  JSHandlerCall *handler = &JoystickBindings[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 }
210