From 4d5f56531b77b7cbde442e442c2b15674da32c8c Mon Sep 17 00:00:00 2001 From: havoc Date: Wed, 18 May 2011 13:18:43 +0000 Subject: [PATCH] implemented support for Windows XInput (using different keys/cvars to allow non-conflicting configs) in vid_shared.c, usable in WGL and SDL clients on Windows redesigned joystick code to encode any joystick data into a vid_joystate_t struct and use a common code path for all handling of axes and button events (also rewrote joy_axiskeyevents as part of this) changed joystick deadzone handling to make values start at 0 at the edge of the deadzone removed legacy joystick code in vid_wgl.c because it was too annoying to maintain removed some hacky SDL 1.3 iOS code that is no longer needed git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@11154 d7cf8633-e32d-0410-b094-e92efae38249 ::stable-branch::merge=5737591d891d69836735abb645a1b173baaabe96 --- keys.c | 25 ++ keys.h | 26 ++ vid.h | 27 +++ vid_agl.c | 27 +++ vid_glx.c | 27 +++ vid_null.c | 4 + vid_sdl.c | 318 ++++++------------------ vid_shared.c | 438 +++++++++++++++++++++++++++++++++ vid_wgl.c | 674 +++------------------------------------------------ 9 files changed, 682 insertions(+), 884 deletions(-) diff --git a/keys.c b/keys.c index a24e83fb..c955570c 100644 --- a/keys.c +++ b/keys.c @@ -473,6 +473,31 @@ static const keyname_t keynames[] = { {"AUX31", K_AUX31}, {"AUX32", K_AUX32}, + {"X360_DPAD_UP", K_X360_DPAD_UP}, + {"X360_DPAD_DOWN", K_X360_DPAD_DOWN}, + {"X360_DPAD_LEFT", K_X360_DPAD_LEFT}, + {"X360_DPAD_RIGHT", K_X360_DPAD_RIGHT}, + {"X360_START", K_X360_START}, + {"X360_BACK", K_X360_BACK}, + {"X360_LEFT_THUMB", K_X360_LEFT_THUMB}, + {"X360_RIGHT_THUMB", K_X360_RIGHT_THUMB}, + {"X360_LEFT_SHOULDER", K_X360_LEFT_SHOULDER}, + {"X360_RIGHT_SHOULDER", K_X360_RIGHT_SHOULDER}, + {"X360_A", K_X360_A}, + {"X360_B", K_X360_B}, + {"X360_X", K_X360_X}, + {"X360_Y", K_X360_Y}, + {"X360_LEFT_TRIGGER", K_X360_LEFT_TRIGGER}, + {"X360_RIGHT_TRIGGER", K_X360_RIGHT_TRIGGER}, + {"X360_LEFT_THUMB_UP", K_X360_LEFT_THUMB_UP}, + {"X360_LEFT_THUMB_DOWN", K_X360_LEFT_THUMB_DOWN}, + {"X360_LEFT_THUMB_LEFT", K_X360_LEFT_THUMB_LEFT}, + {"X360_LEFT_THUMB_RIGHT", K_X360_LEFT_THUMB_RIGHT}, + {"X360_RIGHT_THUMB_UP", K_X360_RIGHT_THUMB_UP}, + {"X360_RIGHT_THUMB_DOWN", K_X360_RIGHT_THUMB_DOWN}, + {"X360_RIGHT_THUMB_LEFT", K_X360_RIGHT_THUMB_LEFT}, + {"X360_RIGHT_THUMB_RIGHT", K_X360_RIGHT_THUMB_RIGHT}, + {"SEMICOLON", ';'}, // because a raw semicolon separates commands {"TILDE", '~'}, {"BACKQUOTE", '`'}, diff --git a/keys.h b/keys.h index c330454b..a02acf2f 100644 --- a/keys.h +++ b/keys.h @@ -188,6 +188,32 @@ typedef enum keynum_e K_AUX31, K_AUX32, + // Microsoft Xbox 360 Controller For Windows + K_X360_DPAD_UP, + K_X360_DPAD_DOWN, + K_X360_DPAD_LEFT, + K_X360_DPAD_RIGHT, + K_X360_START, + K_X360_BACK, + K_X360_LEFT_THUMB, + K_X360_RIGHT_THUMB, + K_X360_LEFT_SHOULDER, + K_X360_RIGHT_SHOULDER, + K_X360_A, + K_X360_B, + K_X360_X, + K_X360_Y, + K_X360_LEFT_TRIGGER, + K_X360_RIGHT_TRIGGER, + K_X360_LEFT_THUMB_UP, + K_X360_LEFT_THUMB_DOWN, + K_X360_LEFT_THUMB_LEFT, + K_X360_LEFT_THUMB_RIGHT, + K_X360_RIGHT_THUMB_UP, + K_X360_RIGHT_THUMB_DOWN, + K_X360_RIGHT_THUMB_LEFT, + K_X360_RIGHT_THUMB_RIGHT, + K_MIDINOTE0 = 896, // to this, the note number is added K_MIDINOTE1, K_MIDINOTE2, diff --git a/vid.h b/vid.h index cea3a1ec..6ff6a652 100644 --- a/vid.h +++ b/vid.h @@ -138,6 +138,33 @@ extern viddef_t vid; extern void (*vid_menudrawfn)(void); extern void (*vid_menukeyfn)(int key); +#define MAXJOYAXIS 16 +// if this is changed, the corresponding code in vid_shared.c must be updated +#define MAXJOYBUTTON 36 +typedef struct vid_joystate_s +{ + float axis[MAXJOYAXIS]; // -1 to +1 + unsigned char button[MAXJOYBUTTON]; // 0 or 1 + qboolean is360; // indicates this joystick is a Microsoft Xbox 360 Controller For Windows +} +vid_joystate_t; + +extern vid_joystate_t vid_joystate; + +extern cvar_t joy_index; +extern cvar_t joy_enable; +extern cvar_t joy_detected; +extern cvar_t joy_active; + +float VID_JoyState_GetAxis(const vid_joystate_t *joystate, int axis, float sensitivity, float deadzone); +void VID_ApplyJoyState(vid_joystate_t *joystate); +void VID_BuildJoyState(vid_joystate_t *joystate); +void VID_Shared_BuildJoyState_Begin(vid_joystate_t *joystate); +void VID_Shared_BuildJoyState_Finish(vid_joystate_t *joystate); +int VID_Shared_SetJoystick(int index); +qboolean VID_JoyBlockEmulatedKeys(int keycode); +void VID_EnableJoystick(qboolean enable); + extern qboolean vid_hidden; extern qboolean vid_activewindow; extern cvar_t vid_hardwaregammasupported; diff --git a/vid_agl.c b/vid_agl.c index b0afdcf9..d21ffc06 100644 --- a/vid_agl.c +++ b/vid_agl.c @@ -396,6 +396,7 @@ void VID_Shutdown(void) if (context == NULL && window == NULL) return; + VID_EnableJoystick(false); VID_SetMouse(false, false, false); VID_RestoreSystemGamma(); @@ -1110,8 +1111,34 @@ void Sys_SendKeyEvents(void) } } +void VID_BuildJoyState(vid_joystate_t *joystate) +{ + VID_Shared_BuildJoyState_Begin(joystate); + VID_Shared_BuildJoyState_Finish(joystate); +} + +void VID_EnableJoystick(qboolean enable) +{ + int index = joy_enable.integer > 0 ? joy_index.integer : -1; + qboolean success = false; + int sharedcount = 0; + sharedcount = VID_Shared_SetJoystick(index); + if (index >= 0 && index < sharedcount) + success = true; + + // update cvar containing count of XInput joysticks + if (joy_detected.integer != sharedcount) + Cvar_SetValueQuick(&joy_detected, sharedcount); + + Cvar_SetValueQuick(&joy_active, success ? 1 : 0); +} + void IN_Move (void) { + vid_joystate_t joystate; + VID_EnableJoystick(true); + VID_BuildJoyState(&joystate); + VID_ApplyJoyState(&joystate); } static bool GetDictionaryBoolean(CFDictionaryRef d, const void *key) diff --git a/vid_glx.c b/vid_glx.c index 293fdd45..6773b86c 100644 --- a/vid_glx.c +++ b/vid_glx.c @@ -832,6 +832,7 @@ void VID_Shutdown(void) if (!vidx11_display) return; + VID_EnableJoystick(false); VID_SetMouse(false, false, false); VID_RestoreSystemGamma(); @@ -1670,8 +1671,34 @@ void Sys_SendKeyEvents(void) HandleEvents(); } +void VID_BuildJoyState(vid_joystate_t *joystate) +{ + VID_Shared_BuildJoyState_Begin(joystate); + VID_Shared_BuildJoyState_Finish(joystate); +} + +void VID_EnableJoystick(qboolean enable) +{ + int index = joy_enable.integer > 0 ? joy_index.integer : -1; + qboolean success = false; + int sharedcount = 0; + sharedcount = VID_Shared_SetJoystick(index); + if (index >= 0 && index < sharedcount) + success = true; + + // update cvar containing count of XInput joysticks + if (joy_detected.integer != sharedcount) + Cvar_SetValueQuick(&joy_detected, sharedcount); + + Cvar_SetValueQuick(&joy_active, success ? 1 : 0); +} + void IN_Move (void) { + vid_joystate_t joystate; + VID_EnableJoystick(true); + VID_BuildJoyState(&joystate); + VID_ApplyJoyState(&joystate); } size_t VID_ListModes(vid_mode_t *modes, size_t maxcount) diff --git a/vid_null.c b/vid_null.c index 2be1bf27..4ac7ab11 100644 --- a/vid_null.c +++ b/vid_null.c @@ -88,6 +88,10 @@ void Sys_SendKeyEvents(void) { } +void VID_BuildJoyState(vid_joystate_t *joystate) +{ +} + void IN_Move(void) { } diff --git a/vid_sdl.c b/vid_sdl.c index fdd70caa..2f9bba72 100644 --- a/vid_sdl.c +++ b/vid_sdl.c @@ -66,30 +66,6 @@ int cl_available = true; qboolean vid_supportrefreshrate = false; -cvar_t joy_detected = {CVAR_READONLY, "joy_detected", "0", "number of joysticks detected by engine"}; -cvar_t joy_enable = {CVAR_SAVE, "joy_enable", "0", "enables joystick support"}; -cvar_t joy_index = {0, "joy_index", "0", "selects which joystick to use if you have multiple"}; -cvar_t joy_axisforward = {0, "joy_axisforward", "1", "which joystick axis to query for forward/backward movement"}; -cvar_t joy_axisside = {0, "joy_axisside", "0", "which joystick axis to query for right/left movement"}; -cvar_t joy_axisup = {0, "joy_axisup", "-1", "which joystick axis to query for up/down movement"}; -cvar_t joy_axispitch = {0, "joy_axispitch", "3", "which joystick axis to query for looking up/down"}; -cvar_t joy_axisyaw = {0, "joy_axisyaw", "2", "which joystick axis to query for looking right/left"}; -cvar_t joy_axisroll = {0, "joy_axisroll", "-1", "which joystick axis to query for tilting head right/left"}; -cvar_t joy_deadzoneforward = {0, "joy_deadzoneforward", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"}; -cvar_t joy_deadzoneside = {0, "joy_deadzoneside", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"}; -cvar_t joy_deadzoneup = {0, "joy_deadzoneup", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"}; -cvar_t joy_deadzonepitch = {0, "joy_deadzonepitch", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"}; -cvar_t joy_deadzoneyaw = {0, "joy_deadzoneyaw", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"}; -cvar_t joy_deadzoneroll = {0, "joy_deadzoneroll", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"}; -cvar_t joy_sensitivityforward = {0, "joy_sensitivityforward", "-1", "movement multiplier"}; -cvar_t joy_sensitivityside = {0, "joy_sensitivityside", "1", "movement multiplier"}; -cvar_t joy_sensitivityup = {0, "joy_sensitivityup", "1", "movement multiplier"}; -cvar_t joy_sensitivitypitch = {0, "joy_sensitivitypitch", "1", "movement multiplier"}; -cvar_t joy_sensitivityyaw = {0, "joy_sensitivityyaw", "-1", "movement multiplier"}; -cvar_t joy_sensitivityroll = {0, "joy_sensitivityroll", "1", "movement multiplier"}; -cvar_t joy_axiskeyevents = {CVAR_SAVE, "joy_axiskeyevents", "0", "generate uparrow/leftarrow etc. keyevents for joystick axes, use if your joystick driver is not generating them"}; -cvar_t joy_axiskeyevents_deadzone = {CVAR_SAVE, "joy_axiskeyevents_deadzone", "0.5", "deadzone value for axes"}; - #ifdef USE_GLES2 # define SETVIDEOMODE 0 #else @@ -111,9 +87,7 @@ static qboolean vid_isfullscreen; #else static qboolean vid_usingvsync = false; #endif -static int vid_numjoysticks = 0; -#define MAX_JOYSTICKS 8 -static SDL_Joystick *vid_joysticks[MAX_JOYSTICKS]; +static SDL_Joystick *vid_sdljoystick = NULL; static int win_half_width = 50; static int win_half_height = 50; @@ -129,20 +103,9 @@ static int window_flags; #endif static SDL_Surface *vid_softsurface; -// joystick axes state -#define MAX_JOYSTICK_AXES 16 -typedef struct -{ - float oldmove; - float move; - double keytime; -}joy_axiscache_t; -static joy_axiscache_t joy_axescache[MAX_JOYSTICK_AXES]; - ///////////////////////// // Input handling //// -//TODO: Add joystick support //TODO: Add error checking #ifndef SDLK_PERCENT @@ -518,80 +481,6 @@ void VID_SetMouse(qboolean fullscreengrab, qboolean relative, qboolean hidecurso #endif } -static double IN_JoystickGetAxis(SDL_Joystick *joy, int axis, double sensitivity, double deadzone) -{ - double value; - if (axis < 0 || axis >= SDL_JoystickNumAxes(joy)) - return 0; // no such axis on this joystick - value = SDL_JoystickGetAxis(joy, axis) * (1.0 / 32767.0); - value = bound(-1, value, 1); - if (fabs(value) < deadzone) - return 0; // within deadzone around center - return value * sensitivity; -} - -///////////////////// -// Joystick axis keyevents -// a sort of hack emulating Arrow keys for joystick axises -// as some drives dont send such keyevents for them -// additionally we should block drivers that do send arrow keyevents to prevent double events -//// - -static void IN_JoystickKeyeventForAxis(SDL_Joystick *joy, int axis, int key_pos, int key_neg) -{ - double joytime; - - if (axis < 0 || axis >= SDL_JoystickNumAxes(joy)) - return; // no such axis on this joystick - - joytime = Sys_DoubleTime(); - // no key event, continuous keydown event - if (joy_axescache[axis].move == joy_axescache[axis].oldmove) - { - if (joy_axescache[axis].move != 0 && joytime > joy_axescache[axis].keytime) - { - //Con_Printf("joy %s %i %f\n", Key_KeynumToString((joy_axescache[axis].move > 0) ? key_pos : key_neg), 1, cl.time); - Key_Event((joy_axescache[axis].move > 0) ? key_pos : key_neg, 0, 1); - joy_axescache[axis].keytime = joytime + 0.5 / 20; - } - return; - } - // generate key up event - if (joy_axescache[axis].oldmove) - { - //Con_Printf("joy %s %i %f\n", Key_KeynumToString((joy_axescache[axis].oldmove > 0) ? key_pos : key_neg), 1, cl.time); - Key_Event((joy_axescache[axis].oldmove > 0) ? key_pos : key_neg, 0, 0); - } - // generate key down event - if (joy_axescache[axis].move) - { - //Con_Printf("joy %s %i %f\n", Key_KeynumToString((joy_axescache[axis].move > 0) ? key_pos : key_neg), 1, cl.time); - Key_Event((joy_axescache[axis].move > 0) ? key_pos : key_neg, 0, 1); - joy_axescache[axis].keytime = joytime + 0.5; - } -} - -static qboolean IN_JoystickBlockDoubledKeyEvents(int keycode) -{ - if (!joy_axiskeyevents.integer) - return false; - - // block keyevent if it's going to be provided by joystick keyevent system - if (vid_numjoysticks && joy_enable.integer && joy_index.integer >= 0 && joy_index.integer < vid_numjoysticks) - { - SDL_Joystick *joy = vid_joysticks[joy_index.integer]; - - if (keycode == K_UPARROW || keycode == K_DOWNARROW) - if (IN_JoystickGetAxis(joy, joy_axisforward.integer, 1, joy_axiskeyevents_deadzone.value) || joy_axescache[joy_axisforward.integer].move || joy_axescache[joy_axisforward.integer].oldmove) - return true; - if (keycode == K_RIGHTARROW || keycode == K_LEFTARROW) - if (IN_JoystickGetAxis(joy, joy_axisside.integer, 1, joy_axiskeyevents_deadzone.value) || joy_axescache[joy_axisside.integer].move || joy_axescache[joy_axisside.integer].oldmove) - return true; - } - - return false; -} - // multitouch[10][] represents the mouse pointer // X and Y coordinates are 0-32767 as per SDL spec #define MAXFINGERS 11 @@ -656,16 +545,37 @@ qboolean VID_TouchscreenArea(int corner, float px, float py, float pwidth, float return button; } +void VID_BuildJoyState(vid_joystate_t *joystate) +{ + VID_Shared_BuildJoyState_Begin(joystate); + + if (vid_sdljoystick) + { + SDL_Joystick *joy = vid_sdljoystick; + int j; + int numaxes; + int numbuttons; + numaxes = SDL_JoystickNumAxes(joy); + for (j = 0;j < numaxes;j++) + joystate->axis[j] = SDL_JoystickGetAxis(joy, j) * (1.0f / 32767.0f); + numbuttons = SDL_JoystickNumButtons(joy); + for (j = 0;j < numbuttons;j++) + joystate->button[j] = SDL_JoystickGetButton(joy, j); + } + + VID_Shared_BuildJoyState_Finish(joystate); +} + ///////////////////// // Movement handling //// void IN_Move( void ) { - int j; static int old_x = 0, old_y = 0; static int stuck = 0; - int x, y, numaxes, numballs; + int x, y; + vid_joystate_t joystate; scr_numtouchscreenareas = 0; if (vid_touchscreen.integer) @@ -798,42 +708,8 @@ void IN_Move( void ) in_windowmouse_y = y; } - if (vid_numjoysticks && joy_enable.integer && joy_index.integer >= 0 && joy_index.integer < vid_numjoysticks) - { - SDL_Joystick *joy = vid_joysticks[joy_index.integer]; - - // balls convert to mousemove - numballs = SDL_JoystickNumBalls(joy); - for (j = 0;j < numballs;j++) - { - SDL_JoystickGetBall(joy, j, &x, &y); - in_mouse_x += x; - in_mouse_y += y; - } - - // axes - cl.cmd.forwardmove += IN_JoystickGetAxis(joy, joy_axisforward.integer, joy_sensitivityforward.value, joy_deadzoneforward.value) * cl_forwardspeed.value; - cl.cmd.sidemove += IN_JoystickGetAxis(joy, joy_axisside.integer, joy_sensitivityside.value, joy_deadzoneside.value) * cl_sidespeed.value; - cl.cmd.upmove += IN_JoystickGetAxis(joy, joy_axisup.integer, joy_sensitivityup.value, joy_deadzoneup.value) * cl_upspeed.value; - cl.viewangles[0] += IN_JoystickGetAxis(joy, joy_axispitch.integer, joy_sensitivitypitch.value, joy_deadzonepitch.value) * cl.realframetime * cl_pitchspeed.value; - cl.viewangles[1] += IN_JoystickGetAxis(joy, joy_axisyaw.integer, joy_sensitivityyaw.value, joy_deadzoneyaw.value) * cl.realframetime * cl_yawspeed.value; - //cl.viewangles[2] += IN_JoystickGetAxis(joy, joy_axisroll.integer, joy_sensitivityroll.value, joy_deadzoneroll.value) * cl.realframetime * cl_rollspeed.value; - - // cache state of axes to emulate button events for them - numaxes = min(MAX_JOYSTICK_AXES, SDL_JoystickNumAxes(joy)); - for (j = 0; j < numaxes; j++) - { - joy_axescache[j].oldmove = joy_axescache[j].move; - joy_axescache[j].move = IN_JoystickGetAxis(joy, j, 1, joy_axiskeyevents_deadzone.value); - } - - // run keyevents - if (joy_axiskeyevents.integer) - { - IN_JoystickKeyeventForAxis(joy, joy_axisforward.integer, K_DOWNARROW, K_UPARROW); - IN_JoystickKeyeventForAxis(joy, joy_axisside.integer, K_RIGHTARROW, K_LEFTARROW); - } - } + VID_BuildJoyState(&joystate); + VID_ApplyJoyState(&joystate); } ///////////////////// @@ -886,6 +762,8 @@ void Sys_SendKeyEvents( void ) int keycode; SDL_Event event; + VID_EnableJoystick(true); + while( SDL_PollEvent( &event ) ) switch( event.type ) { case SDL_QUIT: @@ -894,7 +772,7 @@ void Sys_SendKeyEvents( void ) case SDL_KEYDOWN: case SDL_KEYUP: keycode = MapKey(event.key.keysym.sym); - if (!IN_JoystickBlockDoubledKeyEvents(keycode)) + if (!VID_JoyBlockEmulatedKeys(keycode)) Key_Event(keycode, event.key.keysym.unicode, (event.key.state == SDL_PRESSED)); break; case SDL_ACTIVEEVENT: @@ -913,12 +791,7 @@ void Sys_SendKeyEvents( void ) Key_Event( buttonremap[event.button.button - 1], 0, event.button.state == SDL_PRESSED ); break; case SDL_JOYBUTTONDOWN: - if (!joy_enable.integer) - break; // ignore down events if joystick has been disabled case SDL_JOYBUTTONUP: - if (event.jbutton.button < 48) - Key_Event( event.jbutton.button + (event.jbutton.button < 16 ? K_JOY1 : K_AUX1 - 16), 0, (event.jbutton.state == SDL_PRESSED) ); - break; case SDL_JOYAXISMOTION: case SDL_JOYBALLMOTION: case SDL_JOYHATMOTION: @@ -1000,6 +873,8 @@ void Sys_SendKeyEvents( void ) int unicode; SDL_Event event; + VID_EnableJoystick(true); + while( SDL_PollEvent( &event ) ) switch( event.type ) { case SDL_QUIT: @@ -1008,16 +883,7 @@ void Sys_SendKeyEvents( void ) case SDL_KEYDOWN: case SDL_KEYUP: keycode = MapKey(event.key.keysym.sym); - if (!IN_JoystickBlockDoubledKeyEvents(keycode)) -#ifdef __IPHONEOS__ - // the virtual keyboard seems to produce no unicode values... - if (missingunicodehack && keycode >= ' ' && keycode < 0x7F && event.key.keysym.unicode == 0) - { - Con_DPrintf("SDL hack: no unicode value reported, substituting ascii value %i\n", keycode); - Key_Event(keycode, keycode, (event.key.state == SDL_PRESSED)); - } - else -#endif + if (!VID_JoyBlockEmulatedKeys(keycode)) Key_Event(keycode, 0, (event.key.state == SDL_PRESSED)); break; case SDL_MOUSEBUTTONDOWN: @@ -1027,12 +893,7 @@ void Sys_SendKeyEvents( void ) Key_Event( buttonremap[event.button.button - 1], 0, event.button.state == SDL_PRESSED ); break; case SDL_JOYBUTTONDOWN: - if (!joy_enable.integer) - break; // ignore down events if joystick has been disabled case SDL_JOYBUTTONUP: - if (event.jbutton.button < 48) - Key_Event( event.jbutton.button + (event.jbutton.button < 16 ? K_JOY1 : K_AUX1 - 16), 0, (event.jbutton.state == SDL_PRESSED) ); - break; case SDL_JOYAXISMOTION: case SDL_JOYBALLMOTION: case SDL_JOYHATMOTION: @@ -1182,15 +1043,6 @@ void Sys_SendKeyEvents( void ) case SDL_TOUCHBUTTONUP: // not sure what to do with this... break; - case SDL_JOYAXISMOTION: - // we poll the joystick instead - break; - case SDL_JOYBALLMOTION: - // we poll the joystick instead - break; - case SDL_JOYHATMOTION: - // we poll the joystick instead - break; default: Con_DPrintf("Received unrecognized SDL_Event type 0x%x\n", event.type); break; @@ -1778,29 +1630,6 @@ void VID_Init (void) Cvar_RegisterVariable(&apple_mouse_noaccel); #endif #endif - Cvar_RegisterVariable(&joy_detected); - Cvar_RegisterVariable(&joy_enable); - Cvar_RegisterVariable(&joy_index); - Cvar_RegisterVariable(&joy_axisforward); - Cvar_RegisterVariable(&joy_axisside); - Cvar_RegisterVariable(&joy_axisup); - Cvar_RegisterVariable(&joy_axispitch); - Cvar_RegisterVariable(&joy_axisyaw); - //Cvar_RegisterVariable(&joy_axisroll); - Cvar_RegisterVariable(&joy_deadzoneforward); - Cvar_RegisterVariable(&joy_deadzoneside); - Cvar_RegisterVariable(&joy_deadzoneup); - Cvar_RegisterVariable(&joy_deadzonepitch); - Cvar_RegisterVariable(&joy_deadzoneyaw); - //Cvar_RegisterVariable(&joy_deadzoneroll); - Cvar_RegisterVariable(&joy_sensitivityforward); - Cvar_RegisterVariable(&joy_sensitivityside); - Cvar_RegisterVariable(&joy_sensitivityup); - Cvar_RegisterVariable(&joy_sensitivitypitch); - Cvar_RegisterVariable(&joy_sensitivityyaw); - //Cvar_RegisterVariable(&joy_sensitivityroll); - Cvar_RegisterVariable(&joy_axiskeyevents); - Cvar_RegisterVariable(&joy_axiskeyevents_deadzone); #ifdef __IPHONEOS__ Cvar_SetValueQuick(&vid_touchscreen, 1); #endif @@ -1817,6 +1646,53 @@ void VID_Init (void) vid_isfullscreen = false; } +static int vid_sdljoystickindex = -1; +void VID_EnableJoystick(qboolean enable) +{ + int index = joy_enable.integer > 0 ? joy_index.integer : -1; + int numsdljoysticks; + qboolean success = false; + int sharedcount = 0; + int sdlindex = -1; + sharedcount = VID_Shared_SetJoystick(index); + if (index >= 0 && index < sharedcount) + success = true; + sdlindex = index - sharedcount; + + numsdljoysticks = SDL_NumJoysticks(); + if (sdlindex < 0 || sdlindex >= numsdljoysticks) + sdlindex = -1; + + // update cvar containing count of XInput joysticks + SDL joysticks + if (joy_detected.integer != sharedcount + numsdljoysticks) + Cvar_SetValueQuick(&joy_detected, sharedcount + numsdljoysticks); + + if (vid_sdljoystickindex != sdlindex) + { + vid_sdljoystickindex = sdlindex; + // close SDL joystick if active + if (vid_sdljoystick) + SDL_JoystickClose(vid_sdljoystick); + vid_sdljoystick = NULL; + if (sdlindex >= 0) + { + vid_sdljoystick = SDL_JoystickOpen(sdlindex); + if (vid_sdljoystick) + Con_Printf("Joystick %i opened (SDL_Joystick %i is \"%s\" with %i axes, %i buttons, %i balls)\n", index, sdlindex, SDL_JoystickName(sdlindex), (int)SDL_JoystickNumAxes(vid_sdljoystick), (int)SDL_JoystickNumButtons(vid_sdljoystick), (int)SDL_JoystickNumBalls(vid_sdljoystick)); + else + { + Con_Printf("Joystick %i failed (SDL_JoystickOpen(%i) returned: %s)\n", index, sdlindex, SDL_GetError()); + sdlindex = -1; + } + } + } + + if (sdlindex >= 0) + success = true; + + Cvar_SetValueQuick(&joy_active, success ? 1 : 0); +} + #if SETVIDEOMODE // set the icon (we dont use SDL here since it would be too much a PITA) #ifdef WIN32 @@ -2270,23 +2146,6 @@ qboolean VID_InitModeGL(viddef_mode_t *mode) GL_Init(); #endif - vid_numjoysticks = SDL_NumJoysticks(); - vid_numjoysticks = bound(0, vid_numjoysticks, MAX_JOYSTICKS); - Cvar_SetValueQuick(&joy_detected, vid_numjoysticks); - Con_Printf("%d SDL joystick(s) found:\n", vid_numjoysticks); - memset(vid_joysticks, 0, sizeof(vid_joysticks)); - for (i = 0;i < vid_numjoysticks;i++) - { - SDL_Joystick *joy; - joy = vid_joysticks[i] = SDL_JoystickOpen(i); - if (!joy) - { - Con_Printf("joystick #%i: open failed: %s\n", i, SDL_GetError()); - continue; - } - Con_Printf("joystick #%i: opened \"%s\" with %i axes, %i buttons, %i balls\n", i, SDL_JoystickName(i), (int)SDL_JoystickNumAxes(joy), (int)SDL_JoystickNumButtons(joy), (int)SDL_JoystickNumBalls(joy)); - } - vid_hidden = false; vid_activewindow = false; vid_hasfocus = true; @@ -2329,7 +2188,6 @@ extern cvar_t gl_info_driver; qboolean VID_InitModeSoft(viddef_mode_t *mode) { - int i; #if SETVIDEOMODE int flags = SDL_HWSURFACE; if(!COM_CheckParm("-noasyncblit")) flags |= SDL_ASYNCBLIT; @@ -2409,23 +2267,6 @@ qboolean VID_InitModeSoft(viddef_mode_t *mode) VID_Soft_SharedSetup(); - vid_numjoysticks = SDL_NumJoysticks(); - vid_numjoysticks = bound(0, vid_numjoysticks, MAX_JOYSTICKS); - Cvar_SetValueQuick(&joy_detected, vid_numjoysticks); - Con_Printf("%d SDL joystick(s) found:\n", vid_numjoysticks); - memset(vid_joysticks, 0, sizeof(vid_joysticks)); - for (i = 0;i < vid_numjoysticks;i++) - { - SDL_Joystick *joy; - joy = vid_joysticks[i] = SDL_JoystickOpen(i); - if (!joy) - { - Con_Printf("joystick #%i: open failed: %s\n", i, SDL_GetError()); - continue; - } - Con_Printf("joystick #%i: opened \"%s\" with %i axes, %i buttons, %i balls\n", i, SDL_JoystickName(i), (int)SDL_JoystickNumAxes(joy), (int)SDL_JoystickNumButtons(joy), (int)SDL_JoystickNumBalls(joy)); - } - vid_hidden = false; vid_activewindow = false; vid_hasfocus = true; @@ -2452,6 +2293,7 @@ qboolean VID_InitMode(viddef_mode_t *mode) void VID_Shutdown (void) { + VID_EnableJoystick(false); VID_SetMouse(false, false, false); VID_RestoreSystemGamma(); diff --git a/vid_shared.c b/vid_shared.c index aad8446e..05a19eac 100644 --- a/vid_shared.c +++ b/vid_shared.c @@ -11,6 +11,63 @@ LPDIRECT3DDEVICE9 vid_d3d9dev; #endif +#ifdef WIN32 +//#include +#define XINPUT_GAMEPAD_DPAD_UP 0x0001 +#define XINPUT_GAMEPAD_DPAD_DOWN 0x0002 +#define XINPUT_GAMEPAD_DPAD_LEFT 0x0004 +#define XINPUT_GAMEPAD_DPAD_RIGHT 0x0008 +#define XINPUT_GAMEPAD_START 0x0010 +#define XINPUT_GAMEPAD_BACK 0x0020 +#define XINPUT_GAMEPAD_LEFT_THUMB 0x0040 +#define XINPUT_GAMEPAD_RIGHT_THUMB 0x0080 +#define XINPUT_GAMEPAD_LEFT_SHOULDER 0x0100 +#define XINPUT_GAMEPAD_RIGHT_SHOULDER 0x0200 +#define XINPUT_GAMEPAD_A 0x1000 +#define XINPUT_GAMEPAD_B 0x2000 +#define XINPUT_GAMEPAD_X 0x4000 +#define XINPUT_GAMEPAD_Y 0x8000 +#define XINPUT_GAMEPAD_LEFT_THUMB_DEADZONE 7849 +#define XINPUT_GAMEPAD_RIGHT_THUMB_DEADZONE 8689 +#define XINPUT_GAMEPAD_TRIGGER_THRESHOLD 30 +#define XUSER_INDEX_ANY 0x000000FF + +typedef struct xinput_gamepad_s +{ + WORD wButtons; + BYTE bLeftTrigger; + BYTE bRightTrigger; + SHORT sThumbLX; + SHORT sThumbLY; + SHORT sThumbRX; + SHORT sThumbRY; +} +xinput_gamepad_t; + +typedef struct xinput_state_s +{ + DWORD dwPacketNumber; + xinput_gamepad_t Gamepad; +} +xinput_state_t; + +typedef struct xinput_keystroke_s +{ + WORD VirtualKey; + WCHAR Unicode; + WORD Flags; + BYTE UserIndex; + BYTE HidCode; +} +xinput_keystroke_t; + +DWORD (WINAPI *qXInputGetState)(DWORD index, xinput_state_t *state); +DWORD (WINAPI *qXInputGetKeystroke)(DWORD index, DWORD reserved, xinput_keystroke_t *keystroke); + +qboolean vid_xinputinitialized = false; +int vid_xinputindex = -1; +#endif + // global video state viddef_t vid; @@ -31,6 +88,54 @@ qboolean vid_hidden = true; // let go of the mouse, turn off sound, and restore system gamma ramps... qboolean vid_activewindow = true; +vid_joystate_t vid_joystate; + +#ifdef WIN32 +cvar_t joy_xinputavailable = {CVAR_READONLY, "joy_xinputavailable", "0", "indicates which devices are being reported by the Windows XInput API (first controller = 1, second = 2, third = 4, fourth = 8, added together)"}; +#endif +cvar_t joy_active = {CVAR_READONLY, "joy_active", "0", "indicates that a joystick is active (detected and enabled)"}; +cvar_t joy_detected = {CVAR_READONLY, "joy_detected", "0", "number of joysticks detected by engine"}; +cvar_t joy_enable = {CVAR_SAVE, "joy_enable", "0", "enables joystick support"}; +cvar_t joy_index = {0, "joy_index", "0", "selects which joystick to use if you have multiple (0 uses the first controller, 1 uses the second, ...)"}; +cvar_t joy_axisforward = {0, "joy_axisforward", "1", "which joystick axis to query for forward/backward movement"}; +cvar_t joy_axisside = {0, "joy_axisside", "0", "which joystick axis to query for right/left movement"}; +cvar_t joy_axisup = {0, "joy_axisup", "-1", "which joystick axis to query for up/down movement"}; +cvar_t joy_axispitch = {0, "joy_axispitch", "3", "which joystick axis to query for looking up/down"}; +cvar_t joy_axisyaw = {0, "joy_axisyaw", "2", "which joystick axis to query for looking right/left"}; +cvar_t joy_axisroll = {0, "joy_axisroll", "-1", "which joystick axis to query for tilting head right/left"}; +cvar_t joy_deadzoneforward = {0, "joy_deadzoneforward", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"}; +cvar_t joy_deadzoneside = {0, "joy_deadzoneside", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"}; +cvar_t joy_deadzoneup = {0, "joy_deadzoneup", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"}; +cvar_t joy_deadzonepitch = {0, "joy_deadzonepitch", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"}; +cvar_t joy_deadzoneyaw = {0, "joy_deadzoneyaw", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"}; +cvar_t joy_deadzoneroll = {0, "joy_deadzoneroll", "0", "deadzone tolerance, suggested values are in the range 0 to 0.01"}; +cvar_t joy_sensitivityforward = {0, "joy_sensitivityforward", "-1", "movement multiplier"}; +cvar_t joy_sensitivityside = {0, "joy_sensitivityside", "1", "movement multiplier"}; +cvar_t joy_sensitivityup = {0, "joy_sensitivityup", "1", "movement multiplier"}; +cvar_t joy_sensitivitypitch = {0, "joy_sensitivitypitch", "1", "movement multiplier"}; +cvar_t joy_sensitivityyaw = {0, "joy_sensitivityyaw", "-1", "movement multiplier"}; +cvar_t joy_sensitivityroll = {0, "joy_sensitivityroll", "1", "movement multiplier"}; +cvar_t joy_axiskeyevents = {CVAR_SAVE, "joy_axiskeyevents", "0", "generate uparrow/leftarrow etc. keyevents for joystick axes, use if your joystick driver is not generating them"}; +cvar_t joy_axiskeyevents_deadzone = {CVAR_SAVE, "joy_axiskeyevents_deadzone", "0.5", "deadzone value for axes"}; +cvar_t joy_x360_axisforward = {0, "joy_x360_axisforward", "1", "which joystick axis to query for forward/backward movement"}; +cvar_t joy_x360_axisside = {0, "joy_x360_axisside", "0", "which joystick axis to query for right/left movement"}; +cvar_t joy_x360_axisup = {0, "joy_x360_axisup", "-1", "which joystick axis to query for up/down movement"}; +cvar_t joy_x360_axispitch = {0, "joy_x360_axispitch", "3", "which joystick axis to query for looking up/down"}; +cvar_t joy_x360_axisyaw = {0, "joy_x360_axisyaw", "2", "which joystick axis to query for looking right/left"}; +cvar_t joy_x360_axisroll = {0, "joy_x360_axisroll", "-1", "which joystick axis to query for tilting head right/left"}; +cvar_t joy_x360_deadzoneforward = {0, "joy_x360_deadzoneforward", "0.266", "deadzone tolerance, suggested values are in the range 0 to 0.01"}; +cvar_t joy_x360_deadzoneside = {0, "joy_x360_deadzoneside", "0.266", "deadzone tolerance, suggested values are in the range 0 to 0.01"}; +cvar_t joy_x360_deadzoneup = {0, "joy_x360_deadzoneup", "0.266", "deadzone tolerance, suggested values are in the range 0 to 0.01"}; +cvar_t joy_x360_deadzonepitch = {0, "joy_x360_deadzonepitch", "0.266", "deadzone tolerance, suggested values are in the range 0 to 0.01"}; +cvar_t joy_x360_deadzoneyaw = {0, "joy_x360_deadzoneyaw", "0.266", "deadzone tolerance, suggested values are in the range 0 to 0.01"}; +cvar_t joy_x360_deadzoneroll = {0, "joy_x360_deadzoneroll", "0.266", "deadzone tolerance, suggested values are in the range 0 to 0.01"}; +cvar_t joy_x360_sensitivityforward = {0, "joy_x360_sensitivityforward", "1", "movement multiplier"}; +cvar_t joy_x360_sensitivityside = {0, "joy_x360_sensitivityside", "1", "movement multiplier"}; +cvar_t joy_x360_sensitivityup = {0, "joy_x360_sensitivityup", "1", "movement multiplier"}; +cvar_t joy_x360_sensitivitypitch = {0, "joy_x360_sensitivitypitch", "-1", "movement multiplier"}; +cvar_t joy_x360_sensitivityyaw = {0, "joy_x360_sensitivityyaw", "-1", "movement multiplier"}; +cvar_t joy_x360_sensitivityroll = {0, "joy_x360_sensitivityroll", "1", "movement multiplier"}; + // cvars for DPSOFTRAST cvar_t vid_soft = {CVAR_SAVE, "vid_soft", "0", "enables use of the DarkPlaces Software Rasterizer rather than OpenGL or Direct3D"}; cvar_t vid_soft_threads = {CVAR_SAVE, "vid_soft_threads", "2", "the number of threads the DarkPlaces Software Rasterizer should use"}; @@ -1010,6 +1115,271 @@ void VID_CheckExtensions(void) Cvar_SetQuick(&gl_info_driver, gl_driver); } +float VID_JoyState_GetAxis(const vid_joystate_t *joystate, int axis, float sensitivity, float deadzone) +{ + float value; + value = (axis >= 0 && axis < MAXJOYAXIS) ? joystate->axis[axis] : 0.0f; + value = value > deadzone ? (value - deadzone) : (value < -deadzone ? (value + deadzone) : 0.0f); + value *= deadzone > 0 ? (1.0f / (1.0f - deadzone)) : 1.0f; + value = bound(-1, value, 1); + return value * sensitivity; +} + +qboolean VID_JoyBlockEmulatedKeys(int keycode) +{ + int j; + vid_joystate_t joystate; + + if (!joy_axiskeyevents.integer) + return false; + if (vid_joystate.is360) + return false; + if (keycode != K_UPARROW && keycode != K_DOWNARROW && keycode != K_RIGHTARROW && keycode != K_LEFTARROW) + return false; + + // block system-generated key events for arrow keys if we're emulating the arrow keys ourselves + VID_BuildJoyState(&joystate); + for (j = 32;j < 36;j++) + if (vid_joystate.button[j] || joystate.button[j]) + return true; + + return false; +} + +void VID_Shared_BuildJoyState_Begin(vid_joystate_t *joystate) +{ +#ifdef WIN32 + xinput_state_t xinputstate; +#endif + memset(joystate, 0, sizeof(*joystate)); +#ifdef WIN32 + if (vid_xinputindex >= 0 && qXInputGetState && qXInputGetState(vid_xinputindex, &xinputstate) == S_OK) + { + joystate->is360 = true; + joystate->button[ 0] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_UP) != 0; + joystate->button[ 1] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_DOWN) != 0; + joystate->button[ 2] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_LEFT) != 0; + joystate->button[ 3] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_DPAD_RIGHT) != 0; + joystate->button[ 4] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_START) != 0; + joystate->button[ 5] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_BACK) != 0; + joystate->button[ 6] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_THUMB) != 0; + joystate->button[ 7] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_THUMB) != 0; + joystate->button[ 8] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_LEFT_SHOULDER) != 0; + joystate->button[ 9] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_RIGHT_SHOULDER) != 0; + joystate->button[10] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_A) != 0; + joystate->button[11] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_B) != 0; + joystate->button[12] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_X) != 0; + joystate->button[13] = (xinputstate.Gamepad.wButtons & XINPUT_GAMEPAD_Y) != 0; + joystate->button[14] = xinputstate.Gamepad.bLeftTrigger >= XINPUT_GAMEPAD_TRIGGER_THRESHOLD; + joystate->button[15] = xinputstate.Gamepad.bRightTrigger >= XINPUT_GAMEPAD_TRIGGER_THRESHOLD; + joystate->button[16] = xinputstate.Gamepad.sThumbLY < -16384; + joystate->button[17] = xinputstate.Gamepad.sThumbLY > 16384; + joystate->button[18] = xinputstate.Gamepad.sThumbLX < -16384; + joystate->button[19] = xinputstate.Gamepad.sThumbLX > 16384; + joystate->button[20] = xinputstate.Gamepad.sThumbRY < -16384; + joystate->button[21] = xinputstate.Gamepad.sThumbRY > 16384; + joystate->button[22] = xinputstate.Gamepad.sThumbRX < -16384; + joystate->button[23] = xinputstate.Gamepad.sThumbRX > 16384; + joystate->axis[ 4] = xinputstate.Gamepad.bLeftTrigger * (1.0f / 255.0f); + joystate->axis[ 5] = xinputstate.Gamepad.bRightTrigger * (1.0f / 255.0f); + joystate->axis[ 0] = xinputstate.Gamepad.sThumbLX * (1.0f / 32767.0f); + joystate->axis[ 1] = xinputstate.Gamepad.sThumbLY * (1.0f / 32767.0f); + joystate->axis[ 2] = xinputstate.Gamepad.sThumbRX * (1.0f / 32767.0f); + joystate->axis[ 3] = xinputstate.Gamepad.sThumbRY * (1.0f / 32767.0f); + } +#endif +} + +void VID_Shared_BuildJoyState_Finish(vid_joystate_t *joystate) +{ + float f, r; + if (!joy_axiskeyevents.integer || joystate->is360) + return; + // emulate key events for thumbstick + f = VID_JoyState_GetAxis(joystate, joy_axisforward.integer, 1, joy_axiskeyevents_deadzone.value) * joy_sensitivityforward.value; + r = VID_JoyState_GetAxis(joystate, joy_axisside.integer , 1, joy_axiskeyevents_deadzone.value) * joy_sensitivityside.value; +#if MAXJOYBUTTON != 36 +#error this code must be updated if MAXJOYBUTTON changes! +#endif + joystate->button[32] = f > 0.0f; + joystate->button[33] = f < 0.0f; + joystate->button[34] = r > 0.0f; + joystate->button[35] = r < 0.0f; + +} + +void VID_KeyEventForButton(qboolean oldbutton, qboolean newbutton, int key, double *timer) +{ + if (oldbutton) + { + if (newbutton) + { + if (realtime >= *timer) + { + Key_Event(key, 0, true); + *timer = realtime + 0.1; + } + } + else + { + Key_Event(key, 0, false); + *timer = 0; + } + } + else + { + if (newbutton) + { + Key_Event(key, 0, true); + *timer = realtime + 0.5; + } + } +} + +#if MAXJOYBUTTON != 36 +#error this code must be updated if MAXJOYBUTTON changes! +#endif +static int joybuttonkey[MAXJOYBUTTON] = +{ + K_JOY1, K_JOY2, K_JOY3, K_JOY4, K_JOY5, K_JOY6, K_JOY7, K_JOY8, K_JOY9, K_JOY10, K_JOY11, K_JOY12, K_JOY13, K_JOY14, K_JOY15, K_JOY16, + K_AUX1, K_AUX2, K_AUX3, K_AUX4, K_AUX5, K_AUX6, K_AUX7, K_AUX8, K_AUX9, K_AUX10, K_AUX11, K_AUX12, K_AUX13, K_AUX14, K_AUX15, K_AUX16, + K_UPARROW, K_DOWNARROW, K_RIGHTARROW, K_LEFTARROW +}; + +static int joybuttonkey360[] = +{ + K_X360_DPAD_UP, + K_X360_DPAD_DOWN, + K_X360_DPAD_LEFT, + K_X360_DPAD_RIGHT, + K_X360_START, + K_X360_BACK, + K_X360_LEFT_THUMB, + K_X360_RIGHT_THUMB, + K_X360_LEFT_SHOULDER, + K_X360_RIGHT_SHOULDER, + K_X360_A, + K_X360_B, + K_X360_X, + K_X360_Y, + K_X360_LEFT_TRIGGER, + K_X360_RIGHT_TRIGGER, + K_X360_LEFT_THUMB_DOWN, + K_X360_LEFT_THUMB_UP, + K_X360_LEFT_THUMB_LEFT, + K_X360_LEFT_THUMB_RIGHT, + K_X360_RIGHT_THUMB_DOWN, + K_X360_RIGHT_THUMB_UP, + K_X360_RIGHT_THUMB_LEFT, + K_X360_RIGHT_THUMB_RIGHT, +}; + +static int joybuttonkey360menu[] = +{ + K_UPARROW, + K_DOWNARROW, + K_LEFTARROW, + K_RIGHTARROW, + K_PAUSE, + K_ESCAPE, + 0, + 0, + 0, + 0, + K_ENTER, + K_ESCAPE, + 0, + 0, + 0, + 0, + K_DOWNARROW, + K_UPARROW, + K_LEFTARROW, + K_RIGHTARROW, + 0, + 0, + 0, + 0, +}; + +double vid_joybuttontimer[MAXJOYBUTTON]; +void VID_ApplyJoyState(vid_joystate_t *joystate) +{ + int j; + if (joystate->is360) + { +#if 0 + // keystrokes (chatpad) + // DOES NOT WORK - no driver support in xinput1_3.dll :( + xinput_keystroke_t keystroke; + while (qXInputGetKeystroke && qXInputGetKeystroke(XUSER_INDEX_ANY, 0, &keystroke) == S_OK) + Con_Printf("XInput KeyStroke: VirtualKey %i, Unicode %i, Flags %x, UserIndex %i, HidCode %i\n", keystroke.VirtualKey, keystroke.Unicode, keystroke.Flags, keystroke.UserIndex, keystroke.HidCode); +#endif + + // emit key events for buttons + for (j = 0;j < (int)(sizeof(joybuttonkey360)/sizeof(joybuttonkey360[0]));j++) + VID_KeyEventForButton(vid_joystate.button[j] != 0, joystate->button[j] != 0, key_dest == key_menu ? joybuttonkey360menu[j] : joybuttonkey360[j], &vid_joybuttontimer[j]); + + // axes + cl.cmd.forwardmove += VID_JoyState_GetAxis(joystate, joy_x360_axisforward.integer, joy_x360_sensitivityforward.value, joy_x360_deadzoneforward.value) * cl_forwardspeed.value; + cl.cmd.sidemove += VID_JoyState_GetAxis(joystate, joy_x360_axisside.integer, joy_x360_sensitivityside.value, joy_x360_deadzoneside.value) * cl_sidespeed.value; + cl.cmd.upmove += VID_JoyState_GetAxis(joystate, joy_x360_axisup.integer, joy_x360_sensitivityup.value, joy_x360_deadzoneup.value) * cl_upspeed.value; + cl.viewangles[0] += VID_JoyState_GetAxis(joystate, joy_x360_axispitch.integer, joy_x360_sensitivitypitch.value, joy_x360_deadzonepitch.value) * cl.realframetime * cl_pitchspeed.value; + cl.viewangles[1] += VID_JoyState_GetAxis(joystate, joy_x360_axisyaw.integer, joy_x360_sensitivityyaw.value, joy_x360_deadzoneyaw.value) * cl.realframetime * cl_yawspeed.value; + //cl.viewangles[2] += VID_JoyState_GetAxis(joystate, joy_x360_axisroll.integer, joy_x360_sensitivityroll.value, joy_x360_deadzoneroll.value) * cl.realframetime * cl_rollspeed.value; + } + else + { + // emit key events for buttons + for (j = 0;j < MAXJOYBUTTON;j++) + VID_KeyEventForButton(vid_joystate.button[j] != 0, joystate->button[j] != 0, joybuttonkey[j], &vid_joybuttontimer[j]); + + // axes + cl.cmd.forwardmove += VID_JoyState_GetAxis(joystate, joy_axisforward.integer, joy_sensitivityforward.value, joy_deadzoneforward.value) * cl_forwardspeed.value; + cl.cmd.sidemove += VID_JoyState_GetAxis(joystate, joy_axisside.integer, joy_sensitivityside.value, joy_deadzoneside.value) * cl_sidespeed.value; + cl.cmd.upmove += VID_JoyState_GetAxis(joystate, joy_axisup.integer, joy_sensitivityup.value, joy_deadzoneup.value) * cl_upspeed.value; + cl.viewangles[0] += VID_JoyState_GetAxis(joystate, joy_axispitch.integer, joy_sensitivitypitch.value, joy_deadzonepitch.value) * cl.realframetime * cl_pitchspeed.value; + cl.viewangles[1] += VID_JoyState_GetAxis(joystate, joy_axisyaw.integer, joy_sensitivityyaw.value, joy_deadzoneyaw.value) * cl.realframetime * cl_yawspeed.value; + //cl.viewangles[2] += VID_JoyState_GetAxis(joystate, joy_axisroll.integer, joy_sensitivityroll.value, joy_deadzoneroll.value) * cl.realframetime * cl_rollspeed.value; + } + + vid_joystate = *joystate; +} + +int VID_Shared_SetJoystick(int index) +{ +#ifdef WIN32 + int i; + int xinputcount = 0; + int xinputindex = -1; + int xinputavailable = 0; + xinput_state_t state; + // detect available XInput controllers + for (i = 0;i < 4;i++) + { + if (qXInputGetState && qXInputGetState(i, &state) == S_OK) + { + xinputavailable |= 1<= 0) + Con_Printf("Joystick %i opened (XInput Device %i)\n", index, xinputindex); + } + return xinputcount; +#else + return 0; +#endif +} + + void Force_CenterView_f (void) { cl.viewangles[PITCH] = 0; @@ -1232,6 +1602,23 @@ void VID_RestoreSystemGamma(void) } } +#ifdef WIN32 +static dllfunction_t xinputdllfuncs[] = +{ + {"XInputGetState", (void **) &qXInputGetState}, + {"XInputGetKeystroke", (void **) &qXInputGetKeystroke}, + {NULL, NULL} +}; +static const char* xinputdllnames [] = +{ + "xinput1_3.dll", + "xinput1_2.dll", + "xinput1_1.dll", + NULL +}; +static dllhandle_t xinputdll_dll = NULL; +#endif + void VID_Shared_Init(void) { #ifdef SSE_POSSIBLE @@ -1297,6 +1684,57 @@ void VID_Shared_Init(void) Cvar_RegisterVariable(&vid_gl20); Cvar_RegisterVariable(&gl_finish); Cvar_RegisterVariable(&vid_sRGB); + + Cvar_RegisterVariable(&joy_active); +#ifdef WIN32 + Cvar_RegisterVariable(&joy_xinputavailable); +#endif + Cvar_RegisterVariable(&joy_detected); + Cvar_RegisterVariable(&joy_enable); + Cvar_RegisterVariable(&joy_index); + Cvar_RegisterVariable(&joy_axisforward); + Cvar_RegisterVariable(&joy_axisside); + Cvar_RegisterVariable(&joy_axisup); + Cvar_RegisterVariable(&joy_axispitch); + Cvar_RegisterVariable(&joy_axisyaw); + //Cvar_RegisterVariable(&joy_axisroll); + Cvar_RegisterVariable(&joy_deadzoneforward); + Cvar_RegisterVariable(&joy_deadzoneside); + Cvar_RegisterVariable(&joy_deadzoneup); + Cvar_RegisterVariable(&joy_deadzonepitch); + Cvar_RegisterVariable(&joy_deadzoneyaw); + //Cvar_RegisterVariable(&joy_deadzoneroll); + Cvar_RegisterVariable(&joy_sensitivityforward); + Cvar_RegisterVariable(&joy_sensitivityside); + Cvar_RegisterVariable(&joy_sensitivityup); + Cvar_RegisterVariable(&joy_sensitivitypitch); + Cvar_RegisterVariable(&joy_sensitivityyaw); + //Cvar_RegisterVariable(&joy_sensitivityroll); + Cvar_RegisterVariable(&joy_axiskeyevents); + Cvar_RegisterVariable(&joy_axiskeyevents_deadzone); + Cvar_RegisterVariable(&joy_x360_axisforward); + Cvar_RegisterVariable(&joy_x360_axisside); + Cvar_RegisterVariable(&joy_x360_axisup); + Cvar_RegisterVariable(&joy_x360_axispitch); + Cvar_RegisterVariable(&joy_x360_axisyaw); + //Cvar_RegisterVariable(&joy_x360_axisroll); + Cvar_RegisterVariable(&joy_x360_deadzoneforward); + Cvar_RegisterVariable(&joy_x360_deadzoneside); + Cvar_RegisterVariable(&joy_x360_deadzoneup); + Cvar_RegisterVariable(&joy_x360_deadzonepitch); + Cvar_RegisterVariable(&joy_x360_deadzoneyaw); + //Cvar_RegisterVariable(&joy_x360_deadzoneroll); + Cvar_RegisterVariable(&joy_x360_sensitivityforward); + Cvar_RegisterVariable(&joy_x360_sensitivityside); + Cvar_RegisterVariable(&joy_x360_sensitivityup); + Cvar_RegisterVariable(&joy_x360_sensitivitypitch); + Cvar_RegisterVariable(&joy_x360_sensitivityyaw); + //Cvar_RegisterVariable(&joy_x360_sensitivityroll); + +#ifdef WIN32 + Sys_LoadLibrary(xinputdllnames, &xinputdll_dll, xinputdllfuncs); +#endif + Cmd_AddCommand("force_centerview", Force_CenterView_f, "recenters view (stops looking up/down)"); Cmd_AddCommand("vid_restart", VID_Restart_f, "restarts video system (closes and reopens the window, restarts renderer)"); } diff --git a/vid_wgl.c b/vid_wgl.c index 2d0ac602..167e6ea7 100644 --- a/vid_wgl.c +++ b/vid_wgl.c @@ -204,90 +204,15 @@ static qboolean dinput_acquired; static unsigned int mstate_di; #endif -// joystick defines and variables -// where should defines be moved? -#define JOY_ABSOLUTE_AXIS 0x00000000 // control like a joystick -#define JOY_RELATIVE_AXIS 0x00000010 // control like a mouse, spinner, trackball -#define JOY_MAX_AXES 6 // X, Y, Z, R, U, V -#define JOY_AXIS_X 0 -#define JOY_AXIS_Y 1 -#define JOY_AXIS_Z 2 -#define JOY_AXIS_R 3 -#define JOY_AXIS_U 4 -#define JOY_AXIS_V 5 - -// joystick axes state -typedef struct -{ - float oldmove; - float move; - float mdelta; - double keytime; -}joy_axiscache_t; -static joy_axiscache_t joy_axescache[JOY_MAX_AXES]; - -enum _ControlList -{ - AxisNada = 0, AxisForward, AxisLook, AxisSide, AxisTurn -}; - -static DWORD dwAxisFlags[JOY_MAX_AXES] = -{ - JOY_RETURNX, JOY_RETURNY, JOY_RETURNZ, JOY_RETURNR, JOY_RETURNU, JOY_RETURNV -}; - -static DWORD dwAxisMap[JOY_MAX_AXES]; -static DWORD dwControlMap[JOY_MAX_AXES]; -static PDWORD pdwRawValue[JOY_MAX_AXES]; - -// 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. -static cvar_t in_joystick = {CVAR_SAVE, "joystick","0", "enables joysticks"}; -static cvar_t joy_name = {0, "joyname", "joystick", "name of joystick to use (informational only, used only by joyadvanced 1 mode)"}; -static cvar_t joy_advanced = {0, "joyadvanced", "0", "use more than 2 axis joysticks (configuring this is very technical)"}; -static cvar_t joy_advaxisx = {0, "joyadvaxisx", "0", "axis mapping for joyadvanced 1 mode"}; -static cvar_t joy_advaxisy = {0, "joyadvaxisy", "0", "axis mapping for joyadvanced 1 mode"}; -static cvar_t joy_advaxisz = {0, "joyadvaxisz", "0", "axis mapping for joyadvanced 1 mode"}; -static cvar_t joy_advaxisr = {0, "joyadvaxisr", "0", "axis mapping for joyadvanced 1 mode"}; -static cvar_t joy_advaxisu = {0, "joyadvaxisu", "0", "axis mapping for joyadvanced 1 mode"}; -static cvar_t joy_advaxisv = {0, "joyadvaxisv", "0", "axis mapping for joyadvanced 1 mode"}; -static cvar_t joy_forwardthreshold = {0, "joyforwardthreshold", "0.15", "minimum joystick movement necessary to move forward"}; -static cvar_t joy_sidethreshold = {0, "joysidethreshold", "0.15", "minimum joystick movement necessary to move sideways (strafing)"}; -static cvar_t joy_pitchthreshold = {0, "joypitchthreshold", "0.15", "minimum joystick movement necessary to look up/down"}; -static cvar_t joy_yawthreshold = {0, "joyyawthreshold", "0.15", "minimum joystick movement necessary to turn left/right"}; -static cvar_t joy_forwardsensitivity = {0, "joyforwardsensitivity", "-1.0", "how fast the joystick moves forward"}; -static cvar_t joy_sidesensitivity = {0, "joysidesensitivity", "-1.0", "how fast the joystick moves sideways (strafing)"}; -static cvar_t joy_pitchsensitivity = {0, "joypitchsensitivity", "1.0", "how fast the joystick looks up/down"}; -static cvar_t joy_yawsensitivity = {0, "joyyawsensitivity", "-1.0", "how fast the joystick turns left/right"}; -static cvar_t joy_wwhack1 = {0, "joywwhack1", "0.0", "special hack for wingman warrior"}; -static cvar_t joy_wwhack2 = {0, "joywwhack2", "0.0", "special hack for wingman warrior"}; -static cvar_t joy_axiskeyevents = {CVAR_SAVE, "joy_axiskeyevents", "0", "generate uparrow/leftarrow etc. keyevents for joystick axes, use if your joystick driver is not generating them"}; -static cvar_t joy_axiskeyevents_deadzone = {CVAR_SAVE, "joy_axiskeyevents_deadzone", "0.5", "deadzone value for axes"}; - static cvar_t vid_forcerefreshrate = {0, "vid_forcerefreshrate", "0", "try to set the given vid_refreshrate even if Windows doesn't list it as valid video mode"}; -static qboolean joy_avail, joy_advancedinit, joy_haspov; -static DWORD joy_oldbuttonstate, joy_oldpovstate; - -static int joy_id; -static DWORD joy_flags; -static DWORD joy_numbuttons; - #ifdef SUPPORTDIRECTX static LPDIRECTINPUT g_pdi; static LPDIRECTINPUTDEVICE g_pMouse; static HINSTANCE hInstDI; #endif -static JOYINFOEX ji; - // forward-referenced functions -static void IN_StartupJoystick (void); -static void Joy_AdvancedUpdate_f (void); -static void IN_JoyMove (void); static void IN_StartupMouse (void); @@ -609,7 +534,6 @@ static keynum_t buttonremap[16] = }; /* main window procedure */ -static qboolean IN_JoystickBlockDoubledKeyEvents(int keycode); LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { LONG lRet = 1; @@ -654,7 +578,7 @@ LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) else if( charlength == 2 ) { asciichar[0] = asciichar[1]; } - if (!IN_JoystickBlockDoubledKeyEvents(vkey)) + if (!VID_JoyBlockEmulatedKeys(vkey)) Key_Event (vkey, asciichar[0], down); break; @@ -1390,7 +1314,6 @@ qboolean VID_InitModeGL(viddef_mode_t *mode) vid_initialized = true; IN_StartupMouse (); - IN_StartupJoystick (); if (qwglSwapIntervalEXT) { @@ -1636,7 +1559,6 @@ qboolean VID_InitModeDX(viddef_mode_t *mode, int version) vid_initialized = true; IN_StartupMouse (); - IN_StartupJoystick (); return true; } @@ -1918,7 +1840,6 @@ qboolean VID_InitModeSOFT(viddef_mode_t *mode) vid_initialized = true; IN_StartupMouse (); - IN_StartupJoystick (); return true; } @@ -1948,6 +1869,7 @@ void VID_Shutdown (void) if(vid_initialized == false) return; + VID_EnableJoystick(false); VID_SetMouse(false, false, false); VID_RestoreSystemGamma(); @@ -2087,6 +2009,27 @@ void VID_SetMouse(qboolean fullscreengrab, qboolean relative, qboolean hidecurso } } +void VID_BuildJoyState(vid_joystate_t *joystate) +{ + VID_Shared_BuildJoyState_Begin(joystate); + VID_Shared_BuildJoyState_Finish(joystate); +} + +void VID_EnableJoystick(qboolean enable) +{ + int index = joy_enable.integer > 0 ? joy_index.integer : -1; + qboolean success = false; + int sharedcount = 0; + sharedcount = VID_Shared_SetJoystick(index); + if (index >= 0 && index < sharedcount) + success = true; + + // update cvar containing count of XInput joysticks + if (joy_detected.integer != sharedcount) + Cvar_SetValueQuick(&joy_detected, sharedcount); + + Cvar_SetValueQuick(&joy_active, success ? 1 : 0); +} #ifdef SUPPORTDIRECTX /* @@ -2198,7 +2141,7 @@ static void IN_StartupMouse (void) mouseinitialized = true; #ifdef SUPPORTDIRECTX -// COMMANDLINEOPTION: Windows Input: -dinput enables DirectInput for mouse/joystick input +// COMMANDLINEOPTION: Windows Input: -dinput enables DirectInput for mouse input if (COM_CheckParm ("-dinput")) dinput = IN_InitDInput (); @@ -2308,580 +2251,19 @@ IN_Move */ void IN_Move (void) { + vid_joystate_t joystate; if (vid_activewindow && !vid_reallyhidden) - { IN_MouseMove (); - IN_JoyMove (); - } + VID_EnableJoystick(true); + VID_BuildJoyState(&joystate); + VID_ApplyJoyState(&joystate); } -/* -=============== -IN_StartupJoystick -=============== -*/ -static void IN_StartupJoystick (void) -{ - int numdevs; - JOYCAPS jc; - MMRESULT mmr; - mmr = 0; - - // assume no joystick - joy_avail = false; - - // abort startup if user requests no joystick -// COMMANDLINEOPTION: Windows Input: -nojoy disables joystick support, may be a small speed increase - if (COM_CheckParm ("-nojoy")) - return; - - // verify joystick driver is present - if ((numdevs = joyGetNumDevs ()) == 0) - { - Con_Print("\njoystick not found -- driver not present\n\n"); - return; - } - - // cycle through the joystick ids for the first valid one - for (joy_id=0 ; joy_id= JOY_MAX_AXES) - return 0; // no such axis on this joystick - - // get the floating point zero-centered, potentially-inverted data for the current axis - fAxisValue = (float) *pdwRawValue[axis]; - - // move centerpoint to zero - fAxisValue -= 32768.0; - - if (joy_wwhack2.integer != 0.0) - { - if (dwAxisMap[axis] == AxisTurn) - { - // this is a special formula for the Logitech WingMan Warrior - // y=ax^b; where a = 300 and b = 1.3 - // also x values are in increments of 800 (so this is factored out) - // then bounds check result to level out excessively high spin rates - fTemp = 300.0 * pow(abs(fAxisValue) / 800.0, 1.3); - if (fTemp > 14000.0) - fTemp = 14000.0; - // restore direction information - fAxisValue = (fAxisValue > 0.0) ? fTemp : -fTemp; - } - } - - // convert range from -32768..32767 to -1..1 - fAxisValue /= 32768.0; - - // deadzone around center - if (fabs(fAxisValue) < deadzone) - return 0; - - // apply sensitivity - return fAxisValue * sensitivity; -} - -/* -=========== - IN_JoystickKeyeventForAxis -=========== -*/ - -static void IN_JoystickKeyeventForAxis(int axis, int key_pos, int key_neg) -{ - double joytime; - - if (axis < 0 || axis >= JOY_MAX_AXES) - return; // no such axis on this joystick - - joytime = Sys_DoubleTime(); - // no key event, continuous keydown event - if (joy_axescache[axis].move == joy_axescache[axis].oldmove) - { - if (joy_axescache[axis].move != 0 && joytime > joy_axescache[axis].keytime) - { - //Con_Printf("joy %s %i %f\n", Key_KeynumToString((joy_axescache[axis].move > 0) ? key_pos : key_neg), 1, cl.time); - Key_Event((joy_axescache[axis].move > 0) ? key_pos : key_neg, 0, 1); - joy_axescache[axis].keytime = joytime + 0.5 / 20; - } - return; - } - // generate key up event - if (joy_axescache[axis].oldmove) - { - //Con_Printf("joy %s %i %f\n", Key_KeynumToString((joy_axescache[axis].oldmove > 0) ? key_pos : key_neg), 1, cl.time); - Key_Event((joy_axescache[axis].oldmove > 0) ? key_pos : key_neg, 0, 0); - } - // generate key down event - if (joy_axescache[axis].move) - { - //Con_Printf("joy %s %i %f\n", Key_KeynumToString((joy_axescache[axis].move > 0) ? key_pos : key_neg), 1, cl.time); - Key_Event((joy_axescache[axis].move > 0) ? key_pos : key_neg, 0, 1); - joy_axescache[axis].keytime = joytime + 0.5; - } -} - -/* -=========== - IN_JoystickBlockDoubledKeyEvents -=========== -*/ - -static qboolean IN_ReadJoystick (void); -static qboolean IN_JoystickBlockDoubledKeyEvents(int keycode) -{ - int axis; - - if (!joy_axiskeyevents.integer) - return false; - - // block keyevent if it's going to be provided by joystick keyevent system - if (joy_avail) - { - // collect the joystick data, if possible - if (IN_ReadJoystick() != true) - return false; - axis = IN_JoystickGetAxisNum(AxisForward); - if (keycode == K_UPARROW || keycode == K_DOWNARROW) - if (IN_JoystickGetAxis(axis, 1, joy_axiskeyevents_deadzone.value) || joy_axescache[axis].move || joy_axescache[axis].oldmove) - return true; - axis = IN_JoystickGetAxisNum(AxisSide); - if (keycode == K_RIGHTARROW || keycode == K_LEFTARROW) - if (IN_JoystickGetAxis(axis, 1, joy_axiskeyevents_deadzone.value) || joy_axescache[axis].move || joy_axescache[axis].oldmove) - return true; - } - - return false; -} - -/* -=========== - IN_JoyMove -=========== -*/ -static void IN_JoyMove (void) -{ - float speed, aspeed; - float fAxisValue; - int i, mouselook = (in_mlook.state & 1) || freelook.integer, AxisForwardIndex = -1, AxisSideIndex = -1; - - // complete initialization if first time in - // this is needed as cvars are not available at initialization time - if( joy_advancedinit != true ) - { - Joy_AdvancedUpdate_f(); - joy_advancedinit = true; - } - - if (joy_avail) - { - int i, key_index; - DWORD buttonstate, povstate; - - // loop through the joystick buttons - // key a joystick event or auxillary event for higher number buttons for each state change - buttonstate = ji.dwButtons; - for (i=0 ; i < (int) joy_numbuttons ; i++) - { - if ( (buttonstate & (1<