From fb55453675ee4885f490ad8c16b53d6950770c5e Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Tue, 26 Sep 2000 03:21:06 +0000 Subject: [PATCH] David Jeffery's linux joystick support ala nuq --- configure.in | 2 +- include/joystick.h | 25 +++++++ source/Makefile.am | 2 +- source/in_svgalib.c | 3 + source/in_x11.c | 3 + source/joystick.c | 164 ++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 197 insertions(+), 2 deletions(-) create mode 100644 include/joystick.h create mode 100644 source/joystick.c diff --git a/configure.in b/configure.in index 9bae6e4..4f34c28 100644 --- a/configure.in +++ b/configure.in @@ -64,7 +64,7 @@ AC_CHECK_HEADERS( dsound.h mmsystem.h initguid.h mme/mmsystem.h mme/mme_public.h \ sys/asoundlib.h string.h strings.h memory.h malloc.h \ errno.h sys/dir.h fnmatch.h \ - mgraph.h + mgraph.h linux/joystick.h ) diff --git a/include/joystick.h b/include/joystick.h new file mode 100644 index 0000000..0cbc5d2 --- /dev/null +++ b/include/joystick.h @@ -0,0 +1,25 @@ + +/* + * Copyright (C) 2000 David Jeffery + */ + +/* + * 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 the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +void joystick_init(void); +void joystick_command(void); +void joystick_move(usercmd_t *cmd); diff --git a/source/Makefile.am b/source/Makefile.am index de795fe..c3ec758 100644 --- a/source/Makefile.am +++ b/source/Makefile.am @@ -136,7 +136,7 @@ client_SOURCES= cl_cmd.c cl_cvar.c cl_demo.c cl_ents.c cl_input.c cl_main.c \ cl_misc.c cl_parse.c cl_pred.c cl_tent.c cl_cam.c teamplay.c \ r_view.c wad.c model_alias.c model_sprite.c \ console.c keys.c menu.c nonintel.c skin.c sbar.c \ - cl_slist.c $(client_ASM) + cl_slist.c $(client_ASM) joystick.c # # Software-rendering clients diff --git a/source/in_svgalib.c b/source/in_svgalib.c index 6f7a511..39cb8c7 100644 --- a/source/in_svgalib.c +++ b/source/in_svgalib.c @@ -38,6 +38,7 @@ #include "cvar.h" #include "cmd.h" #include "qargs.h" +#include "joystick.h" #include #include @@ -112,6 +113,8 @@ int IN_Init(void) if (UseMouse) IN_init_mouse(); + joystick_init(); + in_svgalib_inited = 1; return 1; } diff --git a/source/in_x11.c b/source/in_x11.c index 16398c8..79511bf 100644 --- a/source/in_x11.c +++ b/source/in_x11.c @@ -60,6 +60,7 @@ #include "client.h" #include "context_x11.h" #include "qargs.h" +#include "joystick.h" #ifdef HAVE_STRINGS_H #include @@ -424,6 +425,8 @@ IN_Init(void) XChangeWindowAttributes(x_disp, x_win, attribmask, &attribs_2); } + joystick_init(); + _windowed_mouse = Cvar_Get ("_windowed_mouse","0",CVAR_ARCHIVE,"None"); m_filter = Cvar_Get ("m_filter","0",CVAR_ARCHIVE,"None"); diff --git a/source/joystick.c b/source/joystick.c new file mode 100644 index 0000000..572de30 --- /dev/null +++ b/source/joystick.c @@ -0,0 +1,164 @@ + +/* + * Copyright (C) 2000 David Jeffery + */ + +/* + * 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 the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#ifdef HAVE_LINUX_JOYSTICK_H + +#include +#include +#include + +#include "console.h" +#include "client.h" +#include "cvar.h" +#include "keys.h" + +//joystick variables and structures +#define MAX_JOYAXIS 6 +#define MAX_JOYBUTTON 10 +#define JOYLOOK_MAX 200 +#define JOYMOVE_MAX 200 +int joydev; + +typedef struct { + char *name; + char *string; +} ocvar_t; + +struct joyaxis{ + int current; + ocvar_t var; + cvar_t *axis; +}; + +struct joybutton{ + int old; + int current; +}; + +struct joyaxis joyaxiscontrol[MAX_JOYAXIS] = { + {0,{"joyaxis1","1"}} , + {0,{"joyaxis2","2"}} , + {0,{"joyaxis3","3"}} , + {0,{"joyaxis4","0"}} , + {0,{"joyaxis5","0"}} , + {0,{"joyaxis6","0"}} +}; + +struct joybutton joybuttoncontrol[MAX_JOYBUTTON]; +int joyfound = 0; + +void joystick_command(void) +{ + struct js_event event; + + if(!joyfound) + return; + while(read(joydev, &event, sizeof(struct js_event)) > -1){ + if(event.type & JS_EVENT_BUTTON){ + if(event.number >= MAX_JOYBUTTON) + continue; + joybuttoncontrol[event.number].current = event.value; + if(joybuttoncontrol[event.number].current > + joybuttoncontrol[event.number].old) + Key_Event(K_AUX1 + event.number, true); + else if(joybuttoncontrol[event.number].current < + joybuttoncontrol[event.number].old) + Key_Event(K_AUX1 + event.number, false); + joybuttoncontrol[event.number].old = + joybuttoncontrol[event.number].current; + } + else if(event.type & JS_EVENT_AXIS){ + if(event.number >= MAX_JOYAXIS) + continue; + joyaxiscontrol[event.number].current = event.value; + } + } +} + +void joystick_move(usercmd_t *cmd) +{ + int i; + if(!joyfound) + return; + + for(i = 0; i < MAX_JOYAXIS; i++) { + switch(joyaxiscontrol[i].axis->int_val){ + case 1: + cl.viewangles[YAW] -= m_yaw->value * (float)joyaxiscontrol[i].current/JOYLOOK_MAX; + break; + case 2: + cmd->forwardmove -= m_forward->value * (float)joyaxiscontrol[i].current/JOYMOVE_MAX; + break; + case 3: + cmd->sidemove += m_side->value * (float)joyaxiscontrol[i].current/JOYMOVE_MAX; + break; + case 4: + if(joyaxiscontrol[i].current){ + V_StopPitchDrift(); + cl.viewangles[PITCH] -= m_pitch->value * (float)joyaxiscontrol[i].current/JOYLOOK_MAX; + if(cl.viewangles[PITCH] > 80) + cl.viewangles[PITCH] = 80; + else if(cl.viewangles[PITCH] < -70) + cl.viewangles[PITCH] = -70; + } + break; + } + } +} + +void joystick_init(void) +{ + /*initialize joystick if found */ + if ((joydev = open("/dev/js0", O_RDONLY | O_NONBLOCK)) < 0) { + joyfound = 0; + Con_DPrintf("no joystick\n"); + } else { + int i; + joyfound = 1; + for(i = 0; i < MAX_JOYAXIS; i++) { + joyaxiscontrol[i].axis = Cvar_Get(joyaxiscontrol[i].var.name, + joyaxiscontrol[i].var.string, + CVAR_ARCHIVE, "None"); + } + for(i = 0; i < MAX_JOYBUTTON; i++) { + joybuttoncontrol[i].old = 0; + joybuttoncontrol[i].current = 0; + } + Con_DPrintf("joystick enabled\n"); + } +} + +#else +void joystick_init(void){ +} + +void joystick_command(void){ +} + +void joystick_move(usercmd_t *cmd){ +} +#endif +