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.
29 HANDLE heventChildSend;
30 HANDLE heventParentSend;
34 DWORD RequestProc (DWORD dwNichts);
35 LPVOID GetMappedBuffer (HANDLE hfileBuffer);
36 void ReleaseMappedBuffer (LPVOID pBuffer);
37 BOOL GetScreenBufferLines (int *piLines);
38 BOOL SetScreenBufferLines (int iLines);
39 BOOL ReadText (LPTSTR pszText, int iBeginLine, int iEndLine);
40 BOOL WriteText (LPCTSTR szText);
41 int CharToCode (char c);
42 BOOL SetConsoleCXCY(HANDLE hStdout, int cx, int cy);
45 void InitConProc (HANDLE hFile, HANDLE heventParent, HANDLE heventChild)
49 // ignore if we don't have all the events.
50 if (!hFile || !heventParent || !heventChild)
54 heventParentSend = heventParent;
55 heventChildSend = heventChild;
57 // so we'll know when to go away.
58 heventDone = CreateEvent (NULL, false, false, NULL);
62 Con_SafePrint("Couldn't create heventDone\n");
66 if (!CreateThread (NULL,
68 (LPTHREAD_START_ROUTINE) RequestProc,
73 CloseHandle (heventDone);
74 Con_SafePrint("Couldn't create QHOST thread\n");
78 // save off the input/output handles.
79 hStdout = GetStdHandle (STD_OUTPUT_HANDLE);
80 hStdin = GetStdHandle (STD_INPUT_HANDLE);
82 // force 80 character width, at least 25 character height
83 SetConsoleCXCY (hStdout, 80, 25);
87 void DeinitConProc (void)
90 SetEvent (heventDone);
94 DWORD RequestProc (DWORD dwNichts)
99 int iBeginLine, iEndLine;
101 heventWait[0] = heventParentSend;
102 heventWait[1] = heventDone;
106 dwRet = WaitForMultipleObjects (2, heventWait, false, INFINITE);
108 // heventDone fired, so we're exiting.
109 if (dwRet == WAIT_OBJECT_0 + 1)
112 pBuffer = (int *) GetMappedBuffer (hfileBuffer);
114 // hfileBuffer is invalid. Just leave.
117 Con_SafePrint("Invalid hfileBuffer\n");
123 case CCOM_WRITE_TEXT:
125 pBuffer[0] = WriteText ((LPCTSTR) (pBuffer + 1));
129 // Param1 : Begin line
131 iBeginLine = pBuffer[1];
132 iEndLine = pBuffer[2];
133 pBuffer[0] = ReadText ((LPTSTR) (pBuffer + 1), iBeginLine,
137 case CCOM_GET_SCR_LINES:
139 pBuffer[0] = GetScreenBufferLines (&pBuffer[1]);
142 case CCOM_SET_SCR_LINES:
143 // Param1 : Number of lines
144 pBuffer[0] = SetScreenBufferLines (pBuffer[1]);
148 ReleaseMappedBuffer (pBuffer);
149 SetEvent (heventChildSend);
156 LPVOID GetMappedBuffer (HANDLE hfileBuffer)
160 pBuffer = MapViewOfFile (hfileBuffer,
161 FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
167 void ReleaseMappedBuffer (LPVOID pBuffer)
169 UnmapViewOfFile (pBuffer);
173 BOOL GetScreenBufferLines (int *piLines)
175 CONSOLE_SCREEN_BUFFER_INFO info;
178 bRet = GetConsoleScreenBufferInfo (hStdout, &info);
181 *piLines = info.dwSize.Y;
187 BOOL SetScreenBufferLines (int iLines)
190 return SetConsoleCXCY (hStdout, 80, iLines);
194 BOOL ReadText (LPTSTR pszText, int iBeginLine, int iEndLine)
201 coord.Y = iBeginLine;
203 bRet = ReadConsoleOutputCharacter(
206 80 * (iEndLine - iBeginLine + 1),
210 // Make sure it's null terminated.
212 pszText[dwRead] = '\0';
218 BOOL WriteText (LPCTSTR szText)
224 sz = (LPTSTR) szText;
228 // 13 is the code for a carriage return (\n) instead of 10.
232 upper = toupper(*sz);
234 rec.EventType = KEY_EVENT;
235 rec.Event.KeyEvent.bKeyDown = true;
236 rec.Event.KeyEvent.wRepeatCount = 1;
237 rec.Event.KeyEvent.wVirtualKeyCode = upper;
238 rec.Event.KeyEvent.wVirtualScanCode = CharToCode (*sz);
239 rec.Event.KeyEvent.uChar.AsciiChar = *sz;
240 rec.Event.KeyEvent.uChar.UnicodeChar = *sz;
241 rec.Event.KeyEvent.dwControlKeyState = isupper(*sz) ? 0x80 : 0x0;
249 rec.Event.KeyEvent.bKeyDown = false;
264 int CharToCode (char c)
280 return (30 + upper - 65);
283 return (1 + upper - 47);
289 BOOL SetConsoleCXCY(HANDLE hStdout, int cx, int cy)
291 CONSOLE_SCREEN_BUFFER_INFO info;
294 coordMax = GetLargestConsoleWindowSize(hStdout);
302 if (!GetConsoleScreenBufferInfo(hStdout, &info))
306 info.srWindow.Left = 0;
307 info.srWindow.Right = info.dwSize.X - 1;
308 info.srWindow.Top = 0;
309 info.srWindow.Bottom = cy - 1;
311 if (cy < info.dwSize.Y)
313 if (!SetConsoleWindowInfo(hStdout, true, &info.srWindow))
318 if (!SetConsoleScreenBufferSize(hStdout, info.dwSize))
321 else if (cy > info.dwSize.Y)
325 if (!SetConsoleScreenBufferSize(hStdout, info.dwSize))
328 if (!SetConsoleWindowInfo(hStdout, true, &info.srWindow))
332 if (!GetConsoleScreenBufferInfo(hStdout, &info))
336 info.srWindow.Left = 0;
337 info.srWindow.Right = cx - 1;
338 info.srWindow.Top = 0;
339 info.srWindow.Bottom = info.dwSize.Y - 1;
341 if (cx < info.dwSize.X)
343 if (!SetConsoleWindowInfo(hStdout, true, &info.srWindow))
348 if (!SetConsoleScreenBufferSize(hStdout, info.dwSize))
351 else if (cx > info.dwSize.X)
355 if (!SetConsoleScreenBufferSize(hStdout, info.dwSize))
358 if (!SetConsoleWindowInfo(hStdout, true, &info.srWindow))