home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume25 / letters / kinput.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-02  |  3.6 KB  |  195 lines

  1. /*
  2.  * do non-blocking keyboard reads
  3.  *
  4.  * copyright 1991 by Larry Moss (lm03_cif@uhura.cc.rochester.edu)
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <signal.h>
  9. #include <sys/types.h>
  10. /* I know some systems prefer time.h, but I'm not sure which */
  11. #include <sys/time.h>
  12. #ifdef SYSV
  13. #include <termio.h>
  14. #else  /* SYSV */
  15. #include <sgtty.h>
  16. #endif /* SYSV */
  17.  
  18. #include "kinput.h"
  19. #include "terms.h"
  20. #include "term.h"
  21.  
  22. #define SCREENLENGTH 24
  23.  
  24. extern unsigned int score, word_count, level;
  25.  
  26. int key_pressed();
  27.  
  28. static interrupt();
  29.  
  30. #ifndef NOJOB
  31. static pause();
  32. static cont();
  33. #endif
  34.  
  35. static die();
  36.  
  37. /*
  38.  * This function will return -1 if no key is available, or the key
  39.  * that was pressed by the user.  It is checking stdin, without blocking.
  40.  */
  41. int key_pressed()
  42. {
  43. #ifdef SYSV
  44.     int        chars_read;
  45.     static char    keypressed;
  46.  
  47.     chars_read = read(0, &keypressed, 1);
  48.     if (chars_read == 1)
  49.         return((int)keypressed);
  50.     return(-1);
  51. #else /* SYSV */
  52.     int        mask = 1, chars_read;
  53.     static char    keypressed;
  54.     struct timeval    waittime;
  55.  
  56.     waittime.tv_sec=0;
  57.     waittime.tv_usec=4;
  58.     if (select(1, &mask, 0, 0, &waittime)) {
  59.         chars_read = read(0, &keypressed, 1);
  60.         if (chars_read == 1)
  61.             return((int)keypressed);
  62.     }
  63.     return(-1);
  64. #endif /* SYSV */
  65. }
  66.  
  67. /*
  68.  * Set the terminal to cbreak mode, turn off echo and prevent special
  69.  * characters from affecting the input.  We will handle EOF and other fun
  70.  * stuff our own way.  Backup copies of the current setup will be kept
  71.  * to insure the terminal gets returned to its initial state.
  72.  */
  73. void setterm(setting)
  74. int setting;
  75. {
  76. #ifdef SYSV
  77.     struct termio    termrec;
  78.     static struct termio    old_termrec;
  79. #else  /* SYSV */
  80.     struct sgttyb    termrec;
  81.     struct tchars    trec;
  82.     static struct tchars    old_trec;
  83.     static struct sgttyb    old_termrec;
  84. #endif /* SYSV */
  85.  
  86.     if(setting == NEW) {
  87.         signal(SIGINT, interrupt);
  88.         signal(SIGQUIT, die);
  89.         signal(SIGTERM, die);
  90. #ifndef NOJOB
  91.         signal(SIGTSTP, pause);
  92.         signal(SIGCONT, cont);
  93. #endif /* NOJOB */
  94.         
  95. #ifdef SYSV
  96.         ioctl(0, TCGETA, &termrec);
  97.         old_termrec = termrec;
  98.         termrec.c_iflag &= ~(IGNPAR|PARMRK|INLCR|IGNCR|ICRNL);
  99.         termrec.c_iflag |= BRKINT;
  100.         termrec.c_lflag &= ~(ICANON|ECHO);
  101.         termrec.c_cc[VTIME] = 0;
  102.         termrec.c_cc[VMIN] = 0;
  103.         ioctl(0, TCSETAF, &termrec);
  104. #else /* SYSV */
  105.         ioctl(0, TIOCGETP, &termrec);
  106.         old_termrec = termrec;
  107.         termrec.sg_flags |= CBREAK;
  108.         termrec.sg_flags &= ~ECHO;
  109.         ioctl(0, TIOCSETP, &termrec);
  110.  
  111.         ioctl(0, TIOCGETC, &trec);
  112.         old_trec = trec;
  113.         trec.t_eofc = (char) -1;
  114.         trec.t_quitc = (char) -1;
  115.         ioctl(0, TIOCSETC, &trec);
  116. #endif /* SYSV */
  117.     }
  118.     else {
  119.         signal(SIGINT, SIG_DFL);
  120.         signal(SIGQUIT, SIG_DFL);
  121.         signal(SIGTERM, SIG_DFL);
  122. #ifndef NOJOB
  123.         signal(SIGTSTP, SIG_DFL);
  124. #endif  /* NOJOB */
  125.  
  126. #ifdef SYSV
  127.         ioctl(0, TCSETAF, &old_termrec);
  128. #else /* SYSV */
  129.         ioctl(0, TIOCSETP, &old_termrec);
  130.         ioctl(0, TIOCSETC, &old_trec);
  131. #endif /* SYSV */
  132.     }
  133. }
  134.  
  135.  
  136. /*
  137.  * Interrupt handlers
  138.  */
  139.  
  140. #ifndef NOJOB
  141. static pause(sig)
  142. int    sig;
  143. {
  144. #ifdef SYSV
  145.     signal(sig, interrupt);
  146. #endif /* SYSV */
  147.     gotoxy(0, SCREENLENGTH);
  148.     setterm(ORIG);
  149.     putchar('\n');
  150.     kill(getpid(), SIGSTOP);
  151. }
  152.  
  153. static cont(sig)
  154. int    sig;
  155. {
  156. #ifdef SYSV
  157.     signal(sig, interrupt);
  158. #endif /* SYSV */
  159.     setterm(NEW);
  160.     redraw();
  161. }
  162.  
  163. #endif /* NOJOB */
  164.  
  165. static interrupt(sig)
  166. int    sig;
  167. {
  168.     char    c;
  169.  
  170.     setterm(ORIG);
  171.     printf("\n\rare you sure you want to quit? ");
  172.     if((c = getchar()) == 'y' || c == 'Y') {
  173.         clrdisp();
  174.         printf("\n\nfinal: score = %u\twords = %u\t level = %d\n",
  175.                score, word_count, level);
  176.         highlight(0);
  177.         exit(1);
  178.     } else {
  179. #ifdef SYSV
  180.         signal(sig, interrupt);
  181. #endif /* SYSV */
  182.         setterm(NEW);
  183.         redraw();
  184.     }
  185. }
  186.  
  187. static die(sig)
  188. int    sig;
  189. {
  190.     setterm(ORIG);
  191.     clrdisp();
  192.     highlight(0);
  193.     exit(1);
  194. }
  195.