- Applied joystick patchsets 60, 61, 62, 71, 80, and 122 from the

icculus.org cvs tree.
  - linux joystick support
  - freelook honouring
  - X11 keyrepeat
This commit is contained in:
Jamie Wilkinson 2003-01-09 12:06:13 +00:00
parent 1006220402
commit 0b0e9986ef
3 changed files with 215 additions and 30 deletions

View file

@ -107,7 +107,7 @@ static int (*qglXGetConfig)( Display *dpy, XVisualInfo *vis, int attrib, int *va
static cvar_t * in_joystick; static cvar_t * in_joystick;
static qboolean joystick_avail = false; static qboolean joystick_avail = false;
static int joy_fd, jx, jy, jt; static int joy_fd, jx, jy, jt;
static cvar_t * joystick_invert_y; static cvar_t * j_invert_y;
void init_joystick() { void init_joystick() {
int i, err; int i, err;
@ -147,6 +147,57 @@ void init_joystick() {
} }
#endif #endif
#ifdef Joystick
static cvar_t *in_joystick;
static qboolean joystick_avail = false;
static int joy_fd, jx, jy, jt;
static cvar_t *j_invert_y;
#endif
#ifdef Joystick
void InitJoystick() {
int i, err;
glob_t pglob;
struct js_event e;
joystick_avail = false;
err = glob("/dev/js*", 0, NULL, &pglob);
if (err) {
switch (err) {
case GLOB_NOSPACE:
ri.Con_Printf(PRINT_ALL, "Error, out of memory while looking for joysticks\n");
break;
case GLOB_NOMATCH:
ri.Con_Printf(PRINT_ALL, "No joysticks found\n");
break;
default:
ri.Con_Printf(PRINT_ALL, "Error #%d while looking for joysticks\n",err);
}
return;
}
for (i=0;i<pglob.gl_pathc;i++) {
ri.Con_Printf(PRINT_ALL, "Trying joystick dev %s\n", pglob.gl_pathv[i]);
joy_fd = open (pglob.gl_pathv[i], O_RDONLY | O_NONBLOCK);
if (joy_fd == -1) {
ri.Con_Printf(PRINT_ALL, "Error opening joystick dev %s\n",
pglob.gl_pathv[i]);
}
else {
while (read(joy_fd, &e, sizeof(struct js_event))!=-1 &&
(e.type & JS_EVENT_INIT))
ri.Con_Printf(PRINT_ALL, "Read init event\n");
ri.Con_Printf(PRINT_ALL, "Using joystick dev %s\n", pglob.gl_pathv[i]);
joystick_avail = true;
return;
}
}
globfree(&pglob);
}
#endif
/*****************************************************************************/ /*****************************************************************************/
/* MOUSE */ /* MOUSE */
/*****************************************************************************/ /*****************************************************************************/
@ -313,11 +364,15 @@ void RW_IN_Init(in_state_t *in_state_p)
#ifdef HAVE_JOYSTICK #ifdef HAVE_JOYSTICK
in_joystick = ri.Cvar_Get("in_joystick", "1", CVAR_ARCHIVE); in_joystick = ri.Cvar_Get("in_joystick", "1", CVAR_ARCHIVE);
joystick_invert_y = ri.Cvar_Get("joystick_invert_y", "1", CVAR_ARCHIVE); j_invert_y = ri.Cvar_Get("j_invert_y", "1", CVAR_ARCHIVE);
#endif #endif
// mouse variables // mouse variables
_windowed_mouse = ri.Cvar_Get( "_windowed_mouse", "0", CVAR_ARCHIVE ); _windowed_mouse = ri.Cvar_Get( "_windowed_mouse", "0", CVAR_ARCHIVE );
#ifdef HAVE_JOYSTICK
in_joystick = ri.Cvar_Get("in_joystick", "1", CVAR_ARCHIVE);
j_invert_y = ri.Cvar_Get("j_invert_y", "1", CVAR_ARCHIVE);
#endif
m_filter = ri.Cvar_Get ("m_filter", "0", 0); m_filter = ri.Cvar_Get ("m_filter", "0", 0);
in_mouse = ri.Cvar_Get ("in_mouse", "1", CVAR_ARCHIVE); in_mouse = ri.Cvar_Get ("in_mouse", "1", CVAR_ARCHIVE);
in_dgamouse = ri.Cvar_Get ("in_dgamouse", "1", CVAR_ARCHIVE); in_dgamouse = ri.Cvar_Get ("in_dgamouse", "1", CVAR_ARCHIVE);
@ -461,7 +516,7 @@ void RW_IN_Move (usercmd_t *cmd) {
in_state->viewangles[YAW] -= m_yaw->value * (jx/100); in_state->viewangles[YAW] -= m_yaw->value * (jx/100);
if ((mlooking || freelook->value) && !(*in_state->in_strafe_state & 1)) { if ((mlooking || freelook->value) && !(*in_state->in_strafe_state & 1)) {
if (joystick_invert_y) if (j_invert_y)
in_state->viewangles[PITCH] -= m_pitch->value * (jy/100); in_state->viewangles[PITCH] -= m_pitch->value * (jy/100);
else else
in_state->viewangles[PITCH] += m_pitch->value * (jy/100); in_state->viewangles[PITCH] += m_pitch->value * (jy/100);
@ -481,6 +536,12 @@ static void IN_DeactivateMouse( void )
uninstall_grabs(); uninstall_grabs();
mouse_active = false; mouse_active = false;
} }
#ifdef Joystick
if (joystick_avail)
if (close(joy_fd))
ri.Con_Printf(PRINT_ALL, "Error, Problem closing joystick.");
#endif
} }
static void IN_ActivateMouse( void ) static void IN_ActivateMouse( void )
@ -497,6 +558,50 @@ static void IN_ActivateMouse( void )
void RW_IN_Frame (void) void RW_IN_Frame (void)
{ {
int i;
#ifdef Joystick
struct js_event e;
int key_index;
#endif
if (mouse_avail) {
for (i=0 ; i<3 ; i++) {
if ( (mouse_buttonstate & (1<<i)) && !(mouse_oldbuttonstate & (1<<i)) )
in_state->Key_Event_fp (K_MOUSE1 + i, true);
if ( !(mouse_buttonstate & (1<<i)) && (mouse_oldbuttonstate & (1<<i)) )
in_state->Key_Event_fp (K_MOUSE1 + i, false);
}
mouse_oldbuttonstate = mouse_buttonstate;
}
#ifdef Joystick
if (joystick_avail) {
while (read(joy_fd, &e, sizeof(struct js_event))!=-1) {
if (JS_EVENT_BUTTON & e.type) {
key_index = (e.number < 4) ? K_JOY1 : K_AUX1;
if (e.value) {
in_state->Key_Event_fp (key_index + e.number, true);
}
else {
in_state->Key_Event_fp (key_index + e.number, false);
}
//joy_oldbuttonstate = e.number;
}
else if (JS_EVENT_AXIS & e.type) {
switch (e.number) {
case 0:
jx = e.value;
break;
case 1:
jy = e.value;
break;
case 3:
jt = e.value;
break;
}
}
}
}
#endif
} }
void RW_IN_Activate(qboolean active) void RW_IN_Activate(qboolean active)

View file

@ -104,6 +104,7 @@ static int mouse_x, mouse_y;
static int old_mouse_x, old_mouse_y; static int old_mouse_x, old_mouse_y;
static int mx, my; static int mx, my;
static float old_windowed_mouse; static float old_windowed_mouse;
static qboolean mouse_active;
static cvar_t *_windowed_mouse; static cvar_t *_windowed_mouse;
static cvar_t *m_filter; static cvar_t *m_filter;
@ -128,6 +129,7 @@ static cvar_t *my_freelook;
/************************ /************************
* Joystick * Joystick
************************/ ************************/
#ifdef Joystick
static cvar_t *in_joystick; static cvar_t *in_joystick;
static cvar_t *j_invert_y; static cvar_t *j_invert_y;
static qboolean joystick_avail; static qboolean joystick_avail;
@ -136,6 +138,19 @@ static int joy_oldbuttonstate;
static int joy_numbuttons; static int joy_numbuttons;
static int jx, jy, jt; static int jx, jy, jt;
/************************
* Joystick
************************/
static cvar_t *in_joystick;
static cvar_t *j_invert_y;
static qboolean joystick_avail;
static SDL_Joystick *joy;
static int joy_oldbuttonstate;
static int joy_numbuttons;
static int jx, jy, jt;
static int lr_axis, ud_axis, throttle_axis;
#endif
static void Force_CenterView_f (void) static void Force_CenterView_f (void)
{ {
in_state->viewangles[PITCH] = 0; in_state->viewangles[PITCH] = 0;
@ -160,7 +175,13 @@ void RW_IN_Init(in_state_t *in_state_p)
_windowed_mouse = ri.Cvar_Get ("_windowed_mouse", "0", CVAR_ARCHIVE); _windowed_mouse = ri.Cvar_Get ("_windowed_mouse", "0", CVAR_ARCHIVE);
m_filter = ri.Cvar_Get ("m_filter", "0", 0); m_filter = ri.Cvar_Get ("m_filter", "0", 0);
in_mouse = ri.Cvar_Get ("in_mouse", "1", CVAR_ARCHIVE); in_mouse = ri.Cvar_Get ("in_mouse", "1", CVAR_ARCHIVE);
#ifdef HAVE_JOYSTICK
in_joystick = ri.Cvar_Get("in_joystick", "0", CVAR_ARCHIVE); in_joystick = ri.Cvar_Get("in_joystick", "0", CVAR_ARCHIVE);
j_invert_y = ri.Cvar_Get("j_invert_y", "1", 0);
lr_axis = (int) ri.Cvar_Get("j_lr_axis", "0", CVAR_ARCHIVE)->value;
ud_axis = (int) ri.Cvar_Get("j_ud_axis", "1", CVAR_ARCHIVE)->value;
throttle_axis = (int) ri.Cvar_Get("j_throttle", "3", CVAR_ARCHIVE)->value;
#endif
my_freelook = ri.Cvar_Get( "freelook", "0", 0); my_freelook = ri.Cvar_Get( "freelook", "0", 0);
my_lookstrafe = ri.Cvar_Get ("lookstrafe", "0", 0); my_lookstrafe = ri.Cvar_Get ("lookstrafe", "0", 0);
@ -170,8 +191,6 @@ void RW_IN_Init(in_state_t *in_state_p)
m_forward = ri.Cvar_Get ("m_forward", "1", 0); m_forward = ri.Cvar_Get ("m_forward", "1", 0);
m_side = ri.Cvar_Get ("m_side", "0.8", 0); m_side = ri.Cvar_Get ("m_side", "0.8", 0);
j_invert_y = ri.Cvar_Get("j_invert_y", "1", 0);
ri.Cmd_AddCommand ("+mlook", RW_IN_MLookDown); ri.Cmd_AddCommand ("+mlook", RW_IN_MLookDown);
ri.Cmd_AddCommand ("-mlook", RW_IN_MLookUp); ri.Cmd_AddCommand ("-mlook", RW_IN_MLookUp);
@ -192,8 +211,10 @@ void RW_IN_Shutdown(void) {
} }
#ifdef HAVE_JOYSTICK #ifdef HAVE_JOYSTICK
if (joy) if (joy) {
SDL_JoystickClose(joy); SDL_JoystickClose(joy);
joy = NULL;
}
#endif #endif
} }
@ -204,7 +225,10 @@ IN_Commands
*/ */
void RW_IN_Commands (void) void RW_IN_Commands (void)
{ {
int i, key_index; int i;
#ifdef HAVE_JOYSTICK
int key_index;
#endif
if (mouse_avail) { if (mouse_avail) {
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
@ -227,6 +251,7 @@ void RW_IN_Commands (void)
mouse_oldbuttonstate = mouse_buttonstate; mouse_oldbuttonstate = mouse_buttonstate;
} }
#ifdef HAVE_JOYSTICK
if (joystick_avail && joy) { if (joystick_avail && joy) {
for (i = 0; i < joy_numbuttons; i++) { for (i = 0; i < joy_numbuttons; i++) {
if (SDL_JoystickGetButton(joy, i) && joy_oldbuttonstate != i) { if (SDL_JoystickGetButton(joy, i) && joy_oldbuttonstate != i) {
@ -241,6 +266,7 @@ void RW_IN_Commands (void)
} }
} }
} }
#endif
} }
/* /*
@ -273,7 +299,7 @@ void RW_IN_Move (usercmd_t *cmd)
mouse_x *= sensitivity->value; mouse_x *= sensitivity->value;
mouse_y *= sensitivity->value; mouse_y *= sensitivity->value;
// add mouse X/Y movement to cmd /* add mouse X/Y movement to cmd */
if ( (*in_state->in_strafe_state & 1) || if ( (*in_state->in_strafe_state & 1) ||
(my_lookstrafe->value && mlooking )) (my_lookstrafe->value && mlooking ))
cmd->sidemove += m_side->value * mouse_x; cmd->sidemove += m_side->value * mouse_x;
@ -293,8 +319,9 @@ void RW_IN_Move (usercmd_t *cmd)
mx = my = 0; mx = my = 0;
} }
} }
#ifdef HAVE_JOYSTICK
if (joystick_avail && joy) { if (joystick_avail && joy) {
// add joy X/Y movement to cmd /* add joy X/Y movement to cmd */
if ( (*in_state->in_strafe_state & 1) || if ( (*in_state->in_strafe_state & 1) ||
(my_lookstrafe->value && mlooking )) (my_lookstrafe->value && mlooking ))
cmd->sidemove += m_side->value * (jx/100); cmd->sidemove += m_side->value * (jx/100);
@ -312,9 +339,9 @@ void RW_IN_Move (usercmd_t *cmd)
} }
jt = jx = jy = 0; jt = jx = jy = 0;
} }
#endif
} }
#if 0
static void IN_DeactivateMouse( void ) static void IN_DeactivateMouse( void )
{ {
if (!mouse_avail) if (!mouse_avail)
@ -337,7 +364,6 @@ static void IN_ActivateMouse( void )
mouse_active = true; mouse_active = true;
} }
} }
#endif
void RW_IN_Frame (void) void RW_IN_Frame (void)
{ {
@ -345,12 +371,11 @@ void RW_IN_Frame (void)
void RW_IN_Activate(qboolean active) void RW_IN_Activate(qboolean active)
{ {
#if 0 /* if (active || vidmode_active) */
if (active || vidmode_active) if (active)
IN_ActivateMouse(); IN_ActivateMouse();
else else
IN_DeactivateMouse(); IN_DeactivateMouse();
#endif
} }
/*****************************************************************************/ /*****************************************************************************/
@ -483,6 +508,7 @@ void GetEvent(SDL_Event *event)
break; break;
case SDL_MOUSEBUTTONUP: case SDL_MOUSEBUTTONUP:
break; break;
#ifdef HAVE_JOYSTICK
case SDL_JOYBUTTONDOWN: case SDL_JOYBUTTONDOWN:
keyq[keyq_head].key = keyq[keyq_head].key =
((((SDL_JoyButtonEvent*)event)->button < 4)?K_JOY1:K_AUX1)+ ((((SDL_JoyButtonEvent*)event)->button < 4)?K_JOY1:K_AUX1)+
@ -497,6 +523,7 @@ void GetEvent(SDL_Event *event)
keyq[keyq_head].down = false; keyq[keyq_head].down = false;
keyq_head = (keyq_head+1)&63; keyq_head = (keyq_head+1)&63;
break; break;
#endif
case SDL_KEYDOWN: case SDL_KEYDOWN:
if ( (KeyStates[SDLK_LALT] || KeyStates[SDLK_RALT]) && if ( (KeyStates[SDLK_LALT] || KeyStates[SDLK_RALT]) &&
(event->key.keysym.sym == SDLK_RETURN) ) { (event->key.keysym.sym == SDLK_RETURN) ) {
@ -561,6 +588,48 @@ void init_joystick() {
int num_joysticks, i; int num_joysticks, i;
joy = NULL; joy = NULL;
if (!(SDL_INIT_JOYSTICK&SDL_WasInit(SDL_INIT_JOYSTICK))) {
ri.Con_Printf(PRINT_ALL, "SDL Joystick not initialized, trying to init...\n");
SDL_Init(SDL_INIT_JOYSTICK);
}
if (in_joystick) {
ri.Con_Printf(PRINT_ALL, "Trying to start-up joystick...\n");
if ((num_joysticks=SDL_NumJoysticks())) {
for(i=0;i<num_joysticks;i++) {
ri.Con_Printf(PRINT_ALL, "Trying joystick [%s]\n",
SDL_JoystickName(i));
if (!SDL_JoystickOpened(i)) {
joy = SDL_JoystickOpen(i);
if (joy) {
ri.Con_Printf(PRINT_ALL, "Joytick activated.\n");
joystick_avail = true;
joy_numbuttons = SDL_JoystickNumButtons(joy);
break;
}
}
}
if (!joy) {
ri.Con_Printf(PRINT_ALL, "Failed to open any joysticks\n");
joystick_avail = false;
}
}
else {
ri.Con_Printf(PRINT_ALL, "No joysticks available\n");
joystick_avail = false;
}
}
else {
ri.Con_Printf(PRINT_ALL, "Joystick Inactive\n");
joystick_avail = false;
}
#endif
}
void InitJoystick() {
#ifdef HAVE_JOYSTICK
int num_joysticks, i;
joy = NULL;
if (!(SDL_INIT_JOYSTICK&SDL_WasInit(SDL_INIT_JOYSTICK))) { if (!(SDL_INIT_JOYSTICK&SDL_WasInit(SDL_INIT_JOYSTICK))) {
ri.Con_Printf(PRINT_ALL, "SDL Joystick not initialized, trying to init...\n"); ri.Con_Printf(PRINT_ALL, "SDL Joystick not initialized, trying to init...\n");
SDL_Init(SDL_INIT_JOYSTICK); SDL_Init(SDL_INIT_JOYSTICK);
@ -632,14 +701,12 @@ int SWimp_Init( void *hInstance, void *wndProc )
sigaction(SIGTERM, &sa, 0); sigaction(SIGTERM, &sa, 0);
} }
#endif #endif
#ifdef HAVE_JOYSTICK
init_joystick(); init_joystick();
#endif
return true; return true;
} }
#ifdef OPENGL #ifdef OPENGL
void *GLimp_GetProcAddress(const char *func) void *GLimp_GetProcAddress(const char *func)
{ {
@ -803,6 +870,8 @@ static qboolean GLimp_InitGraphics( qboolean fullscreen )
X11_active = true; X11_active = true;
return true; return true;
} }
#endif #endif
@ -1012,12 +1081,13 @@ void KBD_Update(void)
if (!mx && !my) if (!mx && !my)
SDL_GetRelativeMouseState(&mx, &my); SDL_GetRelativeMouseState(&mx, &my);
#ifdef Joystick
if (joystick_avail && joy) { if (joystick_avail && joy) {
jx = SDL_JoystickGetAxis(joy, 0); jx = SDL_JoystickGetAxis(joy, lr_axis);
jy = SDL_JoystickGetAxis(joy, 1); jy = SDL_JoystickGetAxis(joy, ud_axis);
jt = SDL_JoystickGetAxis(joy, 3); jt = SDL_JoystickGetAxis(joy, throttle_axis);
} }
#endif
mouse_buttonstate = 0; mouse_buttonstate = 0;
bstate = SDL_GetMouseState(NULL, NULL); bstate = SDL_GetMouseState(NULL, NULL);
if (SDL_BUTTON(1) & bstate) if (SDL_BUTTON(1) & bstate)

View file

@ -43,6 +43,10 @@
#include <ctype.h> #include <ctype.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/types.h> #include <sys/types.h>
#ifdef HAVE_JOYSTICK
#include <sys/stat.h>
#include <fcntl.h>
#endif
#include <unistd.h> #include <unistd.h>
#include <signal.h> #include <signal.h>
#include <stdlib.h> #include <stdlib.h>
@ -60,10 +64,11 @@
#ifdef HAVE_XF86_DGA #ifdef HAVE_XF86_DGA
#include <X11/extensions/xf86dga.h> #include <X11/extensions/xf86dga.h>
#endif #endif
#ifdef HAVE_XF86_VIDMODE
#include <X11/extensions/xf86vmode.h>
#endif
#ifdef HAVE_JOYSTICK #ifdef HAVE_JOYSTICK
# include <sys/stat.h>
# include <fcntl.h>
# include <linux/joystick.h> # include <linux/joystick.h>
# include <glob.h> # include <glob.h>
#endif #endif
@ -285,7 +290,7 @@ static cvar_t *vid_ypos; // Y coordinate of window position
static cvar_t *in_joystick; static cvar_t *in_joystick;
static qboolean joystick_avail = false; static qboolean joystick_avail = false;
static int joy_fd, jx, jy, jt; static int joy_fd, jx, jy, jt;
static cvar_t * joystick_invert_y; static cvar_t *j_invert_y;
#endif #endif
static qboolean mlooking; static qboolean mlooking;
@ -367,7 +372,7 @@ void RW_IN_Init(in_state_t *in_state_p){
in_dgamouse = ri.Cvar_Get ("in_dgamouse", "1", CVAR_ARCHIVE); in_dgamouse = ri.Cvar_Get ("in_dgamouse", "1", CVAR_ARCHIVE);
#ifdef HAVE_JOYSTICK #ifdef HAVE_JOYSTICK
in_joystick = ri.Cvar_Get("in_joystick", "1", CVAR_ARCHIVE); in_joystick = ri.Cvar_Get("in_joystick", "1", CVAR_ARCHIVE);
joystick_invert_y = ri.Cvar_Get("joystick_invert_y", "1", CVAR_ARCHIVE); j_invert_y = ri.Cvar_Get("j_invert_y", "1", CVAR_ARCHIVE);
#endif #endif
freelook = ri.Cvar_Get( "freelook", "0", 0 ); freelook = ri.Cvar_Get( "freelook", "0", 0 );
lookstrafe = ri.Cvar_Get ("lookstrafe", "0", 0); lookstrafe = ri.Cvar_Get ("lookstrafe", "0", 0);
@ -492,7 +497,7 @@ void RW_IN_Move(usercmd_t *cmd) {
in_state->viewangles[YAW] -= m_yaw->value * (jx/100); in_state->viewangles[YAW] -= m_yaw->value * (jx/100);
if ((mlooking || freelook->value) && !(*in_state->in_strafe_state & 1)) { if ((mlooking || freelook->value) && !(*in_state->in_strafe_state & 1)) {
if (joystick_invert_y) if (j_invert_y)
in_state->viewangles[PITCH] -= m_pitch->value * (jy/100); in_state->viewangles[PITCH] -= m_pitch->value * (jy/100);
else else
in_state->viewangles[PITCH] += m_pitch->value * (jy/100); in_state->viewangles[PITCH] += m_pitch->value * (jy/100);
@ -615,6 +620,12 @@ static void IN_ActivateMouse( void )
install_grabs(); install_grabs();
mouse_active = true; mouse_active = true;
} }
#ifdef Joystick
if (joystick_avail)
if (close(joy_fd))
ri.Con_Printf(PRINT_ALL, "Error, Problem closing joystick.");
#endif
} }
void RW_IN_Frame (void) void RW_IN_Frame (void)
@ -691,7 +702,6 @@ char *RW_Sys_GetClipboardData()
} }
/*****************************************************************************/ /*****************************************************************************/
void ResetFrameBuffer(void) void ResetFrameBuffer(void)
{ {
int mem; int mem;