inputsystem: touch event handling

This commit is contained in:
nillerusr 2021-10-06 21:09:43 +03:00
parent 0d2a493312
commit 81b976c384
5 changed files with 156 additions and 3 deletions

View File

@ -31,7 +31,6 @@ EXPOSE_SINGLE_INTERFACE_GLOBALVAR( CInputSystem, IInputSystem,
INPUTSYSTEM_INTERFACE_VERSION, g_InputSystem ); INPUTSYSTEM_INTERFACE_VERSION, g_InputSystem );
#if defined( WIN32 ) && !defined( _X360 ) #if defined( WIN32 ) && !defined( _X360 )
typedef BOOL (WINAPI *RegisterRawInputDevices_t) typedef BOOL (WINAPI *RegisterRawInputDevices_t)
( (
@ -70,6 +69,7 @@ CInputSystem::CInputSystem()
m_JoysticksEnabled.ClearAllFlags(); m_JoysticksEnabled.ClearAllFlags();
m_nJoystickCount = 0; m_nJoystickCount = 0;
m_bJoystickInitialized = false; m_bJoystickInitialized = false;
m_bTouchInitialized = false;
m_nPollCount = 0; m_nPollCount = 0;
m_PrimaryUserId = INVALID_USER_ID; m_PrimaryUserId = INVALID_USER_ID;
m_uiMouseWheel = 0; m_uiMouseWheel = 0;
@ -166,6 +166,10 @@ InitReturnVal_t CInputSystem::Init()
ButtonCode_UpdateScanCodeLayout(); ButtonCode_UpdateScanCodeLayout();
joy_xcontroller_found.SetValue( 0 ); joy_xcontroller_found.SetValue( 0 );
if( !m_bConsoleTextMode )
InitializeTouch();
if ( IsPC() && !m_bConsoleTextMode ) if ( IsPC() && !m_bConsoleTextMode )
{ {
InitializeJoysticks(); InitializeJoysticks();
@ -1526,6 +1530,16 @@ bool CInputSystem::GetRawMouseAccumulators( int& accumX, int& accumY )
#endif #endif
} }
bool CInputSystem::GetTouchAccumulators( InputEventType_t &event, int &fingerId, int& accumX, int& accumY )
{
event = m_touchAccumEvent;
fingerId = m_touchAccumFingerId;
accumX = m_touchAccumX;
accumY = m_touchAccumY;
return m_bJoystickInitialized;
}
void CInputSystem::SetConsoleTextMode( bool bConsoleTextMode ) void CInputSystem::SetConsoleTextMode( bool bConsoleTextMode )
{ {
/* If someone calls this after init, shut it down. */ /* If someone calls this after init, shut it down. */

View File

@ -101,6 +101,8 @@ public:
virtual void *GetHapticsInterfaceAddress() const { return NULL;} virtual void *GetHapticsInterfaceAddress() const { return NULL;}
#endif #endif
bool GetRawMouseAccumulators( int& accumX, int& accumY ); bool GetRawMouseAccumulators( int& accumX, int& accumY );
bool GetTouchAccumulators( InputEventType_t &event, int &fingerId, int& accumX, int& accumY );
virtual void SetConsoleTextMode( bool bConsoleTextMode ); virtual void SetConsoleTextMode( bool bConsoleTextMode );
// Windows proc // Windows proc
@ -240,6 +242,13 @@ public:
// Record button state and post the event // Record button state and post the event
void JoystickButtonEvent( ButtonCode_t button, int sample ); void JoystickButtonEvent( ButtonCode_t button, int sample );
// Init touch
void InitializeTouch( void );
// Shut down touch
void ShutdownTouch( void );
#if defined( WIN32 ) && !defined ( _X360 ) #if defined( WIN32 ) && !defined ( _X360 )
// NVNT attaches window to novint devices // NVNT attaches window to novint devices
@ -324,6 +333,11 @@ public:
void JoystickButtonRelease( int joystickId, int button ); // same as above. void JoystickButtonRelease( int joystickId, int button ); // same as above.
void JoystickAxisMotion( int joystickId, int axis, int value ); void JoystickAxisMotion( int joystickId, int axis, int value );
void FingerDown( int fingerId, int x, int y );
void FingerUp( int fingerId, int x, int y );
void FingerMotion( int fingerId, int x, int y );
// Steam Controller // Steam Controller
void ReadSteamController( int iIndex ); void ReadSteamController( int iIndex );
void PostKeyEvent( int iIndex, sKey_t sKey, int nSample ); void PostKeyEvent( int iIndex, sKey_t sKey, int nSample );
@ -390,6 +404,7 @@ public:
CUtlFlags<unsigned short> m_JoysticksEnabled; CUtlFlags<unsigned short> m_JoysticksEnabled;
int m_nJoystickCount; int m_nJoystickCount;
bool m_bJoystickInitialized; bool m_bJoystickInitialized;
bool m_bTouchInitialized;
bool m_bXController; bool m_bXController;
JoystickInfo_t m_pJoystickInfo[ MAX_JOYSTICKS ]; JoystickInfo_t m_pJoystickInfo[ MAX_JOYSTICKS ];
@ -444,6 +459,9 @@ public:
bool m_bRawInputSupported; bool m_bRawInputSupported;
int m_mouseRawAccumX, m_mouseRawAccumY; int m_mouseRawAccumX, m_mouseRawAccumY;
InputEventType_t m_touchAccumEvent;
int m_touchAccumFingerId, m_touchAccumX, m_touchAccumY;
// For the 'SleepUntilInput' feature // For the 'SleepUntilInput' feature
HANDLE m_hEvent; HANDLE m_hEvent;

98
inputsystem/touch_sdl.cpp Normal file
View File

@ -0,0 +1,98 @@
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose: Linux/Android touch implementation for inputsystem
//
//===========================================================================//
/* For force feedback testing. */
#include "inputsystem.h"
#include "tier1/convar.h"
#include "tier0/icommandline.h"
#include "SDL.h"
#include "SDL_touch.h"
// NOTE: This has to be the last file included!
#include "tier0/memdbgon.h"
//-----------------------------------------------------------------------------
// Handle the events coming from the Touch SDL subsystem.
//-----------------------------------------------------------------------------
int TouchSDLWatcher( void *userInfo, SDL_Event *event )
{
CInputSystem *pInputSystem = (CInputSystem *)userInfo;
SDL_Window *window = SDL_GetWindowFromID(event->tfinger.windowID);
if( !window )
return 0;
int width, height;
width = height = 0;
SDL_GetWindowSize(window, &width, &height);
switch ( event->type ) {
case SDL_FINGERDOWN:
pInputSystem->FingerDown( event->tfinger.fingerId, event->tfinger.x*width, event->tfinger.y*height );
break;
case SDL_FINGERUP:
pInputSystem->FingerUp( event->tfinger.fingerId, event->tfinger.x*width, event->tfinger.y*height );
break;
case SDL_FINGERMOTION:
pInputSystem->FingerMotion( event->tfinger.fingerId, event->tfinger.x*width, event->tfinger.y*height );
break;
}
return 1;
}
//-----------------------------------------------------------------------------
// Initialize all joysticks
//-----------------------------------------------------------------------------
void CInputSystem::InitializeTouch( void )
{
if ( m_bTouchInitialized )
ShutdownTouch();
// abort startup if user requests no touch
if ( CommandLine()->FindParm("-notouch") ) return;
m_bJoystickInitialized = true;
SDL_AddEventWatch(TouchSDLWatcher, this);
}
void CInputSystem::ShutdownTouch()
{
if ( !m_bTouchInitialized )
return;
SDL_DelEventWatch( TouchSDLWatcher, this );
m_bTouchInitialized = false;
}
void CInputSystem::FingerDown(int fingerId, int x, int y)
{
m_touchAccumEvent = IE_FingerDown;
m_touchAccumFingerId = fingerId;
m_touchAccumX = x;
m_touchAccumY = y;
PostEvent(IE_FingerDown, m_nLastSampleTick, fingerId, x, y);
}
void CInputSystem::FingerUp(int fingerId, int x, int y)
{
m_touchAccumEvent = IE_FingerUp;
m_touchAccumFingerId = fingerId;
m_touchAccumX = x;
m_touchAccumY = y;
PostEvent(IE_FingerUp, m_nLastSampleTick, fingerId, x, y);
}
void CInputSystem::FingerMotion(int fingerId, int x, int y)
{
m_touchAccumEvent = IE_FingerMotion;
m_touchAccumFingerId = fingerId;
m_touchAccumX = x;
m_touchAccumY = y;
PostEvent(IE_FingerMotion, m_nLastSampleTick, fingerId, x, y);
}

View File

@ -76,7 +76,10 @@ enum InputEventType_t
IE_ButtonReleased, // m_nData contains a ButtonCode_t IE_ButtonReleased, // m_nData contains a ButtonCode_t
IE_ButtonDoubleClicked, // m_nData contains a ButtonCode_t IE_ButtonDoubleClicked, // m_nData contains a ButtonCode_t
IE_AnalogValueChanged, // m_nData contains an AnalogCode_t, m_nData2 contains the value IE_AnalogValueChanged, // m_nData contains an AnalogCode_t, m_nData2 contains the value
IE_FingerDown,
IE_FingerUp,
IE_FingerMotion,
IE_FirstSystemEvent = 100, IE_FirstSystemEvent = 100,
IE_Quit = IE_FirstSystemEvent, IE_Quit = IE_FirstSystemEvent,
IE_ControllerInserted, // m_nData contains the controller ID IE_ControllerInserted, // m_nData contains the controller ID

View File

@ -423,7 +423,27 @@ bool InputHandleInputEvent( const InputEvent_t &event )
} }
} }
break; break;
case IE_FingerDown:
{
//g_pIInput->InternalCursorMoved( event.m_nData2, event.m_nData3 );
g_pIInput->UpdateCursorPosInternal( event.m_nData2, event.m_nData3 );
g_pIInput->SetMouseCodeState( MOUSE_LEFT, vgui::BUTTON_PRESSED );
g_pIInput->InternalMousePressed( MOUSE_LEFT );
}
return true;
case IE_FingerUp:
{
g_pIInput->UpdateCursorPosInternal( event.m_nData2, event.m_nData3 );
g_pIInput->SetMouseCodeState( MOUSE_LEFT, vgui::BUTTON_RELEASED );
g_pIInput->InternalMouseReleased( MOUSE_LEFT );
}
return true;
case IE_FingerMotion:
{
//g_pIInput->UpdateCursorPosInternal( event.m_nData2, event.m_nData3 );
g_pIInput->InternalCursorMoved( event.m_nData2, event.m_nData3 );
}
return true;
case IE_ButtonDoubleClicked: case IE_ButtonDoubleClicked:
{ {
// NOTE: data2 is the virtual key code (data1 contains the scan-code one) // NOTE: data2 is the virtual key code (data1 contains the scan-code one)