Skip to content

Commit

Permalink
engine: client: input: slight refactoring of mouse grabbing, add verb…
Browse files Browse the repository at this point in the history
…ose output for debugging mouse issues
  • Loading branch information
a1batross committed Aug 16, 2024
1 parent 70e11cc commit 1514056
Showing 1 changed file with 58 additions and 42 deletions.
100 changes: 58 additions & 42 deletions engine/client/input.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ static CVAR_DEFINE_AUTO( cl_forwardspeed, "400", FCVAR_ARCHIVE | FCVAR_CLIENTDLL
static CVAR_DEFINE_AUTO( cl_backspeed, "400", FCVAR_ARCHIVE | FCVAR_CLIENTDLL | FCVAR_FILTERABLE, "Default back move speed" );
static CVAR_DEFINE_AUTO( cl_sidespeed, "400", FCVAR_ARCHIVE | FCVAR_CLIENTDLL | FCVAR_FILTERABLE, "Default side move speed" );

static CVAR_DEFINE_AUTO( m_grab_debug, "0", FCVAR_PRIVILEGED, "show debug messages on mouse state change" );

/*
================
IN_CollectInputDevices
Expand Down Expand Up @@ -116,6 +118,7 @@ static void IN_StartupMouse( void )
Cvar_RegisterVariable( &m_yaw );
Cvar_RegisterVariable( &look_filter );
Cvar_RegisterVariable( &m_rawinput );
Cvar_RegisterVariable( &m_grab_debug );

// You can use -nomouse argument to prevent using mouse from client
// -noenginemouse will disable all mouse input
Expand Down Expand Up @@ -202,70 +205,83 @@ void IN_ToggleClientMouse( int newstate, int oldstate )
}
}

static void IN_CheckMouseState( qboolean active )
static void IN_SetRelativeMouseMode( qboolean set, qboolean verbose )
{
static qboolean s_bRawInput, s_bMouseGrab;

#if XASH_WIN32
qboolean useRawInput = ( m_rawinput.value && clgame.client_dll_uses_sdl ) || clgame.dllFuncs.pfnLookEvent != NULL;
#else
qboolean useRawInput = true; // always use SDL code
#endif
static qboolean s_bRawInput;

if( m_ignore.value )
return;

if( active && useRawInput && !host.mouse_visible && cls.state == ca_active )
if( set && !s_bRawInput )
{
if( !s_bRawInput )
{
#if XASH_SDL == 2
SDL_GetRelativeMouseState( NULL, NULL );
SDL_SetRelativeMouseMode( SDL_TRUE );
SDL_GetRelativeMouseState( NULL, NULL );
SDL_SetRelativeMouseMode( SDL_TRUE );
#endif

// Con_Printf( "Enable relative mode\n" );
s_bRawInput = true;
}
s_bRawInput = true;
if( verbose )
Con_Printf( "%s: true\n", __func__ );
}
else
else if( !set && s_bRawInput )
{
if( s_bRawInput )
{
#if XASH_SDL == 2
SDL_GetRelativeMouseState( NULL, NULL );
SDL_SetRelativeMouseMode( SDL_FALSE );
SDL_GetRelativeMouseState( NULL, NULL );
SDL_SetRelativeMouseMode( SDL_FALSE );
#endif
// Con_Printf( "Disable relative mode\n" );
s_bRawInput = false;
}
s_bRawInput = false;
if( verbose )
Con_Printf( "%s: false\n", __func__ );
}
}

if( active && !host.mouse_visible && cls.state == ca_active )
static void IN_SetMouseGrab( qboolean set, qboolean verbose )
{
static qboolean s_bMouseGrab;

if( set && !s_bMouseGrab )
{
if( !s_bMouseGrab )
{
#if XASH_SDL
SDL_SetWindowGrab( host.hWnd, SDL_TRUE );
SDL_SetWindowGrab( host.hWnd, SDL_TRUE );
#endif
// Con_Printf( "Enable grab\n" );
s_bMouseGrab = true;
}
s_bMouseGrab = true;
if( verbose )
Con_Printf( "%s: true\n", __func__ );
}
else
else if( !set && s_bMouseGrab )
{
if( s_bMouseGrab )
{
#if XASH_SDL
SDL_SetWindowGrab( host.hWnd, SDL_FALSE );
SDL_SetWindowGrab( host.hWnd, SDL_FALSE );
#endif

// Con_Printf( "Disable grab\n" );
s_bMouseGrab = false;
}
s_bMouseGrab = false;
if( verbose )
Con_Printf( "%s: false\n", __func__ );
}
}

static void IN_CheckMouseState( qboolean active )
{
qboolean use_raw_input, verbose;

#if XASH_WIN32
use_raw_input = ( m_rawinput.value && clgame.client_dll_uses_sdl ) || clgame.dllFuncs.pfnLookEvent != NULL;
#else
use_raw_input = true; // always use SDL code
#endif

verbose = m_grab_debug.value ? true : false;

if( m_ignore.value )
active = false;

if( active && use_raw_input && !host.mouse_visible && cls.state == ca_active )
IN_SetRelativeMouseMode( true, verbose );
else
IN_SetRelativeMouseMode( false, verbose );

if( active && !host.mouse_visible && cls.state == ca_active )
IN_SetMouseGrab( true, verbose );
else
IN_SetMouseGrab( false, verbose );
}

/*
===========
IN_ActivateMouse
Expand Down

0 comments on commit 1514056

Please sign in to comment.