engine: move select-based stdin input function to platform/posix

This commit is contained in:
Alibek Omarov 2024-12-24 11:13:05 +03:00
parent 74c19a1557
commit 4f98187e9a
4 changed files with 66 additions and 48 deletions

View file

@ -595,7 +595,7 @@ static void Host_GetCommands( void )
{
char *cmd;
while( ( cmd = Sys_Input() ) )
while( ( cmd = Platform_Input() ) )
{
Cbuf_AddText( cmd );
Cbuf_Execute();

View file

@ -14,10 +14,7 @@ GNU General Public License for more details.
*/
#include "common.h"
#if XASH_WIN32
#define STDOUT_FILENO 1
#include <io.h>
#elif XASH_ANDROID
#if XASH_ANDROID
#include <android/log.h>
#endif
#include <string.h>
@ -29,18 +26,10 @@ GNU General Public License for more details.
// do not waste precious CPU cycles on mobiles or low memory devices
#if !XASH_WIN32 && !XASH_MOBILE_PLATFORM && !XASH_LOW_MEMORY
#define XASH_COLORIZE_CONSOLE true
// use with caution, running engine in Qt Creator may cause a freeze in read() call
// I have never encountered this bug anywhere else, so still enable by default
#define XASH_USE_SELECT 1
#else
#define XASH_COLORIZE_CONSOLE false
#endif
#if XASH_USE_SELECT
// non-blocking console input
#include <sys/select.h>
#endif
typedef struct {
char title[64];
qboolean log_active;
@ -51,41 +40,6 @@ typedef struct {
static LogData s_ld;
char *Sys_Input( void )
{
#if XASH_USE_SELECT
if( Host_IsDedicated( ))
{
fd_set rfds;
static char line[1024];
static int len;
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = 0;
FD_ZERO( &rfds );
FD_SET( 0, &rfds); // stdin
while( select( 1, &rfds, NULL, NULL, &tv ) > 0 )
{
if( read( 0, &line[len], 1 ) != 1 )
break;
if( line[len] == '\n' || len > 1022 )
{
line[ ++len ] = 0;
len = 0;
return line;
}
len++;
tv.tv_sec = 0;
tv.tv_usec = 0;
}
}
#endif
#if XASH_WIN32
return Wcon_Input();
#endif
return NULL;
}
void Sys_DestroyConsole( void )
{
// last text message into console or log

View file

@ -51,6 +51,7 @@ void IOS_LaunchDialog( void );
#if XASH_POSIX
void Posix_Daemonize( void );
void Posix_SetupSigtermHandling( void );
char *Posix_Input( void );
#endif
#if XASH_SDL
@ -241,6 +242,17 @@ static inline void Platform_SetTimer( float time )
#endif
}
static inline char *Platform_Input( void )
{
#if XASH_WIN32
return Wcon_Input();
#elif XASH_POSIX && !XASH_MOBILE_PLATFORM && !XASH_LOW_MEMORY
return Posix_Input();
#else
return NULL;
#endif
}
#if XASH_WIN32
void Platform_SendKeyEvents( void );
#else

View file

@ -0,0 +1,52 @@
/*
con_posix.c - reading from stdin
Copyright (C) 2024 Flying With Gauss
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
#include "platform/platform.h"
#if !XASH_MOBILE_PLATFORM && !XASH_LOW_MEMORY
// use with caution, running engine in Qt Creator may cause a freeze in read() call
// I have never encountered this bug anywhere else, so still enable by default
#include <sys/select.h>
char *Posix_Input( void )
{
fd_set rfds;
static char line[1024];
static int len;
struct timeval tv = { 0 };
if( !Host_IsDedicated( ))
return NULL;
FD_ZERO( &rfds );
FD_SET( 0, &rfds); // stdin
while( select( 1, &rfds, NULL, NULL, &tv ) > 0 )
{
if( read( 0, &line[len], 1 ) != 1 )
break;
if( line[len] == '\n' || len > ( sizeof( line ) - 2 ))
{
line[ ++len ] = 0;
len = 0;
return line;
}
len++;
tv.tv_sec = 0;
tv.tv_usec = 0;
}
return NULL;
}
#endif // !XASH_MOBILE_PLATFORM && !XASH_LOW_MEMORY