home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / glib / part03 / mac-mach.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-05-14  |  6.2 KB  |  354 lines

  1. /* $Id: mac-mach.c,v 1.6 89/05/06 17:13:32 lee Exp $
  2.  * GLIB - a Generic LIBrarian and editor for synths
  3.  *
  4.  * Machine dependent stuff.
  5.  *
  6.  * macintosh version  -  Steven A. Falco  8/24/87
  7.  * $Log:    mac-mach.c,v $
  8.  * Revision 1.6  89/05/06  17:13:32  lee
  9.  * rel. to comp.sources.misc
  10.  * 
  11.  */
  12.  
  13. #include "glib.h"
  14. #include <ctype.h>
  15.  
  16. int Rows, Cols;
  17.  
  18. hello()
  19. {
  20.  
  21. #ifdef MPW
  22.     /* Substitute our vt driver for the standard console driver.
  23.      * Use slot 1 since that is where the standard driver lives.
  24.      * This will magically hook in printf, etc.  We could do dups
  25.      * and closes which might be more portable, but we'd still need
  26.      * the _addDevHandler() call.  This is all subject to change when
  27.      * MPW 2.0 rolls around.
  28.      *
  29.      * NOTE: vt_read is unused in keynote.  Vt_getch and vt_peekch are
  30.      * used instead to avoid all system buffering.
  31.      */
  32.     _addDevHandler(1, 'CONS', vt_faccess, vt_close, vt_read, vt_write, vt_ioctl);
  33.     setvbuf(stdout, NULL, _IOLBF, BUFSIZ);
  34.     vt_raw(); /* want ^D to work as vanilla character */
  35. #endif
  36.  
  37.     midi_init();
  38. }
  39.  
  40. bye()
  41. {
  42.     { int waste_time;
  43.     while(midi_txst == MIDI_NE) /* wait for all notes to drain */
  44.         ;
  45.     for(waste_time = 0; waste_time < (1 << 14); waste_time++)
  46.         ; /* hardware fifo has to drain too */
  47.     midi_reset(); /* now we can shut it down */
  48.     }
  49.     windgoto(23,0);
  50.     windrefresh();
  51.     exit(0);
  52. }
  53.  
  54. flushconsole()
  55. {
  56.     while ( statconsole() )
  57.         getconsole();
  58. }
  59.  
  60. statconsole()
  61. {
  62.     fflush(stdout);    /* make sure we see prompts */
  63.     return(vt_peekch());
  64. }
  65.  
  66. getconsole()
  67. {
  68.     fflush(stdout); /* flush any prompts */
  69.     return(vt_getch());
  70. }
  71.  
  72. getmidi()
  73. {
  74.     int i;
  75.     
  76.     while((i = midi_rx()) == -1)
  77.         ; /* wait for something good */
  78.     
  79.     return(i);
  80. }
  81.  
  82. sendmidi(c)
  83. {
  84.     while(STATTXBF) /* it is easy to overrun on bulk dumps - slow down to uart rate */
  85.         ; /* wait till last character goes to uart */
  86.         
  87.     midi_tx(c);
  88. }
  89.  
  90. flushmidi()
  91. {
  92.     while ( STATMIDI )
  93.         getmidi(); /* flush any and all characters */
  94. }
  95.  
  96. /* return relative time in 1mS steps */
  97. long milliclock()
  98. {
  99.     /* relative, free-running millisecond counter (5mS granularity) */
  100.     return(midi_time);
  101. }
  102.  
  103. millisleep(i)
  104. int i;
  105. {
  106.     long j = milliclock();
  107.     
  108.     while(i + j > milliclock())
  109.         ;
  110.     
  111.     return;
  112. }
  113.  
  114. int Paw;
  115. FileParam pblock;
  116. VolumeParam dblock;
  117. FInfo *fp;
  118. Str255 dummystr;
  119.  
  120. /* Establish a "working directory reference number" for the user-specified
  121.  * directory (path) in character array "pre".  This only affects
  122.  * those calls that explicitly use the reference number - namely, the 
  123.  * PBGetFInfo() call below.  In particular, none of this has any effect
  124.  * whatsoever on fopen() calls.  For those, a full path name must be used.
  125.  */
  126. openphr(pre)
  127. char *pre; /* the path prefix */
  128. {
  129.     strcpy(&dummystr, pre);    /* make a copy 'cause we change it */
  130.     c2pstr(&dummystr);    /* we need a pascal string */
  131.     dblock.ioCompletion = 0;
  132.     dblock.ioNamePtr = &dummystr;    /* this is the directory path in pascal */
  133.     dblock.ioVRefNum = 0;    /* start at the root */
  134.     
  135.     if(*((short *) FSFCBLen) > 0) {    /* we are running HFS */
  136.         ((WDPBRec *) &dblock)->ioWDProcID = SIGNATURE;    /* our ID */
  137.         ((WDPBRec *) &dblock)->ioWDDirID = 0;
  138.         if(PBOpenWD(&dblock, false) != noErr) {
  139.             printf("No such directory! (%s)\n", pre);
  140.             dblock.ioVRefNum = 0; /* force back to current directory */
  141.             pre[0] = 0; /* nuke the path or we'll get in trouble! */
  142.             /* we could also use a return code to abort the whole
  143.              * thing if desired.
  144.              */
  145.         }
  146.     } else {
  147.         dblock.ioVolIndex = -1;
  148.         if(PBGetVInfo(&dblock, false) != noErr) {
  149.             printf("No such directory! (%s)\n", pre);
  150.             dblock.ioVRefNum = 0; /* force back to current directory */
  151.             pre[0] = 0; /* nuke the path or we'll get in trouble! */
  152.             /* we could also use a return code to abort the whole
  153.              * thing if desired.
  154.              */    
  155.         }
  156.     }
  157.     
  158.     Paw = 0;
  159.     fp = &(pblock.ioFlFndrInfo);
  160.     pblock.ioCompletion = 0;
  161.     pblock.ioVRefNum = dblock.ioVRefNum;    /* use new directory number */
  162.     pblock.ioFVersNum = 0;
  163. }
  164.  
  165. /* Return JUST THE FILENAME COMPONENT of the next phrase file we find. */
  166. char *
  167. nextphr()
  168. {
  169.     static char fname[36];
  170.     char *p, *strrchr();
  171.  
  172.     retry:
  173.     Paw++;
  174.  
  175.     /* this is a little complicated... */
  176.     /* we go through the whole directory in sequence */
  177.     pblock.ioNamePtr = &dummystr;
  178.     pblock.ioFDirIndex = Paw;
  179.     if(PBGetFInfo(&pblock, false) != noErr) {
  180.         /* no such file - we hit the end of the directory */
  181.         return(NULL);
  182.     }
  183.  
  184.     /* OK - we have some file info */
  185.     p2cstr(pblock.ioNamePtr); /* convert from Pascal to C format */
  186.     /* NOTE - '' rather than "" is correct! */
  187.     if(fp->fdType != 'TEXT') {
  188.         goto retry; /* not one of ours */
  189.     }
  190.     /* make it available as a static string */
  191.     strncpy(fname, pblock.ioNamePtr, 35); /* the file name */
  192.     fname[35] = 0; /* insist on a null - really can't happen :-) */
  193.     return(fname);
  194. }
  195. closephr()
  196. {
  197. }
  198.  
  199. /* getmouse - get currect row and column of mouse */
  200. getmouse(amr,amc)
  201. int *amr;
  202. int *amc;
  203. {
  204.     *amr = -1;
  205.     *amc = -1;
  206. }
  207.  
  208. /* statmouse - return mouse button state (0=nothing pressed,1=left,2=right) */
  209. statmouse()
  210. {
  211.     return(-1);
  212. }
  213.  
  214. mouseon()
  215. {
  216. }
  217.  
  218. mouseoff()
  219. {
  220. }
  221.  
  222. /* Return when either a console key or mouse button is pressed. */
  223. mouseorkey()
  224. {
  225.     return(getconsole());
  226. }
  227.  
  228. char *
  229. alloc(n)
  230.     unsigned n;
  231. {
  232.     char *p;
  233.  
  234.     if ( (p = malloc(n)) == NULL ) {
  235.         printf("*** Whoops *** alloc has failed?!?  No more memory!\n");
  236.         fflush(stdout);
  237.         bye();
  238.     }
  239.     return(p);
  240. }
  241.  
  242. windinit()
  243. {
  244.     Rows = 24;
  245.     Cols = 80;
  246.     
  247.     return;
  248. }
  249.  
  250. windgoto(r,c)
  251. int r,c;
  252. {
  253.     printf("\033[%d;%dH",r+1,c+1);
  254. }
  255.  
  256. windeeol()
  257. {
  258.     printf("\033[K");
  259. }
  260.  
  261. winderaserow(r)
  262. {
  263.     windgoto(r,0);
  264.     windeeol();
  265. }
  266.  
  267. windexit(r)
  268. int r;
  269. {
  270.     bye();
  271. }
  272.  
  273. windclear()
  274. {
  275.     printf("\033[H");
  276.     printf("\033[J");
  277. }
  278.  
  279. /* windgets - get a line of input from the console, handling backspaces */
  280. windgets(s)
  281. char *s;
  282. {
  283.     char *origs = s;
  284.     int c;
  285.  
  286.     while ( (c=getconsole()) != '\n' && c!='\r' && c!= EOF ) {
  287.         if ( c == '\b' ) {
  288.             if ( s > origs ) {
  289.                 windstr("\b \b");
  290.                 s--;
  291.             }
  292.         }
  293.         else {
  294.             windputc(c);
  295.             *s++ = c;
  296.         }
  297.         windrefresh();
  298.     }
  299.     *s = '\0';
  300. }
  301.  
  302. windstr(s)
  303. char *s;
  304. {
  305.     int c;
  306.  
  307.     while ( (c=(*s++)) != '\0' )
  308.         windputc(c);
  309. }
  310.  
  311. windputc(c)
  312. int c;
  313. {
  314.     putchar(c);
  315. }
  316.  
  317. windrefresh()
  318. {
  319. }
  320.  
  321. beep()
  322. {
  323.     putchar('\007');
  324. }
  325.  
  326. windhigh()
  327. {
  328. }
  329.  
  330. windnorm()
  331. {
  332. }
  333.  
  334. char *
  335. openls()
  336. {
  337.     /* this should be a whole lot fancier to take care of the path
  338.      * problems on the mac.  But glib currently has no path
  339.      * variables.  The current directory (just ":") will do for now... SAF
  340.      */
  341.     openphr(":");
  342.     return("");
  343. }
  344.  
  345. char *
  346. nextls()
  347. {
  348.     return(nextphr());
  349. }
  350.  
  351. closels()
  352. {
  353. }
  354.