home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 515.lha / MiniTerm / MiniTerm.c < prev    next >
C/C++ Source or Header  |  1991-06-08  |  13KB  |  608 lines

  1. #include <exec/types.h>
  2. #include <exec/io.h>
  3. #include <exec/memory.h>
  4. #include <graphics/gfxbase.h>
  5. #include <intuition/intuition.h>
  6. #include <devices/serial.h>
  7. #include <devices/timer.h>
  8. #include "MiniTerm.h"
  9. #include "MiniTerm.i"
  10.  
  11. #define Buf_Size     1024
  12. #define Chat_Size      128
  13.  
  14. struct Screen *TermScreen = NULL;
  15. struct Window *TermWindow = NULL;
  16. struct Window *ChatWindow = NULL;
  17.  
  18. struct IOExtSer         *SerReadIO         = NULL;
  19. struct IOExtSer         *SerWriteIO     = NULL;
  20.  
  21. struct IOStdReq         *ConReadIO        = NULL;
  22. struct IOStdReq            *ConWriteIO      = NULL;
  23. struct IOStdReq            *ConWriteIO_Chat= NULL;
  24.  
  25. struct MsgPort             *TimerMP         = NULL;
  26. struct MsgPort             *SerReadMP         = NULL;
  27. struct MsgPort             *SerWriteMP     = NULL;
  28. struct MsgPort             *ConReadMP        = NULL;
  29. struct MsgPort             *ConWriteMP        = NULL;
  30. struct MsgPort            *ConWriteMP_Chat= NULL;
  31.  
  32. struct timerequest         *TimerIO        = NULL;
  33. struct timeval             *TimerVal        = NULL;
  34.  
  35. struct GfxBase *GfxBase = NULL;
  36. struct IntuitionBase *IntuitionBase = NULL;
  37.  
  38. ULONG WindowSig, WindowSig_Chat, SerReadSig, ConReadSig, TimerSig, AllSigs;
  39. BYTE TitleBar = 1, SerialOpen, ConsoleOpen, ConsoleOpen_Chat, TimerOpen;
  40. BYTE SerialOn, ConsoleOn, TimerOn, SerialByte, ConsoleByte, CursorOn;
  41. BYTE *Buffer, *ChatBuffer, ChatOn = 0, ChatCounter;
  42.  
  43. VOID _main(VOID)
  44. {
  45.     ULONG signal;
  46.     SHORT Actual;
  47.     BYTE SB, CB;
  48.     
  49.     OpenIntuition();
  50.     CreatePorts();
  51.     CreateIOs();
  52.     OpenMemory();
  53.     OpenDevices();
  54.     
  55.     AllSigs = ConReadSig | WindowSig | WindowSig_Chat | TimerSig;
  56.  
  57.     QueueConRead(&ConsoleByte);
  58.     
  59.     ConsoleOn = 1;
  60.     
  61.     FOREVER {
  62.         signal = Wait(AllSigs);
  63.         if(signal & WindowSig)
  64.             HandleIDCMP(0);
  65.         if(signal & WindowSig_Chat)
  66.             HandleIDCMP(1);
  67.         if(signal & ConReadSig)
  68.             if(CB = ConGetChar(&ConsoleByte)){
  69.                 if(!ChatOn){
  70.                     if(SerialOn)
  71.                         SerPutChar(CB);
  72.                     else
  73.                         ConPutChar(CB);
  74.                 }
  75.                 else{
  76.                     if(CB == '\b'){
  77.                         if(ChatCounter > 0){
  78.                             ChatCounter--;
  79.                             ChatPutString("\b \b");
  80.                         }
  81.                     }
  82.                     else if(CB == 24){
  83.                         ChatPutChar(12);
  84.                         ChatCounter = 0;
  85.                     }
  86.                     else if(CB == '\r'){
  87.                         ChatPutChar(12);
  88.                         ChatBuffer[ChatCounter++] = '\r';
  89.                         ChatBuffer[ChatCounter] = 0;
  90.                         if(SerialOn)
  91.                             SerPutString(ChatBuffer);
  92.                         else
  93.                             ConPutString(ChatBuffer);
  94.                         ChatCounter = 0;
  95.                     }
  96.                     else if(ChatCounter != Chat_Size){
  97.                         ChatPutChar(CB);
  98.                         ChatBuffer[ChatCounter++] = CB;
  99.                     }
  100.                 }    
  101.             }
  102.  
  103.         if(signal & SerReadSig){
  104.             if(SB = SerGetChar(&SerialByte)){
  105.                 ConPutChar(SB);
  106.                 if(Actual = CheckActual()){
  107.                     if(Actual > (Buf_Size-1))
  108.                         Actual = Buf_Size - 1;
  109.                     SerGetBuffer(Buffer, Actual);
  110.                     ConPutBuffer(Buffer, Actual);
  111.                 }
  112.             }
  113.             QueueSerRead(&SerialByte);
  114.         }
  115.     }
  116. }
  117.  
  118. VOID ChatPutChar(BYTE byte)
  119. {
  120.     ConWriteIO_Chat->io_Command = CMD_WRITE;
  121.     ConWriteIO_Chat->io_Data = (APTR) &byte;
  122.     ConWriteIO_Chat->io_Length = 1;
  123.      DoIO(ConWriteIO_Chat);
  124. }
  125.  
  126. VOID ChatPutString(BYTE *string)
  127. {
  128.     ConWriteIO_Chat->io_Command = CMD_WRITE;
  129.     ConWriteIO_Chat->io_Data = (APTR) string;
  130.     ConWriteIO_Chat->io_Length = -1;
  131.     DoIO(ConWriteIO_Chat);
  132. }
  133.  
  134. VOID ToggleCursor(BYTE on)
  135. {
  136.     if(on == 1){
  137.         ConPutString(" p");
  138.         CursorOn = 1;
  139.     }
  140.     else if(on == 0){
  141.         ConPutString(" p");
  142.         CursorOn = 0;
  143.     }
  144.     else if(on == 2){
  145.         if(CursorOn == 1)
  146.             ToggleCursor(0);
  147.         else
  148.             ToggleCursor(1);
  149.     }
  150. }
  151.  
  152. VOID OpenIntuition(VOID)
  153. {
  154.     if(!(IntuitionBase = (struct IntuitionBase *) OpenLibrary("intuition.library",33)))
  155.         CleanExit();
  156.     
  157.     if(!(GfxBase = (struct GfxBase *) OpenLibrary("graphics.library",33)))
  158.         CleanExit();
  159.         
  160.     TermNewScreen.Width = GfxBase->NormalDisplayColumns;
  161.     TermNewScreen.Height = GfxBase->NormalDisplayRows;
  162.     TermNewWindow.Width = TermNewScreen.Width;
  163.     TermNewWindow.Height = TermNewScreen.Height;
  164.     
  165.     CloseLibrary(GfxBase);
  166.     
  167.     if(!(TermScreen = (struct Screen *) OpenScreen(&TermNewScreen)))
  168.         CleanExit();
  169.     
  170.     TermNewWindow.Screen = TermScreen;
  171.     
  172.     TermNewWindow_Chat.Screen = TermScreen;
  173.     TermNewWindow_Chat.Width = TermScreen->Width;
  174.     TermNewWindow_Chat.Height = 21;
  175.     TermNewWindow_Chat.TopEdge = TermScreen->Height-21;
  176.     
  177.     if(!(ChatWindow = (struct Window *) OpenWindow(&TermNewWindow_Chat)))
  178.         CleanExit();
  179.     
  180.     if(!(TermWindow = (struct Window *) OpenWindow(&TermNewWindow)))
  181.         CleanExit();
  182.         
  183.     SetMenuStrip(TermWindow, &TermMenu);
  184.     
  185.     WindowSig = 1 << TermWindow->UserPort->mp_SigBit;
  186.     WindowSig_Chat = 1 << ChatWindow->UserPort->mp_SigBit;
  187. }
  188.  
  189. VOID CreatePorts(VOID)
  190. {
  191.     if(!(TimerMP = (struct MsgPort *) CreatePort(0,0)))
  192.         CleanExit();
  193.     
  194.     if(!(ConReadMP = (struct MsgPort *) CreatePort("WK_Console_Read_I",0)))
  195.         CleanExit();
  196.     
  197.     if(!(ConWriteMP = (struct MsgPort *) CreatePort("WK_Console_Write_I",0)))
  198.         CleanExit();
  199.     
  200.     if(!(ConWriteMP_Chat = (struct MsgPort *) CreatePort("WK_Console_Write_II",0)))
  201.         CleanExit();
  202. }    
  203.  
  204. VOID CreateIOs(VOID)
  205. {
  206.     if(!(TimerIO = (struct timerequest *) CreateExtIO(TimerMP, sizeof(struct timerequest))))
  207.         CleanExit();
  208.     
  209.     if(!(ConReadIO = (struct IOStdReq *) CreateExtIO(ConReadMP, (LONG) sizeof(struct IOStdReq))))
  210.         CleanExit();
  211.     
  212.     if(!(ConWriteIO = (struct IOStdReq *) CreateExtIO(ConWriteMP, (LONG) sizeof(struct IOStdReq))))
  213.         CleanExit();
  214.  
  215.     if(!(ConWriteIO_Chat = (struct IOStdReq *) CreateExtIO(ConWriteMP_Chat, (LONG) sizeof(struct IOStdReq))))
  216.         CleanExit();
  217. }
  218.  
  219. VOID OpenDevices(VOID)
  220. {
  221.     if(OpenConsole())
  222.         CleanExit();
  223.                 
  224.     if(OpenTimer())
  225.         CleanExit();    
  226. }
  227.  
  228. VOID OpenMemory(VOID)
  229. {
  230.     if(!(TimerVal = (struct timeval *) AllocMem(sizeof(struct timeval), MEMF_PUBLIC | MEMF_CLEAR)))
  231.         CleanExit();
  232.     
  233.     if(!(Buffer = (BYTE *) AllocMem(Buf_Size, MEMF_PUBLIC)))
  234.         CleanExit();
  235.     
  236.     if(!(ChatBuffer = (BYTE *) AllocMem(Chat_Size, MEMF_PUBLIC)))
  237.         CleanExit();
  238. }
  239.  
  240. VOID CleanExit(VOID)
  241. {    
  242.     if(SerialOpen)                    CloseSerial();
  243.     if(ConsoleOpen)                    CloseConsole();
  244.     if(TimerOpen)                    CloseTimer();
  245.     
  246.     if(ConReadIO)                    DeleteExtIO(ConReadIO);
  247.     if(ConWriteIO)                    DeleteExtIO(ConWriteIO);
  248.     if(ConWriteIO_Chat)                DeleteExtIO(ConWriteIO_Chat);
  249.     if(SerReadIO)                    DeleteExtIO(SerReadIO);
  250.     if(SerWriteIO)                    DeleteExtIO(SerWriteIO);
  251.     
  252.     if(TimerIO)                        DeleteExtIO(TimerIO);
  253.     
  254.     if(ConReadMP)                    DeletePort(ConReadMP);
  255.     if(ConWriteMP)                    DeletePort(ConWriteMP);
  256.     if(ConWriteMP_Chat)                DeletePort(ConWriteMP_Chat);    
  257.     if(SerReadMP)                    DeletePort(SerReadMP);
  258.     if(SerWriteMP)                    DeletePort(SerWriteMP);
  259.  
  260.     if(TimerMP)                        DeletePort(TimerMP);
  261.     if(TimerVal)                    FreeMem(TimerVal, sizeof(struct timeval));
  262.     if(Buffer)                        FreeMem(Buffer, Buf_Size);
  263.     if(ChatBuffer)                    FreeMem(ChatBuffer, Chat_Size);
  264.     if(TermWindow){
  265.         if(TermWindow->MenuStrip)    ClearMenuStrip(TermWindow);
  266.                                     CloseWindow(TermWindow);
  267.     }
  268.     if(ChatWindow)                    CloseWindow(ChatWindow);
  269.     if(TermScreen)                    CloseScreen(TermScreen);
  270.     if(IntuitionBase)                CloseLibrary(IntuitionBase);
  271.     exit(0);
  272. }
  273.  
  274. VOID HandleIDCMP(BYTE wnd)
  275. {
  276.     struct IntuiMessage *Message;
  277.     ULONG Class;
  278.     USHORT Code;
  279.     
  280.     BYTE MenuNum, ItemNum;
  281.     
  282.     while(Message = (struct IntuiMessage *) ((wnd == 0) ? GetMsg(TermWindow->UserPort) : GetMsg(ChatWindow->UserPort))){
  283.         Class = Message->Class;
  284.         Code = Message->Code;
  285.         ReplyMsg(Message);
  286.         
  287.         switch(Class){
  288.             case ACTIVEWINDOW:
  289.                 ActivateWindow(TermWindow);
  290.                 break;
  291.  
  292.             case MENUPICK:
  293.                 MenuNum = MENUNUM(Code);
  294.                 ItemNum = ITEMNUM(Code);
  295.             
  296.                 switch(ItemNum){
  297.                     case 0:
  298.                         if(TitleBar){
  299.                             ShowTitle(TermScreen, FALSE);
  300.                             TitleBar = 0;
  301.                         }
  302.                         else{
  303.                             ShowTitle(TermScreen, TRUE);
  304.                             TitleBar = 1;
  305.                         }
  306.                         break;
  307.                     case 1:
  308.                         if(SerialOpen){
  309.                             CloseSerial();
  310.                             SerialOpen = 0;
  311.                             ClearMenuStrip(TermWindow);
  312.                             ProjectText[1].IText = "Open Serial";
  313.                             SetMenuStrip(TermWindow, &TermMenu);
  314.                         }
  315.                         else{
  316.                             if(!(OpenSerial())){
  317.                                 SerialOpen = 1;
  318.                                 ClearMenuStrip(TermWindow);
  319.                                 ProjectText[1].IText = "Close Serial";
  320.                                 SetMenuStrip(TermWindow, &TermMenu);
  321.                             }
  322.                             else
  323.                                 ConPutString("Error opening the serial port.");
  324.                         }
  325.                         break;
  326.                     case 2:
  327.                         if(ChatOn){
  328.                             ChatOn = 0;
  329.                             ClearMenuStrip(TermWindow);
  330.                             ProjectText[2].IText = "Open Chat";
  331.                             SetMenuStrip(TermWindow, &TermMenu);
  332.                             SizeWindow(TermWindow, 0, 22);
  333.                         }
  334.                         else {
  335.                             ChatOn = 1;
  336.                             ClearMenuStrip(TermWindow);
  337.                             ProjectText[2].IText = "Close Chat";
  338.                             SetMenuStrip(TermWindow, &TermMenu);
  339.                             SizeWindow(TermWindow, 0, -22);
  340.                         }
  341.                             break;
  342.                     case 3:
  343.                         ChatPutString("");
  344.                         ConPutString("");
  345.                         break;
  346.                     case 4:
  347.                         CleanExit();
  348.                         break;
  349.                 }
  350.         }
  351.     }
  352. }    
  353.     
  354. VOID CloseConsole(VOID)
  355. {
  356.     if(ConsoleOn){
  357.         if(!(CheckIO(ConReadIO)))    AbortIO(ConReadIO);
  358.         WaitIO(ConReadIO);
  359.     }
  360.     if(ConsoleOpen_Chat)    CloseDevice(ConWriteIO_Chat);
  361.     
  362.     CloseDevice(ConWriteIO);
  363. }
  364.  
  365. VOID CloseSerial(VOID)
  366. {
  367.     
  368.     if(SerialOn){
  369.         if(!(CheckIO(SerReadIO))) AbortIO(SerReadIO);
  370.         WaitIO(SerReadIO);
  371.     }
  372.     SerialOn = 0;
  373.     AllSigs &= ~SerReadSig;
  374.     ClearSerial();
  375.     CloseDevice(SerWriteIO);
  376.     
  377.     if(SerReadIO){
  378.         DeleteExtIO(SerReadIO);
  379.         SerReadIO = NULL;
  380.     }
  381.     if(SerWriteIO){
  382.         DeleteExtIO(SerWriteIO);
  383.         SerWriteIO = NULL;
  384.     }
  385.     if(SerReadMP){
  386.         DeletePort(SerReadMP);
  387.         SerReadMP = NULL;
  388.     }
  389.     if(SerWriteMP){
  390.         DeletePort(SerWriteMP);
  391.         SerWriteMP = NULL;
  392.     }
  393. }
  394.  
  395. VOID CloseTimer(VOID)
  396. {
  397.     if(TimerOn){
  398.     if(!(CheckIO(TimerIO)))    AbortIO(TimerIO);
  399.         WaitIO(TimerIO);
  400.     }
  401.     CloseDevice(TimerIO);
  402. }
  403.  
  404. ULONG CheckActual(void)
  405. {
  406.     SerReadIO->IOSer.io_Command = SDCMD_QUERY;
  407.     DoIO(SerReadIO);
  408.     
  409.     return SerReadIO->IOSer.io_Actual;
  410. }
  411.  
  412. VOID CXBRK(VOID){return;}
  413.  
  414. BYTE CheckCD(VOID)
  415. {
  416.     SerWriteIO->IOSer.io_Command = SDCMD_QUERY;
  417.     DoIO(SerWriteIO);
  418.     if(SerWriteIO->io_Status & 040)
  419.         return 0;
  420.     else
  421.         return 1;
  422. }
  423.  
  424. BYTE CheckCTS(VOID)
  425. {
  426.     SerWriteIO->IOSer.io_Command = SDCMD_QUERY;
  427.     DoIO(SerWriteIO);
  428.     if(SerWriteIO->io_Status & 020)
  429.         return 0;
  430.     else
  431.         return 1;
  432. }
  433.  
  434. VOID ClearSerial(VOID)
  435. {
  436.     SerReadIO->IOSer.io_Command = CMD_CLEAR;
  437.     DoIO(SerReadIO);
  438. }
  439.  
  440. BYTE OpenConsole(VOID)    
  441. {
  442.     BYTE error;
  443.     
  444.     ConWriteIO->io_Data = (APTR) TermWindow;
  445.     ConWriteIO->io_Length = sizeof(struct Window);
  446.     
  447.     error = OpenDevice("console.device", 0, ConWriteIO, 0);
  448.     
  449.     ConReadIO->io_Device = ConWriteIO->io_Device;
  450.     ConReadIO->io_Unit   = ConWriteIO->io_Unit;
  451.     if(!error){
  452.         ConsoleOpen = 1;
  453.         ConReadSig = 1 << ConReadMP->mp_SigBit;
  454.         
  455.         ConWriteIO_Chat->io_Data = (APTR) ChatWindow;
  456.         ConWriteIO_Chat->io_Length = sizeof(struct Window);
  457.         error = OpenDevice("console.device", 0, ConWriteIO_Chat, 0);
  458.         if(!error)
  459.             ConsoleOpen_Chat = 1;
  460.     }
  461.     return error;
  462. }
  463.  
  464. BYTE OpenSerial(VOID)
  465. {
  466.     BYTE error;
  467.  
  468.     if(!(SerReadMP = (struct MsgPort *) CreatePort(0,0)))
  469.         CleanExit();
  470.     
  471.     if(!(SerWriteMP = (struct MsgPort *) CreatePort(0,0)))
  472.         CleanExit();
  473.     
  474.     if(!(SerReadIO = (struct IOExtSer *) CreateExtIO(SerReadMP, (LONG) sizeof(struct IOExtSer))))
  475.         CleanExit();
  476.     
  477.     if(!(SerWriteIO = (struct IOExtSer *) CreateExtIO(SerWriteMP, (LONG) sizeof(struct IOExtSer))))
  478.         CleanExit();
  479.         
  480.     SerWriteIO->io_SerFlags = SERF_SHARED;
  481.     
  482.     error = OpenDevice("serial.device", 0, SerWriteIO, 0);
  483.     
  484.     SerReadIO->IOSer.io_Device = SerWriteIO->IOSer.io_Device;
  485.     SerReadIO->IOSer.io_Unit   = SerWriteIO->IOSer.io_Unit;
  486.     
  487.     SerReadIO->io_SerFlags = SERF_SHARED;
  488.     
  489.     if(!error){
  490.         SerialOpen = 1;
  491.         SerReadSig = 1 << SerReadMP->mp_SigBit;
  492.         SerialOn = 1;
  493.         ClearSerial();
  494.         QueueSerRead(&SerialByte);
  495.         AllSigs |= SerReadSig;
  496.     }
  497.     
  498.     return error;
  499. }
  500.  
  501. BYTE OpenTimer(VOID)
  502. {
  503.     BYTE error;
  504.     
  505.     error = OpenDevice(TIMERNAME, UNIT_VBLANK, (struct IORequest *) TimerIO, 0);
  506.     if(!error){
  507.         TimerOpen = 1;
  508.         TimerSig  = 1 << TimerMP->mp_SigBit;
  509.     }
  510.     TimerVal->tv_secs = 1;
  511.     TimerVal->tv_micro = 0;
  512.     TimerIO->tr_node.io_Command = TR_ADDREQUEST;
  513.     TimerIO->tr_time = *TimerVal;
  514.     
  515.     return error;
  516. }
  517.  
  518. VOID QueueTimer(SHORT secs)
  519. {
  520.     TimerVal->tv_secs = secs;
  521.     SendIO((struct IORequest *) TimerIO);
  522. }
  523.  
  524. VOID ConPutString(BYTE *string)
  525. {
  526.     ConWriteIO->io_Command = CMD_WRITE;
  527.     ConWriteIO->io_Data = (APTR) string;
  528.     ConWriteIO->io_Length = -1;
  529.     DoIO(ConWriteIO);
  530. }
  531.  
  532. VOID ConPutBuffer(BYTE *buffer, SHORT Length)
  533. {
  534.     ConWriteIO->io_Command = CMD_WRITE;
  535.     ConWriteIO->io_Data = (APTR) buffer;
  536.     ConWriteIO->io_Length = Length;
  537.     DoIO(ConWriteIO);
  538. }
  539.  
  540. VOID ConPutChar(BYTE byte)
  541. {
  542.     ConWriteIO->io_Command = CMD_WRITE;
  543.     ConWriteIO->io_Data = (APTR) &byte;
  544.     ConWriteIO->io_Length = 1;
  545.     DoIO(ConWriteIO);
  546. }
  547.  
  548. VOID SerPutChar(BYTE byte)
  549. {
  550.     SerWriteIO->IOSer.io_Command = CMD_WRITE;
  551.     SerWriteIO->IOSer.io_Data = (APTR) &byte;
  552.     SerWriteIO->IOSer.io_Length = 1;
  553.     DoIO(SerWriteIO);
  554. }
  555.  
  556. VOID SerPutString(BYTE *string)
  557. {
  558.     SerWriteIO->IOSer.io_Command = CMD_WRITE;
  559.     SerWriteIO->IOSer.io_Data = (APTR) string;
  560.     SerWriteIO->IOSer.io_Length = -1;
  561.     DoIO(SerWriteIO);
  562. }
  563.  
  564. VOID QueueConRead(BYTE *WhereTo)
  565. {
  566.     ConReadIO->io_Command = CMD_READ;
  567.     ConReadIO->io_Data = (APTR) WhereTo;
  568.     ConReadIO->io_Length = 1;
  569.     SendIO(ConReadIO);
  570. }
  571.  
  572. VOID QueueSerRead(BYTE *WhereTo)
  573. {
  574.     SerReadIO->IOSer.io_Command = CMD_READ;
  575.     SerReadIO->IOSer.io_Data = (APTR) WhereTo;
  576.     SerReadIO->IOSer.io_Length = 1;
  577.     SendIO(SerReadIO);
  578. }
  579.  
  580. VOID SerGetBuffer(BYTE *buffer, SHORT Length)
  581. {
  582.     SerReadIO->IOSer.io_Command = CMD_READ;
  583.     SerReadIO->IOSer.io_Data = (APTR) buffer;
  584.     SerReadIO->IOSer.io_Length = Length;
  585.     DoIO(SerReadIO);
  586. }
  587.  
  588. BYTE ConGetChar(BYTE *whereto)
  589. {
  590.     BYTE temp;
  591.     struct IOStdReq *readreq;
  592.     
  593.     if(!(readreq = (struct IOStdReq *) GetMsg(ConReadMP))) return 0;
  594.     temp = *whereto;
  595.     QueueConRead(whereto);
  596.     return(temp);
  597. }
  598.  
  599. BYTE SerGetChar(BYTE *whereto)
  600. {
  601.     BYTE temp;
  602.     struct IOStdSer *readreq;
  603.     
  604.     if(!(readreq = (struct IOStdReq *) GetMsg(SerReadMP))) return 0;
  605.     temp = *whereto;
  606.     return(temp);
  607. }
  608.