home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #20 / NN_1992_20.iso / spool / comp / unix / programm / 4648 < prev    next >
Encoding:
Internet Message Format  |  1992-09-14  |  3.0 KB

  1. Path: sparky!uunet!cs.utexas.edu!bcm!rice!nb.rockwell.com!wade
  2. From: wade@nb.rockwell.com (Wade Guthrie)
  3. Newsgroups: comp.unix.programmer
  4. Subject: Re: checking stdin for keypress
  5. Keywords: keypress stdin
  6. Message-ID: <1992Sep11.162534.2728@nb.rockwell.com>
  7. Date: 11 Sep 1992 16:25:34 GMT
  8. References: <BuDH4F.LFv@news.cso.uiuc.edu>
  9. Sender: wade@nb.rockwell.com (Wade Guthrie)
  10. Organization: Rockwell International
  11. Lines: 58
  12.  
  13.  
  14. In article <BuDH4F.LFv@news.cso.uiuc.edu>, jrpg8255@uxa.cso.uiuc.edu (Jeremy Payne ) writes:
  15. > How does one check for 
  16. > keystrokes in the [UNIX tty] buffer?
  17.  
  18. This is done by talking to the tty driver (one uses the 'ioctl' system
  19. call -- short for I/O control, but usually pronounced 'eye-octal' -- to do this 
  20. under UNIX).  TFM to R is 'man 4 termio', but I'll try to summarize here.
  21.  
  22. ioctl is called with three arguments: the file descriptor, the request (both
  23. are integers) and the argument to the request (check out man 2 ioctl).
  24.  
  25. If I remember right, you need to call 'ioctl(fd, TCGETS, &termios)' to get the 
  26. value of the 'termios' structure.  You then replace termios->c_cc[VMIN] (this
  27. indicates the minimum characters to read on that file descriptor) with 0
  28. and termios->c_cc[VTIME] (this indicates the time before timeout in 0.10 second
  29. intervals) with 0.  Next, you call 'ioctl(fd, TCSETS, &termios)' to reset 
  30. the values for the file descriptor.
  31.  
  32. Once you've done this, you can do a read and check for the number of characters
  33. returned.  In conjunction with buffered I/O, I'm not sure how well this works;
  34. you may have to use 'read' rather than 'getchar'.
  35.  
  36. It's been awhile, so you may want to check out the man pages before you try
  37. this at home.
  38.  
  39. > (how are they buffered?) 
  40.  
  41. Well, how and whether keystrokes get buffered is a matter for the tty driver
  42. which is accessed by your program.  Keystrokes go into a queue in the tty driver
  43. called the raw queue.  The raw queue typically buffers keystrokes, including
  44. things like <delete> (others are interpereted, like <ctrl-c> until a newline
  45. is entered.  When this happens, the raw queue is dumped into another queue
  46. called the cannonical queue; it is during this dump that the <delete> and other
  47. special characters are interpereted.  All of this happens when the tty driver
  48. is in 'cooked' mode.
  49.  
  50. Another couple of modes exist.  The simplest is 'raw' mode (you could taste that
  51. one comming, huh?) wherein ALL characters that go into the raw queue are
  52. immediately flushed into the cannonical queue -- no interpretation involved.
  53. A mode in-between 'raw' and 'cooked' exists; this is called 'cbreak' mode.  This
  54. mode is just like 'raw' mode except a <cntrl-c> still is interpreted as a <break>
  55. (hence the name of the mode).
  56.  
  57. > On a similar note, could 
  58. > someone mention if and how scan codes etc. are generated for things like 
  59. > function keys etc.?  
  60. > A quick RTFM is fine :-) I've never used curses BTW if
  61. > that turns out to be the answer ...
  62.  
  63. 'curses' is a fine answer to the function key stuff, but may be an overkill.
  64. It may be the only portable way to solve that problem, however.
  65.  
  66. Hope this helps
  67.  
  68. Wade
  69. wade@nb.rockwell.com
  70.