2022-01-16 01:18:36 +01:00
//=============================================================================//
//
// Purpose: Windows terminal utilities
//
//=============================================================================//
2021-12-25 22:36:38 +01:00
# include "core/stdafx.h"
2024-04-05 17:35:10 +02:00
# ifndef _TOOLS
2021-12-25 22:36:38 +01:00
# include "core/init.h"
2022-01-14 20:45:36 +01:00
# include "core/logdef.h"
2022-08-19 21:33:31 +02:00
# include "tier0/frametask.h"
2023-05-10 00:05:38 +02:00
# include "engine/cmd.h"
2021-12-25 22:36:38 +01:00
# ifndef DEDICATED
# include "windows/id3dx.h"
# endif // !DEDICATED
2024-04-05 17:35:10 +02:00
# endif // !_TOOLS
2022-07-26 00:36:35 +02:00
# include "windows/system.h"
2021-12-25 22:36:38 +01:00
# include "windows/console.h"
2021-11-05 00:57:52 +01:00
2023-03-15 21:32:08 +01:00
static std : : string s_ConsoleInput ;
2022-01-16 01:18:36 +01:00
//-----------------------------------------------------------------------------
// Purpose: sets the windows terminal background color
// Input : color -
//-----------------------------------------------------------------------------
void SetConsoleBackgroundColor ( COLORREF color )
{
2023-07-04 18:32:35 +02:00
CONSOLE_SCREEN_BUFFER_INFOEX sbInfoEx { 0 } ;
2022-01-16 01:18:36 +01:00
sbInfoEx . cbSize = sizeof ( CONSOLE_SCREEN_BUFFER_INFOEX ) ;
HANDLE consoleOut = GetStdHandle ( STD_OUTPUT_HANDLE ) ;
GetConsoleScreenBufferInfoEx ( consoleOut , & sbInfoEx ) ;
2023-07-04 21:07:56 +02:00
// The +='' 1 is required, else the window will shrink
// by '1' column and row each time this function is
// getting called on the same console window. The
// lower right bounds are detected inclusively on the
// 'GetConsoleScreenBufferEx' call and exclusively
// on the 'SetConsoleScreenBufferEx' call.
sbInfoEx . srWindow . Right + = 1 ;
2023-07-04 18:32:35 +02:00
sbInfoEx . srWindow . Bottom + = 1 ;
2023-07-04 21:07:56 +02:00
2022-01-16 01:18:36 +01:00
sbInfoEx . ColorTable [ 0 ] = color ;
SetConsoleScreenBufferInfoEx ( consoleOut , & sbInfoEx ) ;
}
//-----------------------------------------------------------------------------
// Purpose: flashes the windows terminal background color
// Input : nFlashCount -
2023-03-15 21:32:08 +01:00
// nFlashInterval -
// color -
2022-01-16 01:18:36 +01:00
//-----------------------------------------------------------------------------
void FlashConsoleBackground ( int nFlashCount , int nFlashInterval , COLORREF color )
{
2023-07-04 18:32:35 +02:00
CONSOLE_SCREEN_BUFFER_INFOEX sbInfoEx { 0 } ;
2022-01-16 01:18:36 +01:00
sbInfoEx . cbSize = sizeof ( CONSOLE_SCREEN_BUFFER_INFOEX ) ;
HANDLE consoleOut = GetStdHandle ( STD_OUTPUT_HANDLE ) ;
GetConsoleScreenBufferInfoEx ( consoleOut , & sbInfoEx ) ;
COLORREF storedBG = sbInfoEx . ColorTable [ 0 ] ;
2021-04-13 04:45:22 -07:00
2022-01-16 01:18:36 +01:00
for ( int i = 0 ; i < nFlashCount ; + + i )
{
//-- set BG color
Sleep ( nFlashInterval ) ;
sbInfoEx . ColorTable [ 0 ] = color ;
SetConsoleScreenBufferInfoEx ( consoleOut , & sbInfoEx ) ;
//-- restore previous color
Sleep ( nFlashInterval ) ;
sbInfoEx . ColorTable [ 0 ] = storedBG ;
SetConsoleScreenBufferInfoEx ( consoleOut , & sbInfoEx ) ;
}
}
//-----------------------------------------------------------------------------
// Purpose: terminal window setup
2023-07-01 01:20:47 +02:00
// Input : bAnsiColor -
2022-01-16 01:18:36 +01:00
//-----------------------------------------------------------------------------
2023-07-01 01:20:47 +02:00
void Console_Init ( const bool bAnsiColor )
2021-04-13 04:45:22 -07:00
{
2024-04-05 17:35:10 +02:00
# ifndef _TOOLS
2021-06-28 15:51:32 -07:00
///////////////////////////////////////////////////////////////////////////
2021-04-13 04:45:22 -07:00
// Create the console window
2021-12-25 22:36:38 +01:00
if ( AllocConsole ( ) = = FALSE )
2021-04-13 04:45:22 -07:00
{
2024-04-04 18:50:09 +02:00
char szBuf [ 2048 ] ;
snprintf ( szBuf , sizeof ( szBuf ) , " Failed to create console window! [%s] \n " , std : : system_category ( ) . message ( static_cast < int > ( : : GetLastError ( ) ) ) . c_str ( ) ) ;
OutputDebugStringA ( szBuf ) ;
2021-04-13 04:45:22 -07:00
return ;
}
2022-01-16 01:18:36 +01:00
//-- Set the window title
2022-03-26 01:23:06 +01:00
SetConsoleTitleA ( " R5 " ) ;
2021-04-13 04:45:22 -07:00
2022-01-16 01:18:36 +01:00
//-- Open input/output streams
2021-04-13 04:45:22 -07:00
FILE * fDummy ;
2021-11-05 00:57:52 +01:00
freopen_s ( & fDummy , " CONIN$ " , " r " , stdin ) ;
2021-04-13 04:45:22 -07:00
freopen_s ( & fDummy , " CONOUT$ " , " w " , stdout ) ;
freopen_s ( & fDummy , " CONOUT$ " , " w " , stderr ) ;
2022-01-16 01:18:36 +01:00
//-- Create a worker thread to process console commands
2022-01-14 20:45:36 +01:00
DWORD dwThreadId = NULL ;
2021-12-25 22:36:38 +01:00
DWORD __stdcall ProcessConsoleWorker ( LPVOID ) ;
2022-01-14 20:45:36 +01:00
HANDLE hThread = CreateThread ( NULL , 0 , ProcessConsoleWorker , NULL , 0 , & dwThreadId ) ;
2021-12-25 22:36:38 +01:00
2021-11-05 00:57:52 +01:00
if ( hThread )
2021-04-17 04:51:04 -07:00
{
2021-11-05 00:57:52 +01:00
CloseHandle ( hThread ) ;
2021-04-17 04:51:04 -07:00
}
2024-04-05 17:35:10 +02:00
# endif // !_TOOLS
2023-03-27 02:01:48 +02:00
HANDLE hOutput = GetStdHandle ( STD_OUTPUT_HANDLE ) ;
DWORD dwMode = NULL ;
2022-01-14 20:45:36 +01:00
2023-07-01 01:20:47 +02:00
if ( bAnsiColor )
2022-01-14 20:45:36 +01:00
{
GetConsoleMode ( hOutput , & dwMode ) ;
dwMode | = ENABLE_PROCESSED_OUTPUT | ENABLE_VIRTUAL_TERMINAL_PROCESSING ;
if ( ! SetConsoleMode ( hOutput , dwMode ) ) // Some editions of Windows have 'VirtualTerminalLevel' disabled by default.
{
// Warn the user if 'VirtualTerminalLevel' could not be set on users environment.
2024-04-04 18:50:09 +02:00
MessageBoxA ( NULL , " Failed to set console mode 'VirtualTerminalLevel'; please disable ansi-colors and restart the program if output logging appears distorted. " , " SDK Warning " , MB_ICONEXCLAMATION | MB_OK ) ;
2022-01-14 20:45:36 +01:00
}
2023-03-15 21:11:20 +01:00
2023-02-18 15:49:04 +01:00
SetConsoleBackgroundColor ( 0x00000000 ) ;
2022-01-16 01:18:36 +01:00
AnsiColors_Init ( ) ;
2022-01-14 20:45:36 +01:00
}
2023-03-27 02:01:48 +02:00
2024-04-05 17:35:10 +02:00
# ifndef _TOOLS
2022-07-26 00:36:35 +02:00
SetConsoleCtrlHandler ( ConsoleHandlerRoutine , true ) ;
2024-04-05 17:35:10 +02:00
# endif // !_TOOLS
2021-12-25 22:36:38 +01:00
}
2023-03-18 14:10:29 +01:00
//-----------------------------------------------------------------------------
// Purpose: terminal window shutdown
//-----------------------------------------------------------------------------
void Console_Shutdown ( )
{
///////////////////////////////////////////////////////////////////////////
// Destroy the console window
if ( FreeConsole ( ) = = FALSE )
{
2024-04-04 18:50:09 +02:00
char szBuf [ 2048 ] ;
snprintf ( szBuf , sizeof ( szBuf ) , " Failed to destroy console window! [%s] \n " , std : : system_category ( ) . message ( static_cast < int > ( : : GetLastError ( ) ) ) . c_str ( ) ) ;
OutputDebugStringA ( szBuf ) ;
2023-03-18 14:10:29 +01:00
return ;
}
}
2024-04-05 17:35:10 +02:00
# ifndef _TOOLS
2021-12-25 22:36:38 +01:00
//#############################################################################
2023-03-27 02:01:48 +02:00
// CONSOLE WORKER
2021-12-25 22:36:38 +01:00
//#############################################################################
DWORD __stdcall ProcessConsoleWorker ( LPVOID )
{
while ( true )
{
2022-01-16 01:18:36 +01:00
//printf("] ");
//-- Get the user input on the debug console
2023-03-15 21:32:08 +01:00
std : : getline ( std : : cin , s_ConsoleInput ) ;
2021-12-25 22:36:38 +01:00
2022-07-26 03:00:51 +02:00
// Execute the command.
2023-03-15 21:32:08 +01:00
Cbuf_AddText ( Cbuf_GetCurrentPlayer ( ) , s_ConsoleInput . c_str ( ) , cmd_source_t : : kCommandSrcCode ) ;
2022-03-25 13:17:57 +01:00
2023-03-15 21:32:08 +01:00
if ( ! s_ConsoleInput . empty ( ) )
s_ConsoleInput . clear ( ) ;
2021-12-25 22:36:38 +01:00
Sleep ( 50 ) ;
}
return NULL ;
}
2024-04-05 17:35:10 +02:00
# endif // !_TOOLS