| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include "../qcommon/q_shared.h" |
| 24 |
#include "../qcommon/qcommon.h" |
| 25 |
#include "sys_local.h" |
| 26 |
#include "windows.h" |
| 27 |
|
| 28 |
#define QCONSOLE_HISTORY 32 |
| 29 |
|
| 30 |
static WORD qconsole_attrib; |
| 31 |
static WORD qconsole_backgroundAttrib; |
| 32 |
|
| 33 |
|
| 34 |
static DWORD qconsole_orig_mode; |
| 35 |
static CONSOLE_CURSOR_INFO qconsole_orig_cursorinfo; |
| 36 |
|
| 37 |
|
| 38 |
static char qconsole_history[ QCONSOLE_HISTORY ][ MAX_EDIT_LINE ]; |
| 39 |
static int qconsole_history_pos = -1; |
| 40 |
static int qconsole_history_lines = 0; |
| 41 |
static int qconsole_history_oldest = 0; |
| 42 |
|
| 43 |
|
| 44 |
static char qconsole_line[ MAX_EDIT_LINE ]; |
| 45 |
static int qconsole_linelen = 0; |
| 46 |
static qboolean qconsole_drawinput = qtrue; |
| 47 |
static int qconsole_cursor; |
| 48 |
|
| 49 |
static HANDLE qconsole_hout; |
| 50 |
static HANDLE qconsole_hin; |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
static WORD CON_ColorCharToAttrib( char color ) { |
| 60 |
WORD attrib; |
| 61 |
|
| 62 |
if ( color == COLOR_WHITE ) |
| 63 |
{ |
| 64 |
|
| 65 |
attrib = qconsole_attrib; |
| 66 |
} |
| 67 |
else |
| 68 |
{ |
| 69 |
float *rgba = g_color_table[ ColorIndex( color ) ]; |
| 70 |
|
| 71 |
|
| 72 |
attrib = ( rgba[0] >= 0.5 ? FOREGROUND_RED : 0 ) | |
| 73 |
( rgba[1] >= 0.5 ? FOREGROUND_GREEN : 0 ) | |
| 74 |
( rgba[2] >= 0.5 ? FOREGROUND_BLUE : 0 ) | |
| 75 |
( rgba[3] >= 0.5 ? FOREGROUND_INTENSITY : 0 ); |
| 76 |
|
| 77 |
|
| 78 |
attrib |= qconsole_backgroundAttrib; |
| 79 |
} |
| 80 |
|
| 81 |
return attrib; |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
static BOOL WINAPI CON_CtrlHandler( DWORD sig ) |
| 97 |
{ |
| 98 |
Sys_SigHandler( sig ); |
| 99 |
return TRUE; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
static void CON_HistAdd( void ) |
| 108 |
{ |
| 109 |
Q_strncpyz( qconsole_history[ qconsole_history_oldest ], qconsole_line, |
| 110 |
sizeof( qconsole_history[ qconsole_history_oldest ] ) ); |
| 111 |
|
| 112 |
if( qconsole_history_lines < QCONSOLE_HISTORY ) |
| 113 |
qconsole_history_lines++; |
| 114 |
|
| 115 |
if( qconsole_history_oldest >= QCONSOLE_HISTORY - 1 ) |
| 116 |
qconsole_history_oldest = 0; |
| 117 |
else |
| 118 |
qconsole_history_oldest++; |
| 119 |
|
| 120 |
qconsole_history_pos = qconsole_history_oldest; |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
static void CON_HistPrev( void ) |
| 129 |
{ |
| 130 |
int pos; |
| 131 |
|
| 132 |
pos = ( qconsole_history_pos < 1 ) ? |
| 133 |
( QCONSOLE_HISTORY - 1 ) : ( qconsole_history_pos - 1 ); |
| 134 |
|
| 135 |
|
| 136 |
if( pos == qconsole_history_oldest || pos >= qconsole_history_lines ) |
| 137 |
return; |
| 138 |
|
| 139 |
qconsole_history_pos = pos; |
| 140 |
Q_strncpyz( qconsole_line, qconsole_history[ qconsole_history_pos ], |
| 141 |
sizeof( qconsole_line ) ); |
| 142 |
qconsole_linelen = strlen( qconsole_line ); |
| 143 |
qconsole_cursor = qconsole_linelen; |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
static void CON_HistNext( void ) |
| 152 |
{ |
| 153 |
int pos; |
| 154 |
|
| 155 |
|
| 156 |
if( qconsole_history_pos == qconsole_history_oldest ) |
| 157 |
return; |
| 158 |
|
| 159 |
pos = ( qconsole_history_pos >= QCONSOLE_HISTORY - 1 ) ? |
| 160 |
0 : ( qconsole_history_pos + 1 ); |
| 161 |
|
| 162 |
|
| 163 |
if( pos == qconsole_history_oldest ) |
| 164 |
{ |
| 165 |
qconsole_history_pos = pos; |
| 166 |
qconsole_line[ 0 ] = '\0'; |
| 167 |
qconsole_linelen = 0; |
| 168 |
qconsole_cursor = qconsole_linelen; |
| 169 |
return; |
| 170 |
} |
| 171 |
|
| 172 |
qconsole_history_pos = pos; |
| 173 |
Q_strncpyz( qconsole_line, qconsole_history[ qconsole_history_pos ], |
| 174 |
sizeof( qconsole_line ) ); |
| 175 |
qconsole_linelen = strlen( qconsole_line ); |
| 176 |
qconsole_cursor = qconsole_linelen; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
static void CON_Show( void ) |
| 186 |
{ |
| 187 |
CONSOLE_SCREEN_BUFFER_INFO binfo; |
| 188 |
COORD writeSize = { MAX_EDIT_LINE, 1 }; |
| 189 |
COORD writePos = { 0, 0 }; |
| 190 |
SMALL_RECT writeArea = { 0, 0, 0, 0 }; |
| 191 |
COORD cursorPos; |
| 192 |
int i; |
| 193 |
CHAR_INFO line[ MAX_EDIT_LINE ]; |
| 194 |
WORD attrib; |
| 195 |
|
| 196 |
GetConsoleScreenBufferInfo( qconsole_hout, &binfo ); |
| 197 |
|
| 198 |
|
| 199 |
if( !qconsole_drawinput ) |
| 200 |
return; |
| 201 |
|
| 202 |
writeArea.Left = 0; |
| 203 |
writeArea.Top = binfo.dwCursorPosition.Y; |
| 204 |
writeArea.Bottom = binfo.dwCursorPosition.Y; |
| 205 |
writeArea.Right = MAX_EDIT_LINE; |
| 206 |
|
| 207 |
|
| 208 |
attrib = CON_ColorCharToAttrib( COLOR_WHITE ); |
| 209 |
|
| 210 |
|
| 211 |
for( i = 0; i < MAX_EDIT_LINE; i++ ) |
| 212 |
{ |
| 213 |
if( i < qconsole_linelen ) |
| 214 |
{ |
| 215 |
if( i + 1 < qconsole_linelen && Q_IsColorString( qconsole_line + i ) ) |
| 216 |
attrib = CON_ColorCharToAttrib( *( qconsole_line + i + 1 ) ); |
| 217 |
|
| 218 |
line[ i ].Char.AsciiChar = qconsole_line[ i ]; |
| 219 |
} |
| 220 |
else |
| 221 |
line[ i ].Char.AsciiChar = ' '; |
| 222 |
|
| 223 |
line[ i ].Attributes = attrib; |
| 224 |
} |
| 225 |
|
| 226 |
if( qconsole_linelen > binfo.srWindow.Right ) |
| 227 |
{ |
| 228 |
WriteConsoleOutput( qconsole_hout, |
| 229 |
line + (qconsole_linelen - binfo.srWindow.Right ), |
| 230 |
writeSize, writePos, &writeArea ); |
| 231 |
} |
| 232 |
else |
| 233 |
{ |
| 234 |
WriteConsoleOutput( qconsole_hout, line, writeSize, |
| 235 |
writePos, &writeArea ); |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
cursorPos.Y = binfo.dwCursorPosition.Y; |
| 240 |
cursorPos.X = qconsole_cursor < qconsole_linelen |
| 241 |
? qconsole_cursor |
| 242 |
: qconsole_linelen > binfo.srWindow.Right |
| 243 |
? binfo.srWindow.Right |
| 244 |
: qconsole_linelen; |
| 245 |
|
| 246 |
SetConsoleCursorPosition( qconsole_hout, cursorPos ); |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
static void CON_Hide( void ) |
| 255 |
{ |
| 256 |
int realLen; |
| 257 |
|
| 258 |
realLen = qconsole_linelen; |
| 259 |
|
| 260 |
|
| 261 |
qconsole_linelen = 0; |
| 262 |
CON_Show( ); |
| 263 |
|
| 264 |
qconsole_linelen = realLen; |
| 265 |
} |
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
void CON_Shutdown( void ) |
| 274 |
{ |
| 275 |
CON_Hide( ); |
| 276 |
SetConsoleMode( qconsole_hin, qconsole_orig_mode ); |
| 277 |
SetConsoleCursorInfo( qconsole_hout, &qconsole_orig_cursorinfo ); |
| 278 |
SetConsoleTextAttribute( qconsole_hout, qconsole_attrib ); |
| 279 |
CloseHandle( qconsole_hout ); |
| 280 |
CloseHandle( qconsole_hin ); |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
void CON_Init( void ) |
| 289 |
{ |
| 290 |
CONSOLE_SCREEN_BUFFER_INFO info; |
| 291 |
int i; |
| 292 |
|
| 293 |
|
| 294 |
SetConsoleCtrlHandler( CON_CtrlHandler, TRUE ); |
| 295 |
|
| 296 |
qconsole_hin = GetStdHandle( STD_INPUT_HANDLE ); |
| 297 |
if( qconsole_hin == INVALID_HANDLE_VALUE ) |
| 298 |
return; |
| 299 |
|
| 300 |
qconsole_hout = GetStdHandle( STD_OUTPUT_HANDLE ); |
| 301 |
if( qconsole_hout == INVALID_HANDLE_VALUE ) |
| 302 |
return; |
| 303 |
|
| 304 |
GetConsoleMode( qconsole_hin, &qconsole_orig_mode ); |
| 305 |
|
| 306 |
|
| 307 |
SetConsoleMode( qconsole_hin, |
| 308 |
qconsole_orig_mode & ~ENABLE_MOUSE_INPUT ); |
| 309 |
|
| 310 |
FlushConsoleInputBuffer( qconsole_hin ); |
| 311 |
|
| 312 |
GetConsoleScreenBufferInfo( qconsole_hout, &info ); |
| 313 |
qconsole_attrib = info.wAttributes; |
| 314 |
qconsole_backgroundAttrib = qconsole_attrib & (BACKGROUND_BLUE|BACKGROUND_GREEN|BACKGROUND_RED|BACKGROUND_INTENSITY); |
| 315 |
|
| 316 |
SetConsoleTitle(CLIENT_WINDOW_TITLE " Dedicated Server Console"); |
| 317 |
|
| 318 |
|
| 319 |
for( i = 0; i < QCONSOLE_HISTORY; i++ ) |
| 320 |
qconsole_history[ i ][ 0 ] = '\0'; |
| 321 |
|
| 322 |
|
| 323 |
SetConsoleTextAttribute( qconsole_hout, CON_ColorCharToAttrib( COLOR_WHITE ) ); |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
char *CON_Input( void ) |
| 332 |
{ |
| 333 |
INPUT_RECORD buff[ MAX_EDIT_LINE ]; |
| 334 |
DWORD count = 0, events = 0; |
| 335 |
WORD key = 0; |
| 336 |
int i; |
| 337 |
int newlinepos = -1; |
| 338 |
|
| 339 |
if( !GetNumberOfConsoleInputEvents( qconsole_hin, &events ) ) |
| 340 |
return NULL; |
| 341 |
|
| 342 |
if( events < 1 ) |
| 343 |
return NULL; |
| 344 |
|
| 345 |
|
| 346 |
if( events >= MAX_EDIT_LINE ) |
| 347 |
{ |
| 348 |
ReadConsoleInput( qconsole_hin, buff, 1, &events ); |
| 349 |
return NULL; |
| 350 |
} |
| 351 |
|
| 352 |
if( !ReadConsoleInput( qconsole_hin, buff, events, &count ) ) |
| 353 |
return NULL; |
| 354 |
|
| 355 |
FlushConsoleInputBuffer( qconsole_hin ); |
| 356 |
|
| 357 |
for( i = 0; i < count; i++ ) |
| 358 |
{ |
| 359 |
if( buff[ i ].EventType != KEY_EVENT ) |
| 360 |
continue; |
| 361 |
if( !buff[ i ].Event.KeyEvent.bKeyDown ) |
| 362 |
continue; |
| 363 |
|
| 364 |
key = buff[ i ].Event.KeyEvent.wVirtualKeyCode; |
| 365 |
|
| 366 |
if( key == VK_RETURN ) |
| 367 |
{ |
| 368 |
newlinepos = i; |
| 369 |
qconsole_cursor = 0; |
| 370 |
break; |
| 371 |
} |
| 372 |
else if( key == VK_UP ) |
| 373 |
{ |
| 374 |
CON_HistPrev(); |
| 375 |
break; |
| 376 |
} |
| 377 |
else if( key == VK_DOWN ) |
| 378 |
{ |
| 379 |
CON_HistNext(); |
| 380 |
break; |
| 381 |
} |
| 382 |
else if( key == VK_LEFT ) |
| 383 |
{ |
| 384 |
qconsole_cursor--; |
| 385 |
if ( qconsole_cursor < 0 ) |
| 386 |
{ |
| 387 |
qconsole_cursor = 0; |
| 388 |
} |
| 389 |
break; |
| 390 |
} |
| 391 |
else if( key == VK_RIGHT ) |
| 392 |
{ |
| 393 |
qconsole_cursor++; |
| 394 |
if ( qconsole_cursor > qconsole_linelen ) |
| 395 |
{ |
| 396 |
qconsole_cursor = qconsole_linelen; |
| 397 |
} |
| 398 |
break; |
| 399 |
} |
| 400 |
else if( key == VK_HOME ) |
| 401 |
{ |
| 402 |
qconsole_cursor = 0; |
| 403 |
break; |
| 404 |
} |
| 405 |
else if( key == VK_END ) |
| 406 |
{ |
| 407 |
qconsole_cursor = qconsole_linelen; |
| 408 |
break; |
| 409 |
} |
| 410 |
else if( key == VK_TAB ) |
| 411 |
{ |
| 412 |
field_t f; |
| 413 |
|
| 414 |
Q_strncpyz( f.buffer, qconsole_line, |
| 415 |
sizeof( f.buffer ) ); |
| 416 |
Field_AutoComplete( &f ); |
| 417 |
Q_strncpyz( qconsole_line, f.buffer, |
| 418 |
sizeof( qconsole_line ) ); |
| 419 |
qconsole_linelen = strlen( qconsole_line ); |
| 420 |
qconsole_cursor = qconsole_linelen; |
| 421 |
break; |
| 422 |
} |
| 423 |
|
| 424 |
if( qconsole_linelen < sizeof( qconsole_line ) - 1 ) |
| 425 |
{ |
| 426 |
char c = buff[ i ].Event.KeyEvent.uChar.AsciiChar; |
| 427 |
|
| 428 |
if( key == VK_BACK ) |
| 429 |
{ |
| 430 |
if ( qconsole_cursor > 0 ) |
| 431 |
{ |
| 432 |
int newlen = ( qconsole_linelen > 0 ) ? qconsole_linelen - 1 : 0; |
| 433 |
if ( qconsole_cursor < qconsole_linelen ) |
| 434 |
{ |
| 435 |
memmove( qconsole_line + qconsole_cursor - 1, |
| 436 |
qconsole_line + qconsole_cursor, |
| 437 |
qconsole_linelen - qconsole_cursor ); |
| 438 |
} |
| 439 |
|
| 440 |
qconsole_line[ newlen ] = '\0'; |
| 441 |
qconsole_linelen = newlen; |
| 442 |
qconsole_cursor--; |
| 443 |
} |
| 444 |
} |
| 445 |
else if( c ) |
| 446 |
{ |
| 447 |
if ( qconsole_linelen > qconsole_cursor ) |
| 448 |
{ |
| 449 |
memmove( qconsole_line + qconsole_cursor + 1, |
| 450 |
qconsole_line + qconsole_cursor, |
| 451 |
qconsole_linelen - qconsole_cursor ); |
| 452 |
} |
| 453 |
|
| 454 |
qconsole_line[ qconsole_cursor++ ] = c; |
| 455 |
|
| 456 |
qconsole_linelen++; |
| 457 |
qconsole_line[ qconsole_linelen ] = '\0'; |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
if( newlinepos < 0) { |
| 463 |
CON_Show(); |
| 464 |
return NULL; |
| 465 |
} |
| 466 |
|
| 467 |
if( !qconsole_linelen ) |
| 468 |
{ |
| 469 |
CON_Show(); |
| 470 |
Com_Printf( "\n" ); |
| 471 |
return NULL; |
| 472 |
} |
| 473 |
|
| 474 |
qconsole_linelen = 0; |
| 475 |
CON_Show(); |
| 476 |
|
| 477 |
CON_HistAdd(); |
| 478 |
Com_Printf( "%s\n", qconsole_line ); |
| 479 |
|
| 480 |
return qconsole_line; |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
void CON_WindowsColorPrint( const char *msg ) |
| 491 |
{ |
| 492 |
static char buffer[ MAXPRINTMSG ]; |
| 493 |
int length = 0; |
| 494 |
|
| 495 |
while( *msg ) |
| 496 |
{ |
| 497 |
qconsole_drawinput = ( *msg == '\n' ); |
| 498 |
|
| 499 |
if( Q_IsColorString( msg ) || *msg == '\n' ) |
| 500 |
{ |
| 501 |
|
| 502 |
if( length > 0 ) |
| 503 |
{ |
| 504 |
buffer[ length ] = '\0'; |
| 505 |
fputs( buffer, stderr ); |
| 506 |
length = 0; |
| 507 |
} |
| 508 |
|
| 509 |
if( *msg == '\n' ) |
| 510 |
{ |
| 511 |
|
| 512 |
SetConsoleTextAttribute( qconsole_hout, CON_ColorCharToAttrib( COLOR_WHITE ) ); |
| 513 |
fputs( "\n", stderr ); |
| 514 |
msg++; |
| 515 |
} |
| 516 |
else |
| 517 |
{ |
| 518 |
|
| 519 |
SetConsoleTextAttribute( qconsole_hout, CON_ColorCharToAttrib( *( msg + 1 ) ) ); |
| 520 |
msg += 2; |
| 521 |
} |
| 522 |
} |
| 523 |
else |
| 524 |
{ |
| 525 |
if( length >= MAXPRINTMSG - 1 ) |
| 526 |
break; |
| 527 |
|
| 528 |
buffer[ length ] = *msg; |
| 529 |
length++; |
| 530 |
msg++; |
| 531 |
} |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
if( length > 0 ) |
| 536 |
{ |
| 537 |
buffer[ length ] = '\0'; |
| 538 |
fputs( buffer, stderr ); |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
void CON_Print( const char *msg ) |
| 548 |
{ |
| 549 |
CON_Hide( ); |
| 550 |
|
| 551 |
CON_WindowsColorPrint( msg ); |
| 552 |
|
| 553 |
CON_Show( ); |
| 554 |
} |