home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tvos200.zip / TVISION / HARDNT.CPP < prev    next >
C/C++ Source or Header  |  1995-04-25  |  10KB  |  288 lines

  1. /*------------------------------------------------------------*/
  2. /* filename -       hardnt.cpp                                */
  3. /*                                                            */
  4. /* function(s)                                                */
  5. /*                  Member variables for THardwareInfo class  */
  6. /*                  under the Windows NT operating system.    */
  7. /*                                                            */
  8. /*------------------------------------------------------------*/
  9. /*
  10.  *      Turbo Vision - Version 2.0
  11.  *
  12.  *      Copyright (c) 1994 by Borland International
  13.  *      All Rights Reserved.
  14.  *
  15.  */
  16. #if defined(__NT__)                 // Compile module for NT only.
  17.  
  18. #define Uses_TKeys
  19. #define Uses_TEvent
  20. #define Uses_TScreen
  21. #define Uses_THardwareInfo
  22. #define Uses_TSystemError
  23. #include <tvision\tv.h>
  24.  
  25.  
  26. BOOL THardwareInfo::insertState = True;
  27. THardwareInfo::PlatformType THardwareInfo::platform = THardwareInfo::plDPMI32;
  28. HANDLE THardwareInfo::consoleHandle[2];
  29. DWORD THardwareInfo::consoleMode;
  30. DWORD THardwareInfo::pendingEvent;
  31. INPUT_RECORD THardwareInfo::irBuffer;
  32. CONSOLE_CURSOR_INFO THardwareInfo::crInfo;
  33. CONSOLE_SCREEN_BUFFER_INFO THardwareInfo::sbInfo;
  34.  
  35. static ushort ShiftCvt[89] = {
  36.          0,      0,      0,      0,      0,      0,      0,      0,
  37.          0,      0,      0,      0,      0,      0,      0,      0,
  38.          0,      0,      0,      0,      0,      0,      0,      0,
  39.          0,      0,      0,      0,      0,      0,      0,      0,
  40.          0,      0,      0,      0,      0,      0,      0,      0,
  41.          0,      0,      0,      0,      0,      0,      0,      0,
  42.          0,      0,      0,      0,      0,      0,      0,      0,
  43.          0,      0,      0, 0x5400, 0x5500, 0x5600, 0x5700, 0x5800,
  44.     0x5900, 0x5A00, 0x5B00, 0x5C00, 0x5D00,      0,      0,      0,
  45.          0,      0,      0,      0,      0,      0,      0,      0,
  46.          0,      0, 0x0500, 0x0700,      0,      0,      0, 0x8700,
  47.     0x8800
  48. };
  49.  
  50. static ushort CtrlCvt[89] = {
  51.          0,      0,      0,      0,      0,      0,      0,      0,
  52.          0,      0,      0,      0,      0,      0,      0,      0,
  53.     0x0011, 0x0017, 0x0005, 0x0012, 0x0014, 0x0019, 0x0015, 0x0009,
  54.     0x000F, 0x0010,      0,      0,      0,      0, 0x0001, 0x0013,
  55.     0x0004, 0x0006, 0x0007, 0x0008, 0x000A, 0x000B, 0x000C,      0,
  56.          0,      0,      0,      0, 0x001A, 0x0018, 0x0003, 0x0016,
  57.     0x0002, 0x000E, 0x000D,      0,      0,      0,      0,      0,
  58.          0,      0,      0, 0x5E00, 0x5F00, 0x6000, 0x6100, 0x6200,
  59.     0x6300, 0x6400, 0x6500, 0x6600, 0x6700,      0,      0, 0x7700,
  60.          0, 0x8400,      0, 0x7300,      0, 0x7400,      0, 0x7500,
  61.          0, 0x7600, 0x0400, 0x0600,      0,      0,      0, 0x8900,
  62.     0x8A00
  63. };
  64.  
  65. static ushort AltCvt[89] = {
  66.          0,      0, 0x7800, 0x7900, 0x7A00, 0x7B00, 0x7C00, 0x7D00,
  67.     0x7E00, 0x7F00, 0x8000, 0x8100, 0x8200, 0x8300, 0x0800,      0,
  68.     0x1000, 0x1100, 0x1200, 0x1300, 0x1400, 0x1500, 0x1600, 0x1700,
  69.     0x1800, 0x1900,      0,      0,      0,      0, 0x1E00, 0x1F00,
  70.     0x2000, 0x2100, 0x2200, 0x2300, 0x2400, 0x2500, 0x2600,      0,
  71.          0,      0,      0,      0, 0x2C00, 0x2D00, 0x2E00, 0x2F00,
  72.     0x3000, 0x3100, 0x3200,      0,      0,      0,      0,      0,
  73.          0, 0x0200,      0, 0x6800, 0x6900, 0x6A00, 0x6B00, 0x6C00,
  74.     0x6D00, 0x6E00, 0x6F00, 0x7000, 0x7100,      0,      0,      0,
  75.          0,      0,      0,      0,      0,      0,      0,      0,
  76.          0,      0,      0,      0,      0,      0,      0, 0x8B00,
  77.     0x8C00
  78. };
  79.  
  80.  
  81. #define INT10   { __emit__( 0xCD ); __emit__( 0x10 ); }
  82.  
  83. // Constructor for 16-bit version is in HARDWARE.ASM
  84.  
  85. THardwareInfo::THardwareInfo()
  86. {
  87.     HMODULE mod;
  88.  
  89.     if( (mod = GetModuleHandle( "KERNEL32" )) != 0 &&
  90.          GetProcAddress( mod, "Borland32" ) != 0
  91.       )
  92.         platform = plDPMI32;
  93.     else
  94.         platform = plWinNT;
  95.  
  96.     consoleHandle[cnInput] = GetStdHandle( STD_INPUT_HANDLE );
  97.     consoleHandle[cnOutput] = GetStdHandle( STD_OUTPUT_HANDLE );
  98.     GetConsoleMode( consoleHandle[cnInput], &consoleMode );
  99.     GetConsoleCursorInfo( consoleHandle[cnOutput], &crInfo );
  100.     GetConsoleScreenBufferInfo( consoleHandle[cnOutput], &sbInfo );
  101. }
  102.  
  103. ushort THardwareInfo::getScreenMode()
  104. {
  105.     ushort mode;
  106.  
  107.     if( platform != plDPMI32 )      // B/W, mono not supported if running on
  108.         mode = TDisplay::smCO80;    //   NT.  Would have to use the registry.
  109.     else
  110.         {
  111.         _AH = 0x0F;
  112.         INT10;                      // Emit CD, 10.  Supported by DPMI server.
  113.         mode = _AL;
  114.         }
  115.  
  116.     if( getScreenRows() > 25 )
  117.         mode |= TDisplay::smFont8x8;
  118.     return mode;
  119. }
  120.  
  121. void THardwareInfo::setScreenMode( ushort mode )
  122. {
  123.     COORD newSize = { 80, 25 };
  124.     SMALL_RECT rect = { 0, 0, 79, 24 };
  125.  
  126.     if( mode & TDisplay::smFont8x8 )
  127.         {
  128.         newSize.Y = 50;
  129.         rect.Bottom = 49;
  130.         }
  131.  
  132.     if( platform != plDPMI32 )
  133.         {
  134.         COORD maxSize = GetLargestConsoleWindowSize( consoleHandle[cnOutput] );
  135.         if( newSize.Y > maxSize.Y )
  136.             {
  137.             newSize.Y = maxSize.Y;
  138.             rect.Bottom = newSize.Y-1;
  139.             }
  140.         }
  141.  
  142.     if( mode & TDisplay::smFont8x8 )
  143.         {
  144.         SetConsoleScreenBufferSize( consoleHandle[cnOutput], newSize );
  145.         SetConsoleWindowInfo( consoleHandle[cnOutput], True, &rect );
  146.         }
  147.     else
  148.         {
  149.         SetConsoleWindowInfo( consoleHandle[cnOutput], True, &rect );
  150.         SetConsoleScreenBufferSize( consoleHandle[cnOutput], newSize );
  151.         }
  152.  
  153.     GetConsoleScreenBufferInfo( consoleHandle[cnOutput], &sbInfo );
  154. }
  155.  
  156. void THardwareInfo::setCaretPosition( ushort x, ushort y )
  157. {
  158.     COORD coord = { x, y };
  159.     SetConsoleCursorPosition( consoleHandle[cnOutput], coord );
  160. }
  161.  
  162. void THardwareInfo::setCaretSize( ushort size )
  163. {
  164.     if( size == 0 )
  165.     {
  166.         crInfo.bVisible = False;
  167.         crInfo.dwSize = 1;
  168.     }
  169.     else
  170.     {
  171.         crInfo.bVisible = True;
  172.         crInfo.dwSize = size;
  173.     }
  174.  
  175.     SetConsoleCursorInfo( consoleHandle[cnOutput], &crInfo );
  176. }
  177.  
  178. void THardwareInfo::screenWrite( ushort x, ushort y, ushort *buf, DWORD len )
  179. {
  180.     COORD size = {len,1};
  181.     COORD from = {0,0};
  182.     SMALL_RECT to = {x,y,x+len,y+1};
  183.  
  184.     WriteConsoleOutput( consoleHandle[cnOutput], (CHAR_INFO *) buf, size, from, &to);
  185. }
  186.  
  187. // Event functions.
  188.  
  189. BOOL THardwareInfo::getMouseEvent( MouseEventType& event )
  190. {
  191.     if( !pendingEvent )
  192.         {
  193.         GetNumberOfConsoleInputEvents( consoleHandle[cnInput], &pendingEvent );
  194.         if( pendingEvent )
  195.             ReadConsoleInput( consoleHandle[cnInput], &irBuffer, 1, &pendingEvent );
  196.         }
  197.  
  198.     if( pendingEvent && irBuffer.EventType == MOUSE_EVENT)
  199.         {
  200.         event.where.x = irBuffer.Event.MouseEvent.dwMousePosition.X;
  201.         event.where.y = irBuffer.Event.MouseEvent.dwMousePosition.Y;
  202.         event.buttons = irBuffer.Event.MouseEvent.dwButtonState;
  203.         event.eventFlags = irBuffer.Event.MouseEvent.dwEventFlags;
  204.         event.controlKeyState = irBuffer.Event.MouseEvent.dwControlKeyState;
  205.  
  206.         pendingEvent = 0;
  207.         return True;
  208.         }
  209.     return False;
  210. }
  211.  
  212.  
  213. BOOL THardwareInfo::getKeyEvent( TEvent& event )
  214. {
  215.     if( !pendingEvent )
  216.         {
  217.         GetNumberOfConsoleInputEvents( consoleHandle[cnInput], &pendingEvent );
  218.         if( pendingEvent )
  219.             ReadConsoleInput( consoleHandle[cnInput], &irBuffer, 1, &pendingEvent );
  220.         }
  221.  
  222.     if( pendingEvent )
  223.         {
  224.         if( irBuffer.EventType == KEY_EVENT && irBuffer.Event.KeyEvent.bKeyDown )
  225.             {
  226.             event.what = evKeyDown;
  227.             event.keyDown.charScan.scanCode = irBuffer.Event.KeyEvent.wVirtualScanCode;
  228.             event.keyDown.charScan.charCode = irBuffer.Event.KeyEvent.uChar.AsciiChar;
  229.             event.keyDown.controlKeyState = irBuffer.Event.KeyEvent.dwControlKeyState;
  230.  
  231.             /* Convert NT style virtual scan codes to PC BIOS codes.
  232.              */
  233.             if( (event.keyDown.controlKeyState & (kbShift | kbAltShift | kbCtrlShift)) != 0 )
  234.                 {
  235.                 uchar index = irBuffer.Event.KeyEvent.wVirtualScanCode;
  236.  
  237.                 if ((event.keyDown.controlKeyState & kbShift) && ShiftCvt[index] != 0)
  238.                     event.keyDown.keyCode = ShiftCvt[index];
  239.                 else if ((event.keyDown.controlKeyState & kbCtrlShift) && CtrlCvt[index] != 0)
  240.                     event.keyDown.keyCode = CtrlCvt[index];
  241.                 else if ((event.keyDown.controlKeyState & kbAltShift) && AltCvt[index] != 0)
  242.                     event.keyDown.keyCode = AltCvt[index];
  243.                 }
  244.  
  245.             /* Set/Reset insert flag.
  246.              */
  247.             if( event.keyDown.keyCode == kbIns )
  248.                 insertState = !insertState;
  249.  
  250.             if( insertState )
  251.                 event.keyDown.controlKeyState |= kbInsState;
  252.  
  253.             pendingEvent = 0;
  254.             return True;
  255.             }
  256.         // Ignore all events except mouse events.  Pending mouse events will
  257.         // be read on the next polling loop.
  258.         else if( irBuffer.EventType != MOUSE_EVENT )
  259.             pendingEvent = 0;
  260.         }
  261.  
  262.     return False;
  263. }
  264.  
  265. BOOL __stdcall THardwareInfo::ctrlBreakHandler( DWORD dwCtrlType )
  266. {
  267.     if( dwCtrlType == CTRL_C_EVENT || dwCtrlType == CTRL_BREAK_EVENT )
  268.         {
  269.         TSystemError::ctrlBreakHit = True;
  270.         return TRUE;
  271.         }
  272.     else
  273.         return FALSE; // Don't handle 'CLOSE', 'LOGOFF' or 'SHUTDOWN' events.
  274. }
  275.  
  276. ulong THardwareInfo::getTickCount()
  277. {
  278.     // To change units from ms to clock ticks.
  279.     //   X ms * 1s/1000ms * 18.2ticks/s = X/55 ticks, roughly.
  280.     return GetTickCount() / 55;
  281. }
  282.  
  283.  
  284.  
  285. #endif  // __NT__
  286.  
  287.  
  288.