source-engine/inputsystem/touch_sdl.cpp
Star1xr 067036f6e9 Fix Android arm64 crashes and improve GLES/Input stability
- Fixed 64-bit pointer truncation in CUtlMemoryAligned affecting shadows and meshes.
- Resolved heap corruption in GameUI and GamepadUI music systems (mismatched new[]/delete).
- Fixed out-of-bounds touch input crashes by mapping 64-bit SDL finger IDs.
- Lowered OpenGL requirement to 3.0 on Android and added fallbacks for GLES 3.2+ functions.
- Fixed GamepadUI text wrapping and lifecycle crashes.
2026-05-22 19:48:25 +03:00

129 lines
3.3 KiB
C++

//========= 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;
if( !event || !pInputSystem ) return 1;
switch ( event->type ) {
case SDL_FINGERDOWN:
pInputSystem->FingerEvent( IE_FingerDown, event->tfinger.fingerId, event->tfinger.x, event->tfinger.y, event->tfinger.dx, event->tfinger.dy );
break;
case SDL_FINGERUP:
pInputSystem->FingerEvent( IE_FingerUp, event->tfinger.fingerId, event->tfinger.x, event->tfinger.y, event->tfinger.dx, event->tfinger.dy );
break;
case SDL_FINGERMOTION:
pInputSystem->FingerEvent( IE_FingerMotion ,event->tfinger.fingerId, event->tfinger.x, event->tfinger.y, event->tfinger.dx, event->tfinger.dy );
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;
memset( m_touchAccumX, 0, sizeof(m_touchAccumX) );
memset( m_touchAccumY, 0, sizeof(m_touchAccumY) );
for( int i = 0; i < TOUCH_FINGER_MAX_COUNT; i++ ) m_touchFingerIds[i] = -1;
m_bTouchInitialized = true;
SDL_AddEventWatch(TouchSDLWatcher, this);
}
void CInputSystem::ShutdownTouch()
{
if ( !m_bTouchInitialized )
return;
SDL_DelEventWatch( TouchSDLWatcher, this );
m_bTouchInitialized = false;
}
int CInputSystem::GetFingerIndex( int fingerId, bool bAdd )
{
for( int i = 0; i < TOUCH_FINGER_MAX_COUNT; i++ )
{
if( m_touchFingerIds[i] == fingerId )
return i;
}
if( bAdd )
{
for( int i = 0; i < TOUCH_FINGER_MAX_COUNT; i++ )
{
if( m_touchFingerIds[i] == -1 )
{
m_touchFingerIds[i] = fingerId;
return i;
}
}
}
return -1;
}
bool CInputSystem::GetTouchAccumulators( int index, float &dx, float &dy )
{
if( index < 0 || index >= TOUCH_FINGER_MAX_COUNT )
return false;
dx = m_touchAccumX[index];
dy = m_touchAccumY[index];
m_touchAccumX[index] = m_touchAccumY[index] = 0.f;
return true;
}
void CInputSystem::FingerEvent(int eventType, int fingerId, float x, float y, float dx, float dy)
{
int index = GetFingerIndex( fingerId, eventType != IE_FingerUp );
if( index < 0 || index >= TOUCH_FINGER_MAX_COUNT )
return;
if( eventType == IE_FingerUp )
{
m_touchAccumX[index] = 0.f;
m_touchAccumY[index] = 0.f;
m_touchFingerIds[index] = -1;
}
else
{
m_touchAccumX[index] += dx;
m_touchAccumY[index] += dy;
}
int _x,_y;
memcpy( &_x, &x, sizeof(float) );
memcpy( &_y, &y, sizeof(float) );
PostEvent(eventType, m_nLastSampleTick, index, _x, _y);
}