home *** CD-ROM | disk | FTP | other *** search
/ back2roots/padua / padua.7z / padua / ftp.vapor.com / microdot-1 / md1_src_02.lzx / clxpr.c < prev    next >
C/C++ Source or Header  |  2014-05-19  |  9KB  |  469 lines

  1. #include "microdot.h"
  2.  
  3. #include "ogre_protos.h"
  4. #include "ogre.h"
  5.  
  6. #include "asyncio.h"
  7.  
  8. #include <devices/serial.h>
  9.  
  10. #include "xproto.h"
  11. #include "zmodem.h"
  12. #include "xprzmodem.h"
  13.  
  14. static struct IOExtSer *ior,*iow;
  15. static char **filearray;
  16. static long fac;
  17.  
  18. extern ULONG tvsig;
  19.  
  20. static struct timerequest treq;
  21. static long timeropen;
  22. static struct MsgPort *tport;
  23.  
  24. static struct Window *tw;
  25. static struct RastPort *lrp;
  26. static long inabortion,docheckcd;
  27. static struct ogwin *ogw;
  28.  
  29. static struct XPR_IO xprio;
  30.  
  31. extern int testcarrier( void );
  32.  
  33. /* The timer functions */
  34. static long opentimer(void)
  35. {
  36.     tport = CreatePort( 0, 0 );
  37.     if( !tport )
  38.         return( -1 );
  39.     treq.tr_node.io_Message.mn_ReplyPort = tport;
  40.     if( OpenDevice( "timer.device", UNIT_VBLANK, &treq, 0 ) )
  41.         return( -1 );
  42.     timeropen = 1;
  43.     return( 0 );
  44. }
  45. static void closetimer(void)
  46. {
  47.     if( timeropen )
  48.         CloseDevice( &treq );
  49.     if( tport )
  50.         DeletePort( tport );
  51.     timeropen = 0;
  52.     tport = 0;
  53. }
  54.  
  55. static void __saveds qtimer(long micros)
  56. {
  57.     long secs=0;
  58.     if( micros > 1000000 )
  59.     {
  60.         secs    = micros / 1000000;
  61.         micros    = micros % 1000000;
  62.     }
  63.     treq.tr_time.tv_micro   = micros;
  64.     treq.tr_time.tv_secs    = secs;
  65.     treq.tr_node.io_Command = TR_ADDREQUEST;
  66.     SendIO( &treq );
  67. }
  68.  
  69. long xpr_finfo(char *name,long type)
  70. {
  71.     struct FileInfoBlock *fib=allocfib();
  72.     BPTR lock;
  73.     long result=0;
  74.  
  75.     if(!fib) return(0);
  76.     if(!(lock=Lock(name,SHARED_LOCK))) goto xit;
  77.     if(type==1) {
  78.         Examine(lock,fib);
  79.         result=fib->fib_Size;
  80.     }
  81.     else if(type==2) result=1;
  82.     UnLock(lock);
  83. xit:
  84.     freefib( fib );
  85.     return(result);
  86. }
  87. long xpr_ffirst(char *buffer,char *pattern)
  88. {
  89.     if(!filearray) return(0);
  90.  
  91.     strcpy(buffer,filearray[0]);
  92.     fac=0;
  93.     return(1);
  94. }
  95. long xpr_fnext(long fac,char *buffer,char *pattern)
  96. {
  97.     if(!filearray || !filearray[fac]) return(0);
  98.     strcpy(buffer,filearray[fac++]);
  99.     return(fac);
  100. }
  101.  
  102. long xpr_swrite(char *buffer,long size)
  103. {
  104.     iow->IOSer.io_Length=size;
  105.     iow->IOSer.io_Data=buffer;
  106.     iow->IOSer.io_Command=CMD_WRITE;
  107.     return(DoIO(iow));
  108. }
  109. long xpr_sflush(void)
  110. {
  111.     iow->IOSer.io_Command=CMD_CLEAR;
  112.     return(DoIO(iow));
  113. }
  114. long xpr_sread(char *buffer,long size,long timeout)
  115. {
  116.     long flag=(1L<<ior->IOSer.io_Message.mn_ReplyPort->mp_SigBit);
  117.     long len,nflag;
  118.     long gotlen;
  119.  
  120.     if( inabortion )
  121.         return(0);
  122.     if( docheckcd )
  123.     {
  124.         if(!testcarrier())
  125.         {
  126.             inabortion=1;
  127.             return(0);
  128.         }
  129.     }
  130.     SetSignal( 0, flag );
  131.     flag|=1L<<(tw->UserPort->mp_SigBit);
  132.     gotlen = 0;
  133.     while( size > 0 )
  134.     {
  135.         ior->IOSer.io_Command = SDCMD_QUERY;
  136.         DoIO( ior );
  137.         len = ior->IOSer.io_Actual;
  138.         if( !len )
  139.             break;
  140.         if( len > size )
  141.             len = size;
  142.         ior->IOSer.io_Command    = CMD_READ;
  143.         ior->IOSer.io_Data         = &buffer[ gotlen ];
  144.         ior->IOSer.io_Length     = len;
  145.         DoIO( ior );
  146.         size-= len; /*ior->IOSer.io_Actual;*/
  147.         gotlen += len; /*ior->IOSer.io_Actual;*/
  148.     }
  149.  
  150.     if( size <= 0 || !timeout || gotlen )
  151.     {
  152.         return( gotlen );
  153.     }
  154.  
  155.     flag |= 1L<<tport->mp_SigBit;
  156.     SetSignal( 0, 1L<<tport->mp_SigBit );
  157.     qtimer( timeout );
  158.  
  159.     ior->IOSer.io_Command = CMD_READ;
  160.     ior->IOSer.io_Data = &buffer[ gotlen ];
  161.     ior->IOSer.io_Length = 1;
  162.     SendIO( ior );
  163.  
  164.     for(;;)
  165.     {
  166.         nflag = Wait( flag | tvsig );
  167.  
  168.         if( nflag & tvsig )
  169.             tv_proc();
  170.  
  171.         if( nflag & ( 1L<<tw->UserPort->mp_SigBit ) )
  172.         {
  173.             struct IntuiMessage *m;
  174.             int ex = FALSE;
  175.  
  176.             while( ( m = GetIMsg( tw->UserPort ) ) )
  177.             {
  178.                 if( m->Class == IDCMP_CLOSEWINDOW )
  179.                     ex = TRUE;            
  180.                 ReplyMsg( m );
  181.             }
  182.             if( ex )
  183.             {
  184.                 AbortIO( &treq );
  185.                 AbortIO( ior );
  186.                 WaitIO( &treq );
  187.                 WaitIO( ior );
  188.                 inabortion = 1;
  189.                 return( 0 );
  190.             }
  191.         }
  192.  
  193.         if( CheckIO( &treq ) )
  194.         {
  195.             AbortIO(ior);
  196.             WaitIO(&treq);
  197.             WaitIO(ior);
  198.             /* Timeout */
  199.             return( gotlen );
  200.         }
  201.  
  202.         if( !CheckIO( ior ) )
  203.             continue;
  204.  
  205.         AbortIO( &treq );
  206.         WaitIO( &treq );
  207.         WaitIO( ior );
  208.  
  209.         gotlen++;
  210.         size--;
  211.  
  212.         while( size > 0 )
  213.         {
  214.             ior->IOSer.io_Command = SDCMD_QUERY;
  215.             DoIO( ior );
  216.             if( ( len = ior->IOSer.io_Actual ) ) 
  217.             {
  218.                 if( len > size )
  219.                     len = size;
  220.                 ior->IOSer.io_Length = len;
  221.                 ior->IOSer.io_Command = CMD_READ;
  222.                 ior->IOSer.io_Data = &buffer[ gotlen ];
  223.                 DoIO( ior );
  224.                 gotlen += len;
  225.                 size -= len;
  226.             }
  227.             else break;
  228.         }
  229.  
  230.         return( gotlen );
  231.     }
  232.     return( 0 ); // shouldn't happen
  233. }
  234.  
  235. long xpr_chkabort( void )
  236. {
  237.     struct IntuiMessage *m;
  238.     ULONG class;
  239.     if(inabortion) return(-1);
  240.     if(tw)
  241.     {
  242.         if(m=ogreIM(ogw))
  243.         {
  244.             class = m->Class;
  245.             ogreIMReply( ogw, m );
  246.             if( class == IDCMP_CLOSEWINDOW )
  247.             {
  248.                 inabortion=1;
  249.                 return(-1);
  250.             }
  251.         }
  252.     }
  253.     if(docheckcd)
  254.     {
  255.         if(!testcarrier())
  256.         {
  257.             inabortion=1;
  258.             return(-1);
  259.         }
  260.     }
  261.     return(0);
  262. }
  263.  
  264. static void closewindow(void)
  265. {
  266.     if(tw)
  267.     {
  268.         /*CloseFont(tw->RPort->Font);
  269.         CloseWindow(tw);*/
  270.         freeinfofg();
  271.         ogreExitWindow( ogw );
  272.     }
  273. }
  274.  
  275. static void print(int y,char *txt)
  276. {
  277.  
  278.     ogreSetStringValue( ogw, y, txt );
  279.  
  280. }
  281. static void printn(int y,long n)
  282. {
  283.     char tmp[32];
  284.     sprintf(tmp,"%lD",n);
  285.  
  286.     ogreSetStringValue( ogw, y, tmp );
  287. }
  288.  
  289. static void drawperc(int x1,int x2)
  290. {
  291. /*    char tmp[16];
  292.     ULONG pos=(499*x1)/x2;
  293.     ULONG perc=(100*x1)/x2;
  294.     sabd(lrp,3,0,JAM1);
  295.     RectFill(lrp, 5,111, 5+pos,111+11);
  296.     SetAPen(lrp,0);
  297.     RectFill(lrp,5+pos,111,499+5,111+11);
  298.     sprintf(tmp,"%ld%%",perc);
  299.     SetAPen(lrp,1);
  300.     Move(lrp,5+238,111+8);
  301.     Text(lrp,tmp,strlen(tmp));
  302.     sabd(lrp,1,0,JAM2);*/
  303.     ogreSetMaxVal( ogw, 13, x2 );
  304.     ogreSetValue( ogw, 13, x1 );
  305. }
  306.  
  307.  
  308. static long filesize;
  309. extern struct Screen *scr;
  310. static void openwindow(char *title)
  311. {
  312.     filesize=0;
  313.  
  314.     ogw = ogreInitWindow( scr,
  315.         WFLG_CLOSEGADGET | WFLG_ACTIVATE,
  316.         IDCMP_CLOSEWINDOW,
  317.         title
  318.     );
  319.  
  320.     ogreAddGroup( ogw, 0, OGFRAME_OUTLINE, NULL );
  321.     ogreAddText( ogw, 0, "     Dateiname:", 1, "", 42, 1 );
  322.     ogreAddText( ogw, 1, "    Dateigr÷▀e:", 2, "", 42, 1 );
  323.  
  324.     ogreAddGroup( ogw, 1, OGFRAME_OUTLINE, NULL );
  325.     ogreAddText( ogw, 0, "Letzte Meldung:", 5, "", 42, 1 );
  326.     ogreAddText( ogw, 1, "Letzter Fehler:", 6, "", 42, 1 );
  327.     ogreAddText( ogw, 2, " Anzahl Fehler:", 7, "", 42, 1 );
  328.  
  329.     ogreAddGroup( ogw, 2, OGFRAME_OUTLINE, NULL );
  330.     ogreAddText( ogw, 0, "  Bytes gesendet:", 3, "", 15, 1 );
  331.     ogreAddText( ogw, 0, "      Bl÷cke:", 4, "", 18, 1 );
  332.     ogreAddText( ogw, 1, " Zeichen/Sekunde:", 10, "", 15, 1 );
  333.     ogreAddText( ogw, 1, "   Protokoll:", 0, "", 18, 1 );
  334.  
  335.     ogreAddGroup( ogw, 3, OGFRAME_OUTLINE, NULL );
  336.     ogreAddText( ogw, 0, "      Gesamtzeit:", 8, "", 15, 1 );
  337.     ogreAddText( ogw, 0, " Zeit bisher:", 9, "", 18, 1 );
  338.  
  339.     ogreAddGroup( ogw, 4, OGFRAME_NONE, NULL );
  340.     ogreAddText( ogw, 0, "       Bereits ⁿbertragen:", 16, "", 1, FALSE );
  341.     ogreAddFuelGauge( ogw, 0, 13, 100 );
  342.     ogreAddText( ogw, 1, "Belegter Festplattenplatz:", 14, "", 1, FALSE );
  343.     ogreAddFuelGauge( ogw, 1, 15, 100 );
  344.  
  345.     setupinfofg( "", ogw, 15 );
  346.  
  347.     tw = ogreOpenWindow( ogw );
  348.  
  349.     doinfofg();
  350.  
  351. }
  352. static char tempproto[64],tempblockcheck[32];
  353. static long filesize;
  354. void xpr_update(struct XPR_UPDATE *xu)
  355. {
  356.     long ud=xu->xpru_updatemask;
  357.     char tmp[96];
  358.     static ULONG errors, timeouts;
  359.     static ULONG blockcount, blocksize;
  360.     static int counter;
  361.     
  362.  
  363.     if(!tw) return;
  364.     if(ud&XPRU_PROTOCOL) strcpy(&tempproto[0],xu->xpru_protocol);
  365.     if(ud&XPRU_BLOCKCHECK) strcpy(&tempblockcheck[0],xu->xpru_blockcheck);
  366.     if((ud&XPRU_PROTOCOL) || (ud&XPRU_BLOCKCHECK)) {
  367.         sprintf(tmp,"%s / %s",tempproto,tempblockcheck);
  368.         print(0,tmp);
  369.     }
  370.     if(ud&XPRU_FILENAME) print(1,xu->xpru_filename);
  371.     if(ud&XPRU_FILESIZE) printn(2,filesize=xu->xpru_filesize);
  372.     if(ud&XPRU_BYTES)
  373.     {
  374.         if(filesize) drawperc(xu->xpru_bytes,filesize);
  375.         printn(3,xu->xpru_bytes);
  376.  
  377.         if( ! ( counter++ & 15 ) )
  378.             doinfofg();
  379.     }
  380.     if(ud&XPRU_BLOCKS)
  381.         blockcount = xu->xpru_blocks;
  382.     if(ud&XPRU_BLOCKSIZE)
  383.         blocksize = xu->xpru_blocksize;
  384.  
  385.     if( ud & ( XPRU_BLOCKS | XPRU_BLOCKSIZE ) )
  386.     {
  387.         sprintf( tmp, "%lD (α %lD Bytes)", blockcount, blocksize );
  388.         print( 4, tmp );
  389.     }
  390.  
  391.     if(ud&XPRU_MSG) print(5,xu->xpru_msg);
  392.     if(ud&XPRU_ERRORMSG) print(6,xu->xpru_errormsg);
  393.     if(ud&XPRU_ERRORS)
  394.         errors = xu->xpru_errors;
  395.     if(ud&XPRU_TIMEOUTS)
  396.         timeouts = xu->xpru_timeouts;
  397.  
  398.     if( ud & ( XPRU_TIMEOUTS | XPRU_ERRORS ) )
  399.     {
  400.         sprintf( tmp, "%lD / %lD", errors, timeouts );
  401.         print( 7, tmp );
  402.     }
  403.  
  404.     if(ud&XPRU_EXPECTTIME) print(8,xu->xpru_expecttime);
  405.     if(ud&XPRU_ELAPSEDTIME) print(9,xu->xpru_elapsedtime);
  406.     if(ud&XPRU_DATARATE) printn(10,xu->xpru_datarate);
  407. }
  408.  
  409. static void initio(void)
  410. {
  411.     memset(&xprio,0,sizeof(struct XPR_IO));
  412.     strcpy(&tempproto[0],"--");
  413.     strcpy(&tempblockcheck[0],"--");
  414. }
  415.  
  416. long xpr_send(struct IOExtSer *sr,struct IOExtSer *sw,
  417.     long checkcd,
  418.     char **files)
  419. {
  420.     long retcode=0;
  421.  
  422.     ior=sr; iow=sw;
  423.     filearray=files; fac=0;
  424.     inabortion=0; docheckcd=checkcd;
  425.  
  426.     openwindow("Sende..."); 
  427.     opentimer();
  428.     initio();
  429.  
  430.     xprio.xpr_filename=files[0];
  431.     retcode=XProtocolSend(&xprio);
  432.     if(inabortion) retcode=0;
  433.     XProtocolCleanup(&xprio);
  434. bibi:
  435.     closetimer();
  436.     closewindow();
  437.     return(retcode);
  438. }
  439.  
  440. long xpr_receive(struct IOExtSer *sr,struct IOExtSer *sw,
  441.     long checkcd)
  442. {
  443.     long retcode=0;
  444.  
  445.     ior=sr; iow=sw;
  446.     filearray=NULL; fac=0;
  447.     inabortion=0; docheckcd=checkcd;
  448.  
  449.     openwindow("Empfange...");
  450.     opentimer();
  451.     initio();
  452.  
  453.     xprio.xpr_filename="Unknown";
  454.     retcode=XProtocolReceive(&xprio);
  455.     if(inabortion) retcode=0;    
  456.     XProtocolCleanup(&xprio);
  457. bibi:
  458.     closetimer();
  459.     closewindow();
  460.     return(retcode);
  461. }
  462.  
  463. int zmodemmode;
  464.  
  465. void setzmodemmode( int mode )
  466. {
  467.     zmodemmode = mode;
  468. }
  469.