From 2609aade8a85bb914e5d34417e3f125927292d6b Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Mon, 21 Apr 2003 17:48:59 +0000 Subject: [PATCH] clean up conproc's api as such (what's this thing for, anyway?) --- nq/source/conproc.c | 306 ++++++++++++++++++++++---------------------- 1 file changed, 153 insertions(+), 153 deletions(-) diff --git a/nq/source/conproc.c b/nq/source/conproc.c index 654c5d791..b41c6289c 100644 --- a/nq/source/conproc.c +++ b/nq/source/conproc.c @@ -44,7 +44,7 @@ HANDLE heventChildSend; HANDLE heventParentSend; HANDLE hStdout; HANDLE hStdin; - +/* DWORD RequestProc (DWORD dwNichts); LPVOID GetMappedBuffer (HANDLE hfileBuffer); void ReleaseMappedBuffer (LPVOID pBuffer); @@ -54,110 +54,9 @@ BOOL ReadText (LPTSTR pszText, int iBeginLine, int iEndLine); BOOL WriteText (LPCTSTR szText); int CharToCode (char c); BOOL SetConsoleCXCY (HANDLE hStdout, int cx, int cy); +*/ - -void -InitConProc (HANDLE hFile, HANDLE heventParent, HANDLE heventChild) -{ - DWORD dwID; - - // ignore if we don't have all the events. - if (!hFile || !heventParent || !heventChild) - return; - - hfileBuffer = hFile; - heventParentSend = heventParent; - heventChildSend = heventChild; - - // so we'll know when to go away. - heventDone = CreateEvent (NULL, FALSE, FALSE, NULL); - - if (!heventDone) { - Con_Printf ("Couldn't create heventDone\n"); - return; - } - - if (!CreateThread (NULL, - 0, (LPTHREAD_START_ROUTINE) RequestProc, 0, 0, &dwID)) { - CloseHandle (heventDone); - Con_Printf ("Couldn't create QHOST thread\n"); - return; - } - // save off the input/output handles. - hStdout = GetStdHandle (STD_OUTPUT_HANDLE); - hStdin = GetStdHandle (STD_INPUT_HANDLE); - - // force 80 character width, at least 25 character height - SetConsoleCXCY (hStdout, 80, 25); -} - -void -DeinitConProc (void) -{ - if (heventDone) - SetEvent (heventDone); -} - -DWORD -RequestProc (DWORD dwNichts) -{ - int iBeginLine, iEndLine; - int *pBuffer; - DWORD dwRet; - HANDLE heventWait[2]; - - heventWait[0] = heventParentSend; - heventWait[1] = heventDone; - - while (1) { - dwRet = WaitForMultipleObjects (2, heventWait, FALSE, INFINITE); - - // heventDone fired, so we're exiting. - if (dwRet == WAIT_OBJECT_0 + 1) - break; - - pBuffer = (int *) GetMappedBuffer (hfileBuffer); - - // hfileBuffer is invalid. Just leave. - if (!pBuffer) { - Con_Printf ("Invalid hfileBuffer\n"); - break; - } - - switch (pBuffer[0]) { - case CCOM_WRITE_TEXT: - // Param1 : Text - pBuffer[0] = WriteText ((LPCTSTR) (pBuffer + 1)); - break; - - case CCOM_GET_TEXT: - // Param1 : Begin line - // Param2 : End line - iBeginLine = pBuffer[1]; - iEndLine = pBuffer[2]; - pBuffer[0] = ReadText ((LPTSTR) (pBuffer + 1), iBeginLine, - iEndLine); - break; - - case CCOM_GET_SCR_LINES: - // No params - pBuffer[0] = GetScreenBufferLines (&pBuffer[1]); - break; - - case CCOM_SET_SCR_LINES: - // Param1 : Number of lines - pBuffer[0] = SetScreenBufferLines (pBuffer[1]); - break; - } - - ReleaseMappedBuffer (pBuffer); - SetEvent (heventChildSend); - } - - return 0; -} - -LPVOID +static LPVOID GetMappedBuffer (HANDLE hfileBuffer) { LPVOID pBuffer; @@ -168,13 +67,13 @@ GetMappedBuffer (HANDLE hfileBuffer) return pBuffer; } -void +static void ReleaseMappedBuffer (LPVOID pBuffer) { UnmapViewOfFile (pBuffer); } -BOOL +static BOOL GetScreenBufferLines (int *piLines) { CONSOLE_SCREEN_BUFFER_INFO info; @@ -188,14 +87,7 @@ GetScreenBufferLines (int *piLines) return bRet; } -BOOL -SetScreenBufferLines (int iLines) -{ - - return SetConsoleCXCY (hStdout, 80, iLines); -} - -BOOL +static BOOL ReadText (LPTSTR pszText, int iBeginLine, int iEndLine) { BOOL bRet; @@ -216,44 +108,7 @@ ReadText (LPTSTR pszText, int iBeginLine, int iEndLine) return bRet; } -BOOL -WriteText (LPCTSTR szText) -{ - char upper, *sz; - DWORD dwWritten; - INPUT_RECORD rec; - - sz = (LPTSTR) szText; - - while (*sz) { - // 13 is the code for a carriage return (\n) instead of 10. - if (*sz == 10) - *sz = 13; - - upper = toupper (*sz); - - rec.EventType = KEY_EVENT; - rec.Event.KeyEvent.bKeyDown = TRUE; - rec.Event.KeyEvent.wRepeatCount = 1; - rec.Event.KeyEvent.wVirtualKeyCode = upper; - rec.Event.KeyEvent.wVirtualScanCode = CharToCode (*sz); - rec.Event.KeyEvent.uChar.AsciiChar = *sz; - rec.Event.KeyEvent.uChar.UnicodeChar = *sz; - rec.Event.KeyEvent.dwControlKeyState = isupper (*sz) ? 0x80 : 0x0; - - WriteConsoleInput (hStdin, &rec, 1, &dwWritten); - - rec.Event.KeyEvent.bKeyDown = FALSE; - - WriteConsoleInput (hStdin, &rec, 1, &dwWritten); - - sz++; - } - - return TRUE; -} - -int +static int CharToCode (char c) { char upper; @@ -277,7 +132,7 @@ CharToCode (char c) return c; } -BOOL +static BOOL SetConsoleCXCY (HANDLE hStdout, int cx, int cy) { CONSOLE_SCREEN_BUFFER_INFO info; @@ -347,3 +202,148 @@ SetConsoleCXCY (HANDLE hStdout, int cx, int cy) return TRUE; } + +static BOOL +WriteText (LPCTSTR szText) +{ + char upper, *sz; + DWORD dwWritten; + INPUT_RECORD rec; + + sz = (LPTSTR) szText; + + while (*sz) { + // 13 is the code for a carriage return (\n) instead of 10. + if (*sz == 10) + *sz = 13; + + upper = toupper (*sz); + + rec.EventType = KEY_EVENT; + rec.Event.KeyEvent.bKeyDown = TRUE; + rec.Event.KeyEvent.wRepeatCount = 1; + rec.Event.KeyEvent.wVirtualKeyCode = upper; + rec.Event.KeyEvent.wVirtualScanCode = CharToCode (*sz); + rec.Event.KeyEvent.uChar.AsciiChar = *sz; + rec.Event.KeyEvent.uChar.UnicodeChar = *sz; + rec.Event.KeyEvent.dwControlKeyState = isupper (*sz) ? 0x80 : 0x0; + + WriteConsoleInput (hStdin, &rec, 1, &dwWritten); + + rec.Event.KeyEvent.bKeyDown = FALSE; + + WriteConsoleInput (hStdin, &rec, 1, &dwWritten); + + sz++; + } + + return TRUE; +} + +static BOOL +SetScreenBufferLines (int iLines) +{ + + return SetConsoleCXCY (hStdout, 80, iLines); +} + +static DWORD +RequestProc (DWORD dwNichts) +{ + int iBeginLine, iEndLine; + int *pBuffer; + DWORD dwRet; + HANDLE heventWait[2]; + + heventWait[0] = heventParentSend; + heventWait[1] = heventDone; + + while (1) { + dwRet = WaitForMultipleObjects (2, heventWait, FALSE, INFINITE); + + // heventDone fired, so we're exiting. + if (dwRet == WAIT_OBJECT_0 + 1) + break; + + pBuffer = (int *) GetMappedBuffer (hfileBuffer); + + // hfileBuffer is invalid. Just leave. + if (!pBuffer) { + Con_Printf ("Invalid hfileBuffer\n"); + break; + } + + switch (pBuffer[0]) { + case CCOM_WRITE_TEXT: + // Param1 : Text + pBuffer[0] = WriteText ((LPCTSTR) (pBuffer + 1)); + break; + + case CCOM_GET_TEXT: + // Param1 : Begin line + // Param2 : End line + iBeginLine = pBuffer[1]; + iEndLine = pBuffer[2]; + pBuffer[0] = ReadText ((LPTSTR) (pBuffer + 1), iBeginLine, + iEndLine); + break; + + case CCOM_GET_SCR_LINES: + // No params + pBuffer[0] = GetScreenBufferLines (&pBuffer[1]); + break; + + case CCOM_SET_SCR_LINES: + // Param1 : Number of lines + pBuffer[0] = SetScreenBufferLines (pBuffer[1]); + break; + } + + ReleaseMappedBuffer (pBuffer); + SetEvent (heventChildSend); + } + + return 0; +} + +void +InitConProc (HANDLE hFile, HANDLE heventParent, HANDLE heventChild) +{ + DWORD dwID; + + // ignore if we don't have all the events. + if (!hFile || !heventParent || !heventChild) + return; + + hfileBuffer = hFile; + heventParentSend = heventParent; + heventChildSend = heventChild; + + // so we'll know when to go away. + heventDone = CreateEvent (NULL, FALSE, FALSE, NULL); + + if (!heventDone) { + Con_Printf ("Couldn't create heventDone\n"); + return; + } + + if (!CreateThread (NULL, + 0, (LPTHREAD_START_ROUTINE) RequestProc, 0, 0, &dwID)) { + CloseHandle (heventDone); + Con_Printf ("Couldn't create QHOST thread\n"); + return; + } + // save off the input/output handles. + hStdout = GetStdHandle (STD_OUTPUT_HANDLE); + hStdin = GetStdHandle (STD_INPUT_HANDLE); + + // force 80 character width, at least 25 character height + SetConsoleCXCY (hStdout, 80, 25); +} + +void +DeinitConProc (void) +{ + if (heventDone) + SetEvent (heventDone); +}