home *** CD-ROM | disk | FTP | other *** search
/ Amiga Elysian Archive / AmigaElysianArchive.iso / prog / c / cwrlib31.lha / CWTerm / CWTerm.c < prev    next >
C/C++ Source or Header  |  1992-07-22  |  11KB  |  408 lines

  1. /****************************************************************************
  2. *******                                                               *******
  3. *******                        CW-Term V1.02                          *******
  4. *******                       ---------------                         *******
  5. *******                                                               *******
  6. *******                 Programmed by: Per Hansen                     *******
  7. *******                                                               *******
  8. *******     (C)opyright 1991,92 by Tribune Channel Software Team.     *******
  9. *******                                                               *******
  10. *******         The program is an example on how to use the           *******
  11. *******          cwrite.library in communication software.            *******
  12. *******                                                               *******
  13. *******             It was written using SAS/C v5.10b                 *******
  14. *******                                                               *******
  15. *******  To compile, use:                                             *******
  16. *******                                                               *******
  17. *******  lc -O -v CWTerm CWTermwin comserial                          *******
  18. *******                                                               *******
  19. *******  blink from LIB:c.o,CWTerm.o,CWTermwin.o,comserial.o          *******
  20. *******  to CWTerm                                                    *******
  21. *******  LIB LIB:lc.lib LIB:amiga.lib                                 *******
  22. *******  SMALLCODE SMALLDATA NODEBUG                                  *******
  23. *******                                                               *******
  24. *****************************************************************************/
  25.  
  26. #include <stdio.h>
  27. #include <exec/types.h>
  28. #include <exec/exec.h>
  29. #include <exec/libraries.h>
  30. #include <exec/io.h>
  31. #include <exec/memory.h>
  32. #include <intuition/intuition.h>
  33. #include <graphics/gfxbase.h>
  34. #include <devices/serial.h>
  35. #include <libraries/diskfont.h>
  36. #include <proto/diskfont.h>
  37.  
  38. #include <libraries/cwritelib.h>
  39. #include <libraries/cwritelibbase.h>
  40.  
  41. extern struct IOExtSer *Read_Request;
  42. extern struct MsgPort *ReadPort;
  43. struct Window *ComWindow;
  44. struct Screen *ComScreen;
  45. extern struct Menu Menu1;
  46. extern struct MenuItem SerialItem1,SerialItem2;
  47.  
  48. extern struct MsgPort *CreatePort();
  49. extern struct IOStdReq *CreateStdIO();
  50. char read_in[2];
  51.  
  52. struct ConsoleDevice *ConsoleDevice = NULL;
  53. struct IOStdReq consolereq;
  54.  
  55. int    done = 0;
  56.  
  57. struct IntuitionBase *IntuitionBase;
  58. struct GfxBase *GfxBase;
  59. struct DiskfontBase *DiskfontBase;
  60. struct CwrlibBase *CwrlibBase;
  61. struct CwrData *cdata;
  62.  
  63. int CXBRK(void) {return(0);}    /* Disable lattice CTRL-C/D checking    */
  64.  
  65. UBYTE wtitle[] = "CW-Term v1.02 (C)opyright 1992 Tribune Channel Software";
  66. int baudrate,handshake;
  67. int scrdepth,scrheight=200;
  68.  
  69. BOOL duplex = FALSE;
  70.  
  71. static char devicename[] = "serial.device";
  72.  
  73. struct TextAttr ta;
  74. struct TextFont *tf;
  75.  
  76. static void openfont()
  77. {
  78.     ta.ta_Name = "ibm.font";
  79.     ta.ta_YSize = 8;
  80.     ta.ta_Style = 0;
  81.     ta.ta_Flags = FPF_DISKFONT|FPF_PROPORTIONAL;
  82.  
  83.     DiskfontBase = (struct DiskfontBase*)OpenLibrary("diskfont.library",0);
  84.  
  85.     tf = (struct TextFont*)OpenDiskFont(&ta);
  86. }
  87.  
  88. static void closefont()
  89. {
  90.     if(tf)    CloseFont(tf);
  91.     if(DiskfontBase)    CloseLibrary((struct Library*)DiskfontBase);
  92. }
  93.  
  94. static int allocbitmap(struct BitMap *bm,int p)
  95. {
  96.     if(bm->Depth<p) {
  97.     return((int)(bm->Planes[p-1]=AllocMem(bm->BytesPerRow*bm->Rows,MEMF_CHIP|MEMF_CLEAR)));
  98.     }
  99.     return(0);
  100. }
  101.  
  102. static void freebitmap(struct BitMap *bm,int p)
  103. {
  104.     if(bm->Depth>=p)
  105.     FreeMem(bm->Planes[p-1],bm->BytesPerRow*bm->Rows);
  106. }
  107.  
  108. static void setbitplanes(int depth)
  109. {
  110.     struct Screen *scr;
  111.     struct BitMap *bm;
  112.  
  113.     scr = ComScreen;
  114.     bm = &scr->BitMap;
  115.  
  116.     if(depth<1||depth>4)    return;
  117.     if(depth<4)    freebitmap(bm,4);
  118.     if(depth<3)    freebitmap(bm,3);
  119.     if(depth<2)    freebitmap(bm,2);
  120.     if(depth>1)    if(!allocbitmap(bm,2))    depth = 1;
  121.     if(depth>2)    if(!allocbitmap(bm,3))    depth = 2;
  122.     if(depth>3)    if(!allocbitmap(bm,4))    depth = 3;
  123.     bm->Depth=depth;
  124.     RemakeDisplay();
  125.     return;
  126. }
  127.  
  128.  
  129. void setscreen(int planes)
  130. {
  131.     planes++;
  132.     ClearMenuStrip(ComWindow);
  133.     setbitplanes(planes);
  134.  
  135.     scrdepth = planes;
  136.     CWriteSetColors(ComWindow);
  137.  
  138.     SetMenuStrip(ComWindow,&Menu1);
  139. }
  140.  
  141. void DropDTR()
  142. {
  143.     ComClose();
  144.     Delay(50);
  145.     ComOpen(devicename,0,baudrate,8,0,1,handshake);
  146. }
  147.  
  148. static void cleanup(int returncode)
  149. {
  150.     if(ConsoleDevice)    CloseDevice((struct IORequest*)&consolereq);
  151.     if(ComWindow)    CWriteCloseWindow(ComWindow);
  152.     if(cdata)        CWriteFree(cdata);
  153.     if(CwrlibBase)    CloseLibrary((struct Library*)CwrlibBase);
  154.     closefont();
  155.     exit(returncode);
  156. }
  157.  
  158. void _main()
  159. {
  160.     struct IntuiMessage *Message;
  161.     ULONG Class;
  162.     USHORT Code,Qual;
  163.     APTR MIAddress;
  164.     struct MenuItem *Item;
  165.     long signals;
  166.  
  167.     register int    ch        = 0;
  168.     int     len;
  169.     char    chbuff[128];
  170.  
  171.     if(!(CwrlibBase = (struct CwrlibBase*)OpenLibrary("cwrite.library",0)))
  172.     {
  173.         Write(Output(),"I need cwrite.library to work!\n",31);
  174.         cleanup(205);
  175.     }
  176.     cdata = CWriteAlloc();
  177.  
  178.     IntuitionBase = (struct IntuitionBase *)CwrlibBase->IntLib;
  179.     GfxBase = (struct GfxBase *)CwrlibBase->GfxLib;
  180.  
  181.     if(GfxBase->DisplayFlags & PAL)
  182.         scrheight = 256;
  183.  
  184.     /* Open serial.device unit 0 at 19200 baud 8N1 CTS/RTS */
  185.     handshake = 1; /* for the DropDTR routine.. */
  186.     baudrate = 19200;
  187.     if(!ComOpen(devicename,0,baudrate,8,0,1,handshake))
  188.     {
  189.         Write(Output(),"Error opening the serial port. Aborting!\n",41);
  190.         cleanup(10);
  191.     }
  192.  
  193.     openfont();
  194.     ComWindow = CWriteOpenWindow(wtitle,640,scrheight,3,0);
  195.     ComScreen = ComWindow->WScreen;
  196.  
  197.     if(!ComWindow)
  198.     {
  199.         Write(Output(),"Unable to open the window!\n",27);
  200.         cleanup(10);
  201.     }
  202.  
  203.     SetMenuStrip(ComWindow,&Menu1);
  204.  
  205.     if(tf)
  206.         SetFont(ComWindow->RPort,tf);
  207.  
  208.     CWriteInit(cdata,ComWindow);
  209.     CWriteCursorType(cdata,CURSORTYPE_FAST);
  210.  
  211.     if(!tf)
  212.         CWrite(cdata,"Can't open ibm.font, using system default font.\n",48);
  213.  
  214.     if(OpenDevice("console.device",-1L,(struct IORequest *)&consolereq,0L))
  215.         cleanup(0);
  216.     ConsoleDevice = (struct ConsoleDevice *)consolereq.io_Device;
  217.  
  218.     Read_Request->IOSer.io_Command = CMD_READ;
  219.     Read_Request->IOSer.io_Length = 1;
  220.     Read_Request->IOSer.io_Data = (APTR)&read_in[0];
  221.     SendIO((struct IORequest*)Read_Request);
  222.  
  223.     while(!done)
  224.     {
  225.         signals=Wait(1<<ComWindow->UserPort->mp_SigBit|1<<ReadPort->mp_SigBit);
  226.  
  227.         if(signals & 1<<ComWindow->UserPort->mp_SigBit)
  228.         {
  229.             while(Message=(struct IntuiMessage *)GetMsg(ComWindow->UserPort))
  230.             {
  231.                 Class=Message->Class;
  232.                 Code =Message->Code;
  233.                 Qual =Message->Qualifier;
  234.                 MIAddress=Message->IAddress;
  235.  
  236.                 ReplyMsg((struct Message*)Message);
  237.  
  238.                 if(Class==MENUPICK)
  239.                 {
  240.                     while(Code != MENUNULL)
  241.                     {
  242.                         switch(MENUNUM(Code))
  243.                         {
  244.                             case 0:    /* Project menu    */
  245.                                 switch(ITEMNUM(Code))
  246.                                 {
  247.                                     case 0:    /* Bitplanes */
  248.                                         setscreen(SUBNUM(Code));
  249.                                         break;
  250.                                     case 1:    /* blank line */
  251.                                         break;
  252.                                     case 2:    /* Quit        */
  253.                                         done = 1;
  254.                                         break;
  255.                                 }
  256.                                 break;
  257.                             case 1:    /* Serial menu    */
  258.                                 switch(ITEMNUM(Code))
  259.                                 {
  260.                                     case 0:    /* Baud rates    */
  261.                                         switch(SUBNUM(Code))
  262.                                         {
  263.                                             case 0:    /* 300 baud        */
  264.                                                 baudrate=300;
  265.                                                 break;
  266.                                             case 1:    /* 1200 baud    */
  267.                                                 baudrate=1200;
  268.                                                 break;
  269.                                             case 2:    /* 2400 baud    */
  270.                                                 baudrate=2400;
  271.                                                 break;
  272.                                             case 3:    /* 4800 baud    */
  273.                                                 baudrate=4800;
  274.                                                 break;
  275.                                             case 4:    /* 9600 baud    */
  276.                                                 baudrate=9600;
  277.                                                 break;
  278.                                             case 5:    /* 19200 baud    */
  279.                                                 baudrate=19200;
  280.                                                 break;
  281.                                             case 6:    /* 38400 baud    */
  282.                                                 baudrate=38400;
  283.                                                 break;
  284.                                         }
  285.                                         AbortIO((struct IORequest*)Read_Request);
  286.                                         WaitIO((struct IORequest*)Read_Request);
  287.                                         ComSetBaud(baudrate);
  288.                                         SendIO((struct IORequest*)Read_Request);
  289.                                         break;
  290.                                     case 1:    /* Half/Full duplex    */
  291.                                         if(SerialItem2.Flags & CHECKED)
  292.                                             duplex = TRUE;
  293.                                         else
  294.                                             duplex = FALSE;
  295.                                         break;
  296.                                     case 2:    /* CTS/RTS        */
  297.                                         if(SerialItem1.Flags & CHECKED)
  298.                                             handshake = TRUE;
  299.                                         else
  300.                                             handshake = FALSE;
  301.                                         AbortIO((struct IORequest*)Read_Request);
  302.                                         WaitIO((struct IORequest*)Read_Request);
  303.                                         ComSetFlow(handshake);
  304.                                         SendIO((struct IORequest*)Read_Request);
  305.                                         break;
  306.                                     case 3:    /* Hang up!        */
  307.                                         AbortIO((struct IORequest*)Read_Request);
  308.                                         WaitIO((struct IORequest*)Read_Request);
  309.                                         DropDTR();
  310.                                         SendIO((struct IORequest*)Read_Request);
  311.                                         break;
  312.                                 }
  313.                                 break;
  314.                         }
  315.                         Item = (struct MenuItem *)ItemAddress(&Menu1,(long)Code);
  316.                         Code = Item->NextSelect;
  317.                     }
  318.                 }
  319.                 if(Class==RAWKEY)
  320.                 {
  321.                     if(ch = convertkey(Code,Qual))
  322.                         ComPutc((unsigned char)ch);
  323.                 }
  324.             }
  325.         }
  326.         if(signals & 1<<ReadPort->mp_SigBit)
  327.         {
  328.             if(CheckIO((struct IORequest*)Read_Request))
  329.             {
  330.                 WaitIO((struct IORequest*)Read_Request);
  331.                 CWrite(cdata,read_in,1);
  332.                 if((len = ComRead(chbuff,sizeof(chbuff))) != 0)
  333.                     CWrite(cdata,chbuff,len);
  334.                 SendIO((struct IORequest*)Read_Request);
  335.             }
  336.         }
  337.     }/* end of while(!done) loop */
  338.  
  339.     ComClose();
  340.     cleanup(0);
  341. }
  342.  
  343. convertkey(USHORT code,USHORT qual)
  344. {
  345.     register USHORT i;
  346.     USHORT numchars;
  347.     UBYTE buffer[16];
  348.  
  349.     for(i=0;i<15;i++) buffer[i]=32;
  350.     buffer[15]='\0';
  351.  
  352.     numchars=(int)DeadKeyConvert(code,qual,&buffer[0],15,NULL);
  353.  
  354.     if(numchars>0)
  355.     {
  356.         if((code>79)&&(code<90))    /* The F-keys..    */
  357.             return(0);
  358.         if((code>75)&&(code<80))
  359.         {
  360.             switch(code)
  361.             {
  362.                 case 76:    /* Cursor UP    */
  363.                     ComWrite("A",3);break;
  364.                 case 77:    /* Cursor DOWN    */
  365.                     ComWrite("B",3);break;
  366.                 case 78:    /* Cursor RIGHT    */
  367.                     ComWrite("C",3);break;
  368.                 case 79:    /* Cursor LEFT    */
  369.                     ComWrite("D",3);break;
  370.             }
  371.             return(0);
  372.         }
  373.         if(numchars<16)
  374.         {
  375.             ComWrite(buffer,numchars);
  376.             if(duplex == TRUE)
  377.             {
  378.                 if(numchars == 1)
  379.                     CWrite(cdata,buffer,1);
  380.             }
  381.             return(0);
  382.         }
  383.     }
  384. }    /* end of routine    */
  385.  
  386. /*
  387.     Converts RAWKEYS into VANILLAKEYS, also shows
  388.     special keys like HELP, Cursor Keys, FKeys, etc.
  389.  
  390.     Returns:
  391.     -1 if not enough room in the buffer
  392.     the number of characters placed in the buffer
  393.     */
  394. LONG DeadKeyConvert(
  395. USHORT            Code,
  396. USHORT            Qual,
  397. UBYTE            *kbuffer,
  398. LONG            kbsize,
  399. struct KeyMap    *kmap
  400. )
  401. {
  402.     static struct InputEvent ievent = {NULL,IECLASS_RAWKEY,0,0,0};
  403.  
  404.     ievent.ie_Code = Code;
  405.     ievent.ie_Qualifier = Qual;
  406.     return((LONG)RawKeyConvert(&ievent,kbuffer,kbsize,kmap));
  407. }
  408.