home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR24 / OTHL_100.ZIP / GIO-100.ZIP / GIO-2.C < prev    next >
Text File  |  1992-11-27  |  6KB  |  220 lines

  1. ///////////////////////////////////////////////////////////////////////////
  2. //                                                                       //
  3. // GIO-2.C  Gern's IO example 2                                          //
  4. //                                                                       //
  5. // Link with GIO.LIB                                                     //
  6. ///////////////////////////////////////////////////////////////////////////
  7.  
  8. #define INCL_WIN
  9. #define INCL_GPI
  10. #define INCL_VIO
  11. #define INCL_AVIO
  12. #define INCL_DOS
  13. #define INCL_DOSDEVIOCTL
  14. #include <os2.h>
  15.  
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <process.h>
  20. #include "gio.h"
  21.  
  22. MRESULT EXPENTRY ClientWndProc(HWND, ULONG, MPARAM, MPARAM);
  23. VOID CommIn(PVOID);
  24.  
  25. HAB   Hab;
  26. HMTX  hmtxCommIn;
  27. TID   TidCommIn;
  28. HWND  HwndClient;
  29.  
  30. BOOL  KeepLooping = TRUE;
  31.  
  32. #define COMTHREAD 1
  33.  
  34. int main(void)
  35. {
  36.     static CHAR  SzClientClass[] = "GIO2";
  37.     static ULONG FlFrameFlags    = FCF_TITLEBAR      | FCF_SYSMENU |
  38.                                    FCF_SIZEBORDER    | FCF_MINMAX  |
  39.                                    FCF_SHELLPOSITION | FCF_TASKLIST;
  40.  
  41.     HMQ  Hmq;
  42.     HWND HwndFrame;
  43.     QMSG Qmsg;
  44.     PUSHORT Unused;
  45.  
  46.     Hab = WinInitialize(0);
  47.     Hmq = WinCreateMsgQueue(Hab, 0);
  48.  
  49.     DosCreateMutexSem(NULL, &hmtxCommIn, 0, TRUE);
  50.  
  51.     TidCommIn = _beginthread(CommIn, NULL, 3072, &Unused);
  52.  
  53.     // Open Com1 at 19200,N,8,1 with buffers 128 bytes in size.
  54.     if (ComOpen(NULL, 1, 19200, 'N', 8, 1, 128, 128, 0, FALSE))
  55.     {
  56.         DosBeep(1000, 250);         // ComOpen Failed.. Beep and exit
  57.         DosExit(EXIT_PROCESS, 1);
  58.     }
  59.  
  60.     WinRegisterClass(   Hab,            // Anchor Block Handle
  61.                         SzClientClass,  // Name of Class Being Registered
  62.                         ClientWndProc,  // Window Procedure For Class
  63.                         CS_SIZEREDRAW,  // Class Style
  64.                         0);             // Extra Bytes To Reserve
  65.  
  66.     HwndFrame = WinCreateStdWindow(
  67.                         HWND_DESKTOP,   // Parent Window Handle
  68.                         WS_VISIBLE,     // Style of Frame Window
  69.                         &FlFrameFlags,  // Pointer To Control Data
  70.                         SzClientClass,  // Client Window Class Name
  71.                         NULL,           // Title Bar Text
  72.                         0L,             // Style of Client Window
  73.                         0,              // Module Handle For Resources
  74.                         0,              // ID of Resources
  75.                 (PHWND) &HwndClient);   // Pointer To Client Window Handle
  76.  
  77.     WinSendMsg(HwndFrame,
  78.                WM_SETICON,
  79.                (MPARAM) WinQuerySysPointer(HWND_DESKTOP, SPTR_APPICON, FALSE),
  80.                NULL);
  81.  
  82.     DosReleaseMutexSem(hmtxCommIn);
  83.  
  84.     while (WinGetMsg(Hab, &Qmsg, NULL, 0, 0))
  85.         WinDispatchMsg(Hab, &Qmsg);
  86.  
  87.     WinDestroyWindow(HwndFrame);
  88.  
  89.     WinDestroyMsgQueue(Hmq);
  90.  
  91.     WinTerminate(Hab);
  92.  
  93.     DosSleep(50L);
  94.  
  95.     ComClose(FALSE);
  96.  
  97.     DosSleep(200L);
  98.  
  99.     DosCloseMutexSem(hmtxCommIn);
  100.  
  101.     return(0);
  102. }
  103.  
  104. static HVPS  Hvps;
  105.  
  106. MRESULT EXPENTRY ClientWndProc(HWND Hwnd, ULONG Msg, MPARAM Mp1, MPARAM Mp2)
  107. {
  108.     HDC          Hdc;
  109.     static HPS   Hps;
  110.     static SHORT Rows, Cols;
  111.     static LONG  WinRows, WinCols;
  112.     RECTL        Rect;
  113.  
  114.     switch (Msg)
  115.     {
  116.         case WM_CREATE:
  117.             Hdc = WinOpenWindowDC(Hwnd);
  118.  
  119.             VioCreatePS(&Hvps, 25, 80, FORMAT_CGA, 1, 0);
  120.             VioAssociate(Hdc, Hvps);
  121.  
  122.             VioWrtTTY("\x1B[2J", 4, Hvps);
  123.  
  124.             VioGetDeviceCellSize(&Rows, &Cols, Hvps);
  125.  
  126.             if (! WinSetWindowPos(Hwnd, 0, 0, 0, 300, 200, SWP_ACTIVATE | SWP_SIZE))
  127.                 DosBeep(1200, 200);
  128.  
  129.             return 0;
  130.  
  131.         case WM_CHAR:
  132.             if (CHARMSG(&Msg)->fs & KC_CHAR)
  133.             {
  134.                 ComTXChar((BYTE) CHARMSG(&Msg)->chr);
  135.             }
  136.             else if (CHARMSG(&Msg)->fs & KC_VIRTUALKEY)
  137.             {
  138.                 switch (CHARMSG(&Msg)->vkey)
  139.                 {
  140.                     case VK_ESC:
  141.                         ComTXChar(27);
  142.                         break;
  143.                     case VK_ALT:
  144.                         switch (CHARMSG(&Msg)->chr)
  145.                         {
  146.                             case 'C':
  147.                                 VioWrtTTY("\x1B[2J", 4, Hvps);
  148.                                 break;
  149.                         }
  150.                 }
  151.             }
  152.  
  153.             return 0;
  154.  
  155.         case WM_SIZE:
  156.             return (WinDefAVioWindowProc(Hwnd, Msg, (ULONG) Mp1, (ULONG) Mp2));
  157.  
  158.         case WM_PAINT:
  159.             Hps = WinBeginPaint(Hwnd, NULL, NULL);
  160.  
  161.             WinQueryWindowRect(Hwnd, &Rect);
  162.  
  163.             WinRows = Rect.yTop;
  164.             WinCols = Rect.xRight;
  165.  
  166.             WinFillRect(Hps, &Rect, CLR_BLACK);
  167.  
  168.             VioShowBuf(0, 2 * 25 * 80, Hvps);
  169.  
  170.             WinEndPaint(Hps);
  171.  
  172.             return 0;
  173.  
  174.         case WM_DESTROY:
  175.             KeepLooping = FALSE;
  176.  
  177.             VioAssociate(NULL, Hvps);
  178.  
  179.             VioDestroyPS(Hvps);
  180.  
  181.             return 0;
  182.     }
  183.  
  184.     return (WinDefWindowProc(Hwnd, Msg, Mp1, Mp2));
  185. }
  186.  
  187. VOID CommIn(PVOID Unused)
  188. {
  189.     PVOID   Temp;
  190.     BYTE    TrueChar;
  191.     SHORT   InChar;
  192.  
  193.     Temp = Unused;
  194.  
  195.     DosRequestMutexSem(hmtxCommIn, SEM_INDEFINITE_WAIT);
  196.  
  197.     do
  198.     {
  199.         if (ComRXCharWaiting())
  200.         {
  201.             ComRXChar(&InChar);
  202.  
  203.             if (InChar == 12)   // FormFeed / Clear Screen
  204.                 VioWrtTTY("\x1B[2J", 4, Hvps);
  205.             else
  206.             {
  207.                 TrueChar = (BYTE) InChar;
  208.                 VioWrtTTY(&TrueChar, 1, Hvps);
  209.             }
  210.         }
  211.         else
  212.             DosSleep(1L);
  213.  
  214.     } while (KeepLooping);
  215.  
  216.     DosReleaseMutexSem(hmtxCommIn);
  217.  
  218.     _endthread();
  219. }
  220.