21 #define MAX_JOYSTICKS 16
24 #define MAX_AXES_PER_JOYSTICK 8
25 #define MAX_BUTTONS_PER_JOYSTICK 16
28 int num_joysticks = 0;
46 static struct joyinfo {
49 struct joyaxis axes[MAX_AXES];
50 struct joybutton buttons[MAX_BUTTONS];
57 int axis_map[MAX_AXES_PER_JOYSTICK];
58 int button_map[MAX_BUTTONS_PER_JOYSTICK];
59 } SDL_Joysticks[MAX_JOYSTICKS];
61 void joy_button_handler(SDL_JoyButtonEvent *jbe)
65 button = SDL_Joysticks[jbe->which].button_map[jbe->button];
67 Joystick.buttons[button].state = jbe->state;
70 case SDL_JOYBUTTONDOWN:
71 Joystick.buttons[button].time_went_down
72 = timer_get_fixed_seconds();
73 Joystick.buttons[button].num_downs++;
76 Joystick.buttons[button].num_ups++;
81 void joy_axis_handler(SDL_JoyAxisEvent *jae)
85 axis = SDL_Joysticks[jae->which].axis_map[jae->axis];
87 Joystick.axes[axis].value = jae->value;
91 /* ----------------------------------------------- */
97 if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
98 con_printf(CON_VERBOSE, "sdl-joystick: initialisation failed: %s.",SDL_GetError());
102 memset(&Joystick,0,sizeof(Joystick));
104 n = SDL_NumJoysticks();
106 con_printf(CON_VERBOSE, "sdl-joystick: found %d joysticks\n", n);
107 for (i = 0; i < n; i++) {
108 con_printf(CON_VERBOSE, "sdl-joystick %d: %s\n", i, SDL_JoystickName(i));
109 SDL_Joysticks[num_joysticks].handle = SDL_JoystickOpen(i);
110 if (SDL_Joysticks[num_joysticks].handle) {
112 SDL_Joysticks[num_joysticks].n_axes
113 = SDL_JoystickNumAxes(SDL_Joysticks[num_joysticks].handle);
114 SDL_Joysticks[num_joysticks].n_buttons
115 = SDL_JoystickNumButtons(SDL_Joysticks[num_joysticks].handle);
116 con_printf(CON_VERBOSE, "sdl-joystick: %d axes\n", SDL_Joysticks[num_joysticks].n_axes);
117 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons\n", SDL_Joysticks[num_joysticks].n_buttons);
118 for (j=0; j < SDL_Joysticks[num_joysticks].n_axes; j++)
119 SDL_Joysticks[num_joysticks].axis_map[j] = Joystick.n_axes++;
120 for (j=0; j < SDL_Joysticks[num_joysticks].n_buttons; j++)
121 SDL_Joysticks[num_joysticks].button_map[j] = Joystick.n_buttons++;
124 con_printf(CON_VERBOSE, "sdl-joystick: initialization failed!\n");
125 con_printf(CON_VERBOSE, "sdl-joystick: %d axes (total)\n", Joystick.n_axes);
126 con_printf(CON_VERBOSE, "sdl-joystick: %d buttons (total)\n", Joystick.n_buttons);
134 while (num_joysticks)
135 SDL_JoystickClose(SDL_Joysticks[--num_joysticks].handle);
138 void joy_get_pos(int *x, int *y)
142 if (!num_joysticks) {
147 joystick_read_raw_axis (JOY_ALL_AXIS, axis);
149 *x = joy_get_scaled_reading( axis[0], 0 );
150 *y = joy_get_scaled_reading( axis[1], 1 );
155 #if 0 // This is never used?
157 for (i=0; i++; i<buttons) {
158 switch (Joystick.buttons[i].state) {
172 int joy_get_button_down_cnt( int btn )
181 num_downs = Joystick.buttons[btn].num_downs;
182 Joystick.buttons[btn].num_downs = 0;
187 fix joy_get_button_down_time(int btn)
196 switch (Joystick.buttons[btn].state) {
198 time = timer_get_fixed_seconds() - Joystick.buttons[btn].time_went_down;
199 Joystick.buttons[btn].time_went_down = timer_get_fixed_seconds();
209 ubyte joystick_read_raw_axis( ubyte mask, int * axis )
218 for (i = 0; i <= JOY_NUM_AXES; i++) {
219 axis[i] = Joystick.axes[i].value;
232 for (i = 0; i < Joystick.n_buttons; i++) {
233 Joystick.buttons[i].time_went_down = 0;
234 Joystick.buttons[i].num_downs = 0;
239 int joy_get_button_state( int btn )
244 if(btn >= Joystick.n_buttons)
249 return Joystick.buttons[btn].state;
252 void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max)
256 for (i = 0; i < JOY_NUM_AXES; i++) {
257 axis_center[i] = Joystick.axes[i].center_val;
258 axis_min[i] = Joystick.axes[i].min_val;
259 axis_max[i] = Joystick.axes[i].max_val;
263 void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max)
267 for (i = 0; i < JOY_NUM_AXES; i++) {
268 Joystick.axes[i].center_val = axis_center[i];
269 Joystick.axes[i].min_val = axis_min[i];
270 Joystick.axes[i].max_val = axis_max[i];
274 int joy_get_scaled_reading( int raw, int axis_num )
281 raw -= Joystick.axes[axis_num].center_val;
284 d = Joystick.axes[axis_num].center_val - Joystick.axes[axis_num].min_val;
286 d = Joystick.axes[axis_num].max_val - Joystick.axes[axis_num].center_val;
291 x = ((raw << 7) / d);
300 d = (joy_deadzone) * 6;
301 if ((x > (-1*d)) && (x < d))
308 void joy_set_slow_reading( int flag )