2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
28 HANDLE heventChildSend;
29 HANDLE heventParentSend;
33 DWORD RequestProc (DWORD dwNichts);
34 LPVOID GetMappedBuffer (HANDLE hfileBuffer);
35 void ReleaseMappedBuffer (LPVOID pBuffer);
36 BOOL GetScreenBufferLines (int *piLines);
37 BOOL SetScreenBufferLines (int iLines);
38 BOOL ReadText (LPTSTR pszText, int iBeginLine, int iEndLine);
39 BOOL WriteText (LPCTSTR szText);
40 int CharToCode (char c);
41 BOOL SetConsoleCXCY(HANDLE hStdout, int cx, int cy);
44 void InitConProc (HANDLE hFile, HANDLE heventParent, HANDLE heventChild)
48 // ignore if we don't have all the events.
49 if (!hFile || !heventParent || !heventChild)
53 heventParentSend = heventParent;
54 heventChildSend = heventChild;
56 // so we'll know when to go away.
57 heventDone = CreateEvent (NULL, FALSE, FALSE, NULL);
61 Con_SafePrintf ("Couldn't create heventDone\n");
65 if (!CreateThread (NULL,
67 (LPTHREAD_START_ROUTINE) RequestProc,
72 CloseHandle (heventDone);
73 Con_SafePrintf ("Couldn't create QHOST thread\n");
77 // save off the input/output handles.
78 hStdout = GetStdHandle (STD_OUTPUT_HANDLE);
79 hStdin = GetStdHandle (STD_INPUT_HANDLE);
81 // force 80 character width, at least 25 character height
82 SetConsoleCXCY (hStdout, 80, 25);
86 void DeinitConProc (void)
89 SetEvent (heventDone);
93 DWORD RequestProc (DWORD dwNichts)
98 int iBeginLine, iEndLine;
100 heventWait[0] = heventParentSend;
101 heventWait[1] = heventDone;
105 dwRet = WaitForMultipleObjects (2, heventWait, FALSE, INFINITE);
107 // heventDone fired, so we're exiting.
108 if (dwRet == WAIT_OBJECT_0 + 1)
111 pBuffer = (int *) GetMappedBuffer (hfileBuffer);
113 // hfileBuffer is invalid. Just leave.
116 Con_SafePrintf ("Invalid hfileBuffer\n");
122 case CCOM_WRITE_TEXT:
124 pBuffer[0] = WriteText ((LPCTSTR) (pBuffer + 1));
128 // Param1 : Begin line
130 iBeginLine = pBuffer[1];
131 iEndLine = pBuffer[2];
132 pBuffer[0] = ReadText ((LPTSTR) (pBuffer + 1), iBeginLine,
136 case CCOM_GET_SCR_LINES:
138 pBuffer[0] = GetScreenBufferLines (&pBuffer[1]);
141 case CCOM_SET_SCR_LINES:
142 // Param1 : Number of lines
143 pBuffer[0] = SetScreenBufferLines (pBuffer[1]);
147 ReleaseMappedBuffer (pBuffer);
148 SetEvent (heventChildSend);
155 LPVOID GetMappedBuffer (HANDLE hfileBuffer)
159 pBuffer = MapViewOfFile (hfileBuffer,
160 FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
166 void ReleaseMappedBuffer (LPVOID pBuffer)
168 UnmapViewOfFile (pBuffer);
172 BOOL GetScreenBufferLines (int *piLines)
174 CONSOLE_SCREEN_BUFFER_INFO info;
177 bRet = GetConsoleScreenBufferInfo (hStdout, &info);
180 *piLines = info.dwSize.Y;
186 BOOL SetScreenBufferLines (int iLines)
189 return SetConsoleCXCY (hStdout, 80, iLines);
193 BOOL ReadText (LPTSTR pszText, int iBeginLine, int iEndLine)
200 coord.Y = iBeginLine;
202 bRet = ReadConsoleOutputCharacter(
205 80 * (iEndLine - iBeginLine + 1),
209 // Make sure it's null terminated.
211 pszText[dwRead] = '\0';
217 BOOL WriteText (LPCTSTR szText)
223 sz = (LPTSTR) szText;
227 // 13 is the code for a carriage return (\n) instead of 10.
231 upper = toupper(*sz);
233 rec.EventType = KEY_EVENT;
234 rec.Event.KeyEvent.bKeyDown = TRUE;
235 rec.Event.KeyEvent.wRepeatCount = 1;
236 rec.Event.KeyEvent.wVirtualKeyCode = upper;
237 rec.Event.KeyEvent.wVirtualScanCode = CharToCode (*sz);
238 rec.Event.KeyEvent.uChar.AsciiChar = *sz;
239 rec.Event.KeyEvent.uChar.UnicodeChar = *sz;
240 rec.Event.KeyEvent.dwControlKeyState = isupper(*sz) ? 0x80 : 0x0;
248 rec.Event.KeyEvent.bKeyDown = FALSE;
263 int CharToCode (char c)
279 return (30 + upper - 65);
282 return (1 + upper - 47);
288 BOOL SetConsoleCXCY(HANDLE hStdout, int cx, int cy)
290 CONSOLE_SCREEN_BUFFER_INFO info;
293 coordMax = GetLargestConsoleWindowSize(hStdout);
301 if (!GetConsoleScreenBufferInfo(hStdout, &info))
305 info.srWindow.Left = 0;
306 info.srWindow.Right = info.dwSize.X - 1;
307 info.srWindow.Top = 0;
308 info.srWindow.Bottom = cy - 1;
310 if (cy < info.dwSize.Y)
312 if (!SetConsoleWindowInfo(hStdout, TRUE, &info.srWindow))
317 if (!SetConsoleScreenBufferSize(hStdout, info.dwSize))
320 else if (cy > info.dwSize.Y)
324 if (!SetConsoleScreenBufferSize(hStdout, info.dwSize))
327 if (!SetConsoleWindowInfo(hStdout, TRUE, &info.srWindow))
331 if (!GetConsoleScreenBufferInfo(hStdout, &info))
335 info.srWindow.Left = 0;
336 info.srWindow.Right = cx - 1;
337 info.srWindow.Top = 0;
338 info.srWindow.Bottom = info.dwSize.Y - 1;
340 if (cx < info.dwSize.X)
342 if (!SetConsoleWindowInfo(hStdout, TRUE, &info.srWindow))
347 if (!SetConsoleScreenBufferSize(hStdout, info.dwSize))
350 else if (cx > info.dwSize.X)
354 if (!SetConsoleScreenBufferSize(hStdout, info.dwSize))
357 if (!SetConsoleWindowInfo(hStdout, TRUE, &info.srWindow))