Skip to content

Commit

Permalink
Merge remote-tracking branch 'JACoders/cli-console-win32'
Browse files Browse the repository at this point in the history
  • Loading branch information
Bucky21659 committed Jul 16, 2018
2 parents 004aac0 + 88d9cb2 commit 393abd4
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
2 changes: 1 addition & 1 deletion codemp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ if(BuildMPEngine)
set(MPEngineSysFiles
${MPEngineSysFiles}
"${SharedDir}/sys/sys_win32.cpp"
"${SharedDir}/sys/con_passive.cpp"
"${SharedDir}/sys/con_win32.cpp"
)
else(WIN32)
set(MPEngineSysFiles
Expand Down
5 changes: 4 additions & 1 deletion shared/sys/con_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ along with this program; if not, see <http://www.gnu.org/licenses/>.

#include <cstdio>

/* con_passive.cpp | con_win32.cpp | con_tty.cpp */
/* con_win32.cpp | con_tty.cpp */
void CON_Shutdown( void );
void CON_Init( void );
char *CON_Input( void );
void CON_Print( const char *msg );

void CON_CreateConsoleWindow( void );
void CON_DeleteConsoleWindow( void );

/* con_log.cpp */
void ConsoleLogAppend( const char *string );
void ConsoleLogWriteOut( FILE *fp );
75 changes: 75 additions & 0 deletions shared/sys/con_win32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ static int qconsole_cursor;
static HANDLE qconsole_hout;
static HANDLE qconsole_hin;

static FILE *stdinptr, *stdoutptr, *stderrptr;
static qboolean never_detach;

/*
==================
CON_ColorCharToAttrib
Expand Down Expand Up @@ -190,6 +193,9 @@ static void CON_Show( void )
CHAR_INFO line[ MAX_EDIT_LINE ];
WORD attrib;

if( !GetConsoleWindow() )
return;

GetConsoleScreenBufferInfo( qconsole_hout, &binfo );

// if we're in the middle of printf, don't bother writing the buffer
Expand Down Expand Up @@ -270,6 +276,7 @@ CON_Shutdown
void CON_Shutdown( void )
{
CON_Hide( );
SetConsoleCtrlHandler( CON_CtrlHandler, FALSE );
SetConsoleMode( qconsole_hin, qconsole_orig_mode );
SetConsoleCursorInfo( qconsole_hout, &qconsole_orig_cursorinfo );
SetConsoleTextAttribute( qconsole_hout, qconsole_attrib );
Expand All @@ -287,6 +294,20 @@ void CON_Init( void )
CONSOLE_SCREEN_BUFFER_INFO info;
int i;

#ifndef DEDICATED
if( AttachConsole( ATTACH_PARENT_PROCESS ) ) {
// started from console
stdinptr = freopen( "CONIN$", "r", stdin );
stdoutptr = freopen( "CONOUT$", "w", stdout );
stderrptr = freopen( "CONOUT$", "w", stderr );
never_detach = qtrue;
}
#endif

#ifdef _DEBUG
never_detach = qtrue;
#endif

// handle Ctrl-C or other console termination
SetConsoleCtrlHandler( CON_CtrlHandler, TRUE );

Expand All @@ -309,7 +330,11 @@ void CON_Init( void )
qconsole_attrib = info.wAttributes;
qconsole_backgroundAttrib = qconsole_attrib & (BACKGROUND_BLUE|BACKGROUND_GREEN|BACKGROUND_RED|BACKGROUND_INTENSITY);

#ifdef DEDICATED
SetConsoleTitle(CLIENT_WINDOW_TITLE " Dedicated Server Console");
#else
SetConsoleTitle(CLIENT_WINDOW_TITLE " Console");
#endif

// initialize history
for( i = 0; i < QCONSOLE_HISTORY; i++ )
Expand All @@ -319,6 +344,53 @@ void CON_Init( void )
SetConsoleTextAttribute( qconsole_hout, CON_ColorCharToAttrib( COLOR_WHITE ) );
}

/*
==================
CON_CreateConsoleWindow
==================
*/
void CON_CreateConsoleWindow(void) {
if (!GetConsoleWindow()) {
CON_Shutdown();

if (AllocConsole()) {
stdinptr = freopen("CONIN$", "r", stdin);
stdoutptr = freopen("CONOUT$", "w", stdout);
stderrptr = freopen("CONOUT$", "w", stderr);
}

CON_Init();
}
}

/*
==================
CON_DeleteConsoleWindow
==================
*/
void CON_DeleteConsoleWindow(void) {
if (GetConsoleWindow() && !never_detach) {
CON_Shutdown();

if (stdinptr)
fclose(stdinptr);

if (stdoutptr)
fclose(stdoutptr);

if (stderrptr)
fclose(stderrptr);

if (FreeConsole()) {
freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
}

CON_Init();
}
}

/*
==================
CON_Input
Expand Down Expand Up @@ -547,6 +619,9 @@ CON_Print
*/
void CON_Print( const char *msg )
{
if ( !GetConsoleWindow() )
return;

CON_Hide( );

CON_WindowsColorPrint( msg );
Expand Down
3 changes: 3 additions & 0 deletions shared/sys/sys_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,9 @@ int main ( int argc, char* argv[] )
char commandLine[ MAX_STRING_CHARS ] = { 0 };

Sys_PlatformInit();
#if defined(_DEBUG) && !defined(DEDICATED)
CON_CreateConsoleWindow();
#endif
CON_Init();

// get the initial time base
Expand Down

0 comments on commit 393abd4

Please sign in to comment.