aboutsummaryrefslogtreecommitdiffstats
path: root/code/sys/con_win32.c
blob: 3aa2c78a0ff8e502d4ae3e8f2f396aada016e437 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
===========================================================================
Copyright (C) 1999-2005 Id Software, Inc.

This file is part of Quake III Arena source code.

Quake III Arena source code is free software; you can redistribute it
and/or modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the License,
or (at your option) any later version.

Quake III Arena source code is distributed in the hope that it will be
useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Quake III Arena source code; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
===========================================================================
*/

#include "../qcommon/q_shared.h"
#include "../qcommon/qcommon.h"
#include "windows.h"


#define QCONSOLE_THEME FOREGROUND_RED | \
                       BACKGROUND_RED | \
                       BACKGROUND_GREEN | \
                       BACKGROUND_BLUE

#define QCONSOLE_INPUT_RECORDS 1024

// used to track key input
static int qconsole_chars = 0;

// used to restore original color theme 
static int qconsole_orig_attrib;

/*
==================
CON_Hide
==================
*/
void CON_Hide( void )
{
}

/*
==================
CON_Show
==================
*/
void CON_Show( void )
{
}

/*
==================
CON_Shutdown
==================
*/
void CON_Shutdown( void )
{
  HANDLE hout;
  COORD screen = { 0, 0 };
  DWORD written;

  hout = GetStdHandle( STD_OUTPUT_HANDLE );

  SetConsoleTextAttribute( hout, qconsole_orig_attrib );
  FillConsoleOutputAttribute( hout, qconsole_orig_attrib, 63999,
                              screen, &written ); 
}

/*
==================
CON_Init
==================
*/
void CON_Init( void )
{
  HANDLE hout;
  COORD screen = { 0, 0 };
  DWORD written, read;
  CONSOLE_SCREEN_BUFFER_INFO binfo;
  SMALL_RECT rect;
  WORD oldattrib;

  hout = GetStdHandle( STD_OUTPUT_HANDLE );

  // remember original color theme
  ReadConsoleOutputAttribute( hout, &oldattrib, 1, screen, &read );
  qconsole_orig_attrib = oldattrib;
  
  SetConsoleTitle("ioquake3 Dedicated Server Console");

  SetConsoleTextAttribute( hout, QCONSOLE_THEME );
  FillConsoleOutputAttribute( hout, QCONSOLE_THEME, 63999, screen, &written ); 
  
  // adjust console scroll to match up with cursor position  
  GetConsoleScreenBufferInfo( hout, &binfo );
  rect.Top = binfo.srWindow.Top;
  rect.Left = binfo.srWindow.Left;
  rect.Bottom = binfo.srWindow.Bottom;
  rect.Right = binfo.srWindow.Right;
  rect.Top += ( binfo.dwCursorPosition.Y - binfo.srWindow.Bottom ); 
  rect.Bottom = binfo.dwCursorPosition.Y; 
  SetConsoleWindowInfo( hout, TRUE, &rect );

}

/*
==================
CON_ConsoleInput
==================
*/
char *CON_ConsoleInput( void )
{
  HANDLE hin, hout;
  INPUT_RECORD buff[ QCONSOLE_INPUT_RECORDS ];
  DWORD count = 0;
  int i;
  static char input[ 1024 ] = { "" };
  int inputlen;
  int newlinepos = -1;
  CHAR_INFO line[ QCONSOLE_INPUT_RECORDS ];
  int linelen = 0;

  inputlen = 0;
  input[ 0 ] = '\0';

  hin = GetStdHandle( STD_INPUT_HANDLE );
  if( hin == INVALID_HANDLE_VALUE )
    return NULL;
  hout = GetStdHandle( STD_OUTPUT_HANDLE );
  if( hout == INVALID_HANDLE_VALUE )
    return NULL;

  if( !PeekConsoleInput( hin, buff, QCONSOLE_INPUT_RECORDS, &count ) )
    return NULL;

  // if we have overflowed, start dropping oldest input events
  if( count == QCONSOLE_INPUT_RECORDS )
  {
    ReadConsoleInput( hin, buff, 1, &count );
    return NULL;
  } 

  for( i = 0; i < count; i++ )
  {
    if( buff[ i ].EventType == KEY_EVENT && buff[ i ].Event.KeyEvent.bKeyDown ) 
    {
      if( buff[ i ].Event.KeyEvent.wVirtualKeyCode == VK_RETURN )
      {
        newlinepos = i;
        break;
      }

      if( linelen < QCONSOLE_INPUT_RECORDS &&
          buff[ i ].Event.KeyEvent.uChar.AsciiChar )
      {
        if( buff[ i ].Event.KeyEvent.wVirtualKeyCode == VK_BACK )
        {
          if( linelen > 0 )
            linelen--;
            
        }
        else
        {
          line[ linelen ].Attributes =  QCONSOLE_THEME;
          line[ linelen++ ].Char.AsciiChar =
            buff[ i ].Event.KeyEvent.uChar.AsciiChar;
        }
      }
    }
  }

  // provide visual feedback for incomplete commands
  if( linelen != qconsole_chars )
  {
    CONSOLE_SCREEN_BUFFER_INFO binfo;
    COORD writeSize = { QCONSOLE_INPUT_RECORDS, 1 };
    COORD writePos = { 0, 0 };
    SMALL_RECT writeArea = { 0, 0, 0, 0 };
    int i;

    // keep track of this so we don't need to re-write to console every frame
    qconsole_chars = linelen;

    GetConsoleScreenBufferInfo( hout, &binfo );

    // adjust scrolling to cursor when typing
    if( binfo.dwCursorPosition.Y > binfo.srWindow.Bottom )
    {
      SMALL_RECT rect;

      rect.Top = binfo.srWindow.Top;
      rect.Left = binfo.srWindow.Left;
      rect.Bottom = binfo.srWindow.Bottom;
      rect.Right = binfo.srWindow.Right;

      rect.Top += ( binfo.dwCursorPosition.Y - binfo.srWindow.Bottom ); 
      rect.Bottom = binfo.dwCursorPosition.Y; 
      
      SetConsoleWindowInfo( hout, TRUE, &rect );
      GetConsoleScreenBufferInfo( hout, &binfo );
    }
    
    writeArea.Left = 0;
    writeArea.Top = binfo.srWindow.Bottom; 
    writeArea.Bottom = binfo.srWindow.Bottom; 
    writeArea.Right = QCONSOLE_INPUT_RECORDS;

    // pad line with ' ' to handle VK_BACK
    for( i = linelen; i < QCONSOLE_INPUT_RECORDS; i++ )
    {
      line[ i ].Char.AsciiChar = ' '; 
      line[ i ].Attributes =  QCONSOLE_THEME;
    }

    if( linelen > binfo.srWindow.Right )
    {
      WriteConsoleOutput( hout, line + (linelen - binfo.srWindow.Right ),
                          writeSize, writePos, &writeArea );
    }
    else
    {
      WriteConsoleOutput( hout, line, writeSize, writePos, &writeArea );
    }

    if( binfo.dwCursorPosition.X != linelen )
    {
      COORD cursorPos = { 0, 0 };

      cursorPos.X = linelen;
      cursorPos.Y = binfo.srWindow.Bottom;
      SetConsoleCursorPosition( hout, cursorPos );
    }
  }

  // don't touch the input buffer if this is an incomplete command
  if( newlinepos < 0)
  {
    return NULL;
  }
  else
  {
    // add a newline
    COORD cursorPos = { 0, 0 };
    CONSOLE_SCREEN_BUFFER_INFO binfo;

    GetConsoleScreenBufferInfo( hout, &binfo );
    cursorPos.Y = binfo.srWindow.Bottom + 1;
    SetConsoleCursorPosition( hout, cursorPos );
  }


  if( !ReadConsoleInput( hin, buff, newlinepos+1, &count ) )
    return NULL;

  for( i = 0; i < count; i++ )
  {
    if( buff[ i ].EventType == KEY_EVENT && buff[ i ].Event.KeyEvent.bKeyDown ) 
    {
      if( buff[ i ].Event.KeyEvent.wVirtualKeyCode == VK_BACK )
      {
        if( inputlen > 0 )
          input[ --inputlen ] = '\0';
        continue;
      }
      if( inputlen < ( sizeof( input ) - 1 ) &&
          buff[ i ].Event.KeyEvent.uChar.AsciiChar )
      {
        input[ inputlen++ ] = buff[ i ].Event.KeyEvent.uChar.AsciiChar;
        input[ inputlen ] = '\0'; 
      }
    }
  }
  if( !inputlen )
    return NULL;
  return input;
}