home *** CD-ROM | disk | FTP | other *** search
- /****************************************************************************
- ******* *******
- ******* CW-Term V1.02 *******
- ******* --------------- *******
- ******* *******
- ******* Programmed by: Per Hansen *******
- ******* *******
- ******* (C)opyright 1991,92 by Tribune Channel Software Team. *******
- ******* *******
- ******* The program is an example on how to use the *******
- ******* cwrite.library in communication software. *******
- ******* *******
- ******* It was written using SAS/C v5.10b *******
- ******* *******
- ******* To compile, use: *******
- ******* *******
- ******* lc -O -v CWTerm CWTermwin comserial *******
- ******* *******
- ******* blink from LIB:c.o,CWTerm.o,CWTermwin.o,comserial.o *******
- ******* to CWTerm *******
- ******* LIB LIB:lc.lib LIB:amiga.lib *******
- ******* SMALLCODE SMALLDATA NODEBUG *******
- ******* *******
- *****************************************************************************/
-
- #include <stdio.h>
- #include <exec/types.h>
- #include <exec/exec.h>
- #include <exec/libraries.h>
- #include <exec/io.h>
- #include <exec/memory.h>
- #include <intuition/intuition.h>
- #include <graphics/gfxbase.h>
- #include <devices/serial.h>
- #include <libraries/diskfont.h>
- #include <proto/diskfont.h>
-
- #include <libraries/cwritelib.h>
- #include <libraries/cwritelibbase.h>
-
- extern struct IOExtSer *Read_Request;
- extern struct MsgPort *ReadPort;
- struct Window *ComWindow;
- struct Screen *ComScreen;
- extern struct Menu Menu1;
- extern struct MenuItem SerialItem1,SerialItem2;
-
- extern struct MsgPort *CreatePort();
- extern struct IOStdReq *CreateStdIO();
- char read_in[2];
-
- struct ConsoleDevice *ConsoleDevice = NULL;
- struct IOStdReq consolereq;
-
- int done = 0;
-
- struct IntuitionBase *IntuitionBase;
- struct GfxBase *GfxBase;
- struct DiskfontBase *DiskfontBase;
- struct CwrlibBase *CwrlibBase;
- struct CwrData *cdata;
-
- int CXBRK(void) {return(0);} /* Disable lattice CTRL-C/D checking */
-
- UBYTE wtitle[] = "CW-Term v1.02 (C)opyright 1992 Tribune Channel Software";
- int baudrate,handshake;
- int scrdepth,scrheight=200;
-
- BOOL duplex = FALSE;
-
- static char devicename[] = "serial.device";
-
- struct TextAttr ta;
- struct TextFont *tf;
-
- static void openfont()
- {
- ta.ta_Name = "ibm.font";
- ta.ta_YSize = 8;
- ta.ta_Style = 0;
- ta.ta_Flags = FPF_DISKFONT|FPF_PROPORTIONAL;
-
- DiskfontBase = (struct DiskfontBase*)OpenLibrary("diskfont.library",0);
-
- tf = (struct TextFont*)OpenDiskFont(&ta);
- }
-
- static void closefont()
- {
- if(tf) CloseFont(tf);
- if(DiskfontBase) CloseLibrary((struct Library*)DiskfontBase);
- }
-
- static int allocbitmap(struct BitMap *bm,int p)
- {
- if(bm->Depth<p) {
- return((int)(bm->Planes[p-1]=AllocMem(bm->BytesPerRow*bm->Rows,MEMF_CHIP|MEMF_CLEAR)));
- }
- return(0);
- }
-
- static void freebitmap(struct BitMap *bm,int p)
- {
- if(bm->Depth>=p)
- FreeMem(bm->Planes[p-1],bm->BytesPerRow*bm->Rows);
- }
-
- static void setbitplanes(int depth)
- {
- struct Screen *scr;
- struct BitMap *bm;
-
- scr = ComScreen;
- bm = &scr->BitMap;
-
- if(depth<1||depth>4) return;
- if(depth<4) freebitmap(bm,4);
- if(depth<3) freebitmap(bm,3);
- if(depth<2) freebitmap(bm,2);
- if(depth>1) if(!allocbitmap(bm,2)) depth = 1;
- if(depth>2) if(!allocbitmap(bm,3)) depth = 2;
- if(depth>3) if(!allocbitmap(bm,4)) depth = 3;
- bm->Depth=depth;
- RemakeDisplay();
- return;
- }
-
-
- void setscreen(int planes)
- {
- planes++;
- ClearMenuStrip(ComWindow);
- setbitplanes(planes);
-
- scrdepth = planes;
- CWriteSetColors(ComWindow);
-
- SetMenuStrip(ComWindow,&Menu1);
- }
-
- void DropDTR()
- {
- ComClose();
- Delay(50);
- ComOpen(devicename,0,baudrate,8,0,1,handshake);
- }
-
- static void cleanup(int returncode)
- {
- if(ConsoleDevice) CloseDevice((struct IORequest*)&consolereq);
- if(ComWindow) CWriteCloseWindow(ComWindow);
- if(cdata) CWriteFree(cdata);
- if(CwrlibBase) CloseLibrary((struct Library*)CwrlibBase);
- closefont();
- exit(returncode);
- }
-
- void _main()
- {
- struct IntuiMessage *Message;
- ULONG Class;
- USHORT Code,Qual;
- APTR MIAddress;
- struct MenuItem *Item;
- long signals;
-
- register int ch = 0;
- int len;
- char chbuff[128];
-
- if(!(CwrlibBase = (struct CwrlibBase*)OpenLibrary("cwrite.library",0)))
- {
- Write(Output(),"I need cwrite.library to work!\n",31);
- cleanup(205);
- }
- cdata = CWriteAlloc();
-
- IntuitionBase = (struct IntuitionBase *)CwrlibBase->IntLib;
- GfxBase = (struct GfxBase *)CwrlibBase->GfxLib;
-
- if(GfxBase->DisplayFlags & PAL)
- scrheight = 256;
-
- /* Open serial.device unit 0 at 19200 baud 8N1 CTS/RTS */
- handshake = 1; /* for the DropDTR routine.. */
- baudrate = 19200;
- if(!ComOpen(devicename,0,baudrate,8,0,1,handshake))
- {
- Write(Output(),"Error opening the serial port. Aborting!\n",41);
- cleanup(10);
- }
-
- openfont();
- ComWindow = CWriteOpenWindow(wtitle,640,scrheight,3,0);
- ComScreen = ComWindow->WScreen;
-
- if(!ComWindow)
- {
- Write(Output(),"Unable to open the window!\n",27);
- cleanup(10);
- }
-
- SetMenuStrip(ComWindow,&Menu1);
-
- if(tf)
- SetFont(ComWindow->RPort,tf);
-
- CWriteInit(cdata,ComWindow);
- CWriteCursorType(cdata,CURSORTYPE_FAST);
-
- if(!tf)
- CWrite(cdata,"Can't open ibm.font, using system default font.\n",48);
-
- if(OpenDevice("console.device",-1L,(struct IORequest *)&consolereq,0L))
- cleanup(0);
- ConsoleDevice = (struct ConsoleDevice *)consolereq.io_Device;
-
- Read_Request->IOSer.io_Command = CMD_READ;
- Read_Request->IOSer.io_Length = 1;
- Read_Request->IOSer.io_Data = (APTR)&read_in[0];
- SendIO((struct IORequest*)Read_Request);
-
- while(!done)
- {
- signals=Wait(1<<ComWindow->UserPort->mp_SigBit|1<<ReadPort->mp_SigBit);
-
- if(signals & 1<<ComWindow->UserPort->mp_SigBit)
- {
- while(Message=(struct IntuiMessage *)GetMsg(ComWindow->UserPort))
- {
- Class=Message->Class;
- Code =Message->Code;
- Qual =Message->Qualifier;
- MIAddress=Message->IAddress;
-
- ReplyMsg((struct Message*)Message);
-
- if(Class==MENUPICK)
- {
- while(Code != MENUNULL)
- {
- switch(MENUNUM(Code))
- {
- case 0: /* Project menu */
- switch(ITEMNUM(Code))
- {
- case 0: /* Bitplanes */
- setscreen(SUBNUM(Code));
- break;
- case 1: /* blank line */
- break;
- case 2: /* Quit */
- done = 1;
- break;
- }
- break;
- case 1: /* Serial menu */
- switch(ITEMNUM(Code))
- {
- case 0: /* Baud rates */
- switch(SUBNUM(Code))
- {
- case 0: /* 300 baud */
- baudrate=300;
- break;
- case 1: /* 1200 baud */
- baudrate=1200;
- break;
- case 2: /* 2400 baud */
- baudrate=2400;
- break;
- case 3: /* 4800 baud */
- baudrate=4800;
- break;
- case 4: /* 9600 baud */
- baudrate=9600;
- break;
- case 5: /* 19200 baud */
- baudrate=19200;
- break;
- case 6: /* 38400 baud */
- baudrate=38400;
- break;
- }
- AbortIO((struct IORequest*)Read_Request);
- WaitIO((struct IORequest*)Read_Request);
- ComSetBaud(baudrate);
- SendIO((struct IORequest*)Read_Request);
- break;
- case 1: /* Half/Full duplex */
- if(SerialItem2.Flags & CHECKED)
- duplex = TRUE;
- else
- duplex = FALSE;
- break;
- case 2: /* CTS/RTS */
- if(SerialItem1.Flags & CHECKED)
- handshake = TRUE;
- else
- handshake = FALSE;
- AbortIO((struct IORequest*)Read_Request);
- WaitIO((struct IORequest*)Read_Request);
- ComSetFlow(handshake);
- SendIO((struct IORequest*)Read_Request);
- break;
- case 3: /* Hang up! */
- AbortIO((struct IORequest*)Read_Request);
- WaitIO((struct IORequest*)Read_Request);
- DropDTR();
- SendIO((struct IORequest*)Read_Request);
- break;
- }
- break;
- }
- Item = (struct MenuItem *)ItemAddress(&Menu1,(long)Code);
- Code = Item->NextSelect;
- }
- }
- if(Class==RAWKEY)
- {
- if(ch = convertkey(Code,Qual))
- ComPutc((unsigned char)ch);
- }
- }
- }
- if(signals & 1<<ReadPort->mp_SigBit)
- {
- if(CheckIO((struct IORequest*)Read_Request))
- {
- WaitIO((struct IORequest*)Read_Request);
- CWrite(cdata,read_in,1);
- if((len = ComRead(chbuff,sizeof(chbuff))) != 0)
- CWrite(cdata,chbuff,len);
- SendIO((struct IORequest*)Read_Request);
- }
- }
- }/* end of while(!done) loop */
-
- ComClose();
- cleanup(0);
- }
-
- convertkey(USHORT code,USHORT qual)
- {
- register USHORT i;
- USHORT numchars;
- UBYTE buffer[16];
-
- for(i=0;i<15;i++) buffer[i]=32;
- buffer[15]='\0';
-
- numchars=(int)DeadKeyConvert(code,qual,&buffer[0],15,NULL);
-
- if(numchars>0)
- {
- if((code>79)&&(code<90)) /* The F-keys.. */
- return(0);
- if((code>75)&&(code<80))
- {
- switch(code)
- {
- case 76: /* Cursor UP */
- ComWrite("A",3);break;
- case 77: /* Cursor DOWN */
- ComWrite("B",3);break;
- case 78: /* Cursor RIGHT */
- ComWrite("C",3);break;
- case 79: /* Cursor LEFT */
- ComWrite("D",3);break;
- }
- return(0);
- }
- if(numchars<16)
- {
- ComWrite(buffer,numchars);
- if(duplex == TRUE)
- {
- if(numchars == 1)
- CWrite(cdata,buffer,1);
- }
- return(0);
- }
- }
- } /* end of routine */
-
- /*
- Converts RAWKEYS into VANILLAKEYS, also shows
- special keys like HELP, Cursor Keys, FKeys, etc.
-
- Returns:
- -1 if not enough room in the buffer
- the number of characters placed in the buffer
- */
- LONG DeadKeyConvert(
- USHORT Code,
- USHORT Qual,
- UBYTE *kbuffer,
- LONG kbsize,
- struct KeyMap *kmap
- )
- {
- static struct InputEvent ievent = {NULL,IECLASS_RAWKEY,0,0,0};
-
- ievent.ie_Code = Code;
- ievent.ie_Qualifier = Qual;
- return((LONG)RawKeyConvert(&ievent,kbuffer,kbsize,kmap));
- }
-