home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / os / linux / 9725 < prev    next >
Encoding:
Text File  |  1992-09-02  |  2.9 KB  |  82 lines

  1. Path: sparky!uunet!mcsun!sun4nl!hgatenl!tedux!clifton!baron
  2. From: baron@clifton.hobby.nl (Wim `Blue Baron' van Dorst)
  3. Newsgroups: comp.os.linux
  4. Distribution: world
  5. Subject: Re: Polling keyboard in linux. What works best?
  6. References: <1992Aug31.124931.8528@ncsu.edu>
  7. Message-ID: <9209023072@clifton.hobby.nl>
  8. Organization: Minix Hobbynet Host
  9. Date: Wed,  2 Sep 1992 22:12:55 MDT
  10. Lines: 70
  11.  
  12. > In article <17nf23INNfr8@matt.ksu.ksu.edu>, kxb@matt.ksu.ksu.edu (Karl Buck) writes:
  13. > I want to check the keyboard for input (using gcc of course) without
  14. > actually reading (ie: getc etc..). The unix FAQ suggests looking at
  15. > the FIONREAD ioctl or using select() and the C FAQ states that this is
  16. > a purely OS dependant question.
  17. > What is best for Linux? Can this be non-blocking? A snippet of code
  18. > for this would be great since I have not had to do this kind of thing
  19. > before. Thanks.
  20.  
  21. jlnance@eos.ncsu.edu (JAMES LEWIS NANCE) writes:
  22. > I was given the following segement of code when I asked this question.  
  23. > [ stuff deleted -b-b- ]
  24. > #include     <sgtty.h>
  25. > #include    <fcntl.h>
  26. > struct sgttyb normal_mode, cbreak_mode;
  27. > [ stuff deleted -b-b- ]
  28. >     ioctl(0, TIOCGETP, &normal_mode);
  29. >     cbreak_mode = normal_mode;
  30. >     cbreak_mode.sg_flags |= CBREAK;    /* make characters available as typed */
  31. >     cbreak_mode.sg_flags &= ~ECHO;    /* turn off echo */
  32.  
  33. Sorry, but that won't work. I currently am working on a little game, and
  34. of course you have to test for key-pressing. I had code like the above working
  35. properly in Minix, but for Linux the implementation is purely Posix, which
  36. the above is not.
  37.  
  38. I very easily changed my code to Termios (i.e., the Posix and thus Linux
  39. way of doing such things) for the what I wanted, using indeed the non-blocked 
  40. read(). A very simple example is this:
  41.  
  42. #include <sys/types.h>
  43. #include <termios.h>
  44. #include <fcntl.h>
  45. #include <stdlib.h>
  46. #include <stdio.h>
  47.  
  48. main()
  49. {
  50.     int ch = 'a', counter;
  51.     struct termios *tp;
  52.  
  53.     tcgetattr(0, tp);
  54.     tp->c_lflag &= ~(ECHO | ICANON);
  55.     tcsetattr(0, TCSANOW, tp);
  56.     fcntl(0, F_SETFL, O_NONBLOCK);
  57.  
  58.     while (ch != 'q') {
  59.         for (counter=0;counter<100000;counter++) /* */ ;
  60.         if (read(0, &ch, 1) == -1)
  61.             printf("Non-blocked ch=%c\n", (char) ch);
  62.         else 
  63.             printf("Typed ch=%c\n", (char) ch);
  64.     }
  65.         
  66.     tp->c_lflag |= (ECHO | ICANON);
  67.     tcsetattr(0, TCSANOW, tp);
  68.     fcntl(0, F_SETFL, O_ACCMODE);
  69. }
  70.  
  71. Of course this code actually _reads_ the character: It is not a test 
  72. whether a character is available. Using code like this, however, such an
  73. implementation could easily be made using a buffer for actually read
  74. characters, and passing them on only when wanted. 
  75.  
  76. Met vriendelijke groeten, Wim `Blue Baron' van Dorst
  77. ---------------------------------------------------------------------
  78. Blue Baron = Wim van Dorst, Voice (+31) 074-443937, (+31) 02152-42319
  79. (-:       baron@clifton.hobby.nl          tgcpwd@urc.tue.nl       :-)
  80. ---------------------------------------------------------------------
  81.