home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ober1096.zip / libsrc / keypress.c < prev    next >
C/C++ Source or Header  |  1996-10-16  |  3KB  |  107 lines

  1. #ifdef djgpp
  2. #  define DOS_CONIO
  3. #endif
  4. #ifdef os2
  5. #  define DOS_CONIO
  6. #endif
  7.  
  8. #ifdef DOS_CONIO
  9. #  include <conio.h>
  10. #  include <io.h>
  11. #else
  12. #  include <fcntl.h>
  13. #  include <sys/ioctl.h>
  14. #  include <stdio.h>
  15. #  ifdef opsys_sysV
  16. #    include <sys/termio.h>      /* sysV  */
  17. #  else
  18. #    include <sgtty.h>           /* bsd   */
  19. #  endif
  20. #endif
  21.  
  22.  
  23. /* *************** XYplane_Key () *************** */
  24.  
  25. #ifdef DOS_CONIO
  26.  
  27. char XYplane_Key ()
  28. {
  29.    char c = getch();
  30.    if (c == '\r') c = '\n';    /* Must convert carriage return */
  31.    return c;
  32. }
  33. #else
  34. #ifdef opsys_sysV
  35.  
  36. char XYplane_Key ()
  37. {
  38.     int sav,idummy;
  39.     char x;
  40.     struct termio savio, newio;
  41. /* save current mode */
  42.     ioctl(0,TCGETA,&savio);
  43.     newio = savio;
  44. /* set raw mode */
  45.     newio.c_lflag &= ~(ECHO + ICANON + ISIG);
  46.     newio.c_cc[VMIN] = '\001';
  47.     ioctl(0,TCSETA,&newio);
  48. /* save the file descriptor status flags */
  49.     sav=fcntl(0,F_GETFL,0);
  50. /* set the status flag for O_NDELAY = O_NONBLOCK */
  51.     idummy=fcntl(0,F_SETFL,O_NDELAY);
  52. /*
  53.  * try to read from stdin ... if read returns -1 then there was no character
  54.  * to be read in the buffer. If the read returns any other value then it is
  55.  * the character read.
  56.  */
  57.     x = getchar();
  58. /* set the flags to zero to cancel the O_NDELAY bit then set the flags back
  59.    to the saved original values.  */
  60.     idummy=fcntl(0,F_SETFL,0);
  61.     idummy=fcntl(0,F_SETFL,sav);
  62. /* return to canonical mode */
  63.     ioctl(0,TCSETA,&savio);
  64.     if ((x == savio.c_cc[VINTR]) && (savio.c_lflag & ISIG)) exit(1);
  65.  
  66.     if(x != -1) return x;
  67.     return '\0';
  68. }
  69. #endif  /* SystemV version of Key */
  70.  
  71.  
  72. #ifdef opsys_bsd
  73.  
  74. char XYplane_Key ()
  75. {
  76.     struct sgttyb savio, newio;
  77.     struct tchars chrs;
  78.     int x, sav,idummy;
  79.  
  80. /* save current mode */
  81.     ioctl(0,TIOCGETP,&savio);
  82.     ioctl(0,TIOCGETC,&chrs);
  83.     newio = savio;
  84. /* set into cbreak mode */
  85.     newio.sg_flags = (newio.sg_flags & ~(ECHO + CBREAK)) | RAW ;
  86.     ioctl(0,TIOCSETP,&newio);
  87. /* save the file descriptor status flags */
  88.     sav=fcntl(0,F_GETFL,0);
  89. /* set the status flag for O_NONBLOCK */
  90.     idummy=fcntl(0,F_SETFL,O_NONBLOCK);
  91. /* try to read from stdin...if read returns -1 then there
  92.    was no character to be read in the buffer. If the read returns
  93.    any other value (0) then there was a character to be read.  */
  94.     x = getchar();
  95. /* set the flags to zero to cancel the O_NDELAY bit then set the flags back
  96.    to the saved original values.  */
  97.     idummy=fcntl(0,F_SETFL,0);
  98.     idummy=fcntl(0,F_SETFL,sav);
  99. /* now return to canonical mode */
  100.     ioctl(0,TIOCSETP,&savio);
  101.     if ((char)x == chrs.t_intrc) exit(1);
  102.     if (x == EOF) x = 0;
  103.     return (char)x;
  104. }
  105. # endif /* BSD version of Key */
  106. #endif        /* DOS_CONIO */
  107.