home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 206.lha / Flist_v1.2 / Sources / ttyio.c < prev   
C/C++ Source or Header  |  1988-12-28  |  16KB  |  593 lines

  1. /*
  2.         This is the I/O stuff for Flist... Inspired by the Mg2a
  3.         project. 
  4.         
  5.         Written by Stephen W. Berry. 8/19/88
  6. */
  7.  
  8. #include <libraries/arpbase.h>
  9. #include "flist.h"
  10.  
  11. /*
  12.  * External Amiga functions.
  13.  */
  14.  
  15. extern     LONG       AbortIO();
  16. extern     LONG       CloseDevice();
  17. extern     struct     IOStdReq     *CreateStdIO();
  18. extern     int        OpenConsole();
  19. extern     struct     Window          *OpenWindow();
  20. extern     LONG       RawKeyConvert();
  21. extern     top;
  22.  
  23. /* extern Flist variables */
  24.  
  25. extern struct NewWindow *winptr;
  26. extern struct Window mywin;
  27. extern struct NewScreen *scrptr;
  28. extern struct Screen scr;
  29. extern struct Menu fmenu;
  30. extern struct Gadget pg;
  31. extern struct RastPort *rp;
  32. extern struct Image cursor_image;
  33. extern struct Image cursor_blank;
  34. extern Enable_Abort;
  35. extern struct FileLock *lock, *olddir;
  36. extern char (*fname[MAXDIR])[FCHARS];
  37. extern long (*finfo[MAXDIR])[4];
  38. extern short remember;
  39.  
  40. /* Some external functions */
  41.  
  42. extern void swapdir(), parent();
  43. extern long DoDOS(), spawnproc(), kill(), changedir();
  44.  
  45. /* Global variables */
  46.  
  47. struct Device *ConsoleDevice;
  48. struct IOStdReq ConsoleReq;
  49. struct InputEvent RawKeyEvent = {NULL,IECLASS_RAWKEY,0,0,0};
  50.  
  51. #define BUFSIZ 10
  52.  
  53. UBYTE KeyBuffer[BUFSIZ];
  54.  
  55. struct IntuiText input = {
  56.         TEXTCOLOR,0,  /* Front & Back pen */
  57.         JAM2,   /* Drawmode */
  58.         0,0,    /* Left, Top */
  59.         NULL,   /* Font */
  60.         KeyBuffer,
  61.         NULL    /* Next text */
  62.         };
  63.  
  64. char conline[256];
  65. char titlestr[80];
  66. long row = 0,collum = 0,line = 0,first = 0;
  67.  
  68. void ttclose(),tthide(),ttshow();
  69.  
  70. /*
  71.  * Iconify Flist's window using tthide(), iconify(), and ttshow().
  72.  */
  73.  
  74.  
  75. VOID
  76. tthide(resize)
  77. int resize;
  78. {
  79.      if (resize == TRUE) {               /* if we're resizing,     */
  80.           mywin.LeftEdge = winptr->LeftEdge; /* use current window size */
  81.           mywin.TopEdge = winptr->TopEdge;
  82.           mywin.Width = winptr->Width;
  83.           mywin.Height = winptr->Height;
  84.      }
  85.      ttclose();                    /* reset to zero     */
  86. }
  87.  
  88. VOID
  89. ttshow(resize)
  90. int resize;
  91. {
  92.      openstuff();
  93.      SetMenuStrip(winptr, &fmenu);
  94.      Enable_Abort = 1;          
  95. }
  96.  
  97. VOID
  98. ttclose()
  99. {
  100.      ClearMenuStrip(winptr);
  101.      CloseWindowSafely(winptr,NULL);
  102.      CloseScreen(scrptr);
  103.      winptr = NULL; scrptr = NULL;
  104.      Enable_Abort = 0;
  105. }
  106.  
  107. extern long numfiles, lastsort;
  108.  
  109. refresh()
  110. {
  111.     char buf[100], buf1[200];
  112.     remember = -10;
  113.  
  114.     collum = 0;
  115.     if (row+1 > numfiles && row+2 == numfiles) {
  116.         row--;
  117.         line--;
  118.     } else 
  119.         if (row > numfiles) {
  120.             top = 0;
  121.             row = 0;    
  122.             line = 0;
  123.         }
  124.  
  125.     sort(lastsort);
  126.     first = 0;
  127.     scrprt(NUMLINES,top);
  128.  
  129.     PathName(lock, buf1);
  130.     sprintf(buf,"%s   Files = %ld", buf1, numfiles);
  131.     strncpy(titlestr,buf,80L);
  132.     SetWindowTitles(winptr, -1L,titlestr);
  133. }
  134.  
  135. OpenConsole()
  136. {
  137.         if (OpenDevice("console.device",-1L,&ConsoleReq,0L)){
  138.                 DisplayBeep();
  139.                 Cleanup();
  140.         }
  141.         ConsoleDevice = ConsoleReq.io_Device; /* Lib base address */
  142. }
  143.  
  144. /* this little guy takes input from the IDCMP and stuffs it out to the
  145.         routines involved... */
  146.  
  147. getkey(message) 
  148. struct IntuiMessage *message; 
  149.                 /* this routine has the potential to grow to enormous */
  150. {               /* size... I'll try to restrain myself */
  151.         long keycodes,i,rc;
  152.         char buf[1000];
  153.  
  154.         RawKeyEvent.ie_Code = message->Code;
  155.         RawKeyEvent.ie_Qualifier = message->Qualifier;
  156.         RawKeyEvent.ie_position.ie_addr = *((APTR *)message->IAddress);
  157.         keycodes = RawKeyConvert(&RawKeyEvent,KeyBuffer,BUFSIZ,NULL);
  158.  
  159.         ReplyMsg(message);
  160.  
  161.         if (keycodes > 0){      /* we have something */ 
  162.  
  163.         blankcur();
  164.   
  165.         switch (KeyBuffer[0]) {
  166.             case 0x7f:              /* here we got a delete */
  167.                     KeyBuffer[0] = 0;
  168.                     if (collum == 0)
  169.                         conline[0] = 0;
  170.                     for(i=collum;i<254;i++)
  171.                         conline[i] = conline[i+1];
  172.                     drawline(collum,TRUE);
  173.                     break;
  174.             case 0x08:              /* back space */
  175.                     KeyBuffer[0] = 0;
  176.                     if (collum == 0)
  177.                             conline[0] = 0;
  178.                     if (collum > 0) {
  179.                             collum--;
  180.                             for(i=collum;i<254;i++)
  181.                                     conline[i] = conline[i+1];
  182.                     }
  183.                     drawline(collum, TRUE);
  184.                     break;
  185.             case 0x0d:
  186.                     KeyBuffer[0] = 0;
  187.                     DoDOS(conline);     /* This is the Biggie! */
  188.                     if ( row == NLMO ) {
  189.                             if ( top + NUMLINES < numfiles ) {
  190.                                     line++;
  191.                                     scrprt(NUMLINES,++top);
  192.                             }
  193.                             else
  194.                                     DisplayBeep(0L);
  195.                     }
  196.                     first = 0;
  197.                     collum = 0;
  198.                     for(i=0;i<255;i++)
  199.                             conline[i] = 0;
  200.                     break;
  201.             case 0x9b:
  202.                     KeyBuffer[0] = 0;
  203.                     switch (KeyBuffer[1]) {
  204.                         case 0x3f:        /* looking for the Help key */
  205.                             if (KeyBuffer[2] == 0x7e)
  206.                                 Help();
  207.                             break;
  208.                         case 0x41:      /* up arrow */
  209.                             if (collum == 0) {
  210.                                 for(i=0;i<255;i++)
  211.                                     conline[i] = 0;
  212.                             }
  213.                             else {
  214.                                 strcpy(buf,conline);
  215.                                 conline[first] = 0;
  216.                                 drawline(collum,TRUE);
  217.                                 strcpy(conline,buf);
  218.                             }
  219.                             if (row > 0) {
  220.                                 line--;
  221.                                 row--;
  222.                             }
  223.                             else if (row == 0 && top > 0) {
  224.                                 line--;
  225.                                 scrprt(NUMLINES,--top);
  226.                             }
  227.                             drawline(collum,TRUE);
  228.                             break;
  229.                         case 0x42:      /* Down Arrow */
  230.                             if (collum == 0) {
  231.                                 for(i=0;i<255;i++)
  232.                                     conline[i] = 0;
  233.                             }
  234.                             else {
  235.                                 strcpy(buf,conline);
  236.                                 conline[first] = 0;
  237.                                 drawline(collum,TRUE);
  238.                                 strcpy(conline,buf);
  239.                             }
  240.                             if (row < NLMO) {
  241.                                 if (line < numfiles - 1){
  242.                                     line++;
  243.                                     row++;
  244.                                 }        
  245.                             }
  246.                             else if (row == NLMO && line < numfiles - 1) {
  247.                                 line++;
  248.                                 scrprt(NUMLINES,++top);
  249.                             }
  250.                             drawline(collum,TRUE);
  251.                             break;
  252.                         case 0x44:      /* Left Arrow */
  253.                             if (collum > 0)
  254.                                 collum--;
  255.                                 drawline(collum,TRUE);
  256.                             break;
  257.                         case 0x43:      /* Right Arrow */
  258.                             if (collum < 90)
  259.                                 collum++;
  260.                             else 
  261.                                 DisplayBeep(NULL);
  262.                             drawline(collum,FALSE);
  263.                             break;
  264.                         default:
  265.                             check_for_shift();
  266.                             break;
  267.                     }
  268.                     KeyBuffer[1] = 0;
  269.                     break;
  270.             case 0x1b:      /* The File name insertion thing... */
  271.                     insert_name();
  272.                     break;
  273.             default:
  274.                     if (KeyBuffer[0] < 0x20)
  275.                         control_keys(KeyBuffer[0]);
  276.                     else
  277.                         enter_char(KeyBuffer[0]);
  278.                     break;
  279.             }
  280.     drawcur();
  281.     }
  282. }
  283.  
  284. /* I split up the switch for two reasons... One, I though that the first
  285.    was getting a bit big, Two, I think MANX chokes on large switches.
  286. */
  287.  
  288. /* Looking for the shift-arrow keys */
  289.  
  290. check_for_shift()
  291. {
  292.     char buf[256];
  293.  
  294.     switch (KeyBuffer[1]) {
  295.  
  296.         case 'T':
  297.             if (top > 0) {
  298.  
  299.                 if (line < 10)
  300.                     line = 0;
  301.                 else
  302.                     if (top < 10)
  303.                         line -= top;
  304.                     else 
  305.                         line -= 10;
  306.  
  307.                 if (top < 10)
  308.                     top = 0;
  309.                 else 
  310.                     top -= 10;
  311.  
  312.             }
  313.             scrprt(NUMLINES,top);
  314.             strcpy(buf,conline);
  315.             conline[first] = 0;
  316.             drawline(collum,TRUE);
  317.             strcpy(conline,buf);
  318.             drawline(collum,TRUE);
  319.             break;
  320.  
  321.         case 'S':
  322.  
  323.             if (numfiles - 11 > line) {
  324.  
  325.                 if (numfiles - 11 < line)
  326.                     line = numfiles - 1;
  327.                 else
  328.                     line += 10;
  329.  
  330.                 if (top+10 > numfiles - 1)
  331.                     top = numfiles - 1;
  332.                 else 
  333.                     top += 10;
  334.             }
  335.             scrprt(NUMLINES,top);
  336.             strcpy(buf,conline);
  337.             conline[first] = 0;
  338.             drawline(collum,TRUE);
  339.             strcpy(conline,buf);
  340.             drawline(collum,TRUE);
  341.             break;
  342.         case ' ':
  343.             switch(KeyBuffer[2]) {
  344.                 case 'A':           /* left arrow */
  345.                     collum = 0;
  346.                     first = 0;
  347.                     drawline(collum,TRUE);
  348.                     break;
  349.                 case '@':           /* right arrow */
  350.                     collum = strlen(conline);
  351.                     first = collum > 29 ? collum - 29: 0;
  352.                     drawline(collum,TRUE);
  353.                     break;
  354.             }
  355.         }
  356. }
  357.  
  358. control_keys(key)
  359. char key;
  360. {
  361.     char buf[256];
  362.     int i;
  363.  
  364.     switch(key) {
  365.  
  366.         case 0x01:              /* ^A - get the Arp file-requestor */
  367.             if (Wbargs() == 0){
  368.                 UnLock(olddir);
  369.                 refresh();
  370.             }
  371.             break;
  372.  
  373.         case 0x03:              /* user typed ^C */
  374.             _abort();
  375.             break;
  376.  
  377.         case 0x04:              /* ^D - change directory to fname */
  378.  
  379. /* ***** Hidden SECRET of AmigaDOS !! *****
  380.    only found in the RKM includes... You can tell if a file is
  381.    really a file or if it is a directory by the value of DirEntryType.
  382.    If it is < 0 then it is a file, if > 0 then it is a directory */
  383.  
  384.             if (*finfo[line][0] < 0){
  385.                 sprintf(buf,"%s Not a directory",fname[line]);
  386.                 auto_req(buf);
  387.             }else {
  388.                 changedir(fname[line]);
  389.                 refresh();
  390.             }
  391.             break;
  392.  
  393.         case 0x07:              /* ^G - Re-get the current dir */
  394.             getdir();
  395.             refresh();
  396.             break;
  397.  
  398.         case 0x0b:              /* ^K - Delete file */
  399.             kill(fname[line]);
  400.             refresh();
  401.             break;
  402.  
  403.         case 0x0c:              /* ^L - refresh the screen */
  404.             collum = 0;
  405.             row = 0;
  406.             line = 0;
  407.             first = 0;
  408.             top = 0;
  409.             refresh();          /* also resets the cursor to zero pos */
  410.             break;
  411.  
  412.         case 0x0e:              /* ^N - Make directory */
  413.             make_dir();
  414.             refresh();
  415.             break;
  416.  
  417.         case 0x0f:              /* ^O - sort by pattern */
  418.             sort(3);
  419.             refresh();
  420.             break;
  421.  
  422.         case 0x10:              /* ^P - change directory to Parent */
  423.             parent();
  424.             refresh();
  425.             break;
  426.  
  427.         case 0x12:              /* ^R - rename file */
  428.             rename_file(fname[line]);
  429.             refresh();
  430.             break;
  431.  
  432.         case 0x13:              /* ^S - sort by names */
  433.             sort(0);
  434.             refresh();
  435.             break;
  436.  
  437.         case 0x14:              /* ^T - sort by Time */
  438.             sort(2);
  439.             refresh();
  440.             break;
  441.  
  442.         case 0x15:              /* ^U - erase line to start */
  443.             for (i=0;i<254;i++)
  444.                 conline[i] = '\0';
  445.             first = 0;
  446.             collum = 0;
  447.             drawline(collum,TRUE);
  448.             break;
  449.  
  450.         case 0x18:              /* ^X - Start REXX macro */
  451.             dorexx();
  452.             break;
  453.  
  454.         case 0x19:              /* ^Y - sort by day */
  455.             sort(4);
  456.             refresh();
  457.             break;
  458.  
  459.         case 0x1a:              /* ^Z - sort by size */
  460.             sort(1);
  461.             refresh();
  462.             break;
  463.     }
  464. }
  465.  
  466.  
  467. /* Enter a char into the array and screen */
  468.  
  469. enter_char(c)
  470. register char c;
  471. {
  472.     int i;
  473.     
  474.     for(i=254;i>collum;i--)     /* This is for insert mode */
  475.         conline[i] = conline[i-1];
  476.  
  477.     KeyBuffer[0] = c;
  478.     if (collum < 90) {
  479.             conline[collum] = c;
  480.             collum++;
  481.             drawline(collum,FALSE);
  482.     } else 
  483.             DisplayBeep(NULL);
  484.     KeyBuffer[0] = 0;
  485. }
  486.  
  487. /* Draw the current line from the cursor position, for scrolling */
  488.  
  489. drawline (curpos,flag)
  490. long curpos,flag;
  491. {
  492.         long i,temp1,temp2;
  493.         char tbuf[31];
  494. #ifdef DEBUG
  495.         char buf[90];
  496.  
  497.         sprintf(buf," Row %ld Collumn %ld Top %ld First %ld line %ld \
  498.             numfiles %ld",row,collum,top,first,line,numfiles);
  499.         SetWindowTitles(winptr,-1L,buf);
  500. #endif
  501.  
  502.         temp2 = LINEH * row + TOP + 2;
  503.  
  504.         if ((collum >= first+30) || (collum == first)) {
  505.  
  506.                 if (flag){
  507.                         if (first>0)
  508.                                 first--;
  509.                 }
  510.                 else {
  511.                         if (first<89)
  512.                                 first++;
  513.                 }
  514.         }
  515.     
  516.     strncpy(tbuf,&conline[first],30);
  517.  
  518.     temp1 = strlen(tbuf);
  519.     for(i=temp1;i<29;i++)
  520.         tbuf[i] = ' ';
  521.  
  522.     tbuf[29] = '\0';
  523.  
  524.     input.IText = tbuf;
  525.     PrintIText(rp,&input,(LONG)LEFT,temp2);
  526.     input.IText = KeyBuffer;
  527. }
  528.  
  529. long getcol()
  530. {
  531.         register long temp1;
  532.  
  533.         if (collum == first + 29)
  534.                 temp1 = CHARW * 29 + LEFT;
  535.         else if (first == collum)
  536.                 temp1 = LEFT;
  537.         else {
  538.                 temp1 = CHARW * (collum - first) + LEFT;
  539.         }
  540.         return temp1;
  541. }
  542.  
  543. /* Draw the cursor at the current row & collum */
  544.  
  545. drawcur()
  546. {
  547.         register long temp1,temp2;
  548.  
  549.         temp1 = getcol();
  550.         temp2 = LINEH * row + TOP + 2;
  551.  
  552.         SetDrMd(rp,COMPLEMENT|JAM1);
  553.         SetAPen(rp,4);
  554.         RectFill(rp,temp1,temp2,temp1+7,temp2+7);
  555.         SetAPen(rp,2);
  556.         SetDrMd(rp,JAM1);
  557. }
  558.  
  559. /* Blank the cursur, where-ever it is. */
  560.  
  561. blankcur()
  562. {
  563.         register long temp1,temp2;
  564.                 
  565.         temp1 = getcol();
  566.         temp2 = LINEH * row + TOP + 2;
  567.  
  568.         SetDrMd(rp,COMPLEMENT|JAM2);
  569.         SetAPen(rp,0);
  570.         RectFill(rp,temp1,temp2,temp1+7,temp2+7);
  571.         SetAPen(rp,2);
  572.         SetDrMd(rp,JAM1); 
  573. }
  574.  
  575. /* This is where the File insertion code is (in case you were wondering) */
  576.  
  577. insert_name()
  578. {
  579.         register int i, j;
  580.         register char *temp;        
  581.         j=0;
  582.         
  583.         if(line > numfiles)
  584.                 return;
  585.                 
  586.         for (i=collum;i<89;i++){
  587.                 temp = fname[line];
  588.                 if (temp[j] == 0)
  589.                         break;
  590.                 enter_char(temp[j++]);
  591.         }
  592. }
  593.