diff --git a/engine/common/host.c b/engine/common/host.c index 764a1285..563c0658 100644 --- a/engine/common/host.c +++ b/engine/common/host.c @@ -595,7 +595,7 @@ static void Host_GetCommands( void ) { char *cmd; - while( ( cmd = Sys_Input() ) ) + while( ( cmd = Platform_Input() ) ) { Cbuf_AddText( cmd ); Cbuf_Execute(); diff --git a/engine/common/sys_con.c b/engine/common/sys_con.c index e3c41fd6..cdaa8162 100644 --- a/engine/common/sys_con.c +++ b/engine/common/sys_con.c @@ -14,10 +14,7 @@ GNU General Public License for more details. */ #include "common.h" -#if XASH_WIN32 -#define STDOUT_FILENO 1 -#include -#elif XASH_ANDROID +#if XASH_ANDROID #include #endif #include @@ -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 -#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 diff --git a/engine/platform/platform.h b/engine/platform/platform.h index 0ec16b2a..6798b7df 100644 --- a/engine/platform/platform.h +++ b/engine/platform/platform.h @@ -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 diff --git a/engine/platform/posix/con_posix.c b/engine/platform/posix/con_posix.c new file mode 100644 index 00000000..db8b9115 --- /dev/null +++ b/engine/platform/posix/con_posix.c @@ -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 + +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