| 1 | /* $Id: conbuffervio.cpp,v 1.7 2003-04-11 12:08:35 sandervl 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 | // Vio/Kbd/Mou declarations conflict in GCC and in real OS2TK headers;
|
|---|
| 57 | // force GCC declarations since we link against GCC libs
|
|---|
| 58 | #if defined (__EMX__) && defined (USE_OS2_TOOLKIT_HEADERS)
|
|---|
| 59 | #undef USE_OS2_TOOLKIT_HEADERS
|
|---|
| 60 | #endif
|
|---|
| 61 |
|
|---|
| 62 | #define INCL_WIN
|
|---|
| 63 | #define INCL_DOSMEMMGR
|
|---|
| 64 | #define INCL_DOSSEMAPHORES
|
|---|
| 65 | #define INCL_DOSERRORS
|
|---|
| 66 | #define INCL_DOSPROCESS
|
|---|
| 67 | #define INCL_DOSMODULEMGR
|
|---|
| 68 | #define INCL_VIO
|
|---|
| 69 | #define INCL_AVIO
|
|---|
| 70 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
|---|
| 71 |
|
|---|
| 72 | #include <win32api.h>
|
|---|
| 73 | #include <misc.h>
|
|---|
| 74 | #include <string.h>
|
|---|
| 75 | #include <stdlib.h>
|
|---|
| 76 | #include <stdio.h>
|
|---|
| 77 | #include <malloc.h>
|
|---|
| 78 |
|
|---|
| 79 | #include "conwin.h" // Windows Header for console only
|
|---|
| 80 | #include "HandleManager.h"
|
|---|
| 81 | #include "HMDevice.h"
|
|---|
| 82 | #include "ConBuffervio.H"
|
|---|
| 83 | #include "Console2.h"
|
|---|
| 84 | #include <heapstring.h>
|
|---|
| 85 |
|
|---|
| 86 | #define DBG_LOCALLOG DBG_conbuffer
|
|---|
| 87 | #include "dbglocal.h"
|
|---|
| 88 |
|
|---|
| 89 | /*****************************************************************************
|
|---|
| 90 | * Name :
|
|---|
| 91 | * Purpose :
|
|---|
| 92 | * Parameters:
|
|---|
| 93 | * Variables :
|
|---|
| 94 | * Result :
|
|---|
| 95 | * Remark :
|
|---|
| 96 | * Status :
|
|---|
| 97 | *
|
|---|
| 98 | * Author : Patrick Haller [Wed, 1998/02/11 20:44]
|
|---|
| 99 | *****************************************************************************/
|
|---|
| 100 |
|
|---|
| 101 | BOOL HMDeviceConsoleVioBufferClass::WriteFile(PHMHANDLEDATA pHMHandleData,
|
|---|
| 102 | LPCVOID lpBuffer,
|
|---|
| 103 | DWORD nNumberOfBytesToWrite,
|
|---|
| 104 | LPDWORD lpNumberOfBytesWritten,
|
|---|
| 105 | LPOVERLAPPED lpOverlapped,
|
|---|
| 106 | LPOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
|
|---|
| 107 | {
|
|---|
| 108 | PCONSOLEBUFFER pConsoleBuffer = (PCONSOLEBUFFER)pHMHandleData->lpHandlerData;
|
|---|
| 109 | ULONG ulCounter; /* counter for the byte transfer */
|
|---|
| 110 | PSZ pszBuffer;
|
|---|
| 111 | char filler[4] = {' ', 0x07, ' ', 0x07};
|
|---|
| 112 | register UCHAR ucChar;
|
|---|
| 113 | APIRET rc;
|
|---|
| 114 | ULONG Row;
|
|---|
| 115 | USHORT Column;
|
|---|
| 116 | int numchar;
|
|---|
| 117 |
|
|---|
| 118 | #ifdef DEBUG_LOCAL2
|
|---|
| 119 | WriteLog("KERNEL32/CONSOLE:HMDeviceConsoleVioBufferClass:WriteFile %s(%08x,%08x,%08x,%08x,%08x)\n",
|
|---|
| 120 | lpHMDeviceName,
|
|---|
| 121 | pHMHandleData->hHMHandle,
|
|---|
| 122 | lpBuffer,
|
|---|
| 123 | nNumberOfBytesToWrite,
|
|---|
| 124 | lpNumberOfBytesWritten,
|
|---|
| 125 | lpOverlapped);
|
|---|
| 126 | #endif
|
|---|
| 127 |
|
|---|
| 128 | if(lpCompletionRoutine) {
|
|---|
| 129 | dprintf(("!WARNING!: lpCompletionRoutine not supported -> fall back to sync IO"));
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | /* check if we're called with non-existing line buffer */
|
|---|
| 133 | if (pConsoleBuffer->ppszLine == NULL) {
|
|---|
| 134 | SetLastError(ERROR_OUTOFMEMORY_W);
|
|---|
| 135 | return FALSE;
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | dprintf(("Current cursor position (%d,%d)", pConsoleBuffer->coordCursorPosition.X, pConsoleBuffer->coordCursorPosition.Y));
|
|---|
| 139 |
|
|---|
| 140 | if(nNumberOfBytesToWrite > 1024)
|
|---|
| 141 | {
|
|---|
| 142 | int tmp = 0;
|
|---|
| 143 | BOOL retcode;
|
|---|
| 144 |
|
|---|
| 145 | while(nNumberOfBytesToWrite) {
|
|---|
| 146 | *lpNumberOfBytesWritten = 0;
|
|---|
| 147 | retcode = WriteFile(pHMHandleData, lpBuffer,
|
|---|
| 148 | min(nNumberOfBytesToWrite, 512), lpNumberOfBytesWritten,
|
|---|
| 149 | lpOverlapped, lpCompletionRoutine);
|
|---|
| 150 | if(retcode != TRUE) break;
|
|---|
| 151 |
|
|---|
| 152 | tmp += *lpNumberOfBytesWritten;
|
|---|
| 153 | nNumberOfBytesToWrite -= *lpNumberOfBytesWritten;
|
|---|
| 154 | lpBuffer = (LPCVOID)((char *)lpBuffer + *lpNumberOfBytesWritten);
|
|---|
| 155 | }
|
|---|
| 156 | *lpNumberOfBytesWritten = tmp;
|
|---|
| 157 | return retcode;
|
|---|
| 158 | }
|
|---|
| 159 | pszBuffer = (PSZ)alloca(nNumberOfBytesToWrite);
|
|---|
| 160 | if(pszBuffer == NULL) {
|
|---|
| 161 | DebugInt3();
|
|---|
| 162 | return FALSE;
|
|---|
| 163 | }
|
|---|
| 164 | memcpy(pszBuffer, lpBuffer, nNumberOfBytesToWrite);
|
|---|
| 165 |
|
|---|
| 166 | ulCounter = 0;
|
|---|
| 167 | while(ulCounter < nNumberOfBytesToWrite)
|
|---|
| 168 | {
|
|---|
| 169 | ucChar = pszBuffer[ulCounter]; /* map to register */
|
|---|
| 170 |
|
|---|
| 171 | if ( (pConsoleBuffer->dwConsoleMode & ENABLE_PROCESSED_OUTPUT) &&
|
|---|
| 172 | (ucChar < 32) ) /* this is faster than a large switch statement */
|
|---|
| 173 | {
|
|---|
| 174 | switch (ucChar)
|
|---|
| 175 | {
|
|---|
| 176 | case 7: /* BEL */
|
|---|
| 177 | if (pConsoleGlobals->Options.fSpeakerEnabled == TRUE)
|
|---|
| 178 | DosBeep(pConsoleGlobals->Options.ulSpeakerFrequency,
|
|---|
| 179 | pConsoleGlobals->Options.ulSpeakerDuration);
|
|---|
| 180 | break;
|
|---|
| 181 | #if 0
|
|---|
| 182 | case 8: /* Backspace */
|
|---|
| 183 | // not correct if deleting expanded tab character
|
|---|
| 184 | rc = VioGetCurPos(&Row, &Column, 0);
|
|---|
| 185 | if(!rc) {
|
|---|
| 186 |
|
|---|
| 187 | }
|
|---|
| 188 | if (pConsoleBuffer->coordCursorPosition.X > 0)
|
|---|
| 189 | pConsoleBuffer->coordCursorPosition.X--;
|
|---|
| 190 |
|
|---|
| 191 | //@@@PH overwrite old character
|
|---|
| 192 | *(pConsoleBuffer->ppszLine[pConsoleBuffer->coordCursorPosition.Y] +
|
|---|
| 193 | pConsoleBuffer->coordCursorPosition.X * 2) = 0x20;
|
|---|
| 194 | break;
|
|---|
| 195 |
|
|---|
| 196 | case 9: /* Tab */
|
|---|
| 197 | {
|
|---|
| 198 | rc = VioWrite
|
|---|
| 199 | pConsoleBuffer->coordCursorPosition.X =
|
|---|
| 200 | (pConsoleBuffer->coordCursorPosition.X
|
|---|
| 201 | / pConsoleGlobals->Options.ulTabSize
|
|---|
| 202 | + 1)
|
|---|
| 203 | * pConsoleGlobals->Options.ulTabSize;
|
|---|
| 204 |
|
|---|
| 205 | if (pConsoleBuffer->coordCursorPosition.X >=
|
|---|
| 206 | pConsoleBuffer->coordBufferSize.X)
|
|---|
| 207 | {
|
|---|
| 208 | pConsoleBuffer->coordCursorPosition.X = 0;
|
|---|
| 209 | pConsoleBuffer->coordCursorPosition.Y++;
|
|---|
| 210 |
|
|---|
| 211 | if (pConsoleBuffer->coordCursorPosition.Y >=
|
|---|
| 212 | pConsoleBuffer->coordBufferSize.Y)
|
|---|
| 213 | {
|
|---|
| 214 | if (pConsoleBuffer->dwConsoleMode & ENABLE_WRAP_AT_EOL_OUTPUT)
|
|---|
| 215 | {
|
|---|
| 216 | iConsoleBufferScrollUp(pConsoleBuffer, /* scroll one line up */
|
|---|
| 217 | 1);
|
|---|
| 218 | pConsoleBuffer->coordCursorPosition.Y--;
|
|---|
| 219 | }
|
|---|
| 220 | }
|
|---|
| 221 | }
|
|---|
| 222 | break;
|
|---|
| 223 | #endif
|
|---|
| 224 | case 13: /* CARRIAGE RETURN */
|
|---|
| 225 | dprintf(("CR"));
|
|---|
| 226 | pConsoleBuffer->coordCursorPosition.X = 0;
|
|---|
| 227 | VioSetCurPos(pConsoleBuffer->coordCursorPosition.Y, pConsoleBuffer->coordCursorPosition.X, 0);
|
|---|
| 228 | break;
|
|---|
| 229 |
|
|---|
| 230 | case 10: /* LINEFEED */
|
|---|
| 231 | {
|
|---|
| 232 | dprintf(("LF"));
|
|---|
| 233 | pConsoleBuffer->coordCursorPosition.Y++;
|
|---|
| 234 | pConsoleBuffer->coordCursorPosition.X = 0;
|
|---|
| 235 | if(pConsoleBuffer->coordCursorPosition.Y >= pConsoleBuffer->coordWindowSize.Y) {
|
|---|
| 236 | dprintf(("scrollup"));
|
|---|
| 237 | VioScrollUp(0, 0, pConsoleBuffer->coordWindowSize.Y-1, pConsoleBuffer->coordWindowSize.X-1,
|
|---|
| 238 | 1, &filler[0], 0);
|
|---|
| 239 | pConsoleBuffer->coordCursorPosition.Y = pConsoleBuffer->coordWindowSize.Y-1;
|
|---|
| 240 | }
|
|---|
| 241 | VioSetCurPos(pConsoleBuffer->coordCursorPosition.Y, pConsoleBuffer->coordCursorPosition.X, 0);
|
|---|
| 242 | break;
|
|---|
| 243 | }
|
|---|
| 244 | default:
|
|---|
| 245 | break;
|
|---|
| 246 | }
|
|---|
| 247 | ulCounter++;
|
|---|
| 248 | }
|
|---|
| 249 | else
|
|---|
| 250 | {
|
|---|
| 251 | //// dprintf(("Current cursor position (%d,%d)", pConsoleBuffer->coordCursorPosition.X, pConsoleBuffer->coordCursorPosition.Y));
|
|---|
| 252 | numchar = ulCounter;
|
|---|
| 253 | while(pszBuffer[numchar] >= 32 && numchar < nNumberOfBytesToWrite) {
|
|---|
| 254 | numchar++;
|
|---|
| 255 | }
|
|---|
| 256 | numchar = numchar - ulCounter;
|
|---|
| 257 |
|
|---|
| 258 | if(pConsoleBuffer->coordCursorPosition.X + numchar > pConsoleBuffer->coordWindowSize.X)
|
|---|
| 259 | {
|
|---|
| 260 | int tmp = pConsoleBuffer->coordWindowSize.X - pConsoleBuffer->coordCursorPosition.X;
|
|---|
| 261 |
|
|---|
| 262 | VioWrtCharStr(&pszBuffer[ulCounter], tmp, pConsoleBuffer->coordCursorPosition.Y, pConsoleBuffer->coordCursorPosition.X, 0);
|
|---|
| 263 | ulCounter += tmp;
|
|---|
| 264 | numchar -= tmp;
|
|---|
| 265 |
|
|---|
| 266 | pConsoleBuffer->coordCursorPosition.X = 0;
|
|---|
| 267 | pConsoleBuffer->coordCursorPosition.Y++;
|
|---|
| 268 | if(pConsoleBuffer->coordCursorPosition.Y >= pConsoleBuffer->coordWindowSize.Y) {
|
|---|
| 269 | dprintf(("scrollup"));
|
|---|
| 270 | VioScrollUp(0, 0, pConsoleBuffer->coordWindowSize.Y-1, pConsoleBuffer->coordWindowSize.X-1,
|
|---|
| 271 | 1, &filler[0], 0);
|
|---|
| 272 | pConsoleBuffer->coordCursorPosition.Y = pConsoleBuffer->coordWindowSize.Y-1;
|
|---|
| 273 | }
|
|---|
| 274 | VioWrtCharStr(&pszBuffer[ulCounter], numchar, pConsoleBuffer->coordCursorPosition.Y, pConsoleBuffer->coordCursorPosition.X, 0);
|
|---|
| 275 | pConsoleBuffer->coordCursorPosition.X += numchar;
|
|---|
| 276 | VioSetCurPos(pConsoleBuffer->coordCursorPosition.Y, pConsoleBuffer->coordCursorPosition.X, 0);
|
|---|
| 277 | }
|
|---|
| 278 | else {
|
|---|
| 279 | VioWrtCharStr(&pszBuffer[ulCounter], numchar, pConsoleBuffer->coordCursorPosition.Y, pConsoleBuffer->coordCursorPosition.X, 0);
|
|---|
| 280 | pConsoleBuffer->coordCursorPosition.X += numchar;
|
|---|
| 281 | VioSetCurPos(pConsoleBuffer->coordCursorPosition.Y, pConsoleBuffer->coordCursorPosition.X, 0);
|
|---|
| 282 | }
|
|---|
| 283 | ulCounter += numchar;
|
|---|
| 284 | }
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | *lpNumberOfBytesWritten = ulCounter;
|
|---|
| 288 |
|
|---|
| 289 | return TRUE;
|
|---|
| 290 | }
|
|---|
| 291 |
|
|---|
| 292 | DWORD HMDeviceConsoleVioBufferClass::FillConsoleOutputAttribute(PHMHANDLEDATA pHMHandleData,
|
|---|
| 293 | WORD wAttribute,
|
|---|
| 294 | DWORD nLength,
|
|---|
| 295 | COORD dwWriteCoord,
|
|---|
| 296 | LPDWORD lpNumberOfAttrsWritten)
|
|---|
| 297 | {
|
|---|
| 298 | APIRET rc;
|
|---|
| 299 |
|
|---|
| 300 | #ifdef DEBUG_LOCAL2
|
|---|
| 301 | WriteLog("KERNEL32/CONSOLE: HMDeviceConsoleVioBufferClass::FillConsoleOutputAttribute(%08x,attr=%04x,%u,x=%u y=%u,res=%08x).\n",
|
|---|
| 302 | pHMHandleData,
|
|---|
| 303 | wAttribute,
|
|---|
| 304 | nLength,
|
|---|
| 305 | dwWriteCoord.X,
|
|---|
| 306 | dwWriteCoord.Y,
|
|---|
| 307 | lpNumberOfAttrsWritten);
|
|---|
| 308 | #endif
|
|---|
| 309 |
|
|---|
| 310 | if (HMDeviceConsoleBufferClass::FillConsoleOutputAttribute(pHMHandleData,
|
|---|
| 311 | wAttribute,
|
|---|
| 312 | nLength,
|
|---|
| 313 | dwWriteCoord,
|
|---|
| 314 | lpNumberOfAttrsWritten))
|
|---|
| 315 | {
|
|---|
| 316 | rc = VioWrtNAttr((PBYTE)&wAttribute, *lpNumberOfAttrsWritten, dwWriteCoord.Y, dwWriteCoord.X,
|
|---|
| 317 | 0);
|
|---|
| 318 | if (rc == NO_ERROR)
|
|---|
| 319 | {
|
|---|
| 320 | return TRUE;
|
|---|
| 321 | } else
|
|---|
| 322 | {
|
|---|
| 323 | return FALSE;
|
|---|
| 324 | }
|
|---|
| 325 | } else
|
|---|
| 326 | return FALSE;
|
|---|
| 327 | }
|
|---|
| 328 |
|
|---|
| 329 | DWORD HMDeviceConsoleVioBufferClass::FillConsoleOutputCharacterA(PHMHANDLEDATA pHMHandleData,
|
|---|
| 330 | UCHAR ucCharacter,
|
|---|
| 331 | DWORD nLength,
|
|---|
| 332 | COORD dwWriteCoord,
|
|---|
| 333 | LPDWORD lpNumberOfCharsWritten)
|
|---|
| 334 | {
|
|---|
| 335 | APIRET rc;
|
|---|
| 336 |
|
|---|
| 337 | #ifdef DEBUG_LOCAL2
|
|---|
| 338 | WriteLog("KERNEL32/CONSOLE: HMDeviceConsoleVioBufferClass::FillConsoleOutputCharacterA(%08x,char=%02x,%u,x=%u y=%u,res=%08x).\n",
|
|---|
| 339 | pHMHandleData,
|
|---|
| 340 | ucCharacter,
|
|---|
| 341 | nLength,
|
|---|
| 342 | dwWriteCoord.X,
|
|---|
| 343 | dwWriteCoord.Y,
|
|---|
| 344 | lpNumberOfCharsWritten);
|
|---|
| 345 | #endif
|
|---|
| 346 | if (HMDeviceConsoleBufferClass::FillConsoleOutputCharacterA(pHMHandleData,
|
|---|
| 347 | ucCharacter,
|
|---|
| 348 | nLength,
|
|---|
| 349 | dwWriteCoord,
|
|---|
| 350 | lpNumberOfCharsWritten))
|
|---|
| 351 | {
|
|---|
| 352 | rc = VioWrtNChar((PCH)&ucCharacter, *lpNumberOfCharsWritten, dwWriteCoord.Y, dwWriteCoord.X,
|
|---|
| 353 | 0);
|
|---|
| 354 | if (rc == NO_ERROR)
|
|---|
| 355 | {
|
|---|
| 356 | return TRUE;
|
|---|
| 357 | } else
|
|---|
| 358 | {
|
|---|
| 359 | return FALSE;
|
|---|
| 360 | }
|
|---|
| 361 | } else
|
|---|
| 362 | return FALSE;
|
|---|
| 363 | }
|
|---|
| 364 |
|
|---|