home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 2 BBS / 02-BBS.zip / lora299s.zip / SCREEN.CPP < prev    next >
C/C++ Source or Header  |  1998-05-12  |  10KB  |  433 lines

  1.  
  2. // LoraBBS Version 2.99 Free Edition
  3. // Copyright (C) 1987-98 Marco Maccaferri
  4. //
  5. // This program is free software; you can redistribute it and/or modify
  6. // it under the terms of the GNU General Public License as published by
  7. // the Free Software Foundation; either version 2 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // This program is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. // GNU General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU General Public License
  16. // along with this program; if not, write to the Free Software
  17. // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19. #include "_ldefs.h"
  20. #include "combase.h"
  21.  
  22. TScreen::TScreen (void)
  23. {
  24.    wh = 0;
  25.    Ansi = FALSE;
  26.    Prec = 0;
  27.    Attr = BLACK|_LGREY;
  28.    RxBytes = 0;
  29.    RxPosition = 0;
  30.    Counter = 0;
  31. }
  32.  
  33. TScreen::~TScreen (void)
  34. {
  35.    if (wh != -1) {
  36.       wactiv (wh);
  37.       wclose ();
  38.       wclose ();
  39.    }
  40.  
  41.    hidecur ();
  42.  
  43. #if defined(__NT__)
  44.    FreeConsole ();
  45. #endif
  46. }
  47.  
  48. USHORT TScreen::BytesReady (VOID)
  49. {
  50.    int c;
  51.    USHORT RetVal = FALSE;
  52.  
  53.    while (kbmhit ()) {
  54.       c = (USHORT)getxch ();
  55.       if (c == 0x4800) {
  56.          memcpy (&RxBuffer[RxBytes], "\x1B[A", 3);
  57.          RxBytes += 3;
  58.       }
  59.       else if (c == 0x5000) {
  60.          memcpy (&RxBuffer[RxBytes], "\x1B[B", 3);
  61.          RxBytes += 3;
  62.       }
  63.       else if (c == 0x4D00) {
  64.          memcpy (&RxBuffer[RxBytes], "\x1B[C", 3);
  65.          RxBytes += 3;
  66.       }
  67.       else if (c == 0x4B00) {
  68.          memcpy (&RxBuffer[RxBytes], "\x1B[D", 3);
  69.          RxBytes += 3;
  70.       }
  71.       else if (c == 0x4700) {
  72.          memcpy (&RxBuffer[RxBytes], "\x1B[H", 3);
  73.          RxBytes += 3;
  74.       }
  75.       else if (c == 0x4F00) {
  76.          memcpy (&RxBuffer[RxBytes], "\x1B[K", 3);
  77.          RxBytes += 3;
  78.       }
  79.       else if (c == 0x5300) {
  80.          memcpy (&RxBuffer[RxBytes], "\x7F", 1);
  81.          RxBytes += 1;
  82.       }
  83.       else if (c == 0x2300)      // Alt-H = Hangup
  84.          Running = FALSE;
  85.       else
  86.          RxBuffer[RxBytes++] = (UCHAR)c;
  87.    }
  88.    if (RxBytes > 0)
  89.       RetVal = TRUE;
  90.  
  91. #if defined(__OS2__)
  92.    if (RetVal == FALSE)
  93.       DosSleep (1L);
  94. #elif defined(__NT__)
  95.    if (RetVal == FALSE)
  96.       Sleep (1L);
  97. #endif
  98.  
  99.    return (RetVal);
  100. }
  101.  
  102. VOID TScreen::BufferByte (UCHAR byte)
  103. {
  104.    short x, y;
  105.    USHORT i;
  106.  
  107.    if (byte == '[' && Prec == ESC) {
  108.       Ansi = TRUE;
  109.       Count = 0;
  110.       Params[Count] = 0;
  111.    }
  112.    else {
  113.       if (Ansi == TRUE) {
  114.          if (isalpha (byte)) {
  115.             if (byte == 'm') {
  116.                for (i = 0; i <= Count; i++) {
  117.                   if (Params[i] >= 30 && Params[i] <= 39)
  118.                      Attr &= 0xF8;
  119.                   else if (Params[i] >= 40 && Params[i] <= 49)
  120.                      Attr &= 0x8F;
  121.  
  122.                   switch (Params[i]) {
  123.                      case 0:
  124.                         Attr = 7;
  125.                         break;
  126.                      case 1:
  127.                         Attr |= 0x08;
  128.                         break;
  129.                      case 5:
  130.                         Attr |= BLINK;
  131.                         break;
  132.                      case 30:
  133.                         Attr |= BLACK;
  134.                         break;
  135.                      case 34:
  136.                         Attr |= BLUE;
  137.                         break;
  138.                      case 32:
  139.                         Attr |= GREEN;
  140.                         break;
  141.                      case 36:
  142.                         Attr |= CYAN;
  143.                         break;
  144.                      case 31:
  145.                         Attr |= RED;
  146.                         break;
  147.                      case 35:
  148.                         Attr |= MAGENTA;
  149.                         break;
  150.                      case 39:
  151.                         Attr |= BROWN;
  152.                         break;
  153.                      case 37:
  154.                         Attr |= LGREY;
  155.                         break;
  156.                      case 33:
  157.                         Attr |= YELLOW;
  158.                         break;
  159.                      case 40:
  160.                         Attr |= _BLACK;
  161.                         break;
  162.                      case 44:
  163.                         Attr |= _BLUE;
  164.                         break;
  165.                      case 42:
  166.                         Attr |= _GREEN;
  167.                         break;
  168.                      case 46:
  169.                         Attr |= _CYAN;
  170.                         break;
  171.                      case 41:
  172.                         Attr |= _RED;
  173.                         break;
  174.                      case 45:
  175.                         Attr |= _MAGENTA;
  176.                         break;
  177.                      case 49:
  178.                         Attr |= _BROWN;
  179.                         break;
  180.                      case 47:
  181.                         Attr |= _LGREY;
  182.                         break;
  183.                   }
  184.                }
  185.                wtextattr (Attr);
  186.             }
  187.             else if (byte == 'A') {
  188.                wreadcur (&y, &x);
  189.                if (Params[0] == 0)
  190.                   Params[0] = 1;
  191.                wgotoxy ((SHORT)(y - Params[0]), x);
  192.             }
  193.             else if (byte == 'B') {
  194.                wreadcur (&y, &x);
  195.                if (Params[0] == 0)
  196.                   Params[0] = 1;
  197.                wgotoxy ((SHORT)(y + Params[0]), x);
  198.             }
  199.             else if (byte == 'C') {
  200.                wreadcur (&y, &x);
  201.                if (Params[0] == 0)
  202.                   Params[0] = 1;
  203.                wgotoxy (y, (SHORT)(x + Params[0]));
  204.             }
  205.             else if (byte == 'D') {
  206.                wreadcur (&y, &x);
  207.                if (Params[0] == 0)
  208.                   Params[0] = 1;
  209.                wgotoxy (y, (SHORT)(x - Params[0]));
  210.             }
  211.             else if (byte == 'n') {
  212.                if (Params[0] == 6) {
  213.                   memcpy (RxBuffer, "\x1B[0;0h", 6);
  214.                   RxBytes = 6;
  215.                   RxPosition = 0;
  216.                }
  217.             }
  218.             else if (byte == 'f' || byte == 'H')
  219.                wgotoxy ((SHORT)(Params[0] - 1), (SHORT)(Params[1] - 1));
  220.             else if (byte == 'J' && Params[0] == 2)
  221.                wclear ();
  222.             else if (byte == 'K')
  223.                wclreol ();
  224.             Ansi = FALSE;
  225.          }
  226.          else if (byte == ';') {
  227.             Count++;
  228.             Params[Count] = 0;
  229.          }
  230.          else if (isdigit (byte)) {
  231.             Params[Count] *= 10;
  232.             Params[Count] += (USHORT)(byte - '0');
  233.          }
  234.          else
  235.             Ansi = FALSE;
  236.       }
  237.       else if (byte == CTRLL) {
  238.          wclear ();
  239.          videoupdate ();
  240.       }
  241.       else if (byte != ESC)
  242.          wputc (byte);
  243.    }
  244.  
  245.    Prec = byte;
  246.  
  247.    if ((++Counter % 64) == 0) {
  248.       videoupdate ();
  249.       Counter = 0;
  250.    }
  251. }
  252.  
  253. VOID TScreen::BufferBytes (UCHAR *bytes, USHORT len)
  254. {
  255.    while (len-- > 0)
  256.       BufferByte (*bytes++);
  257. }
  258.  
  259. USHORT TScreen::Carrier (VOID)
  260. {
  261.    return (Running);
  262. }
  263.  
  264. VOID TScreen::ClearOutbound (VOID)
  265. {
  266.    TxBytes = 0;
  267. }
  268.  
  269. VOID TScreen::ClearInbound (VOID)
  270. {
  271.    RxBytes = 0;
  272. }
  273.  
  274. USHORT TScreen::Initialize (VOID)
  275. {
  276.    USHORT RetVal = FALSE;
  277.  
  278.    RxBytes = 0;
  279.    RxPosition = 0;
  280.    Running = TRUE;
  281.  
  282. #if defined(__NT__)
  283.    AllocConsole ();
  284. #endif
  285.  
  286. #if defined(__OS2__) || defined(__NT__)
  287.    videoinit ();
  288. #endif
  289.    if ((wh = wopen (0, 0, (short)(24 - 1), 79, 5, LGREY|_BLACK, LGREY|_BLACK)) != -1) {
  290.       wopen (24, 0, 24, 79, 5, WHITE|_BLUE, WHITE|_BLUE);
  291.       wprintc (0, 22, WHITE|_BLUE, '│');
  292.       wprintc (0, 43, WHITE|_BLUE, '│');
  293.       wprintc (0, 58, WHITE|_BLUE, '│');
  294.       wprintc (0, 69, WHITE|_BLUE, '│');
  295.       wactiv (wh);
  296.       showcur ();
  297.       RetVal = TRUE;
  298.    }
  299.    videoupdate ();
  300.  
  301.    return (RetVal);
  302. }
  303.  
  304. UCHAR TScreen::ReadByte (VOID)
  305. {
  306.    UCHAR c;
  307.  
  308.    if (RxBytes == 0)
  309.       return ((UCHAR)getxch ());
  310.    else {
  311.       c = RxBuffer[RxPosition++];
  312.       if (--RxBytes == 0)
  313.          RxPosition = 0;
  314.       return (c);
  315.    }
  316. }
  317.  
  318. USHORT TScreen::ReadBytes (UCHAR *bytes, USHORT len)
  319. {
  320.    USHORT Max = 0;
  321.    USHORT c;
  322.  
  323.    if ((Max = len) > RxBytes)
  324.       Max = RxBytes;
  325.    if (Max > 0)
  326.       memcpy (bytes, &RxBuffer[RxPosition], Max);
  327.    RxPosition += Max;
  328.    if ((RxBytes -= Max) == 0)
  329.       RxPosition = 0;
  330.    while (kbhit ()) {
  331.       c = (USHORT)getxch ();
  332.       if (c == 0x4800) {
  333.          memcpy (&bytes[Max], "\x1B[A", 3);
  334.          Max += 3;
  335.       }
  336.       else if (c == 0x5000) {
  337.          memcpy (&bytes[Max], "\x1B[B", 3);
  338.          Max += 3;
  339.       }
  340.       else if (c == 0x4D00) {
  341.          memcpy (&bytes[Max], "\x1B[C", 3);
  342.          Max += 3;
  343.       }
  344.       else if (c == 0x4B00) {
  345.          memcpy (&bytes[Max], "\x1B[D", 3);
  346.          Max += 3;
  347.       }
  348.       else if (c == 0x4700) {
  349.          memcpy (&bytes[Max], "\x1B[H", 3);
  350.          Max += 3;
  351.       }
  352.       else if (c == 0x4F00) {
  353.          memcpy (&bytes[Max], "\x1B[K", 3);
  354.          Max += 3;
  355.       }
  356.       else if (c == 0x2300)      // Alt-H = Hangup
  357.          Running = FALSE;
  358.       else
  359.          bytes[Max++] = (UCHAR)c;
  360.    }
  361.  
  362.    return (Max);
  363. }
  364.  
  365. VOID TScreen::SendByte (UCHAR byte)
  366. {
  367.    BufferByte (byte);
  368.    videoupdate ();
  369. }
  370.  
  371. VOID TScreen::SendBytes (UCHAR *bytes, USHORT len)
  372. {
  373.    BufferBytes (bytes, len);
  374.    videoupdate ();
  375. }
  376.  
  377. VOID TScreen::UnbufferBytes (VOID)
  378. {
  379.    videoupdate ();
  380. }
  381.  
  382. VOID TScreen::SetName (PSZ name)
  383. {
  384.    CHAR Temp[48];
  385.  
  386.    sprintf (Temp, "%-20.20s", name);
  387.    prints (24, 1, WHITE|_BLUE, Temp);
  388.    videoupdate ();
  389. }
  390.  
  391. VOID TScreen::SetCity (PSZ name)
  392. {
  393.    CHAR Temp[48];
  394.  
  395.    sprintf (Temp, "%-18.18s", name);
  396.    prints (24, 24, WHITE|_BLUE, Temp);
  397.    videoupdate ();
  398. }
  399.  
  400. VOID TScreen::SetLevel (PSZ level)
  401. {
  402.    CHAR Temp[48];
  403.  
  404.    sprintf (Temp, "%-12.12s", level);
  405.    prints (24, 45, WHITE|_BLUE, Temp);
  406.    videoupdate ();
  407. }
  408.  
  409. VOID TScreen::SetTimeLeft (ULONG seconds)
  410. {
  411.    CHAR Temp[48];
  412.  
  413.    if (seconds >= 3600L)
  414.       sprintf (Temp, "%2d:%02d:%02d", seconds / 3600L, (seconds % 3600L) / 60L, (seconds % 3600L) % 60L);
  415.    else
  416.       sprintf (Temp, "  %2d:%02d", seconds / 60L, seconds % 60L);
  417.    prints (24, 60, WHITE|_BLUE, Temp);
  418.    videoupdate ();
  419. }
  420.  
  421. VOID TScreen::SetTime (ULONG seconds)
  422. {
  423.    CHAR Temp[48];
  424.    struct tm *ltm;
  425.  
  426.    ltm = localtime ((time_t *)&seconds);
  427.    sprintf (Temp, "%2d:%02d:%02d", ltm->tm_hour, ltm->tm_min, ltm->tm_sec);
  428.    prints (24, 71, WHITE|_BLUE, Temp);
  429.    videoupdate ();
  430. }
  431.  
  432.  
  433.