Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

276 lines
5.6 KiB

  1. //========= Copyright 1996-2005, Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. // $NoKeywords: $
  6. //
  7. //=============================================================================//
  8. // CTextConsoleWin32.cpp: Win32 implementation of the TextConsole class.
  9. //
  10. //////////////////////////////////////////////////////////////////////
  11. #pragma warning(disable:4100) //unreferenced formal parameter
  12. #include "TextConsoleWin32.h"
  13. #pragma warning(default:4100) //unreferenced formal parameter
  14. #include "tier0/dbg.h"
  15. #ifdef _WIN32
  16. BOOL WINAPI ConsoleHandlerRoutine( DWORD CtrlType )
  17. {
  18. NOTE_UNUSED( CtrlType );
  19. /* TODO ?
  20. if ( CtrlType != CTRL_C_EVENT && CtrlType != CTRL_BREAK_EVENT )
  21. m_System->Stop(); // don't quit on break or ctrl+c
  22. */
  23. return TRUE;
  24. }
  25. // GetConsoleHwnd() helper function from MSDN Knowledge Base Article Q124103
  26. // needed, because HWND GetConsoleWindow(VOID) is not avaliable under Win95/98/ME
  27. HWND GetConsoleHwnd(void)
  28. {
  29. HWND hwndFound; // This is what is returned to the caller.
  30. char pszNewWindowTitle[1024]; // Contains fabricated WindowTitle
  31. char pszOldWindowTitle[1024]; // Contains original WindowTitle
  32. // Fetch current window title.
  33. GetConsoleTitle( pszOldWindowTitle, 1024 );
  34. // Format a "unique" NewWindowTitle.
  35. wsprintf( pszNewWindowTitle,"%d/%d", GetTickCount(), GetCurrentProcessId() );
  36. // Change current window title.
  37. SetConsoleTitle(pszNewWindowTitle);
  38. // Ensure window title has been updated.
  39. Sleep(40);
  40. // Look for NewWindowTitle.
  41. hwndFound = FindWindow( NULL, pszNewWindowTitle );
  42. // Restore original window title.
  43. SetConsoleTitle( pszOldWindowTitle );
  44. return hwndFound;
  45. }
  46. CTextConsoleWin32::CTextConsoleWin32()
  47. {
  48. hinput = houtput = INVALID_HANDLE_VALUE;
  49. }
  50. bool CTextConsoleWin32::Init( /*IBaseSystem * system*/ )
  51. {
  52. if ( !AllocConsole () )
  53. //m_System = system;
  54. //if ( m_System )
  55. // SetTitle( m_System->GetName() );
  56. //else
  57. SetTitle( "SOURCE DEDICATED SERVER" );
  58. hinput = GetStdHandle ( STD_INPUT_HANDLE );
  59. houtput = GetStdHandle ( STD_OUTPUT_HANDLE );
  60. if ( !SetConsoleCtrlHandler( &ConsoleHandlerRoutine, TRUE) )
  61. {
  62. Print( "WARNING! TextConsole::Init: Could not attach console hook.\n" );
  63. }
  64. Attrib = FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_INTENSITY ;
  65. SetWindowPos( GetConsoleHwnd(), HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOREPOSITION | SWP_SHOWWINDOW );
  66. return CTextConsole::Init( /*system */ );
  67. }
  68. void CTextConsoleWin32::ShutDown( void )
  69. {
  70. FreeConsole();
  71. CTextConsole::ShutDown();
  72. }
  73. void CTextConsoleWin32::SetVisible( bool visible )
  74. {
  75. ShowWindow ( GetConsoleHwnd(), visible ? SW_SHOW : SW_HIDE );
  76. m_ConsoleVisible = visible;
  77. }
  78. char * CTextConsoleWin32::GetLine( void )
  79. {
  80. while ( 1 )
  81. {
  82. INPUT_RECORD recs[ 1024 ];
  83. unsigned long numread;
  84. unsigned long numevents;
  85. if ( !GetNumberOfConsoleInputEvents( hinput, &numevents ) )
  86. {
  87. Error("CTextConsoleWin32::GetLine: !GetNumberOfConsoleInputEvents");
  88. return NULL;
  89. }
  90. if ( numevents <= 0 )
  91. break;
  92. if ( !ReadConsoleInput( hinput, recs, ARRAYSIZE( recs ), &numread ) )
  93. {
  94. Error("CTextConsoleWin32::GetLine: !ReadConsoleInput");
  95. return NULL;
  96. }
  97. if ( numread == 0 )
  98. return NULL;
  99. for ( int i=0; i < (int)numread; i++ )
  100. {
  101. INPUT_RECORD *pRec = &recs[i];
  102. if ( pRec->EventType != KEY_EVENT )
  103. continue;
  104. if ( pRec->Event.KeyEvent.bKeyDown )
  105. {
  106. // check for cursor keys
  107. if ( pRec->Event.KeyEvent.wVirtualKeyCode == VK_UP )
  108. {
  109. ReceiveUpArrow();
  110. }
  111. else if ( pRec->Event.KeyEvent.wVirtualKeyCode == VK_DOWN )
  112. {
  113. ReceiveDownArrow();
  114. }
  115. else if ( pRec->Event.KeyEvent.wVirtualKeyCode == VK_LEFT )
  116. {
  117. ReceiveLeftArrow();
  118. }
  119. else if ( pRec->Event.KeyEvent.wVirtualKeyCode == VK_RIGHT )
  120. {
  121. ReceiveRightArrow();
  122. }
  123. else
  124. {
  125. char ch;
  126. int nLen;
  127. ch = pRec->Event.KeyEvent.uChar.AsciiChar;
  128. switch ( ch )
  129. {
  130. case '\r': // Enter
  131. nLen = ReceiveNewline();
  132. if ( nLen )
  133. {
  134. return m_szConsoleText;
  135. }
  136. break;
  137. case '\b': // Backspace
  138. ReceiveBackspace();
  139. break;
  140. case '\t': // TAB
  141. ReceiveTab();
  142. break;
  143. default:
  144. if ( ( ch >= ' ') && ( ch <= '~' ) ) // dont' accept nonprintable chars
  145. {
  146. ReceiveStandardChar( ch );
  147. }
  148. break;
  149. }
  150. }
  151. }
  152. }
  153. }
  154. return NULL;
  155. }
  156. void CTextConsoleWin32::PrintRaw( char * pszMsg, int nChars )
  157. {
  158. unsigned long dummy;
  159. if ( houtput == INVALID_HANDLE_VALUE )
  160. return;
  161. if ( nChars == 0 )
  162. {
  163. WriteFile( houtput, pszMsg, strlen( pszMsg ), &dummy, NULL );
  164. }
  165. else
  166. {
  167. WriteFile( houtput, pszMsg, nChars, &dummy, NULL );
  168. }
  169. }
  170. void CTextConsoleWin32::Echo( char * pszMsg, int nChars )
  171. {
  172. PrintRaw( pszMsg, nChars );
  173. }
  174. int CTextConsoleWin32::GetWidth( void )
  175. {
  176. CONSOLE_SCREEN_BUFFER_INFO csbi;
  177. int nWidth;
  178. nWidth = 0;
  179. if ( GetConsoleScreenBufferInfo( houtput, &csbi ) )
  180. {
  181. nWidth = csbi.dwSize.X;
  182. }
  183. if ( nWidth <= 1 )
  184. nWidth = 80;
  185. return nWidth;
  186. }
  187. void CTextConsoleWin32::SetStatusLine( char * pszStatus )
  188. {
  189. strncpy( statusline, pszStatus, 80 );
  190. statusline[ 79 ] = '\0';
  191. UpdateStatus();
  192. }
  193. void CTextConsoleWin32::UpdateStatus( void )
  194. {
  195. COORD coord;
  196. DWORD dwWritten = 0;
  197. WORD wAttrib[ 80 ];
  198. for ( int i = 0; i < 80; i++ )
  199. {
  200. wAttrib[i] = Attrib; //FOREGROUND_GREEN | FOREGROUND_INTENSITY | BACKGROUND_INTENSITY ;
  201. }
  202. coord.X = coord.Y = 0;
  203. WriteConsoleOutputAttribute( houtput, wAttrib, 80, coord, &dwWritten );
  204. WriteConsoleOutputCharacter( houtput, statusline, 80, coord, &dwWritten );
  205. }
  206. void CTextConsoleWin32::SetTitle( char * pszTitle )
  207. {
  208. SetConsoleTitle( pszTitle );
  209. }
  210. void CTextConsoleWin32::SetColor(WORD attrib)
  211. {
  212. Attrib = attrib;
  213. }
  214. #endif