[comp.unix.questions] how to poll keyboard

nakada@husc8.HARVARD.EDU (Paul Nakada) (08/04/88)

I am in need of a way to read a terminal keyboard without
stopping if there is no keypress...  this is the scenario..

   for(ever)
	   checkkeypress();
	   dostuff according to keypress()
   endforever

what i need is to do the keyprocessing even if there is no keypress..
in essence skipping over the checkkeypress if there is no keypress...
is there any way to do this?  i have been unsuccessful in using
rawmode and curses....   can someone please enlighten me??
thanks... please respond by email...
-paul

-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-
 Paul Nakada '89  #8-)                          nakada@husc4.harvard.edu
 Harvard College                                     nakada@husc4.bitnet
 Cambridge, MA       {ihnp4!think, seismo, rutgers}!harvard!husc4!nakada
_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_

spolsky@eniac.seas.upenn.edu (Joel Spolsky) (08/04/88)

In article <5065@husc6.harvard.edu> nakada@husc8.UUCP (Paul Nakada) writes:
>
>I am in need of a way to read a terminal keyboard without
>stopping if there is no keypress...  this is the scenario..
>
>is there any way to do this?  i have been unsuccessful in using
>rawmode and curses....   can someone please enlighten me??
>-paul

What you want is cbreak mode, not raw mode. Raw is overkill.

Joel Spolsky
Bell Communications Research
spolsky@eniac.seas.upenn.edu
or ...!rutgers!bellcore!pyuxss!spolsky

swilson%thetone@Sun.COM (Scott Wilson) (08/04/88)

In article <5065@husc6.harvard.edu> nakada@husc8.UUCP (Paul Nakada) writes:
>I am in need of a way to read a terminal keyboard without
>stopping if there is no keypress...  this is the scenario..

If you are using a UNIX that has the select(2) system call try
opening /dev/tty then calling select with a timeout of zero.  That
is not a NULL pointer for the timeout struct but a struct with
the time fields set to zero.  This is effectively a poll of
the process' controlling terminal to see if any characters have
been typed.  I have found some select man pages to be less than
obvious about its features.

If your UNIX is sys5 based and has streams you can probably do
the same thing with poll(2) but I have never used it.

--
Scott Wilson		arpa: swilson@sun.com		"Why do dogs lick
Sun Microsystems	uucp: ...!sun!swilson		their balls?  Because
Mt. View, CA						they can!"

guy@gorodish.Sun.COM (Guy Harris) (08/04/88)

> If you are using a UNIX that has the select(2) system call try
> opening /dev/tty then calling select with a timeout of zero.

No need to open "/dev/tty", just use the same descriptor you would use for the
"read".

> If your UNIX is sys5 based and has streams you can probably do
> the same thing with poll(2) but I have never used it.

Only if your tty driver is streams-based; the only generally-available
system I know of that has no *non*-streams-based ttys is SunOS - S5R3 as
distributed by AT&T still has non-streams-based ttys.

However, both 4.[23]BSD and S5 support non-blocking mode on ttys; in this mode,
a "read" doesn't block when no data is available, it returns -1 and sets
"errno" to EWOULDBLOCK in 4.[23]BSD and returns 0 in S5.  See FCNTL(2) in the
BSD documentation, or FCNTL(2) and READ(2) in the S5 documentation.

jfh@rpp386.UUCP (John F. Haugh II) (08/05/88)

In article <5065@husc6.harvard.edu> nakada@husc8.UUCP (Paul Nakada) writes:
>
>I am in need of a way to read a terminal keyboard without
>stopping if there is no keypress...

open the file with O_NDELAY.  or, you can use the VTIME/VMIN feature and
try a few different values.  there is, however, no way to tell (in general)
if a key is _currently_ being pressed.
-- 
John F. Haugh II                 +--------- Cute Chocolate Quote ---------
HASA, "S" Division               | "USENET should not be confused with
UUCP:   killer!rpp386!jfh        |  something that matters, like CHOCOLATE"
DOMAIN: jfh@rpp386.uucp          |         -- apologizes to Dennis O'Connor

bowles@lll-crg.llnl.gov (Jeff Bowles) (08/05/88)

In article <62727@sun.uucp> swilson@sun.UUCP (Scott Wilson) writes:
>In article <5065@husc6.harvard.edu> nakada@husc8.UUCP (Paul Nakada) writes:
>>I am in need of a way to read a terminal keyboard without
>>stopping if there is no keypress...
>
>If your UNIX is sys5 based and has streams you can probably do
>the same thing with poll(2) but I have never used it.

Wrong, camel-breath! (Sorry, I'm in a giddy mood.)

The poll(2) system call that was added in System V Release 3 is an
exercise in incomplete implementations. poll(2) takes a list of
file descriptions pointing to STREAMS devices and returns when any
"interesting" event happens or when a timeout period you specify
expires. If the timeout period is 0, poll(2) returns immediately.
Examples of "interesting" events are input coming in, errors occurring
on the stream, and so on.

Unfortunately, it only works for STREAMS devices, and the tty subsystem
is the old, tried-and-not-trusted clist implementation. You can't get
there from here using poll(2) yet.

Now, if you use termio(7)  (ioctl calls) you can request read system
calls to time out, with 0.1 second granularity. Unfortunately, there's
not provisions for immediate return --- and 100 milliseconds, for some
people, feels like a long time.

	Jeff Bowles

friedl@vsi.UUCP (Stephen J. Friedl) (08/06/88)

In article <11069@lll-winken.llnl.gov>, bowles@lll-crg.llnl.gov (Jeff Bowles) writes:
> [discussion of poll(2) under Sys V Rel 3]
> Unfortunately, it only works for STREAMS devices, and the tty subsystem
> is the old, tried-and-not-trusted clist implementation. You can't get
> there from here using poll(2) yet.

I understand that Sys V Rel 4 will include a streams-based tty
subsystem.  This will cure lots of evils besides allowing poll
to work...

-- 
Steve Friedl    V-Systems, Inc.  +1 714 545 6442    3B2-kind-of-guy
friedl@vsi.com     {backbones}!vsi.com!friedl    attmail!vsi!friedl
--------- Nancy Reagan on flood-control: "Just say Noah"-----------