home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 292.lha / TinyTerm / term.c < prev    next >
C/C++ Source or Header  |  1992-09-02  |  6KB  |  280 lines

  1. #include "term.h"
  2.  
  3. struct NewScreen ns = 
  4. {
  5.     0,0,640,200,2,
  6.     0,1,
  7.     HIRES,
  8.     CUSTOMSCREEN,
  9.     NULL,
  10.     "Term Screen",
  11.     NULL, NULL
  12. };
  13.     
  14. struct NewWindow nw = 
  15. {
  16.     0,0,640,200,
  17.     0,1,
  18.     MENUPICK|VANILLAKEY|REQSET|GADGETUP|GADGETDOWN,
  19.     ACTIVATE|BORDERLESS|NOCAREREFRESH,
  20.     NULL,
  21.     NULL,
  22.     NULL,
  23.     NULL,
  24.     NULL,
  25.     100,50,
  26.     640,200,
  27.     CUSTOMSCREEN
  28. };
  29.     
  30. struct MsgPort  *WritePort, *ReadPort;
  31. struct IOExtSer *WriteReq,  *ReadReq;
  32. struct timerequest  TimerIO;
  33. struct Window *w;
  34. struct Screen *s;
  35. struct RastPort *rp;
  36. struct IntuitionBase *IntuitionBase;
  37. struct GfxBase *GfxBase;    
  38. ULONG  ReadSignal, ScreenSignal, TimeSignal;
  39. UBYTE  ReadData[2000];
  40. USHORT BottomLine = (200 - 2);
  41. USHORT Col = 0;
  42. USHORT HDX = 0;
  43. UBYTE  SPC = ' ';
  44.  
  45. void CleanUp(char *msg)
  46. {
  47.     if (msg)
  48.         printf("%s\n", msg);
  49.         
  50.         CloseDevice((struct IORequest *)&TimerIO);
  51.        DeletePort(TimerIO.tr_node.io_Message.mn_ReplyPort);
  52.     
  53.     if (WriteReq)
  54.     {
  55.         CloseDevice((struct IORequest *)WriteReq);
  56.         DeleteExtIO((struct IORequest *)WriteReq);
  57.     }
  58.     if (WritePort)
  59.         DeletePort(WritePort);
  60.         
  61.     if (ReadReq)
  62.     {
  63.         CloseDevice((struct IORequest *)ReadReq);
  64.         DeleteExtIO((struct IORequest *)ReadReq);
  65.     }
  66.     if (ReadPort)
  67.         DeletePort(ReadPort);
  68.         
  69.     if (w)
  70.     {
  71.         ClearMenuStrip(w);
  72.         CloseWindow(w);
  73.     }
  74.     if (s)
  75.         CloseScreen(s);
  76.     exit(0);
  77. }
  78.  
  79. void WriteSerial(char *Msg, int cMsg)
  80. {
  81.     WriteReq->IOSer.io_Data   = (APTR)Msg;
  82.     WriteReq->IOSer.io_Length = cMsg;
  83.     DoIO((struct IORequest *)WriteReq);
  84. }
  85.  
  86. void ScrollUp(void)
  87. {
  88.     ScrollRaster(rp, 0, 8, 0, 0, 639, 199);
  89.     Move(rp, 0, BottomLine);
  90.     Col = 0;
  91. }
  92.  
  93. void Print(char c)
  94. {
  95.     c &= 0x7f;
  96.     switch (c)
  97.     {
  98.         case '\b':
  99.         if (Col)
  100.         {
  101.             Col--;
  102.             Move(rp, Col * 8, BottomLine);
  103.             Text(rp, &SPC, 1);
  104.             Move(rp, Col * 8, BottomLine);
  105.         }
  106.         break;
  107.         
  108.         case '\r':
  109.         Col = 0;
  110.         Move(rp, 0, BottomLine);
  111.         break;
  112.     
  113.         case '\n':
  114.         Col = 0;
  115.         ScrollUp();
  116.         break;
  117.         
  118.         default:
  119.         Text(rp, &c, 1);
  120.         if (++Col > 79)
  121.             ScrollUp();
  122.         break;
  123.     }
  124. }
  125.  
  126. void Prints(char *Msg)
  127. {
  128.     while (*Msg)
  129.         Print(*Msg++);
  130. }
  131.  
  132.  
  133. void ProcessMenu(USHORT code)
  134. {
  135.     switch (MENUNUM(code))
  136.     {
  137.         case PROJECTMENU:
  138.         if (ITEMNUM(code) == PROJECTQUIT)
  139.             CleanUp(0);
  140.         else if (ITEMNUM(code) == PROJECTCONFIG)
  141.             Config(w);
  142.         break;
  143.         
  144.         case TRANSFERMENU:
  145.         if (ITEMNUM(code) == TRANSFERDOWN)
  146.             DownLoad();
  147.         if (ITEMNUM(code) == TRANSFERUP)
  148.             UpLoad();
  149.         break;
  150.     }
  151. }
  152.  
  153. void main(void)
  154. {
  155.     struct IntuiMessage *message;
  156.     ULONG  class;
  157.     USHORT code;
  158.     UBYTE  c;
  159.  
  160.        if (OpenDevice( TIMERNAME
  161.                   , (long)UNIT_VBLANK
  162.                   , (struct IORequest *)&TimerIO
  163.               , 0L
  164.               ) != 0)
  165.     {
  166.         printf("Cannot open timer device\n");
  167.         return;
  168.     }
  169.        TimerIO.tr_node.io_Message.mn_ReplyPort = CreatePort("timer", 0);
  170.     TimeSignal = 1L << 
  171.          TimerIO.tr_node.io_Message.mn_ReplyPort->mp_SigBit;
  172.         
  173.     /****************************************************/
  174.         
  175.     WritePort = CreatePort("SerWrite", 0);
  176.     if (WritePort == NULL)
  177.         exit(100);
  178.     WriteReq = (struct IOExtSer *)CreateExtIO(WritePort,
  179.                                    sizeof(struct IOExtSer));
  180.     if (WriteReq == NULL)
  181.         CleanUp("Cannot open Write Request");
  182.     WriteReq->io_SerFlags = SERF_SHARED | SERF_XDISABLED;
  183.     if (OpenDevice( SERIALNAME
  184.                   , 0
  185.               , (struct IORequest *)WriteReq
  186.               , 0
  187.               ))
  188.         CleanUp("Cannot Open serial device for Write");
  189.     WriteReq->IOSer.io_Command = CMD_WRITE;
  190.     WriteReq->io_SerFlags = SERF_SHARED|SERF_XDISABLED;
  191.         
  192.     /****************************************************/
  193.         
  194.     ReadPort = CreatePort("SerRead", 0);
  195.     if (ReadPort == NULL)
  196.         exit(100);
  197.     ReadReq = (struct IOExtSer *)CreateExtIO(ReadPort,
  198.                                    sizeof(struct IOExtSer));
  199.     if (ReadReq == NULL)
  200.         CleanUp("Cannot open Read Request");
  201.     ReadReq->io_SerFlags = SERF_SHARED | SERF_XDISABLED;
  202.     if (OpenDevice( SERIALNAME
  203.                   , 0
  204.               , (struct IORequest *)ReadReq
  205.               , 0
  206.               ))
  207.         CleanUp("Cannot Open serial device for Read");
  208.     ReadSignal = 1L << ReadPort->mp_SigBit;
  209.         
  210.         ReadReq->io_SerFlags       = SERF_SHARED | SERF_XDISABLED;
  211.         ReadReq->IOSer.io_Length   = 1;
  212.         ReadReq->IOSer.io_Data     = (APTR) &ReadData[0];
  213.         ReadReq->io_Baud           = 1200;
  214.         ReadReq->io_ReadLen        = 8;
  215.         ReadReq->io_WriteLen       = 8;
  216.         ReadReq->io_CtlChar        = 0x11130000L;
  217.         ReadReq->io_RBufLen        = 1024;
  218.         ReadReq->IOSer.io_Command  = SDCMD_SETPARAMS;
  219.  
  220.         DoIO((struct IORequest *)ReadReq);
  221.         ReadReq->IOSer.io_Command  = CMD_READ;
  222.     SendIO((struct IORequest *)ReadReq);
  223.     
  224.     /****************************************************/
  225.         
  226.     GfxBase = OpenLibrary("graphics.library",0);
  227.     if (GfxBase == NULL)
  228.         CleanUp("Cannot open graphics lib");
  229.     IntuitionBase = OpenLibrary("intuition.library",0);
  230.     if (IntuitionBase == NULL)
  231.         CleanUp("Cannot open intuition lib");
  232.         
  233.     s = OpenScreen(&ns);
  234.     if (s == NULL)
  235.         CleanUp("Cannot open screen");
  236.     nw.Screen = s;
  237.     w = OpenWindow(&nw);
  238.     if (w == NULL)
  239.         CleanUp("Cannot Open Window");
  240.     ScreenSignal = 1L << w->UserPort->mp_SigBit;
  241.         
  242.     SetMenuStrip(w, &Menus[0]);
  243.     rp = w->RPort;
  244.     Move(rp, 0, BottomLine);
  245.     SetAPen(rp, 1);
  246.         
  247.     for (;;)
  248.     {
  249.         Wait(ScreenSignal|ReadSignal);
  250.         while (CheckIO((struct IORequest *)ReadReq))
  251.         {
  252.             WaitIO((struct IORequest *)ReadReq);
  253.             c = ReadData[0] & 0x7f;
  254.             Print(c);
  255.             SendIO((struct IORequest *)ReadReq);
  256.         }
  257.              
  258.         while (message = (struct IntuiMessage *)
  259.                          GetMsg(w->UserPort))
  260.         {
  261.             class = message->Class;
  262.             code  = message->Code;
  263.             ReplyMsg((struct Message *)message);
  264.             if (class == VANILLAKEY)
  265.             {
  266.                 c = code & 0x7f;
  267.                 if (HDX)
  268.                     Print(c);
  269.                 if (c == '\n')
  270.                     c = '\r';
  271.                 WriteSerial(&c, 1);
  272.             }
  273.             if (class == MENUPICK)
  274.                 ProcessMenu(code);
  275.         }
  276.     }            
  277.  
  278. }        
  279.  
  280.