Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 378 lines (309 sloc) 10.112 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
/*
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.

$Id: in_linux.c,v 1.12 2007-10-04 13:48:11 dkure Exp $
*/

#include "quakedef.h"
#include "input.h"
#include "keys.h"

#include <sys/time.h>

#ifdef _Soft_SVGA
#include <vgamouse.h>
#endif

#ifdef WITH_JOYSTICK
#include <linux/joystick.h>

#define JOYSTICK_DEVICE_S "/dev/input/js0"
static int joystick = 0;
#endif // WITH_JOYSTICK

#ifdef WITH_JOYSTICK
// none of these cvars are saved over a session.
// this means that advanced controller configuration needs to be executed each time.
// this avoids any problems with getting back to a default usage or when changing from one controller to another.
// this way at least something works.
cvar_t in_joystick = {"joystick","0",CVAR_ARCHIVE};
cvar_t joy_name = {"joyname", "joystick"};
cvar_t joy_advanced = {"joyadvanced", "0"};
cvar_t joy_advaxisx = {"joyadvaxisx", "0"};
cvar_t joy_advaxisy = {"joyadvaxisy", "0"};
cvar_t joy_advaxisz = {"joyadvaxisz", "0"};
cvar_t joy_advaxisr = {"joyadvaxisr", "0"};
cvar_t joy_advaxisu = {"joyadvaxisu", "0"};
cvar_t joy_advaxisv = {"joyadvaxisv", "0"};
cvar_t joy_forwardthreshold = {"joyforwardthreshold", "0.15"};
cvar_t joy_sidethreshold = {"joysidethreshold", "0.15"};
cvar_t joy_flysensitivity = {"joyflysensitivity", "-1.0"};
cvar_t joy_flythreshold = {"joyflythreshold", "0.15"};
cvar_t joy_pitchthreshold = {"joypitchthreshold", "0.15"};
cvar_t joy_yawthreshold = {"joyyawthreshold", "0.15"};
cvar_t joy_forwardsensitivity = {"joyforwardsensitivity", "-1.0"};
cvar_t joy_sidesensitivity = {"joysidesensitivity", "-1.0"};
cvar_t joy_pitchsensitivity = {"joypitchsensitivity", "1.0"};
cvar_t joy_yawsensitivity = {"joyyawsensitivity", "-1.0"};
cvar_t joy_wwhack1 = {"joywwhack1", "0.0"};
cvar_t joy_wwhack2 = {"joywwhack2", "0.0"};
#endif // WITH_JOYSTICK

qbool joy_avail, joy_advancedinit, joy_haspov;
DWORD joy_oldbuttonstate, joy_oldpovstate;
DWORD joy_numbuttons;

#ifdef WITH_JOYSTICK
void IN_StartupJoystick (void);
void IN_DeactivateJoystick (void);
void IN_JoyMove (usercmd_t *cmd);
#endif // WITH_JOYSTICK

#ifdef NDEBUG
#define win_mouse "1"
#else
#define win_mouse "0"
#endif

cvar_t m_filter = {"m_filter", "0", CVAR_SILENT};
cvar_t cl_keypad = {"cl_keypad", "1", CVAR_SILENT};
cvar_t _windowed_mouse = {"_windowed_mouse", win_mouse, CVAR_ARCHIVE | CVAR_SILENT};
cvar_t m_showrate = {"m_showrate", "0", CVAR_SILENT};

extern int mx, my;
extern qbool mouseinitialized;


void IN_StartupMouse( void );

#ifdef GLQUAKE
void IN_DeactivateMouse( void );
void IN_Restart_f(void);
#endif

float mouse_x, mouse_y;

void IN_MouseMove (usercmd_t *cmd)
{
static int old_mouse_x = 0, old_mouse_y = 0;

if (!mouseinitialized)
return;

#ifdef _Soft_SVGA
// poll mouse values
while (mouse_update())
; // FIXME: is there was missed ; or it was ok ?
#endif

//
// Do not move the player if we're in HUD editor or menu mode.
// And don't apply ingame sensitivity, since that will make movements jerky.
//
if(key_dest == key_hudeditor || key_dest == key_menu)
{
old_mouse_x = mouse_x = mx * cursor_sensitivity.value;
old_mouse_y = mouse_y = my * cursor_sensitivity.value;
}
else
{
// Normal game mode.

if (m_filter.value) {
float filterfrac = bound (0.0f, m_filter.value, 1.0f) / 2.0f;
mouse_x = (mx * (1.0f - filterfrac) + old_mouse_x * filterfrac);
mouse_y = (my * (1.0f - filterfrac) + old_mouse_y * filterfrac);
} else {
mouse_x = mx;
mouse_y = my;
}

old_mouse_x = mx;
old_mouse_y = my;

if (m_accel.value) {
float mousespeed = (sqrt (mx * mx + my * my)) / (1000.0f * (float) cls.trueframetime);
mouse_x *= (mousespeed * m_accel.value + sensitivity.value);
mouse_y *= (mousespeed * m_accel.value + sensitivity.value);
} else {
mouse_x *= sensitivity.value;
mouse_y *= sensitivity.value;
}

// add mouse X/Y movement to cmd
if ((in_strafe.state & 1) || (lookstrafe.value && mlook_active))
cmd->sidemove += m_side.value * mouse_x;
else
cl.viewangles[YAW] -= m_yaw.value * mouse_x;

if (mlook_active)
V_StopPitchDrift ();

if (mlook_active && !(in_strafe.state & 1))
{
cl.viewangles[PITCH] += m_pitch.value * mouse_y;
if (cl.viewangles[PITCH] > cl.maxpitch)
cl.viewangles[PITCH] = cl.maxpitch;
if (cl.viewangles[PITCH] < cl.minpitch)
cl.viewangles[PITCH] = cl.minpitch;
} else {
cmd->forwardmove -= m_forward.value * mouse_y;
}
}

mx = my = 0; // clear for next update
}

void IN_Move (usercmd_t *cmd)
{
static struct timeval old_tv = {0, 0};
struct timeval tv;
static long old_mouserate = 0;
long usec, mouserate;
if (m_showrate.value && (mx || my)) {
gettimeofday(&tv, NULL);
usec = (tv.tv_sec - old_tv.tv_sec) * 1000000L + (tv.tv_usec - old_tv.tv_usec);
mouserate = usec ? 1000000L / usec : old_mouserate;
Com_Printf("mouse rate: %4ld\n", (mouserate + old_mouserate) / 2);
old_tv = tv;
old_mouserate = mouserate;
}

IN_MouseMove (cmd);
}

void IN_Init (void)
{
#ifdef GLQUAKE
#ifdef WITH_EVDEV
int i;
#endif
#endif

Cvar_SetCurrentGroup (CVAR_GROUP_INPUT_MOUSE);
Cvar_Register (&m_filter);
Cvar_Register (&m_showrate);
#ifndef _Soft_SVGA
Cvar_Register (&_windowed_mouse);
#endif

Cvar_SetCurrentGroup (CVAR_GROUP_INPUT_KEYBOARD);
Cvar_Register (&cl_keypad);
Cvar_ResetCurrentGroup ();

if (!host_initialized)
{
#ifdef GLQUAKE
typedef enum { mt_none = 0, mt_dga, mt_normal, mt_evdev } mousetype_t;
extern cvar_t in_mouse;
#ifdef WITH_EVDEV
extern cvar_t in_mmt;
extern cvar_t in_evdevice;
#endif /* !WITH_EVDEV */

if (COM_CheckParm ("-nodga") || COM_CheckParm ("-nomdga"))
Cvar_LatchedSetValue (&in_mouse, mt_normal);

#ifdef WITH_EVDEV
if ((i = COM_CheckParm ("-mevdev")) && (i < COM_Argc() - 1)) {
Cvar_LatchedSet (&in_evdevice, COM_Argv(i + 1));
Cvar_LatchedSetValue (&in_mouse, mt_evdev);
}

if (COM_CheckParm ("-mmt"))
Cvar_LatchedSetValue (&in_mmt, 1);
#endif /* !WITH_EVDEV */

if (COM_CheckParm ("-nomouse"))
Cvar_LatchedSetValue (&in_mouse, mt_none);

#ifdef WITH_EVDEV
extern void IN_EvdevList_f(void);
Cmd_AddCommand ("in_evdevlist", IN_EvdevList_f);
#endif /* !WITH_EVDEV */
#endif /* !GLQUAKE */

#ifdef WITH_KEYMAP
IN_StartupKeymap();
#endif // WITH_KEYMAP
#ifdef GLQUAKE
Cmd_AddCommand ("in_restart", IN_Restart_f);
#endif
}

IN_StartupMouse ();
#ifdef WITH_JOYSTICK
IN_StartupJoystick ();
#endif // WITH_JOYSTICK
}

void IN_Shutdown(void)
{
//#if defined (_Soft_X11) || defined (_Soft_SVGA)

#ifdef _Soft_SVGA
if (mouseinitialized)
mouse_close();
#endif

#ifdef GLQUAKE
  IN_DeactivateMouse(); // btw we trying de init this in video shutdown too...
#endif

mouseinitialized = false;

#ifdef WITH_JOYSTICK
IN_DeactivateJoystick();
#endif // WITH_JOYSTICK

//#endif
}

#ifdef WITH_JOYSTICK
void IN_StartupJoystick (void) {
int value = 0;
        char joy_desc[1024];
 
  // assume no joystick
joy_avail = false;

// only initialize if the user wants it
if (!COM_CheckParm ("-joystick"))
return;

Cvar_SetCurrentGroup(CVAR_GROUP_INPUT_JOY);
Cvar_Register (&joy_name);
Cvar_Register (&joy_advanced);
Cvar_Register (&joy_advaxisx);
Cvar_Register (&joy_advaxisy);
Cvar_Register (&joy_advaxisz);
Cvar_Register (&joy_advaxisr);
Cvar_Register (&joy_advaxisu);
Cvar_Register (&joy_advaxisv);
Cvar_Register (&joy_forwardthreshold);
Cvar_Register (&joy_sidethreshold);
Cvar_Register (&joy_flythreshold);
Cvar_Register (&joy_pitchthreshold);
Cvar_Register (&joy_yawthreshold);
Cvar_Register (&joy_forwardsensitivity);
Cvar_Register (&joy_sidesensitivity);
Cvar_Register (&joy_flysensitivity);
Cvar_Register (&joy_pitchsensitivity);
Cvar_Register (&joy_yawsensitivity);
Cvar_Register (&joy_wwhack1);
Cvar_Register (&joy_wwhack2);
Cvar_ResetCurrentGroup();

joystick = open (JOYSTICK_DEVICE_S, O_RDONLY|O_NONBLOCK);
        if (joystick == 0) {
Com_Printf ("\njoystick not found -- driver not present\n\n");
return;
        }

        // save the joystick's number of buttons and POV status
        ioctl (joystick, JSIOCGNAME (sizeof (joy_desc)), joy_desc);
        ioctl (joystick, JSIOCGBUTTONS, &value);
joy_numbuttons = value;
joy_haspov = 0;

// old button and POV states default to no buttons pressed
joy_oldbuttonstate = joy_oldpovstate = 0;

// mark the joystick as available and advanced initialization not completed
// this is needed as cvars are not available during initialization
joy_avail = true;
joy_advancedinit = false;

Com_Printf ("\njoystick detected (%s, %d buttons)\n\n", joy_desc, joy_numbuttons);

}

void IN_DeactivateJoystick (void) {
if (joystick)
close (joystick);
}

void IN_CommandsJoystick (void) {
int i, key_index;
DWORD buttonstate;//, povstate;
        struct js_event js;

if (!joy_avail)
return;

if (read (joystick, &js, sizeof (struct js_event)) != sizeof (struct js_event))
          return;

        switch (js.type & ~JS_EVENT_INIT) {
case JS_EVENT_BUTTON:
buttonstate = js.value;
i = js.number;
if (js.value) {
key_index = (i < 4) ? K_JOY1 : K_AUX1;
Key_Event (key_index + i, true);
}

if (!js.value) {
key_index = (i < 4) ? K_JOY1 : K_AUX1;
Key_Event (key_index + i, false);
}
joy_oldbuttonstate = buttonstate;
}
}

void IN_JoyMove (usercmd_t *cmd) {
}
#endif // WITH_JOYSTICK

int ctrlDown = 0;
int shiftDown = 0;
int altDown = 0;

int isAltDown(void) {return altDown;}
int isCtrlDown(void) {return ctrlDown;}
int isShiftDown(void) {return shiftDown;}
Something went wrong with that request. Please try again.