| 1 | /* $Id: conbuffervio.cpp,v 1.4 2001-10-01 01:45:18 bird Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * Win32 Console API Translation for OS/2 | 
|---|
| 5 | * | 
|---|
| 6 | * 1998/02/10 Patrick Haller (haller@zebra.fh-weingarten.de) | 
|---|
| 7 | * | 
|---|
| 8 | * @(#) console.cpp         1.0.0   1998/02/10 PH Start from scratch | 
|---|
| 9 | * | 
|---|
| 10 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 11 | * | 
|---|
| 12 | */ | 
|---|
| 13 |  | 
|---|
| 14 |  | 
|---|
| 15 | #ifdef DEBUG | 
|---|
| 16 | #define DEBUG_LOCAL | 
|---|
| 17 | #define DEBUG_LOCAL2 | 
|---|
| 18 | #endif | 
|---|
| 19 |  | 
|---|
| 20 | //#undef DEBUG_LOCAL | 
|---|
| 21 | //#undef DEBUG_LOCAL2 | 
|---|
| 22 |  | 
|---|
| 23 |  | 
|---|
| 24 | /***************************************************************************** | 
|---|
| 25 | * Remark                                                                    * | 
|---|
| 26 | ***************************************************************************** | 
|---|
| 27 |  | 
|---|
| 28 | - DWORD HandlerRoutine (DWORD dwCtrlType) | 
|---|
| 29 | basically an exception handler routine. handles a few signals / excpts. | 
|---|
| 30 | should be somewhere near the exception handling code ... :) | 
|---|
| 31 |  | 
|---|
| 32 | Hmm, however as PM applications don't really get a ctrl-c signal, | 
|---|
| 33 | I'll have to do this on my own ... | 
|---|
| 34 |  | 
|---|
| 35 | - supply unicode<->ascii conversions for all the _A and _W function pairs. | 
|---|
| 36 |  | 
|---|
| 37 | - problem: we can't prevent thread1 from blocking the message queue ? | 
|---|
| 38 | what will happen if a WinTerminate() is issued there ? | 
|---|
| 39 | will the message queue be closed and provide smooth tasking ? | 
|---|
| 40 | how will open32 react on this ? | 
|---|
| 41 |  | 
|---|
| 42 | - ECHO_LINE_INPUT / ReadFile blocks till CR | 
|---|
| 43 |  | 
|---|
| 44 | - scrollbars | 
|---|
| 45 | * do some flowchart to exactly determine WHEN to use WHICH setting | 
|---|
| 46 | and perform WHAT action | 
|---|
| 47 |  | 
|---|
| 48 | - clipboard support | 
|---|
| 49 | */ | 
|---|
| 50 |  | 
|---|
| 51 |  | 
|---|
| 52 | /***************************************************************************** | 
|---|
| 53 | * Includes                                                                  * | 
|---|
| 54 | *****************************************************************************/ | 
|---|
| 55 |  | 
|---|
| 56 | #define  INCL_WIN | 
|---|
| 57 | #define  INCL_DOSMEMMGR | 
|---|
| 58 | #define  INCL_DOSSEMAPHORES | 
|---|
| 59 | #define  INCL_DOSERRORS | 
|---|
| 60 | #define  INCL_DOSPROCESS | 
|---|
| 61 | #define  INCL_DOSMODULEMGR | 
|---|
| 62 | #define  INCL_VIO | 
|---|
| 63 | #define  INCL_AVIO | 
|---|
| 64 | #include <os2wrap.h>    //Odin32 OS/2 api wrappers | 
|---|
| 65 |  | 
|---|
| 66 | #include <win32api.h> | 
|---|
| 67 | #include <misc.h> | 
|---|
| 68 | #include <string.h> | 
|---|
| 69 | #include <stdlib.h> | 
|---|
| 70 | #include <stdio.h> | 
|---|
| 71 | #include <malloc.h> | 
|---|
| 72 |  | 
|---|
| 73 | #include "conwin.h"          // Windows Header for console only | 
|---|
| 74 | #include "HandleManager.h" | 
|---|
| 75 | #include "HMDevice.h" | 
|---|
| 76 | #include "ConBuffervio.H" | 
|---|
| 77 | #include "Console2.h" | 
|---|
| 78 | #include <heapstring.h> | 
|---|
| 79 |  | 
|---|
| 80 | #define DBG_LOCALLOG    DBG_conbuffer | 
|---|
| 81 | #include "dbglocal.h" | 
|---|
| 82 |  | 
|---|
| 83 | /***************************************************************************** | 
|---|
| 84 | * Name      : | 
|---|
| 85 | * Purpose   : | 
|---|
| 86 | * Parameters: | 
|---|
| 87 | * Variables : | 
|---|
| 88 | * Result    : | 
|---|
| 89 | * Remark    : | 
|---|
| 90 | * Status    : | 
|---|
| 91 | * | 
|---|
| 92 | * Author    : Patrick Haller [Wed, 1998/02/11 20:44] | 
|---|
| 93 | *****************************************************************************/ | 
|---|
| 94 |  | 
|---|
| 95 | BOOL HMDeviceConsoleVioBufferClass::WriteFile(PHMHANDLEDATA pHMHandleData, | 
|---|
| 96 | LPCVOID       lpBuffer, | 
|---|
| 97 | DWORD         nNumberOfBytesToWrite, | 
|---|
| 98 | LPDWORD       lpNumberOfBytesWritten, | 
|---|
| 99 | LPOVERLAPPED  lpOverlapped) | 
|---|
| 100 | { | 
|---|
| 101 | PCONSOLEBUFFER pConsoleBuffer = (PCONSOLEBUFFER)pHMHandleData->lpHandlerData; | 
|---|
| 102 | ULONG ulCounter;                 /* counter for the byte transfer */ | 
|---|
| 103 | PSZ   pszBuffer; | 
|---|
| 104 | char  filler[4] = {' ', 0x07, ' ', 0x07}; | 
|---|
| 105 | register UCHAR ucChar; | 
|---|
| 106 | APIRET rc; | 
|---|
| 107 | ULONG  Row; | 
|---|
| 108 | USHORT Column; | 
|---|
| 109 | int    numchar; | 
|---|
| 110 |  | 
|---|
| 111 | #ifdef DEBUG_LOCAL2 | 
|---|
| 112 | WriteLog("KERNEL32/CONSOLE:HMDeviceConsoleVioBufferClass:WriteFile %s(%08x,%08x,%08x,%08x,%08x)\n", | 
|---|
| 113 | lpHMDeviceName, | 
|---|
| 114 | pHMHandleData->hHMHandle, | 
|---|
| 115 | lpBuffer, | 
|---|
| 116 | nNumberOfBytesToWrite, | 
|---|
| 117 | lpNumberOfBytesWritten, | 
|---|
| 118 | lpOverlapped); | 
|---|
| 119 | #endif | 
|---|
| 120 |  | 
|---|
| 121 | /* check if we're called with non-existing line buffer */ | 
|---|
| 122 | if (pConsoleBuffer->ppszLine == NULL) { | 
|---|
| 123 | SetLastError(ERROR_OUTOFMEMORY_W); | 
|---|
| 124 | return FALSE; | 
|---|
| 125 | } | 
|---|
| 126 |  | 
|---|
| 127 | dprintf(("Current cursor position (%d,%d)", pConsoleBuffer->coordCursorPosition.X, pConsoleBuffer->coordCursorPosition.Y)); | 
|---|
| 128 |  | 
|---|
| 129 | if(nNumberOfBytesToWrite > 1024) | 
|---|
| 130 | { | 
|---|
| 131 | int  tmp = 0; | 
|---|
| 132 | BOOL retcode; | 
|---|
| 133 |  | 
|---|
| 134 | while(nNumberOfBytesToWrite) { | 
|---|
| 135 | *lpNumberOfBytesWritten = 0; | 
|---|
| 136 | retcode = WriteFile(pHMHandleData, lpBuffer, | 
|---|
| 137 | min(nNumberOfBytesToWrite, 512), lpNumberOfBytesWritten, | 
|---|
| 138 | lpOverlapped); | 
|---|
| 139 | if(retcode != TRUE)     break; | 
|---|
| 140 |  | 
|---|
| 141 | tmp                   += *lpNumberOfBytesWritten; | 
|---|
| 142 | nNumberOfBytesToWrite -= *lpNumberOfBytesWritten; | 
|---|
| 143 | lpBuffer               = (LPCVOID)((char *)lpBuffer + *lpNumberOfBytesWritten); | 
|---|
| 144 | } | 
|---|
| 145 | *lpNumberOfBytesWritten = tmp; | 
|---|
| 146 | return retcode; | 
|---|
| 147 | } | 
|---|
| 148 | pszBuffer = (PSZ)alloca(nNumberOfBytesToWrite); | 
|---|
| 149 | if(pszBuffer == NULL) { | 
|---|
| 150 | DebugInt3(); | 
|---|
| 151 | return FALSE; | 
|---|
| 152 | } | 
|---|
| 153 | memcpy(pszBuffer, lpBuffer, nNumberOfBytesToWrite); | 
|---|
| 154 |  | 
|---|
| 155 | ulCounter = 0; | 
|---|
| 156 | while(ulCounter < nNumberOfBytesToWrite) | 
|---|
| 157 | { | 
|---|
| 158 | ucChar = pszBuffer[ulCounter];                        /* map to register */ | 
|---|
| 159 |  | 
|---|
| 160 | if ( (pConsoleBuffer->dwConsoleMode & ENABLE_PROCESSED_OUTPUT) && | 
|---|
| 161 | (ucChar < 32) )     /* this is faster than a large switch statement */ | 
|---|
| 162 | { | 
|---|
| 163 | switch (ucChar) | 
|---|
| 164 | { | 
|---|
| 165 | case 7: /* BEL */ | 
|---|
| 166 | if (pConsoleGlobals->Options.fSpeakerEnabled == TRUE) | 
|---|
| 167 | DosBeep(pConsoleGlobals->Options.ulSpeakerFrequency, | 
|---|
| 168 | pConsoleGlobals->Options.ulSpeakerDuration); | 
|---|
| 169 | break; | 
|---|
| 170 | #if 0 | 
|---|
| 171 | case 8: /* Backspace */ | 
|---|
| 172 | // not correct if deleting expanded tab character | 
|---|
| 173 | rc = VioGetCurPos(&Row, &Column, 0); | 
|---|
| 174 | if(!rc) { | 
|---|
| 175 |  | 
|---|
| 176 | } | 
|---|
| 177 | if (pConsoleBuffer->coordCursorPosition.X > 0) | 
|---|
| 178 | pConsoleBuffer->coordCursorPosition.X--; | 
|---|
| 179 |  | 
|---|
| 180 | //@@@PH overwrite old character | 
|---|
| 181 | *(pConsoleBuffer->ppszLine[pConsoleBuffer->coordCursorPosition.Y] + | 
|---|
| 182 | pConsoleBuffer->coordCursorPosition.X * 2) = 0x20; | 
|---|
| 183 | break; | 
|---|
| 184 |  | 
|---|
| 185 | case 9: /* Tab */ | 
|---|
| 186 | { | 
|---|
| 187 | rc = VioWrite | 
|---|
| 188 | pConsoleBuffer->coordCursorPosition.X = | 
|---|
| 189 | (pConsoleBuffer->coordCursorPosition.X | 
|---|
| 190 | / pConsoleGlobals->Options.ulTabSize | 
|---|
| 191 | + 1) | 
|---|
| 192 | * pConsoleGlobals->Options.ulTabSize; | 
|---|
| 193 |  | 
|---|
| 194 | if (pConsoleBuffer->coordCursorPosition.X >= | 
|---|
| 195 | pConsoleBuffer->coordBufferSize.X) | 
|---|
| 196 | { | 
|---|
| 197 | pConsoleBuffer->coordCursorPosition.X = 0; | 
|---|
| 198 | pConsoleBuffer->coordCursorPosition.Y++; | 
|---|
| 199 |  | 
|---|
| 200 | if (pConsoleBuffer->coordCursorPosition.Y >= | 
|---|
| 201 | pConsoleBuffer->coordBufferSize.Y) | 
|---|
| 202 | { | 
|---|
| 203 | if (pConsoleBuffer->dwConsoleMode & ENABLE_WRAP_AT_EOL_OUTPUT) | 
|---|
| 204 | { | 
|---|
| 205 | iConsoleBufferScrollUp(pConsoleBuffer,   /* scroll one line up */ | 
|---|
| 206 | 1); | 
|---|
| 207 | pConsoleBuffer->coordCursorPosition.Y--; | 
|---|
| 208 | } | 
|---|
| 209 | } | 
|---|
| 210 | } | 
|---|
| 211 | break; | 
|---|
| 212 | #endif | 
|---|
| 213 | case 13: /* CARRIAGE RETURN */ | 
|---|
| 214 | dprintf(("CR")); | 
|---|
| 215 | pConsoleBuffer->coordCursorPosition.X = 0; | 
|---|
| 216 | VioSetCurPos(pConsoleBuffer->coordCursorPosition.Y, pConsoleBuffer->coordCursorPosition.X, 0); | 
|---|
| 217 | break; | 
|---|
| 218 |  | 
|---|
| 219 | case 10: /* LINEFEED */ | 
|---|
| 220 | { | 
|---|
| 221 | dprintf(("LF")); | 
|---|
| 222 | pConsoleBuffer->coordCursorPosition.Y++; | 
|---|
| 223 | pConsoleBuffer->coordCursorPosition.X = 0; | 
|---|
| 224 | if(pConsoleBuffer->coordCursorPosition.Y >= pConsoleBuffer->coordWindowSize.Y) { | 
|---|
| 225 | dprintf(("scrollup")); | 
|---|
| 226 | VioScrollUp(0, 0, pConsoleBuffer->coordWindowSize.Y-1, pConsoleBuffer->coordWindowSize.X-1, | 
|---|
| 227 | 1, &filler[0], 0); | 
|---|
| 228 | pConsoleBuffer->coordCursorPosition.Y = pConsoleBuffer->coordWindowSize.Y-1; | 
|---|
| 229 | } | 
|---|
| 230 | VioSetCurPos(pConsoleBuffer->coordCursorPosition.Y, pConsoleBuffer->coordCursorPosition.X, 0); | 
|---|
| 231 | break; | 
|---|
| 232 | } | 
|---|
| 233 | default: | 
|---|
| 234 | break; | 
|---|
| 235 | } | 
|---|
| 236 | ulCounter++; | 
|---|
| 237 | } | 
|---|
| 238 | else | 
|---|
| 239 | { | 
|---|
| 240 | ////        dprintf(("Current cursor position (%d,%d)", pConsoleBuffer->coordCursorPosition.X, pConsoleBuffer->coordCursorPosition.Y)); | 
|---|
| 241 | numchar = ulCounter; | 
|---|
| 242 | while(pszBuffer[numchar] >= 32 && numchar < nNumberOfBytesToWrite) { | 
|---|
| 243 | numchar++; | 
|---|
| 244 | } | 
|---|
| 245 | numchar = numchar - ulCounter; | 
|---|
| 246 |  | 
|---|
| 247 | if(pConsoleBuffer->coordCursorPosition.X + numchar > pConsoleBuffer->coordWindowSize.X) | 
|---|
| 248 | { | 
|---|
| 249 | int tmp = pConsoleBuffer->coordWindowSize.X - pConsoleBuffer->coordCursorPosition.X; | 
|---|
| 250 |  | 
|---|
| 251 | VioWrtCharStr(&pszBuffer[ulCounter], tmp, pConsoleBuffer->coordCursorPosition.Y, pConsoleBuffer->coordCursorPosition.X, 0); | 
|---|
| 252 | ulCounter += tmp; | 
|---|
| 253 | numchar   -= tmp; | 
|---|
| 254 |  | 
|---|
| 255 | pConsoleBuffer->coordCursorPosition.X = 0; | 
|---|
| 256 | pConsoleBuffer->coordCursorPosition.Y++; | 
|---|
| 257 | if(pConsoleBuffer->coordCursorPosition.Y >= pConsoleBuffer->coordWindowSize.Y) { | 
|---|
| 258 | dprintf(("scrollup")); | 
|---|
| 259 | VioScrollUp(0, 0, pConsoleBuffer->coordWindowSize.Y-1, pConsoleBuffer->coordWindowSize.X-1, | 
|---|
| 260 | 1, &filler[0], 0); | 
|---|
| 261 | pConsoleBuffer->coordCursorPosition.Y = pConsoleBuffer->coordWindowSize.Y-1; | 
|---|
| 262 | } | 
|---|
| 263 | VioWrtCharStr(&pszBuffer[ulCounter], numchar, pConsoleBuffer->coordCursorPosition.Y, pConsoleBuffer->coordCursorPosition.X, 0); | 
|---|
| 264 | pConsoleBuffer->coordCursorPosition.X += numchar; | 
|---|
| 265 | VioSetCurPos(pConsoleBuffer->coordCursorPosition.Y, pConsoleBuffer->coordCursorPosition.X, 0); | 
|---|
| 266 | } | 
|---|
| 267 | else { | 
|---|
| 268 | VioWrtCharStr(&pszBuffer[ulCounter], numchar, pConsoleBuffer->coordCursorPosition.Y, pConsoleBuffer->coordCursorPosition.X, 0); | 
|---|
| 269 | pConsoleBuffer->coordCursorPosition.X += numchar; | 
|---|
| 270 | } | 
|---|
| 271 | ulCounter += numchar; | 
|---|
| 272 | } | 
|---|
| 273 | } | 
|---|
| 274 |  | 
|---|
| 275 | *lpNumberOfBytesWritten = ulCounter; | 
|---|
| 276 |  | 
|---|
| 277 | return TRUE; | 
|---|
| 278 | } | 
|---|
| 279 |  | 
|---|
| 280 |  | 
|---|