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.
30 HANDLE heventChildSend;
31 HANDLE heventParentSend;
35 DWORD RequestProc (DWORD dwNichts);
36 LPVOID GetMappedBuffer (HANDLE hfileBuffer);
37 void ReleaseMappedBuffer (LPVOID pBuffer);
38 BOOL GetScreenBufferLines (int *piLines);
39 BOOL SetScreenBufferLines (int iLines);
40 BOOL ReadText (LPTSTR pszText, int iBeginLine, int iEndLine);
41 BOOL WriteText (LPCTSTR szText);
42 int CharToCode (int c);
43 BOOL SetConsoleCXCY(HANDLE hStdout, int cx, int cy);
46 void InitConProc (HANDLE hFile, HANDLE heventParent, HANDLE heventChild)
50 // ignore if we don't have all the events.
51 if (!hFile || !heventParent || !heventChild)
55 heventParentSend = heventParent;
56 heventChildSend = heventChild;
58 // so we'll know when to go away.
59 heventDone = CreateEvent (NULL, false, false, NULL);
63 Con_Print("Couldn't create heventDone\n");
67 if (!CreateThread (NULL,
69 (LPTHREAD_START_ROUTINE) RequestProc,
74 CloseHandle (heventDone);
75 Con_Print("Couldn't create QHOST thread\n");
79 // save off the input/output handles.
80 hStdout = GetStdHandle (STD_OUTPUT_HANDLE);
81 hStdin = GetStdHandle (STD_INPUT_HANDLE);
83 // force 80 character width, at least 25 character height
84 SetConsoleCXCY (hStdout, 80, 25);
88 void DeinitConProc (void)
91 SetEvent (heventDone);
95 DWORD RequestProc (DWORD dwNichts)
100 int iBeginLine, iEndLine;
102 heventWait[0] = heventParentSend;
103 heventWait[1] = heventDone;
107 dwRet = WaitForMultipleObjects (2, heventWait, false, INFINITE);
109 // heventDone fired, so we're exiting.
110 if (dwRet == WAIT_OBJECT_0 + 1)
113 pBuffer = (int *) GetMappedBuffer (hfileBuffer);
115 // hfileBuffer is invalid. Just leave.
118 Con_Print("Invalid hfileBuffer\n");
124 case CCOM_WRITE_TEXT:
126 pBuffer[0] = WriteText ((LPCTSTR) (pBuffer + 1));
130 // Param1 : Begin line
132 iBeginLine = pBuffer[1];
133 iEndLine = pBuffer[2];
134 pBuffer[0] = ReadText ((LPTSTR) (pBuffer + 1), iBeginLine,
138 case CCOM_GET_SCR_LINES:
140 pBuffer[0] = GetScreenBufferLines (&pBuffer[1]);
143 case CCOM_SET_SCR_LINES:
144 // Param1 : Number of lines
145 pBuffer[0] = SetScreenBufferLines (pBuffer[1]);
149 ReleaseMappedBuffer (pBuffer);
150 SetEvent (heventChildSend);
157 LPVOID GetMappedBuffer (HANDLE hfileBuffer)
161 pBuffer = MapViewOfFile (hfileBuffer,
162 FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
168 void ReleaseMappedBuffer (LPVOID pBuffer)
170 UnmapViewOfFile (pBuffer);
174 BOOL GetScreenBufferLines (int *piLines)
176 CONSOLE_SCREEN_BUFFER_INFO info;
179 bRet = GetConsoleScreenBufferInfo (hStdout, &info);
182 *piLines = info.dwSize.Y;
188 BOOL SetScreenBufferLines (int iLines)
191 return SetConsoleCXCY (hStdout, 80, iLines);
195 BOOL ReadText (LPTSTR pszText, int iBeginLine, int iEndLine)
202 coord.Y = iBeginLine;
204 bRet = ReadConsoleOutputCharacter(
207 80 * (iEndLine - iBeginLine + 1),
211 // Make sure it's null terminated.
213 pszText[dwRead] = '\0';
219 BOOL WriteText (LPCTSTR szText)
225 sz = (LPTSTR) szText;
229 // 13 is the code for a carriage return (\n) instead of 10.
233 upper = toupper(*sz);
235 rec.EventType = KEY_EVENT;
236 rec.Event.KeyEvent.bKeyDown = true;
237 rec.Event.KeyEvent.wRepeatCount = 1;
238 rec.Event.KeyEvent.wVirtualKeyCode = upper;
239 rec.Event.KeyEvent.wVirtualScanCode = CharToCode (*sz);
240 rec.Event.KeyEvent.uChar.AsciiChar = *sz;
241 rec.Event.KeyEvent.uChar.UnicodeChar = *sz;
242 rec.Event.KeyEvent.dwControlKeyState = isupper(*sz) ? 0x80 : 0x0;
250 rec.Event.KeyEvent.bKeyDown = false;
265 int CharToCode (int c)
281 return (30 + upper - 65);
284 return (1 + upper - 47);
290 BOOL SetConsoleCXCY(HANDLE hStdout, int cx, int cy)
292 CONSOLE_SCREEN_BUFFER_INFO info;
295 coordMax = GetLargestConsoleWindowSize(hStdout);
303 if (!GetConsoleScreenBufferInfo(hStdout, &info))
307 info.srWindow.Left = 0;
308 info.srWindow.Right = info.dwSize.X - 1;
309 info.srWindow.Top = 0;
310 info.srWindow.Bottom = cy - 1;
312 if (cy < info.dwSize.Y)
314 if (!SetConsoleWindowInfo(hStdout, true, &info.srWindow))
319 if (!SetConsoleScreenBufferSize(hStdout, info.dwSize))
322 else if (cy > info.dwSize.Y)
326 if (!SetConsoleScreenBufferSize(hStdout, info.dwSize))
329 if (!SetConsoleWindowInfo(hStdout, true, &info.srWindow))
333 if (!GetConsoleScreenBufferInfo(hStdout, &info))
337 info.srWindow.Left = 0;
338 info.srWindow.Right = cx - 1;
339 info.srWindow.Top = 0;
340 info.srWindow.Bottom = info.dwSize.Y - 1;
342 if (cx < info.dwSize.X)
344 if (!SetConsoleWindowInfo(hStdout, true, &info.srWindow))
349 if (!SetConsoleScreenBufferSize(hStdout, info.dwSize))
352 else if (cx > info.dwSize.X)
356 if (!SetConsoleScreenBufferSize(hStdout, info.dwSize))
359 if (!SetConsoleWindowInfo(hStdout, true, &info.srWindow))