Merge pull request #2300 from OTHGMars/SDL_Joystick2

Sdl joystick2
This commit is contained in:
Areloch 2019-01-21 15:17:31 -06:00 committed by GitHub
commit 77a316079a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 1541 additions and 33 deletions

View file

@ -249,6 +249,14 @@ enum InputObjectInstancesEnum
SI_DPOV2 = 0x215,
SI_LPOV2 = 0x216,
SI_RPOV2 = 0x217,
SI_POVMASK = 0x218,
SI_POVMASK2 = 0x219,
/// Trackball event codes.
SI_XBALL = 0x21A,
SI_YBALL = 0x21B,
SI_XBALL2 = 0x21C,
SI_YBALL2 = 0x21D,
XI_CONNECT = 0x300,
XI_THUMBLX = 0x301,
@ -262,7 +270,7 @@ enum InputObjectInstancesEnum
XI_DPAD_DOWN = 0x308,
XI_DPAD_LEFT = 0x309,
XI_DPAD_RIGHT = 0x310,*/
XI_START = 0x311,
XI_BACK = 0x312,
XI_LEFT_THUMB = 0x313,
@ -273,7 +281,8 @@ enum InputObjectInstancesEnum
XI_A = 0x317,
XI_B = 0x318,
XI_X = 0x319,
XI_Y = 0x320,
XI_Y = 0x31A,
XI_GUIDE = 0x31B,
INPUT_DEVICE_PLUGIN_CODES_START = 0x400,
};

View file

@ -27,15 +27,11 @@
#include "sdlInput.h"
#include "platform/platformInput.h"
#include "sdlInputManager.h"
#include "SDL.h"
#ifdef LOG_INPUT
#include <time.h>
#include <stdarg.h>
#endif
// Static class variables:
InputManager* Input::smManager;
InputManager* Input::smManager = NULL;
bool Input::smActive;
U8 Input::smModifierKeys;
bool Input::smLastKeyboardActivated;
@ -43,10 +39,6 @@ bool Input::smLastMouseActivated;
bool Input::smLastJoystickActivated;
InputEvent Input::smInputEvent;
#ifdef LOG_INPUT
static HANDLE gInputLog;
#endif
static void fillAsciiTable() {}
//------------------------------------------------------------------------------
@ -91,24 +83,17 @@ void Input::init()
fillAsciiTable();
Con::printf( "" );
smManager = new SDLInputManager;
if (smManager)
{
SDLInputManager::init();
}
// Set ourselves to participate in per-frame processing.
Process::notify(Input::process, PROCESS_INPUT_ORDER);
}
//------------------------------------------------------------------------------
DefineEngineFunction(isJoystickDetected, bool, (),, "")
{
return(SDL_NumJoysticks() > 0);
}
//------------------------------------------------------------------------------
DefineEngineFunction(getJoystickAxes, const char*, (const char* instance), , "")
{
// TODO SDL
return("");
}
//------------------------------------------------------------------------------
U16 Input::getKeyCode( U16 asciiCode )
{
@ -159,6 +144,13 @@ void Input::destroy()
SDL_QuitSubSystem( SDL_INIT_JOYSTICK | SDL_INIT_HAPTIC | SDL_INIT_GAMECONTROLLER );
if (smManager)
{
if (smManager->isEnabled())
smManager->disable();
delete smManager;
smManager = NULL;
}
}
//------------------------------------------------------------------------------
@ -186,8 +178,8 @@ void Input::activate()
//ImmReleaseContext( getWin32WindowHandle(), winState.imeHandle );
#endif
if ( !Con::getBoolVariable( "$enableDirectInput" ) )
return;
if (smManager && !smManager->isEnabled())
smManager->enable();
if ( smManager && smManager->isEnabled() && !smActive )
{
@ -199,7 +191,10 @@ void Input::activate()
//------------------------------------------------------------------------------
void Input::deactivate()
{
if ( smManager && smManager->isEnabled() && smActive )
if (smManager && smManager->isEnabled())
smManager->disable();
if (smActive)
{
smActive = false;
Con::printf( "Input deactivated." );

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,114 @@
//-----------------------------------------------------------------------------
// Copyright (c) 2012 GarageGames, LLC
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
// IN THE SOFTWARE.
//-----------------------------------------------------------------------------
#ifndef _SDLINPUTMANAGER_H_
#define _SDLINPUTMANAGER_H_
#ifndef _PLATFORMINPUT_H_
#include "platform/platformInput.h"
#endif
#include "SDL.h"
//------------------------------------------------------------------------------
class SDLInputManager : public InputManager
{
enum Constants {
MaxJoysticks = 4, // Up to 4 simultaneous joysticks
MaxControllers = 4, // Up to 4 simultaneous controllers
MaxHats = 2, // Maximum 2 hats per device
MaxBalls = 2, // Maximum 2 trackballs per device
MaxControllerAxes = 7, // From map_StringForControllerAxis[] in SDL_gamecontroller.c
MaxControllerButtons = 16 // From map_StringForControllerButton[] in SDL_gamecontroller.c
};
struct controllerState
{
S32 sdlInstID; // SDL device instance id
U32 torqueInstID; // Torque device instance id
SDL_GameController *inputDevice;
};
struct joystickState
{
S32 sdlInstID; // SDL device instance id
U32 torqueInstID; // Torque device instance id
SDL_Joystick *inputDevice;
U32 numAxes;
U8 lastHatState[MaxHats];
void reset();
};
private:
typedef InputManager Parent;
static S32 map_EventForControllerAxis[MaxControllerAxes];
static S32 map_EventForControllerButton[MaxControllerButtons];
static bool smJoystickEnabled;
static bool smJoystickSplitAxesLR;
static bool smControllerEnabled;
static bool smPOVButtonEvents;
static bool smPOVMaskEvents;
joystickState mJoysticks[MaxJoysticks];
controllerState mControllers[MaxControllers];
// Used to look up a torque instance based on a device inst
HashTable<S32, joystickState*> mJoystickMap;
HashTable<S32, controllerState*> mControllerMap;
bool mJoystickActive;
void deviceConnectedCallback(S32 index);
bool closeControllerByIndex(S32 index);
void closeController(SDL_JoystickID sdlId);
bool closeJoystickByIndex(S32 index);
void closeJoystick(SDL_JoystickID sdlId);
void buildInputEvent(U32 deviceType, U32 deviceInst, InputEventType objType, InputObjectInstances objInst, InputActionType action, S32 iValue);
void buildInputEvent(U32 deviceType, U32 deviceInst, InputEventType objType, InputObjectInstances objInst, InputActionType action, F32 fValue);
void buildHatEvents(U32 deviceType, U32 deviceInst, U8 lastState, U8 currentState, S32 hatIndex);
public:
DECLARE_STATIC_CLASS(SDLInputManager);
public:
SDLInputManager();
bool enable();
void disable();
void process();
void processEvent(SDL_Event &evt);
static void init();
// Console interface:
S32 openJoystick(S32 sdlIndex, S32 requestedTID);
S32 openController(S32 sdlIndex, S32 requestedTID);
void closeDevice(S32 sdlIndex);
S32 getJoystickOpenState(S32 sdlIndex);
void getJoystickTorqueInst(S32 sdlIndex, char* instBuffer);
};
#endif // _SDLINPUTMANAGER_H_

View file

@ -21,6 +21,7 @@
//-----------------------------------------------------------------------------
#include "windowManager/sdl/sdlWindowMgr.h"
#include "platformSDL/sdlInputManager.h"
#include "gfx/gfxDevice.h"
#include "core/util/journal/process.h"
#include "core/strings/unicode.h"
@ -269,6 +270,13 @@ void PlatformWindowManagerSDL::_process()
SDL_Event evt;
while( SDL_PollEvent(&evt) )
{
if (evt.type >= SDL_JOYAXISMOTION && evt.type <= SDL_CONTROLLERDEVICEREMAPPED)
{
SDLInputManager* mgr = static_cast<SDLInputManager*>(Input::getManager());
if (mgr)
mgr->processEvent(evt);
continue;
}
switch(evt.type)
{
case SDL_QUIT:
@ -356,15 +364,16 @@ void PlatformWindowManagerSDL::_process()
case(SDL_DROPCOMPLETE):
{
if (!Con::isFunction("onDropEnd"))
break;
Con::executef("onDropEnd");
if (Con::isFunction("onDropEnd"))
Con::executef("onDropEnd");
break;
}
default:
{
//Con::printf("Event: %d", evt.type);
#ifdef TORQUE_DEBUG
Con::warnf("Unhandled SDL input event: 0x%04x", evt.type);
#endif
}
}
}