2001-04-11 19:56:01 +00:00
|
|
|
/*
|
|
|
|
joy.c
|
|
|
|
|
|
|
|
Joystick input interface
|
|
|
|
|
|
|
|
Copyright (C) 2000 David Jeffery
|
|
|
|
Copyright (C) 2000 Jeff Teunissen <deek@dusknet.dhs.org>
|
2001-12-28 18:02:33 +00:00
|
|
|
Copyright (C) 2001 Ragnvald `Despair` Maartmann-Moe IV
|
2001-04-11 19:56:01 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2005-08-04 15:27:09 +00:00
|
|
|
static __attribute__ ((used)) const char rcsid[] =
|
2003-01-15 15:31:36 +00:00
|
|
|
"$Id$";
|
|
|
|
|
2001-04-11 19:56:01 +00:00
|
|
|
#include "QF/cvar.h"
|
|
|
|
#include "QF/input.h"
|
|
|
|
#include "QF/joystick.h"
|
|
|
|
#include "QF/keys.h"
|
|
|
|
#include "QF/mathlib.h"
|
2007-11-06 10:17:14 +00:00
|
|
|
#include "QF/sys.h"
|
2001-12-28 18:02:33 +00:00
|
|
|
#include "QF/va.h"
|
2001-04-11 19:56:01 +00:00
|
|
|
|
2001-08-17 08:35:26 +00:00
|
|
|
#include "compat.h"
|
|
|
|
|
2001-04-11 19:56:01 +00:00
|
|
|
cvar_t *joy_device; // Joystick device name
|
|
|
|
cvar_t *joy_enable; // Joystick enabling flag
|
2001-08-17 08:35:26 +00:00
|
|
|
cvar_t *joy_amp; // Joystick amplification
|
|
|
|
cvar_t *joy_pre_amp; // Joystick pre-amplification
|
|
|
|
|
2001-04-11 19:56:01 +00:00
|
|
|
qboolean joy_found = false;
|
|
|
|
qboolean joy_active = false;
|
|
|
|
|
|
|
|
typedef struct {
|
2003-01-06 18:28:13 +00:00
|
|
|
const char *name;
|
|
|
|
const char *string;
|
2001-04-11 19:56:01 +00:00
|
|
|
} ocvar_t;
|
|
|
|
|
|
|
|
ocvar_t joy_axes_cvar_init[JOY_MAX_AXES] = {
|
|
|
|
{"joyaxis1", "1"},
|
|
|
|
{"joyaxis2", "2"},
|
|
|
|
{"joyaxis3", "3"},
|
|
|
|
{"joyaxis4", "0"},
|
|
|
|
{"joyaxis5", "0"},
|
2001-12-28 18:02:33 +00:00
|
|
|
{"joyaxis6", "0"},
|
|
|
|
{"joyaxis7", "0"},
|
|
|
|
{"joyaxis8", "0"}
|
2001-04-11 19:56:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct joy_axis joy_axes[JOY_MAX_AXES];
|
|
|
|
struct joy_button joy_buttons[JOY_MAX_BUTTONS];
|
|
|
|
|
2001-08-27 01:00:03 +00:00
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-04-11 19:56:01 +00:00
|
|
|
JOY_Command (void)
|
|
|
|
{
|
|
|
|
JOY_Read ();
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-04-11 19:56:01 +00:00
|
|
|
JOY_Move (void)
|
|
|
|
{
|
2001-08-17 08:35:26 +00:00
|
|
|
float mult_joy;
|
2001-04-11 19:56:01 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!joy_active || !joy_enable->int_val)
|
|
|
|
return;
|
|
|
|
|
2001-12-30 06:08:24 +00:00
|
|
|
mult_joy = (joy_amp->value * joy_pre_amp->value *
|
|
|
|
in_amp->value * in_pre_amp->value) / 200.0;
|
2001-08-17 08:35:26 +00:00
|
|
|
// Yes, mult_joy looks like a mess, but use of pre_amp values is useful in
|
|
|
|
// scripts, and *_pre_amp will matter once joystick filtering/acceleration
|
|
|
|
// is implemented
|
|
|
|
|
2001-04-11 19:56:01 +00:00
|
|
|
for (i = 0; i < JOY_MAX_AXES; i++) {
|
|
|
|
switch (joy_axes[i].axis->int_val) {
|
|
|
|
case 1:
|
2001-12-30 06:08:24 +00:00
|
|
|
if (joy_axes[i].current)
|
|
|
|
viewdelta.angles[YAW] -= joy_axes[i].current * mult_joy;
|
2001-12-28 18:02:33 +00:00
|
|
|
break;
|
|
|
|
case -1:
|
2001-12-30 06:08:24 +00:00
|
|
|
if (joy_axes[i].current)
|
|
|
|
viewdelta.angles[YAW] += joy_axes[i].current * mult_joy;
|
2001-04-11 19:56:01 +00:00
|
|
|
break;
|
|
|
|
case 2:
|
2001-12-30 06:08:24 +00:00
|
|
|
if (joy_axes[i].current)
|
|
|
|
viewdelta.position[2] -= joy_axes[i].current * mult_joy;
|
2001-12-28 18:02:33 +00:00
|
|
|
break;
|
|
|
|
case -2:
|
2001-12-30 06:08:24 +00:00
|
|
|
if (joy_axes[i].current)
|
|
|
|
viewdelta.position[2] += joy_axes[i].current * mult_joy;
|
2001-04-11 19:56:01 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
2001-12-30 06:08:24 +00:00
|
|
|
if (joy_axes[i].current)
|
|
|
|
viewdelta.position[0] += joy_axes[i].current * mult_joy;
|
2001-12-28 18:02:33 +00:00
|
|
|
break;
|
|
|
|
case -3:
|
2001-12-30 06:08:24 +00:00
|
|
|
if (joy_axes[i].current)
|
|
|
|
viewdelta.position[0] -= joy_axes[i].current * mult_joy;
|
2001-04-11 19:56:01 +00:00
|
|
|
break;
|
|
|
|
case 4:
|
2001-12-30 06:08:24 +00:00
|
|
|
if (joy_axes[i].current)
|
|
|
|
viewdelta.angles[PITCH] -= joy_axes[i].current * mult_joy;
|
2001-12-28 18:02:33 +00:00
|
|
|
break;
|
|
|
|
case -4:
|
2001-12-30 06:08:24 +00:00
|
|
|
if (joy_axes[i].current)
|
|
|
|
viewdelta.angles[PITCH] += joy_axes[i].current * mult_joy;
|
2001-12-28 18:02:33 +00:00
|
|
|
break;
|
|
|
|
case 5:
|
2001-12-30 06:08:24 +00:00
|
|
|
if (joy_axes[i].current)
|
|
|
|
viewdelta.position[1] -= joy_axes[i].current * mult_joy;
|
2001-04-11 19:56:01 +00:00
|
|
|
break;
|
2001-12-28 18:02:33 +00:00
|
|
|
case -5:
|
2001-12-30 06:08:24 +00:00
|
|
|
if (joy_axes[i].current)
|
|
|
|
viewdelta.position[1] += joy_axes[i].current * mult_joy;
|
2001-12-28 18:02:33 +00:00
|
|
|
break;
|
2004-01-06 03:38:16 +00:00
|
|
|
// Futureproofing
|
|
|
|
case 6:
|
|
|
|
if (joy_axes[i].current)
|
|
|
|
viewdelta.angles[ROLL] += joy_axes[i].current * mult_joy;
|
|
|
|
break;
|
|
|
|
case -6:
|
|
|
|
if (joy_axes[i].current)
|
|
|
|
viewdelta.angles[ROLL] -= joy_axes[i].current * mult_joy;
|
|
|
|
break;
|
2001-12-28 18:02:33 +00:00
|
|
|
default:
|
|
|
|
break;
|
2001-04-11 19:56:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-04-11 19:56:01 +00:00
|
|
|
JOY_Init (void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (JOY_Open () == -1) {
|
2010-11-26 23:48:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_VID, "JOY: Joystick not found.\n");
|
2001-04-11 19:56:01 +00:00
|
|
|
joy_found = false;
|
|
|
|
joy_active = false;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
joy_found = true;
|
|
|
|
|
|
|
|
if (!joy_enable->int_val) {
|
2010-11-26 23:48:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_VID, "JOY: Joystick found, but not enabled.\n");
|
2001-04-11 19:56:01 +00:00
|
|
|
joy_active = false;
|
|
|
|
JOY_Close ();
|
|
|
|
}
|
|
|
|
|
2010-11-26 23:48:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_VID, "JOY: Joystick found and activated.\n");
|
2001-04-11 19:56:01 +00:00
|
|
|
|
|
|
|
// Initialize joystick if found and enabled
|
|
|
|
for (i = 0; i < JOY_MAX_BUTTONS; i++) {
|
|
|
|
joy_buttons[i].old = 0;
|
|
|
|
joy_buttons[i].current = 0;
|
2001-12-28 18:02:33 +00:00
|
|
|
}
|
2001-04-11 19:56:01 +00:00
|
|
|
joy_active = true;
|
|
|
|
}
|
|
|
|
|
2001-12-28 18:02:33 +00:00
|
|
|
static void
|
|
|
|
joyamp_f (cvar_t *var)
|
|
|
|
{
|
|
|
|
Cvar_Set (var, va ("%g", max (0.0001, var->value)));
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-04-11 19:56:01 +00:00
|
|
|
JOY_Init_Cvars (void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2001-08-17 08:35:26 +00:00
|
|
|
joy_device = Cvar_Get ("joy_device", "/dev/js0", CVAR_NONE | CVAR_ROM, 0,
|
|
|
|
"Joystick device");
|
|
|
|
joy_enable = Cvar_Get ("joy_enable", "1", CVAR_NONE | CVAR_ARCHIVE, 0,
|
|
|
|
"Joystick enable flag");
|
2001-12-28 18:02:33 +00:00
|
|
|
joy_amp = Cvar_Get ("joy_amp", "1", CVAR_NONE | CVAR_ARCHIVE, joyamp_f,
|
2001-08-17 08:35:26 +00:00
|
|
|
"Joystick amplification");
|
2001-12-28 18:02:33 +00:00
|
|
|
joy_pre_amp = Cvar_Get ("joy_pre_amp", "1", CVAR_NONE | CVAR_ARCHIVE,
|
|
|
|
joyamp_f, "Joystick pre-amplification");
|
2001-04-11 19:56:01 +00:00
|
|
|
|
|
|
|
for (i = 0; i < JOY_MAX_AXES; i++) {
|
|
|
|
joy_axes[i].axis = Cvar_Get (joy_axes_cvar_init[i].name,
|
|
|
|
joy_axes_cvar_init[i].string,
|
|
|
|
CVAR_ARCHIVE, 0, "Set joystick axes");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-10 12:00:59 +00:00
|
|
|
VISIBLE void
|
2001-04-11 19:56:01 +00:00
|
|
|
JOY_Shutdown (void)
|
|
|
|
{
|
|
|
|
if (!joy_active)
|
|
|
|
return;
|
|
|
|
|
|
|
|
JOY_Close ();
|
|
|
|
|
|
|
|
joy_active = false;
|
|
|
|
joy_found = false;
|
|
|
|
}
|