diff options
author | /dev/humancontroller <devhc@example.com> | 2015-06-08 14:38:33 +0200 |
---|---|---|
committer | /dev/humancontroller <devhc@example.com> | 2017-03-09 13:51:06 +0100 |
commit | 770927d96349d9cefa4cb08bed8796a7e583e03d (patch) | |
tree | e98e5fc0c1a5338743f9457bca9a22c03520bccd | |
parent | bb08049e669d5658967ec4a9979003d644601530 (diff) |
on Windows, allow running without a system console (without crashing)
-rw-r--r-- | src/sys/con_win32.c | 39 |
1 files changed, 26 insertions, 13 deletions
diff --git a/src/sys/con_win32.c b/src/sys/con_win32.c index 758fed79..8763816d 100644 --- a/src/sys/con_win32.c +++ b/src/sys/con_win32.c @@ -47,8 +47,8 @@ static int qconsole_linelen = 0; static qboolean qconsole_drawinput = qtrue; static int qconsole_cursor; -static HANDLE qconsole_hout; -static HANDLE qconsole_hin; +static HANDLE qconsole_hout = INVALID_HANDLE_VALUE; +static HANDLE qconsole_hin = INVALID_HANDLE_VALUE; /* ================== @@ -194,6 +194,9 @@ static void CON_Show( void ) CHAR_INFO line[ MAX_EDIT_LINE ]; WORD attrib; + if( qconsole_hout == INVALID_HANDLE_VALUE ) + return; + GetConsoleScreenBufferInfo( qconsole_hout, &binfo ); // if we're in the middle of printf, don't bother writing the buffer @@ -274,11 +277,19 @@ CON_Shutdown void CON_Shutdown( void ) { CON_Hide( ); - SetConsoleMode( qconsole_hin, qconsole_orig_mode ); - SetConsoleCursorInfo( qconsole_hout, &qconsole_orig_cursorinfo ); - SetConsoleTextAttribute( qconsole_hout, qconsole_attrib ); - CloseHandle( qconsole_hout ); - CloseHandle( qconsole_hin ); + if( qconsole_hout != INVALID_HANDLE_VALUE ) + { + SetConsoleCursorInfo( qconsole_hout, &qconsole_orig_cursorinfo ); + SetConsoleTextAttribute( qconsole_hout, qconsole_attrib ); + CloseHandle( qconsole_hout ); + qconsole_hout = INVALID_HANDLE_VALUE; + } + if( qconsole_hin != INVALID_HANDLE_VALUE ) + { + SetConsoleMode( qconsole_hin, qconsole_orig_mode ); + CloseHandle( qconsole_hin ); + qconsole_hin = INVALID_HANDLE_VALUE; + } } /* @@ -298,10 +309,6 @@ void CON_Init( void ) if( qconsole_hin == INVALID_HANDLE_VALUE ) return; - qconsole_hout = GetStdHandle( STD_OUTPUT_HANDLE ); - if( qconsole_hout == INVALID_HANDLE_VALUE ) - return; - GetConsoleMode( qconsole_hin, &qconsole_orig_mode ); // allow mouse wheel scrolling @@ -310,6 +317,10 @@ void CON_Init( void ) FlushConsoleInputBuffer( qconsole_hin ); + qconsole_hout = GetStdHandle( STD_OUTPUT_HANDLE ); + if( qconsole_hout == INVALID_HANDLE_VALUE ) + return; + GetConsoleScreenBufferInfo( qconsole_hout, &info ); qconsole_attrib = info.wAttributes; qconsole_backgroundAttrib = qconsole_attrib & (BACKGROUND_BLUE|BACKGROUND_GREEN|BACKGROUND_RED|BACKGROUND_INTENSITY); @@ -510,14 +521,16 @@ void CON_WindowsColorPrint( const char *msg ) if( *msg == '\n' ) { // Reset color and then add the newline - SetConsoleTextAttribute( qconsole_hout, CON_ColorCharToAttrib( COLOR_WHITE ) ); + if( qconsole_hout != INVALID_HANDLE_VALUE ) + SetConsoleTextAttribute( qconsole_hout, CON_ColorCharToAttrib( COLOR_WHITE ) ); fputs( "\n", stderr ); msg++; } else { // Set the color - SetConsoleTextAttribute( qconsole_hout, CON_ColorCharToAttrib( *( msg + 1 ) ) ); + if( qconsole_hout != INVALID_HANDLE_VALUE ) + SetConsoleTextAttribute( qconsole_hout, CON_ColorCharToAttrib( *( msg + 1 ) ) ); msg += 2; } } |