Cleaning up Joystick code and on the way supporting more axes and buttons; Also fixed bug with channel_mask byte causing trouble using some axes ingame; Added argument to set environment variable which disables sticky keys (for SDL >= 1.2.14)

This commit is contained in:
zicodxx 2010-02-14 18:48:02 +00:00
parent 66dfa869b1
commit 5afebdf580
9 changed files with 50 additions and 316 deletions

View file

@ -1,5 +1,9 @@
D2X-Rebirth Changelog
20100214
--------
d2x.ini, arch/include/joy.h, arch/sdl/joy.c, main/endlevel.c, main/inferno.c, main/kconfig.c, main/slew.c, misc/args.c: Cleaning up Joystick code and on the way supporting more axes and buttons; Also fixed bug with channel_mask byte causing trouble using some axes ingame; Added argument to set environment variable which disables sticky keys (for SDL >= 1.2.14)
20100208
--------
main/menu.c, main/scores.c, main/scores.h: Move all globals in scores.c into struct members/local variables

View file

@ -1,17 +1,3 @@
/* $Id: joy.h,v 1.1.1.1 2006/03/17 19:54:32 zicodxx Exp $ */
/*
THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
*/
/*
*
* Header for joystick functions
@ -24,129 +10,21 @@ COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
#include "pstypes.h"
#include "fix.h"
#define MAX_JOYSTICKS 8
#define MAX_AXES_PER_JOYSTICK 8
#define MAX_BUTTONS_PER_JOYSTICK 32
#define MAX_HATS_PER_JOYSTICK 4
#define MAX_JOYSTICKS 8
#define MAX_AXES_PER_JOYSTICK 128
#define MAX_BUTTONS_PER_JOYSTICK 128
#define MAX_HATS_PER_JOYSTICK 4
#define JOY_MAX_AXES (MAX_AXES_PER_JOYSTICK * MAX_JOYSTICKS)
#define JOY_MAX_BUTTONS (MAX_BUTTONS_PER_JOYSTICK * MAX_JOYSTICKS)
// added October 24, 2000 20:40 Steven Mueller: more than 4 joysticks now
#define MAX_JOY_DEVS 8
#define JOY_1_BUTTON_A 1
#define JOY_1_BUTTON_B 2
#define JOY_2_BUTTON_A 4
#define JOY_2_BUTTON_B 8
#define JOY_ALL_BUTTONS (1+2+4+8)
#define JOY_1_X_AXIS 1
#define JOY_1_Y_AXIS 2
#define JOY_1_Z_AXIS 4
#define JOY_1_R_AXIS 16
#define JOY_1_U_AXIS 32
#define JOY_1_V_AXIS 64
#define JOY_MAX_AXES (MAX_AXES_PER_JOYSTICK * MAX_JOYSTICKS)
#ifdef WINDOWS
#define JOY_1_POV 8
#define JOY_ALL_AXIS (1+2+4+8)
#define JOY_EXT_AXIS (16+32+64)
#else
#define JOY_2_X_AXIS 4
#define JOY_2_Y_AXIS 8
#define JOY_ALL_AXIS (1+2+4+8)
#endif
#define JOY_SLOW_READINGS 1
#define JOY_POLLED_READINGS 2
#define JOY_BIOS_READINGS 4
/* not present in d2src, maybe we don't really need it? */
#define JOY_FRIENDLY_READINGS 8
#define JOY_MAX_BUTTONS (MAX_BUTTONS_PER_JOYSTICK * MAX_JOYSTICKS)
//==========================================================================
// This initializes the joy and does a "quick" calibration which
// assumes the stick is centered and sets the minimum value to 0 and
// the maximum value to 2 times the centered reading. Returns 0 if no
// joystick was detected, 1 if everything is ok.
// joy_init() is called.
#ifdef WINDOWS
# ifdef __NT__
extern int joy_init(int joy, int spjoy);
# endif
extern int joy95_init_stick(int joy, int spjoy);
#else
extern int joy_init();
#endif
extern int joy_num_axes; // set to Joystick.n_axes. solve different?
extern void joy_init();
extern void joy_close();
extern char joy_installed;
extern char joy_present;
//==========================================================================
// The actual number of available axes. If the arch can't set this
// properly, it should set joy_num_axes=JOY_NUM_AXES;
extern int joy_num_axes;
//==========================================================================
// The following 3 routines can be used to zero in on better joy
// calibration factors. To use them, ask the user to hold the stick
// in either the upper left, lower right, or center and then have them
// press a key or button and then call the appropriate one of these
// routines, and it will read the stick and update the calibration factors.
// Usually, assuming that the stick was centered when joy_init was
// called, you really only need to call joy_set_lr, since the upper
// left position is usually always 0,0 on most joys. But, the safest
// bet is to do all three, or let the user choose which ones to set.
extern void joy_set_ul();
extern void joy_set_lr();
extern void joy_set_cen();
//==========================================================================
// This reads the joystick. X and Y will be between -128 and 127.
// Takes about 1 millisecond in the worst case when the stick
// is in the lower right hand corner. Always returns 0,0 if no stick
// is present.
extern void joy_get_pos(int *x, int *y);
//==========================================================================
// This just reads the buttons and returns their status. When bit 0
// is 1, button 1 is pressed, when bit 1 is 1, button 2 is pressed.
extern int joy_get_btns();
//==========================================================================
// This returns the number of times a button went either down or up since
// the last call to this function.
extern int joy_get_button_up_cnt(int btn);
extern int joy_get_button_down_cnt(int btn);
//==========================================================================
// This returns how long (in approximate milliseconds) that each of the
// buttons has been held down since the last call to this function.
// It is the total time... say you pressed it down for 3 ticks, released
// it, and held it down for 6 more ticks. The time returned would be 9.
extern fix joy_get_button_down_time(int btn);
extern ubyte joy_read_raw_buttons();
extern ubyte joystick_read_raw_axis(ubyte mask, int *axis);
extern int joy_get_button_down_cnt(int btn);
extern void joystick_read_raw_axis(int *axis);
extern void joy_flush();
extern ubyte joy_get_present_mask();
extern void joy_set_timer_rate(int max_value);
extern int joy_get_timer_rate();
extern int joy_get_button_state(int btn);
extern void joy_set_cen_fake(int channel);
extern ubyte joy_read_stick(ubyte masks, int *axis);
extern void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max);
extern void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max);
extern void joy_set_btn_values(int btn, int state, fix timedown, int downcount, int upcount);
extern int joy_get_scaled_reading(int raw, int axn);
extern void joy_set_slow_reading(int flag);
#endif // _JOY_H

View file

@ -1,9 +1,7 @@
/* $Id: joy.c,v 1.1.1.1 2006/03/17 19:53:38 zicodxx Exp $ */
/*
*
* SDL joystick support
*
*
*/
#include <string.h> // for memset
@ -21,7 +19,6 @@
extern char *joybutton_text[]; //from kconfig.c
extern char *joyaxis_text[]; //from kconfig.c
char joy_present = 0;
int num_joysticks = 0;
int joy_num_axes = 0;
@ -127,14 +124,14 @@ void joy_axis_handler(SDL_JoyAxisEvent *jae)
/* ----------------------------------------------- */
int joy_init()
void joy_init()
{
int i,j,n;
char temp[10];
if (SDL_Init(SDL_INIT_JOYSTICK) < 0) {
con_printf(CON_NORMAL, "sdl-joystick: initialisation failed: %s.",SDL_GetError());
return 0;
return;
}
memset(&Joystick,0,sizeof(Joystick));
@ -148,13 +145,13 @@ int joy_init()
con_printf(CON_NORMAL, "sdl-joystick %d: %s\n", i, SDL_JoystickName(i));
SDL_Joysticks[num_joysticks].handle = SDL_JoystickOpen(i);
if (SDL_Joysticks[num_joysticks].handle) {
joy_present = 1;
SDL_Joysticks[num_joysticks].n_axes
= SDL_JoystickNumAxes(SDL_Joysticks[num_joysticks].handle);
if(SDL_Joysticks[num_joysticks].n_axes > MAX_AXES_PER_JOYSTICK)
{
Warning("sdl-joystick: found %d axes, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_axes, MAX_AXES_PER_JOYSTICK);
Warning("sdl-joystick: found %d axes, only %d supported.\n", SDL_Joysticks[num_joysticks].n_axes, MAX_AXES_PER_JOYSTICK);
Warning("sdl-joystick: found %d axes, only %d supported.\n", SDL_Joysticks[num_joysticks].n_axes, MAX_AXES_PER_JOYSTICK);
SDL_Joysticks[num_joysticks].n_axes = MAX_AXES_PER_JOYSTICK;
}
@ -162,7 +159,7 @@ int joy_init()
= SDL_JoystickNumButtons(SDL_Joysticks[num_joysticks].handle);
if(SDL_Joysticks[num_joysticks].n_buttons > MAX_BUTTONS_PER_JOYSTICK)
{
Warning("sdl-joystick: found %d buttons, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_buttons, MAX_BUTTONS_PER_JOYSTICK);
Warning("sdl-joystick: found %d buttons, only %d supported.\n", SDL_Joysticks[num_joysticks].n_buttons, MAX_BUTTONS_PER_JOYSTICK);
SDL_Joysticks[num_joysticks].n_buttons = MAX_BUTTONS_PER_JOYSTICK;
}
@ -170,7 +167,7 @@ int joy_init()
= SDL_JoystickNumHats(SDL_Joysticks[num_joysticks].handle);
if(SDL_Joysticks[num_joysticks].n_hats > MAX_HATS_PER_JOYSTICK)
{
Warning("sdl-joystick: found %d hats, only %d supported. Game may be unstable.\n", SDL_Joysticks[num_joysticks].n_hats, MAX_HATS_PER_JOYSTICK);
Warning("sdl-joystick: found %d hats, only %d supported.\n", SDL_Joysticks[num_joysticks].n_hats, MAX_HATS_PER_JOYSTICK);
SDL_Joysticks[num_joysticks].n_hats = MAX_HATS_PER_JOYSTICK;
}
@ -214,8 +211,6 @@ int joy_init()
}
joy_num_axes = Joystick.n_axes;
return joy_present;
}
void joy_close()
@ -228,40 +223,6 @@ void joy_close()
d_free(joybutton_text[Joystick.n_buttons]);
}
void joy_get_pos(int *x, int *y)
{
int axis[JOY_MAX_AXES];
if (!num_joysticks) {
*x=*y=0;
return;
}
joystick_read_raw_axis (JOY_ALL_AXIS, axis);
*x = joy_get_scaled_reading( axis[0], 0 );
*y = joy_get_scaled_reading( axis[1], 1 );
}
int joy_get_btns()
{
#if 0 // This is never used?
int i, buttons = 0;
for (i=0; i++; i<buttons) {
switch (Joystick.buttons[i].state) {
case SDL_PRESSED:
buttons |= 1<<i;
break;
case SDL_RELEASED:
break;
}
}
return buttons;
#else
return 0;
#endif
}
int joy_get_button_down_cnt( int btn )
{
int num_downs;
@ -299,23 +260,17 @@ fix joy_get_button_down_time(int btn)
return time;
}
ubyte joystick_read_raw_axis( ubyte mask, int * axis )
void joystick_read_raw_axis( int * axis )
{
int i;
ubyte channel_masks = 0;
if (!num_joysticks)
return 0;
return;
event_poll();
for (i = 0; i < Joystick.n_axes; i++)
{
if ((axis[i] = Joystick.axes[i].value))
channel_masks |= 1 << i;
}
return channel_masks;
axis[i] = Joystick.axes[i].value;
}
void joy_flush()
@ -346,33 +301,8 @@ int joy_get_button_state( int btn )
return Joystick.buttons[btn].state;
}
void joy_get_cal_vals(int *axis_min, int *axis_center, int *axis_max)
{
int i;
for (i = 0; i < Joystick.n_axes; i++)
{
axis_center[i] = Joystick.axes[i].center_val;
axis_min[i] = Joystick.axes[i].min_val;
axis_max[i] = Joystick.axes[i].max_val;
}
}
void joy_set_cal_vals(int *axis_min, int *axis_center, int *axis_max)
{
int i;
for (i = 0; i < Joystick.n_axes; i++)
{
Joystick.axes[i].center_val = axis_center[i];
Joystick.axes[i].min_val = axis_min[i];
Joystick.axes[i].max_val = axis_max[i];
}
}
int joy_get_scaled_reading( int raw, int axis_num )
{
#if 1
int x, d;
d = (PlayerCfg.JoystickDeadzone) * 6;
@ -382,33 +312,4 @@ int joy_get_scaled_reading( int raw, int axis_num )
x = raw/256;
return x;
//return raw/256;
#else
int d, x;
raw -= Joystick.axes[axis_num].center_val;
if (raw < 0)
d = Joystick.axes[axis_num].center_val - Joystick.axes[axis_num].min_val;
else if (raw > 0)
d = Joystick.axes[axis_num].max_val - Joystick.axes[axis_num].center_val;
else
d = 0;
if (d)
x = ((raw << 7) / d);
else
x = 0;
if ( x < -128 )
x = -128;
if ( x > 127 )
x = 127;
d = (PlayerCfg.JoystickDeadzone) * 6;
if ((x > (-1*d)) && (x < d))
x = 0;
return x;
#endif
}

View file

@ -19,6 +19,7 @@
;-nojoystick Deactivate joystick
;-mouselook Activate mouselook. Works in singleplayer only
;-grabmouse Keeps the mouse from wandering out of the window
;-nostickykeys Make CapsLock and NumLock non-sticky so they can be used as normal keys
Sound:

View file

@ -789,7 +789,7 @@ void do_endlevel_frame()
#ifdef SLEW_ON
slew_obj = endlevel_camera;
_do_slew_movement(endlevel_camera,1,1);
_do_slew_movement(endlevel_camera,1);
timer += FrameTime; //make time stop
break;
#else
@ -830,7 +830,7 @@ void do_endlevel_frame()
vm_vec_scale_add2(&ConsoleObject->pos,&ConsoleObject->orient.fvec,fixmul(FrameTime,cur_fly_speed));
#ifdef SLEW_ON
_do_slew_movement(endlevel_camera,1,1);
_do_slew_movement(endlevel_camera,1);
#else
get_angs_to_object(&camera_desired_angles,&ConsoleObject->pos,&endlevel_camera->pos);
@ -857,7 +857,7 @@ void do_endlevel_frame()
fix d,speed_scale;
#ifdef SLEW_ON
_do_slew_movement(endlevel_camera,1,1);
_do_slew_movement(endlevel_camera,1);
#endif
get_angs_to_object(&camera_desired_angles,&ConsoleObject->pos,&endlevel_camera->pos);
@ -1333,13 +1333,11 @@ extern short old_joy_x,old_joy_y; //position last time around
#include "joy.h"
#ifdef SLEW_ON //this is a special routine for slewing around external scene
int _do_slew_movement(object *obj, int check_keys, int check_joy )
int _do_slew_movement(object *obj, int check_keys )
{
int moved = 0;
vms_vector svel, movement; //scaled velocity (per this frame)
vms_matrix rotmat,new_pm;
int joy_x,joy_y,btns;
int joyx_moved,joyy_moved;
vms_angvec rotang;
if (keyd_pressed[KEY_PAD5])
@ -1357,29 +1355,6 @@ int _do_slew_movement(object *obj, int check_keys, int check_joy )
else
rotang.pitch = rotang.bank = rotang.head = 0;
//check for joystick movement
if (check_joy && joy_present) {
joy_get_pos(&joy_x,&joy_y);
btns=joy_get_btns();
joyx_moved = (abs(joy_x - old_joy_x)>JOY_NULL);
joyy_moved = (abs(joy_y - old_joy_y)>JOY_NULL);
if (abs(joy_x) < JOY_NULL) joy_x = 0;
if (abs(joy_y) < JOY_NULL) joy_y = 0;
if (btns)
if (!rotang.pitch) rotang.pitch = fixmul(-joy_y * 512,FrameTime); else;
else
if (joyy_moved) obj->phys_info.velocity.z = -joy_y * 8192;
if (!rotang.head) rotang.head = fixmul(joy_x * 512,FrameTime);
if (joyx_moved) old_joy_x = joy_x;
if (joyy_moved) old_joy_y = joy_y;
}
moved = rotang.pitch | rotang.bank | rotang.head;
vm_angles_2_matrix(&rotmat,&rotang);

View file

@ -143,6 +143,7 @@ void print_commandline_help()
printf( " -nojoystick %s\n", "Deactivate joystick");
printf( " -mouselook %s\n", "Activate mouselook. Works in singleplayer only");
printf( " -grabmouse %s\n", "Keeps the mouse from wandering out of the window");
printf( " -nostickykeys %s\n", "Make CapsLock and NumLock non-sticky so they can be used as normal keys");
printf( "\n Sound:\n\n");
printf( " -nosound %s\n", "Disables sound output");
@ -170,7 +171,7 @@ void print_commandline_help()
printf( " -ipxnetwork <n> %s\n", "Use IPX network number <n>");
printf( " -udp_hostaddr <n> %s\n", "When manually joining a game use default IP Address <n> to connect to");
printf( " -udp_hostport <n> %s\n", "When manually joining a game use default UDP Port <n> to connect to");
printf( " -udp_myport <n> %s\n", "When hosting/joining a game use default UDP Port <n> to send packets from");
printf( " -udp_myport <n> %s\n", "When hosting/joining a game use default UDP Port <n> to send packets from");
#endif // NETWORK
#ifdef EDITOR

View file

@ -678,7 +678,7 @@ void kconfig_start_changing(kc_menu *menu)
game_flush_inputs();
if (menu->items[menu->citem].type == BT_JOY_AXIS)
joystick_read_raw_axis( JOY_ALL_AXIS, menu->old_axis );
joystick_read_raw_axis( menu->old_axis );
menu->changing = 1;
}
@ -1196,7 +1196,7 @@ void kc_change_joyaxis( kc_menu *menu, kc_item * item )
int n,i;
ubyte code = 255;
joystick_read_raw_axis( JOY_ALL_AXIS, axis );
joystick_read_raw_axis( axis );
for (i=0; i<numaxis; i++ ) {
if ( abs(axis[i]-menu->old_axis[i])>4096 )
@ -1306,7 +1306,6 @@ void controls_read_all(int automap_flag)
int mouse_buttons;
fix k0, k1, k2, k3, kp;
fix k4, k5, k6, k7, kh;
ubyte channel_masks;
int use_mouse, use_joystick;
int speed_factor=1;
@ -1328,28 +1327,24 @@ void controls_read_all(int automap_flag)
//--------- Read Joystick -----------
if (CONTROL_USING_JOYSTICK ) {
channel_masks = joystick_read_raw_axis( JOY_ALL_AXIS, raw_joy_axis );
joystick_read_raw_axis( raw_joy_axis );
for (i = 0; i < joy_num_axes; i++)
{
if (channel_masks&(1<<i)) {
int joy_null_value = 10;
int joy_null_value = 10;
raw_joy_axis[i] = joy_get_scaled_reading( raw_joy_axis[i], i );
raw_joy_axis[i] = joy_get_scaled_reading( raw_joy_axis[i], i );
if (kc_joystick[23].value==i) // If this is the throttle
joy_null_value = 20; // Then use a larger dead-zone
if (kc_joystick[23].value==i) // If this is the throttle
joy_null_value = 20; // Then use a larger dead-zone
if (raw_joy_axis[i] > joy_null_value)
raw_joy_axis[i] = ((raw_joy_axis[i]-joy_null_value)*128)/(128-joy_null_value);
else if (raw_joy_axis[i] < -joy_null_value)
raw_joy_axis[i] = ((raw_joy_axis[i]+joy_null_value)*128)/(128-joy_null_value);
else
raw_joy_axis[i] = 0;
joy_axis[i] = (raw_joy_axis[i]*FrameTime)/128;
} else {
joy_axis[i] = 0;
}
if (raw_joy_axis[i] > joy_null_value)
raw_joy_axis[i] = ((raw_joy_axis[i]-joy_null_value)*128)/(128-joy_null_value);
else if (raw_joy_axis[i] < -joy_null_value)
raw_joy_axis[i] = ((raw_joy_axis[i]+joy_null_value)*128)/(128-joy_null_value);
else
raw_joy_axis[i] = 0;
joy_axis[i] = (raw_joy_axis[i]*FrameTime)/128;
}
use_joystick=1;
} else {

View file

@ -82,13 +82,11 @@ void slew_reset_orient()
}
int do_slew_movement(object *obj, int check_keys, int check_joy )
int do_slew_movement(object *obj, int check_keys )
{
int moved = 0;
vms_vector svel, movement; //scaled velocity (per this frame)
vms_matrix rotmat,new_pm;
int joy_x,joy_y,btns;
int joyx_moved,joyy_moved;
vms_angvec rotang;
if (!slew_obj || slew_obj->control_type!=CT_SLEW) return 0;
@ -116,29 +114,6 @@ int do_slew_movement(object *obj, int check_keys, int check_joy )
else
rotang.p = rotang.b = rotang.h = 0;
//check for joystick movement
if (check_joy && joy_present && (Function_mode == FMODE_EDITOR) ) {
joy_get_pos(&joy_x,&joy_y);
btns=joy_get_btns();
joyx_moved = (abs(joy_x - old_joy_x)>JOY_NULL);
joyy_moved = (abs(joy_y - old_joy_y)>JOY_NULL);
if (abs(joy_x) < JOY_NULL) joy_x = 0;
if (abs(joy_y) < JOY_NULL) joy_y = 0;
if (btns)
if (!rotang.p) rotang.p = fixmul(-joy_y * 512,FrameTime); else;
else
if (joyy_moved) obj->mtype.phys_info.velocity.z = -joy_y * 8192;
if (!rotang.h) rotang.h = fixmul(joy_x * 512,FrameTime);
if (joyx_moved) old_joy_x = joy_x;
if (joyy_moved) old_joy_y = joy_y;
}
moved = rotang.p | rotang.b | rotang.h;
vm_angles_2_matrix(&rotmat,&rotang);
@ -166,7 +141,7 @@ int do_slew_movement(object *obj, int check_keys, int check_joy )
//do slew for this frame
int slew_frame(int check_keys)
{
return do_slew_movement( slew_obj, !check_keys, 1 );
return do_slew_movement( slew_obj, !check_keys );
}

View file

@ -155,6 +155,10 @@ void ReadCmdArgs(void)
GameArg.CtlNoJoystick = FindArg("-nojoystick");
GameArg.CtlMouselook = FindArg("-mouselook");
GameArg.CtlGrabMouse = FindArg("-grabmouse");
if (FindArg("-nostickykeys")) // no GameArg, just an environment variable. Must happen before SDL_Init!
putenv("SDL_DISABLE_LOCK_KEYS=1");
else
putenv("SDL_DISABLE_LOCK_KEYS=0");
// Sound Options