icculus quake2 doxygen  1.0 dev
conproc.c File Reference
#include <stdio.h>
#include <process.h>
#include <windows.h>
#include "conproc.h"

Go to the source code of this file.

Macros

#define CCOM_WRITE_TEXT   0x2
 
#define CCOM_GET_TEXT   0x3
 
#define CCOM_GET_SCR_LINES   0x4
 
#define CCOM_SET_SCR_LINES   0x5
 

Functions

unsigned _stdcall RequestProc (void *arg)
 
LPVOID GetMappedBuffer (HANDLE hfileBuffer)
 
void ReleaseMappedBuffer (LPVOID pBuffer)
 
BOOL GetScreenBufferLines (int *piLines)
 
BOOL SetScreenBufferLines (int iLines)
 
BOOL ReadText (LPTSTR pszText, int iBeginLine, int iEndLine)
 
BOOL WriteText (LPCTSTR szText)
 
int CharToCode (char c)
 
BOOL SetConsoleCXCY (HANDLE hStdout, int cx, int cy)
 
int CCheckParm (char *parm)
 
void InitConProc (int argc, char **argv)
 
void DeinitConProc (void)
 

Variables

HANDLE heventDone
 
HANDLE hfileBuffer
 
HANDLE heventChildSend
 
HANDLE heventParentSend
 
HANDLE hStdout
 
HANDLE hStdin
 
int ccom_argc
 
char ** ccom_argv
 

Macro Definition Documentation

◆ CCOM_GET_SCR_LINES

#define CCOM_GET_SCR_LINES   0x4

Definition at line 33 of file conproc.c.

◆ CCOM_GET_TEXT

#define CCOM_GET_TEXT   0x3

Definition at line 29 of file conproc.c.

◆ CCOM_SET_SCR_LINES

#define CCOM_SET_SCR_LINES   0x5

Definition at line 36 of file conproc.c.

◆ CCOM_WRITE_TEXT

#define CCOM_WRITE_TEXT   0x2

Definition at line 26 of file conproc.c.

Function Documentation

◆ CCheckParm()

int CCheckParm ( char *  parm)

Definition at line 68 of file conproc.c.

69 {
70  int i;
71 
72  for (i=1 ; i<ccom_argc ; i++)
73  {
74  if (!ccom_argv[i])
75  continue;
76  if (!strcmp (parm,ccom_argv[i]))
77  return i;
78  }
79 
80  return 0;
81 }

Referenced by InitConProc().

◆ CharToCode()

int CharToCode ( char  c)

Definition at line 331 of file conproc.c.

332 {
333  char upper;
334 
335  upper = toupper(c);
336 
337  switch (c)
338  {
339  case 13:
340  return 28;
341 
342  default:
343  break;
344  }
345 
346  if (isalpha(c))
347  return (30 + upper - 65);
348 
349  if (isdigit(c))
350  return (1 + upper - 47);
351 
352  return c;
353 }

Referenced by WriteText().

◆ DeinitConProc()

void DeinitConProc ( void  )

Definition at line 153 of file conproc.c.

154 {
155  if (heventDone)
156  SetEvent (heventDone);
157 }

Referenced by Sys_Error(), and Sys_Quit().

◆ GetMappedBuffer()

LPVOID GetMappedBuffer ( HANDLE  hfileBuffer)

Definition at line 223 of file conproc.c.

224 {
225  LPVOID pBuffer;
226 
227  pBuffer = MapViewOfFile (hfileBuffer,
228  FILE_MAP_READ | FILE_MAP_WRITE, 0, 0, 0);
229 
230  return pBuffer;
231 }

Referenced by RequestProc().

◆ GetScreenBufferLines()

BOOL GetScreenBufferLines ( int piLines)

Definition at line 240 of file conproc.c.

241 {
242  CONSOLE_SCREEN_BUFFER_INFO info;
243  BOOL bRet;
244 
245  bRet = GetConsoleScreenBufferInfo (hStdout, &info);
246 
247  if (bRet)
248  *piLines = info.dwSize.Y;
249 
250  return bRet;
251 }

Referenced by RequestProc().

◆ InitConProc()

void InitConProc ( int  argc,
char **  argv 
)

Definition at line 84 of file conproc.c.

85 {
86  unsigned threadAddr;
87  HANDLE hFile;
88  HANDLE heventParent;
89  HANDLE heventChild;
90  int t;
91 
92  ccom_argc = argc;
93  ccom_argv = argv;
94 
95 // give QHOST a chance to hook into the console
96  if ((t = CCheckParm ("-HFILE")) > 0)
97  {
98  if (t < argc)
99  hFile = (HANDLE)atoi (ccom_argv[t+1]);
100  }
101 
102  if ((t = CCheckParm ("-HPARENT")) > 0)
103  {
104  if (t < argc)
105  heventParent = (HANDLE)atoi (ccom_argv[t+1]);
106  }
107 
108  if ((t = CCheckParm ("-HCHILD")) > 0)
109  {
110  if (t < argc)
111  heventChild = (HANDLE)atoi (ccom_argv[t+1]);
112  }
113 
114 
115 // ignore if we don't have all the events.
116  if (!hFile || !heventParent || !heventChild)
117  {
118  printf ("Qhost not present.\n");
119  return;
120  }
121 
122  printf ("Initializing for qhost.\n");
123 
124  hfileBuffer = hFile;
125  heventParentSend = heventParent;
126  heventChildSend = heventChild;
127 
128 // so we'll know when to go away.
129  heventDone = CreateEvent (NULL, FALSE, FALSE, NULL);
130 
131  if (!heventDone)
132  {
133  printf ("Couldn't create heventDone\n");
134  return;
135  }
136 
137  if (!_beginthreadex (NULL, 0, RequestProc, NULL, 0, &threadAddr))
138  {
139  CloseHandle (heventDone);
140  printf ("Couldn't create QHOST thread\n");
141  return;
142  }
143 
144 // save off the input/output handles.
145  hStdout = GetStdHandle (STD_OUTPUT_HANDLE);
146  hStdin = GetStdHandle (STD_INPUT_HANDLE);
147 
148 // force 80 character width, at least 25 character height
149  SetConsoleCXCY (hStdout, 80, 25);
150 }

Referenced by Sys_Init().

◆ ReadText()

BOOL ReadText ( LPTSTR  pszText,
int  iBeginLine,
int  iEndLine 
)

Definition at line 261 of file conproc.c.

262 {
263  COORD coord;
264  DWORD dwRead;
265  BOOL bRet;
266 
267  coord.X = 0;
268  coord.Y = iBeginLine;
269 
270  bRet = ReadConsoleOutputCharacter(
271  hStdout,
272  pszText,
273  80 * (iEndLine - iBeginLine + 1),
274  coord,
275  &dwRead);
276 
277  // Make sure it's null terminated.
278  if (bRet)
279  pszText[dwRead] = '\0';
280 
281  return bRet;
282 }

Referenced by RequestProc().

◆ ReleaseMappedBuffer()

void ReleaseMappedBuffer ( LPVOID  pBuffer)

Definition at line 234 of file conproc.c.

235 {
236  UnmapViewOfFile (pBuffer);
237 }

Referenced by RequestProc().

◆ RequestProc()

unsigned _stdcall RequestProc ( void arg)

Definition at line 160 of file conproc.c.

161 {
162  int *pBuffer;
163  DWORD dwRet;
164  HANDLE heventWait[2];
165  int iBeginLine, iEndLine;
166 
167  heventWait[0] = heventParentSend;
168  heventWait[1] = heventDone;
169 
170  while (1)
171  {
172  dwRet = WaitForMultipleObjects (2, heventWait, FALSE, INFINITE);
173 
174  // heventDone fired, so we're exiting.
175  if (dwRet == WAIT_OBJECT_0 + 1)
176  break;
177 
178  pBuffer = (int *) GetMappedBuffer (hfileBuffer);
179 
180  // hfileBuffer is invalid. Just leave.
181  if (!pBuffer)
182  {
183  printf ("Invalid hfileBuffer\n");
184  break;
185  }
186 
187  switch (pBuffer[0])
188  {
189  case CCOM_WRITE_TEXT:
190  // Param1 : Text
191  pBuffer[0] = WriteText ((LPCTSTR) (pBuffer + 1));
192  break;
193 
194  case CCOM_GET_TEXT:
195  // Param1 : Begin line
196  // Param2 : End line
197  iBeginLine = pBuffer[1];
198  iEndLine = pBuffer[2];
199  pBuffer[0] = ReadText ((LPTSTR) (pBuffer + 1), iBeginLine,
200  iEndLine);
201  break;
202 
203  case CCOM_GET_SCR_LINES:
204  // No params
205  pBuffer[0] = GetScreenBufferLines (&pBuffer[1]);
206  break;
207 
208  case CCOM_SET_SCR_LINES:
209  // Param1 : Number of lines
210  pBuffer[0] = SetScreenBufferLines (pBuffer[1]);
211  break;
212  }
213 
214  ReleaseMappedBuffer (pBuffer);
215  SetEvent (heventChildSend);
216  }
217 
218  _endthreadex (0);
219  return 0;
220 }

Referenced by InitConProc().

◆ SetConsoleCXCY()

BOOL SetConsoleCXCY ( HANDLE  hStdout,
int  cx,
int  cy 
)

Definition at line 356 of file conproc.c.

357 {
358  CONSOLE_SCREEN_BUFFER_INFO info;
359  COORD coordMax;
360 
361  coordMax = GetLargestConsoleWindowSize(hStdout);
362 
363  if (cy > coordMax.Y)
364  cy = coordMax.Y;
365 
366  if (cx > coordMax.X)
367  cx = coordMax.X;
368 
369  if (!GetConsoleScreenBufferInfo(hStdout, &info))
370  return FALSE;
371 
372 // height
373  info.srWindow.Left = 0;
374  info.srWindow.Right = info.dwSize.X - 1;
375  info.srWindow.Top = 0;
376  info.srWindow.Bottom = cy - 1;
377 
378  if (cy < info.dwSize.Y)
379  {
380  if (!SetConsoleWindowInfo(hStdout, TRUE, &info.srWindow))
381  return FALSE;
382 
383  info.dwSize.Y = cy;
384 
385  if (!SetConsoleScreenBufferSize(hStdout, info.dwSize))
386  return FALSE;
387  }
388  else if (cy > info.dwSize.Y)
389  {
390  info.dwSize.Y = cy;
391 
392  if (!SetConsoleScreenBufferSize(hStdout, info.dwSize))
393  return FALSE;
394 
395  if (!SetConsoleWindowInfo(hStdout, TRUE, &info.srWindow))
396  return FALSE;
397  }
398 
399  if (!GetConsoleScreenBufferInfo(hStdout, &info))
400  return FALSE;
401 
402 // width
403  info.srWindow.Left = 0;
404  info.srWindow.Right = cx - 1;
405  info.srWindow.Top = 0;
406  info.srWindow.Bottom = info.dwSize.Y - 1;
407 
408  if (cx < info.dwSize.X)
409  {
410  if (!SetConsoleWindowInfo(hStdout, TRUE, &info.srWindow))
411  return FALSE;
412 
413  info.dwSize.X = cx;
414 
415  if (!SetConsoleScreenBufferSize(hStdout, info.dwSize))
416  return FALSE;
417  }
418  else if (cx > info.dwSize.X)
419  {
420  info.dwSize.X = cx;
421 
422  if (!SetConsoleScreenBufferSize(hStdout, info.dwSize))
423  return FALSE;
424 
425  if (!SetConsoleWindowInfo(hStdout, TRUE, &info.srWindow))
426  return FALSE;
427  }
428 
429  return TRUE;
430 }

Referenced by InitConProc(), and SetScreenBufferLines().

◆ SetScreenBufferLines()

BOOL SetScreenBufferLines ( int  iLines)

Definition at line 254 of file conproc.c.

255 {
256 
257  return SetConsoleCXCY (hStdout, 80, iLines);
258 }

Referenced by RequestProc().

◆ WriteText()

BOOL WriteText ( LPCTSTR  szText)

Definition at line 285 of file conproc.c.

286 {
287  DWORD dwWritten;
288  INPUT_RECORD rec;
289  char upper, *sz;
290 
291  sz = (LPTSTR) szText;
292 
293  while (*sz)
294  {
295  // 13 is the code for a carriage return (\n) instead of 10.
296  if (*sz == 10)
297  *sz = 13;
298 
299  upper = toupper(*sz);
300 
301  rec.EventType = KEY_EVENT;
302  rec.Event.KeyEvent.bKeyDown = TRUE;
303  rec.Event.KeyEvent.wRepeatCount = 1;
304  rec.Event.KeyEvent.wVirtualKeyCode = upper;
305  rec.Event.KeyEvent.wVirtualScanCode = CharToCode (*sz);
306  rec.Event.KeyEvent.uChar.AsciiChar = *sz;
307  rec.Event.KeyEvent.uChar.UnicodeChar = *sz;
308  rec.Event.KeyEvent.dwControlKeyState = isupper(*sz) ? 0x80 : 0x0;
309 
310  WriteConsoleInput(
311  hStdin,
312  &rec,
313  1,
314  &dwWritten);
315 
316  rec.Event.KeyEvent.bKeyDown = FALSE;
317 
318  WriteConsoleInput(
319  hStdin,
320  &rec,
321  1,
322  &dwWritten);
323 
324  sz++;
325  }
326 
327  return TRUE;
328 }

Referenced by RequestProc().

Variable Documentation

◆ ccom_argc

int ccom_argc

Definition at line 57 of file conproc.c.

Referenced by CCheckParm(), and InitConProc().

◆ ccom_argv

char** ccom_argv

Definition at line 58 of file conproc.c.

Referenced by CCheckParm(), and InitConProc().

◆ heventChildSend

HANDLE heventChildSend

Definition at line 42 of file conproc.c.

Referenced by InitConProc(), and RequestProc().

◆ heventDone

HANDLE heventDone

Definition at line 40 of file conproc.c.

Referenced by DeinitConProc(), InitConProc(), and RequestProc().

◆ heventParentSend

HANDLE heventParentSend

Definition at line 43 of file conproc.c.

Referenced by InitConProc(), and RequestProc().

◆ hfileBuffer

HANDLE hfileBuffer

Definition at line 41 of file conproc.c.

Referenced by GetMappedBuffer(), InitConProc(), and RequestProc().

◆ hStdin

HANDLE hStdin

Definition at line 45 of file conproc.c.

Referenced by InitConProc(), and WriteText().

◆ hStdout

HANDLE hStdout
CCOM_WRITE_TEXT
#define CCOM_WRITE_TEXT
Definition: conproc.c:26
ReleaseMappedBuffer
void ReleaseMappedBuffer(LPVOID pBuffer)
Definition: conproc.c:234
hStdin
HANDLE hStdin
Definition: conproc.c:45
GetMappedBuffer
LPVOID GetMappedBuffer(HANDLE hfileBuffer)
Definition: conproc.c:223
CCheckParm
int CCheckParm(char *parm)
Definition: conproc.c:68
CharToCode
int CharToCode(char c)
Definition: conproc.c:331
ReadText
BOOL ReadText(LPTSTR pszText, int iBeginLine, int iEndLine)
Definition: conproc.c:261
heventParentSend
HANDLE heventParentSend
Definition: conproc.c:43
i
int i
Definition: q_shared.c:305
argv
char * argv[MAX_NUM_ARGVS]
Definition: sys_win.c:55
argc
int argc
Definition: sys_win.c:54
ccom_argv
char ** ccom_argv
Definition: conproc.c:58
SetConsoleCXCY
BOOL SetConsoleCXCY(HANDLE hStdout, int cx, int cy)
Definition: conproc.c:356
RequestProc
unsigned _stdcall RequestProc(void *arg)
Definition: conproc.c:160
ccom_argc
int ccom_argc
Definition: conproc.c:57
CCOM_GET_SCR_LINES
#define CCOM_GET_SCR_LINES
Definition: conproc.c:33
BOOL
CONST COLORREF COLORREF BOOL
Definition: qgl_win.c:60
t
GLdouble t
Definition: qgl_win.c:328
NULL
#define NULL
Definition: q_shared.h:60
GetScreenBufferLines
BOOL GetScreenBufferLines(int *piLines)
Definition: conproc.c:240
heventDone
HANDLE heventDone
Definition: conproc.c:40
SetScreenBufferLines
BOOL SetScreenBufferLines(int iLines)
Definition: conproc.c:254
CCOM_GET_TEXT
#define CCOM_GET_TEXT
Definition: conproc.c:29
heventChildSend
HANDLE heventChildSend
Definition: conproc.c:42
hStdout
HANDLE hStdout
Definition: conproc.c:44
CCOM_SET_SCR_LINES
#define CCOM_SET_SCR_LINES
Definition: conproc.c:36
DWORD
DWORD
Definition: qgl_win.c:49
WriteText
BOOL WriteText(LPCTSTR szText)
Definition: conproc.c:285
hfileBuffer
HANDLE hfileBuffer
Definition: conproc.c:41