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

  1. # line 1 "unix-mach.c"
  2. /* $Id: unix-mach.c,v 1.6 89/05/06 17:13:45 lee Exp $
  3.  * GLIB - a Generic LIBrarian and editor for synths
  4.  *
  5.  * Machine dependent stuff.
  6.  *
  7.  * Unix version
  8.  * Tim Thompson
  9.  * modifications: Greg Lee
  10.  * $Log:    unix-mach.c,v $
  11.  * Revision 1.6  89/05/06  17:13:45  lee
  12.  * rel. to comp.sources.misc
  13.  * 
  14.  */
  15. /* LINTLIBRARY */
  16.  
  17. #include "glib.h"
  18. #include <ctype.h>
  19.  
  20. int Rows, Cols;
  21.  
  22. #include <curses.h>
  23.  
  24. /* Ultrix curses fix -- gl */
  25. #ifdef ULTRIX
  26. #undef nl()
  27. #undef nonl()
  28. #define nl()    (_tty.sg_flags |= CRMOD,_pfast = _rawmode,stty(_tty_ch, &_tty))
  29. #define nonl()    (_tty.sg_flags &= ~CRMOD, _pfast = TRUE, stty(_tty_ch, &_tty))
  30. #endif
  31.  
  32. #ifdef ARROW
  33. #include <sys/ioctl.h>
  34. #include <signal.h>
  35. #include <setjmp.h>
  36. jmp_buf wakeup;
  37. #endif
  38.  
  39.  
  40. #ifndef ARROW
  41.  
  42. initkbd()
  43. {}
  44.  
  45. resetkbd()
  46. {}
  47.  
  48. nmgetch()
  49. {
  50.     return (getchar() & 0x7f);
  51. }
  52.  
  53. #else /*ARROW*/
  54.  
  55. /* Following code for arrow key support lifted from
  56.  * James Gosling's sc spreadsheet calculator -- gl
  57.  */
  58. #define N_KEY 4
  59.  
  60. struct key_map {
  61.     char *k_str;
  62.     char k_val;
  63.     char k_index;
  64. }; 
  65.  
  66. struct key_map km[N_KEY];
  67.  
  68. char keyarea[N_KEY*10];
  69.  
  70. char *tgetstr();
  71. char *getenv();
  72.  
  73. #ifdef TIOCSLTC
  74. struct ltchars old_chars, new_chars;
  75. #endif
  76.  
  77. char dont_use[] = {
  78.     ctl('z'), ctl('r'), ctl('l'), ctl('b'), ctl('c'), ctl('f'), ctl('g'), ctl('['),
  79.     ctl('h'), ctl('m'), ctl('j'), ctl('n'), ctl('p'), ctl('q'), ctl('s'), ctl('t'),
  80.     ctl('u'), ctl('v'), ctl('e'), ctl('a'), 0,
  81. };
  82.  
  83. initkbd()
  84. {
  85.     register struct key_map *kp;
  86.     register i,j;
  87.     char *ks;
  88.     char *p = keyarea;
  89.     static char buf[1024]; /* Why do I have to do this again? */
  90.  
  91.     if (tgetent(buf, getenv("TERM")) <= 0)
  92.     return;
  93.  
  94.     km[0].k_str = tgetstr("kl", &p); km[0].k_val = ctl('b');
  95.     km[1].k_str = tgetstr("kr", &p); km[1].k_val = ctl('f');
  96.     km[2].k_str = tgetstr("ku", &p); km[2].k_val = ctl('p');
  97.     km[3].k_str = tgetstr("kd", &p); km[3].k_val = ctl('n');
  98.     ks = tgetstr("ks",&p);
  99.     if (ks) 
  100.     printf("%s",ks);
  101.  
  102.     /* Unmap arrow keys which conflict with our ctl keys   */
  103.     /* Ignore unset, longer than length 1, and 1-1 mapped keys */
  104.  
  105.     for (i = 0; i < N_KEY; i++) {
  106.     kp = &km[i];
  107.     if (kp->k_str && (kp->k_str[1] == 0) && (kp->k_str[0] != kp->k_val))
  108.         for (j = 0; dont_use[j] != 0; j++)
  109.             if (kp->k_str[0] == dont_use[j]) {
  110.              kp->k_str = (char *)0;
  111.              break;
  112.         }
  113.     }
  114.  
  115.  
  116. #ifdef TIOCSLTC
  117.     ioctl(fileno(stdin), TIOCGLTC, (char *)&old_chars);
  118.     new_chars = old_chars;
  119.     if (old_chars.t_lnextc == ctl('v'))
  120.     new_chars.t_lnextc = -1;
  121.     if (old_chars.t_rprntc == ctl('r'))
  122.     new_chars.t_rprntc = -1;
  123.     ioctl(fileno(stdin), TIOCSLTC, (char *)&new_chars);
  124. #endif
  125. }
  126.  
  127. resetkbd()
  128. {
  129. #ifdef TIOCSLTC
  130.     ioctl(fileno(stdin), TIOCSLTC, (char *)&old_chars);
  131. #endif
  132. }
  133.  
  134. nmgetch() 
  135. {
  136.     register int c;
  137.     register struct key_map *kp;
  138.     register struct key_map *biggest;
  139.     register int i;
  140.     int almost;
  141.     int maybe;
  142.  
  143.     static char dumpbuf[10];
  144.     static char *dumpindex;
  145.  
  146.     int timeout();
  147.  
  148.     if (dumpindex && *dumpindex)
  149.         return (*dumpindex++);
  150.  
  151.     c = getchar() & 0x7f;
  152.     biggest = 0;
  153.     almost = 0;
  154.  
  155.     for (kp = &km[0]; kp < &km[N_KEY]; kp++) {
  156.     if (!kp->k_str)
  157.         continue;
  158.     if (c == kp->k_str[kp->k_index]) {
  159.         almost = 1;
  160.         kp->k_index++;
  161.         if (kp->k_str[kp->k_index] == 0) {
  162.         c = kp->k_val;
  163.                    for (kp = &km[0]; kp < &km[N_KEY]; kp++)
  164.                 kp->k_index = 0;
  165.             return(c);
  166.         }
  167.     }
  168.     if (!biggest && kp->k_index)
  169.         biggest = kp;
  170.         else if (kp->k_index && biggest->k_index < kp->k_index)
  171.         biggest = kp;
  172.     }
  173.  
  174.     if (almost) { 
  175.  
  176.         (void)signal(SIGALRM, timeout);
  177.         alarm(1);
  178.  
  179.     if (setjmp(wakeup) == 0) { 
  180.         maybe = nmgetch();
  181.         alarm(0);
  182.         return(maybe);
  183.     }
  184.  
  185.     }
  186.     
  187.     if (biggest) {
  188.     for (i = 0; i<biggest->k_index; i++) 
  189.         dumpbuf[i] = biggest->k_str[i];
  190.     dumpbuf[i++] = c;
  191.     dumpbuf[i] = 0;
  192.     dumpindex = &dumpbuf[1];
  193.            for (kp = &km[0]; kp < &km[N_KEY]; kp++)
  194.         kp->k_index = 0;
  195.     return (dumpbuf[0]);
  196.     }
  197.  
  198.     return(c);
  199. }
  200.  
  201. timeout()
  202. {
  203.     longjmp(wakeup, -1);
  204. }
  205.  
  206. #endif /* ARROW */
  207.  
  208. hello()
  209. {
  210. }
  211.  
  212. bye()
  213. {
  214.     windgoto(22,0);
  215.     windrefresh();
  216.     resetkbd();
  217.     windexit(0);
  218. }
  219.  
  220. /* getmouse - get currect row and column of mouse */
  221. getmouse(amr,amc)
  222. int *amr;
  223. int *amc;
  224. {
  225. #ifdef USEMOUSE
  226.     /* no such */
  227. #else
  228.     *amr = -1;
  229.     *amc = -1;
  230. #endif
  231. }
  232.  
  233. /* statmouse - return mouse button state (0=nothing pressed,1=left,2=right) */
  234. statmouse()
  235. {
  236. #ifdef USEMOUSE
  237.     /* no such */
  238. #else
  239.     return(-1);
  240. #endif
  241. }
  242.  
  243. /* Return when either a console key or mouse button is pressed. */
  244. mouseorkey()
  245. {
  246. #ifdef USEMOUSE
  247.     /* no such */
  248. #else
  249.     return(nmgetch());
  250. /*
  251.     return(getconsole());
  252. */
  253. #endif
  254. }
  255.  
  256. flushconsole()
  257. {
  258. }
  259.  
  260. statconsole()
  261. {
  262.     return(1);
  263. }
  264.  
  265. getconsole()
  266. {
  267.     return(getchar());
  268. }
  269.  
  270. int siopenflag = 0;
  271. FILE *sifile;
  272. int soopenflag = 0;
  273. FILE *sofile;
  274. extern char Syinfname[100];
  275. extern char Syofname[100];
  276.  
  277. getmidi()
  278. {    int c;
  279.  
  280.     if (!synthinfileflag) return(-1);
  281.  
  282.     if (!siopenflag) {
  283. #ifdef BSD
  284.         OPENBINFILE(sifile,Syinfname,"r");
  285. #else
  286.         OPENBINFILE(sifile,Syinfname,"rb");
  287. #endif
  288.         if (sifile != NULL)
  289.             siopenflag = 1;
  290.     }
  291.     if (siopenflag) {
  292.         c = getc(sifile);
  293.         if (c == EOF) {
  294.             (void)fclose(sifile);
  295.             siopenflag = 0;
  296.         } else
  297.             return(c);
  298.     }
  299.     return(-1);
  300. }
  301.  
  302. statmidi()
  303. {
  304.     if (!synthinfileflag) return(0);
  305.     return(1);
  306. }
  307.  
  308. /*ARGSUSED*/
  309. sendmidi(c)
  310. int c;
  311. {
  312.     if (!synthoutfileflag) return;
  313.  
  314.     if (!soopenflag) {
  315. #ifdef BSD
  316.         OPENBINFILE(sofile,Syofname,"w");
  317. #else
  318.         OPENBINFILE(sofile,Syofname,"wb");
  319. #endif
  320.         if (sofile != NULL)
  321.             soopenflag = 1;
  322.     }
  323.     if (soopenflag)
  324.         putc(c, sofile);
  325. }
  326.  
  327. flushmidi()
  328. {
  329.     if (!synthinfileflag && !synthoutfileflag)
  330.     while ( STATMIDI )
  331.         (void)getmidi();
  332.  
  333.     if (!synthoutfileflag) return;
  334.  
  335. /* To close files used in redirection of midi output, set
  336.  * synthoutfileflag and call me.
  337.  */
  338.     if (siopenflag) {
  339.         (void)fclose(sifile);
  340.         siopenflag = 0;
  341.     }
  342.     if (soopenflag) {
  343.         (void)fclose(sofile);
  344.         soopenflag = 0;
  345.     }
  346. }
  347.  
  348. long milliclock()
  349. {
  350.     static long hzcount = 0;
  351.  
  352.     return(hzcount++);
  353. }
  354.  
  355. millisleep(n)
  356. {
  357.     sleep((unsigned)((n+500)/1000));
  358. }
  359.  
  360. char *
  361. alloc(n)
  362. {
  363.     char *p;
  364.  
  365.     if ( (p=malloc((unsigned)n)) == (char *)NULL ) {
  366.         printf("*** Whoops *** alloc has failed?!?  No more memory!\n");
  367.         (void)fflush(stdout);
  368.         bye();
  369.     }
  370.     return(p);
  371. }
  372.  
  373. windinit()
  374. {
  375.     char *getenv();
  376.  
  377.     initscr();
  378.     Cols = 80;
  379.     Rows = 24;
  380.     noecho();
  381.     nonl();
  382. #ifdef BSD
  383.     crmode();
  384. #else
  385.     cbreak();
  386. #endif
  387.     initkbd();
  388. }
  389.  
  390. windgoto(r,c)
  391. int r,c;
  392. {
  393.     move(r,c);
  394. }
  395.  
  396. windeeol()
  397. {
  398.     clrtoeol();
  399. }
  400.  
  401. winderaserow(r)
  402. {
  403.     windgoto(r,0);
  404.     windeeol();
  405. }
  406.  
  407. windexit(r)
  408. int r;
  409. {
  410. #ifdef BSD
  411.     nocrmode();
  412. #else
  413.     nocbreak();
  414. #endif
  415.     nl();
  416.     echo();
  417.     endwin();
  418.     (void)exit(r);
  419. }
  420.  
  421. windclear()
  422. {
  423.     clear();
  424. }
  425.  
  426. /* windgets - get a line of input from the console, handling backspaces */
  427. windgets(s)
  428. char *s;
  429. {
  430.     char *origs = s;
  431.     int c;
  432.  
  433.     while ( (c=getconsole()) != '\n' && c!='\r' && c!= EOF ) {
  434.         if ( c == '\b' ) {
  435.             if ( s > origs ) {
  436.                 windstr("\b \b");
  437.                 s--;
  438.             }
  439.         }
  440.         else {
  441.             windputc(c);
  442.             *s++ = c;
  443.         }
  444.         windrefresh();
  445.     }
  446.     *s = '\0';
  447. }
  448.  
  449. windstr(s)
  450. char *s;
  451. {
  452.     int c;
  453.  
  454.     while ( (c=(*s++)) != '\0' )
  455.         windputc(c);
  456. }
  457.  
  458. windputc(c)
  459. int c;
  460. {
  461.     addch(c);
  462. }
  463.  
  464. windrefresh()
  465. {
  466.     refresh();
  467. }
  468.  
  469. beep()
  470. {
  471.     putchar('\007');
  472. }
  473.  
  474. windhigh()
  475. {
  476.     standout();
  477. }
  478.  
  479. windnorm()
  480. {
  481.     standend();
  482. }
  483.  
  484. /****************
  485.  * openls(), nextls(), and closels() are used to scan the current directory.
  486.  ***************/
  487.  
  488. FILE *Phrlist = NULL;
  489.  
  490. openls()
  491. {
  492.     FILE *popen();
  493.     
  494.     Phrlist = popen("ls","r");
  495. }
  496. char *
  497. nextls()
  498. {
  499.     static char fname[65];
  500.  
  501.     if ( fscanf(Phrlist,"%s",fname) != 1 )
  502.         return(NULL);
  503.     return(fname);
  504. }
  505. closels()
  506. {
  507.     pclose(Phrlist);
  508. }
  509.  
  510. #ifdef FAKECBREAK
  511. #include <sys/termio.h>
  512. struct termio Initterm;
  513. static int First = 1;
  514. cbreak()
  515. {
  516.     struct termio termbuff;
  517.  
  518.     if ( First  ) {
  519.         First = 0;
  520.         ioctl(0,TCGETA,&Initterm);
  521.     }
  522.     termbuff = Initterm;
  523.     termbuff.c_lflag &= (~ICANON);
  524.     termbuff.c_cc[4] = 1;
  525.     termbuff.c_cc[5] = 1;
  526.     ioctl(0,TCSETA,&termbuff);
  527. }
  528. nocbreak()
  529. {
  530.     ioctl(0,TCSETA,&Initterm);
  531. }
  532. #endif
  533.