From 81b976c38432d9be5faaa67c54d76a356b136b55 Mon Sep 17 00:00:00 2001 From: nillerusr Date: Wed, 6 Oct 2021 21:09:43 +0300 Subject: [PATCH] inputsystem: touch event handling --- inputsystem/inputsystem.cpp | 16 +++++- inputsystem/inputsystem.h | 18 ++++++ inputsystem/touch_sdl.cpp | 98 +++++++++++++++++++++++++++++++++ public/inputsystem/InputEnums.h | 5 +- vguimatsurface/Input.cpp | 22 +++++++- 5 files changed, 156 insertions(+), 3 deletions(-) create mode 100644 inputsystem/touch_sdl.cpp diff --git a/inputsystem/inputsystem.cpp b/inputsystem/inputsystem.cpp index 5f11ac7d..d55e2169 100644 --- a/inputsystem/inputsystem.cpp +++ b/inputsystem/inputsystem.cpp @@ -31,7 +31,6 @@ EXPOSE_SINGLE_INTERFACE_GLOBALVAR( CInputSystem, IInputSystem, INPUTSYSTEM_INTERFACE_VERSION, g_InputSystem ); - #if defined( WIN32 ) && !defined( _X360 ) typedef BOOL (WINAPI *RegisterRawInputDevices_t) ( @@ -70,6 +69,7 @@ CInputSystem::CInputSystem() m_JoysticksEnabled.ClearAllFlags(); m_nJoystickCount = 0; m_bJoystickInitialized = false; + m_bTouchInitialized = false; m_nPollCount = 0; m_PrimaryUserId = INVALID_USER_ID; m_uiMouseWheel = 0; @@ -166,6 +166,10 @@ InitReturnVal_t CInputSystem::Init() ButtonCode_UpdateScanCodeLayout(); joy_xcontroller_found.SetValue( 0 ); + + if( !m_bConsoleTextMode ) + InitializeTouch(); + if ( IsPC() && !m_bConsoleTextMode ) { InitializeJoysticks(); @@ -1526,6 +1530,16 @@ bool CInputSystem::GetRawMouseAccumulators( int& accumX, int& accumY ) #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 ) { /* If someone calls this after init, shut it down. */ diff --git a/inputsystem/inputsystem.h b/inputsystem/inputsystem.h index 9a85148d..33634727 100644 --- a/inputsystem/inputsystem.h +++ b/inputsystem/inputsystem.h @@ -101,6 +101,8 @@ public: virtual void *GetHapticsInterfaceAddress() const { return NULL;} #endif bool GetRawMouseAccumulators( int& accumX, int& accumY ); + bool GetTouchAccumulators( InputEventType_t &event, int &fingerId, int& accumX, int& accumY ); + virtual void SetConsoleTextMode( bool bConsoleTextMode ); // Windows proc @@ -240,6 +242,13 @@ public: // Record button state and post the event void JoystickButtonEvent( ButtonCode_t button, int sample ); + + // Init touch + void InitializeTouch( void ); + + // Shut down touch + void ShutdownTouch( void ); + #if defined( WIN32 ) && !defined ( _X360 ) // NVNT attaches window to novint devices @@ -324,6 +333,11 @@ public: void JoystickButtonRelease( int joystickId, int button ); // same as above. 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 void ReadSteamController( int iIndex ); void PostKeyEvent( int iIndex, sKey_t sKey, int nSample ); @@ -390,6 +404,7 @@ public: CUtlFlags m_JoysticksEnabled; int m_nJoystickCount; bool m_bJoystickInitialized; + bool m_bTouchInitialized; bool m_bXController; JoystickInfo_t m_pJoystickInfo[ MAX_JOYSTICKS ]; @@ -444,6 +459,9 @@ public: bool m_bRawInputSupported; int m_mouseRawAccumX, m_mouseRawAccumY; + InputEventType_t m_touchAccumEvent; + int m_touchAccumFingerId, m_touchAccumX, m_touchAccumY; + // For the 'SleepUntilInput' feature HANDLE m_hEvent; diff --git a/inputsystem/touch_sdl.cpp b/inputsystem/touch_sdl.cpp new file mode 100644 index 00000000..c5e8b015 --- /dev/null +++ b/inputsystem/touch_sdl.cpp @@ -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); +} diff --git a/public/inputsystem/InputEnums.h b/public/inputsystem/InputEnums.h index ee460be2..939abc23 100644 --- a/public/inputsystem/InputEnums.h +++ b/public/inputsystem/InputEnums.h @@ -76,7 +76,10 @@ enum InputEventType_t IE_ButtonReleased, // 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_FingerDown, + IE_FingerUp, + IE_FingerMotion, + IE_FirstSystemEvent = 100, IE_Quit = IE_FirstSystemEvent, IE_ControllerInserted, // m_nData contains the controller ID diff --git a/vguimatsurface/Input.cpp b/vguimatsurface/Input.cpp index cafacced..a30ff010 100644 --- a/vguimatsurface/Input.cpp +++ b/vguimatsurface/Input.cpp @@ -423,7 +423,27 @@ bool InputHandleInputEvent( const InputEvent_t &event ) } } 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: { // NOTE: data2 is the virtual key code (data1 contains the scan-code one)