* Unbuffered char reads from App from stdin (keyboard)
@ 2002-02-15 15:09 Steven Vacca
2002-02-15 15:31 ` Wolfgang Denk
0 siblings, 1 reply; 5+ messages in thread
From: Steven Vacca @ 2002-02-15 15:09 UTC (permalink / raw)
To: LinuxEmbeddedMailList (E-mail)
App and Linux kernel 2.2.13 running on mpc860T.
I would like to know the best way, from inside my GNU C App,
to sense if there's a keyboard key pressed. Something
similar to kbhit() in Microsoft C. I would like to have a particular
thread execute a loop continuously and only when a key
is present, do a getc() or getchar(), or gets(), and the like.
What is the best way to accomplish this?
Is there a way to make stdin unbuffered?
Thanks,
ShutEyeThinkin
** Sent via the linuxppc-embedded mail list. See http://lists.linuxppc.org/
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Unbuffered char reads from App from stdin (keyboard)
2002-02-15 15:09 Unbuffered char reads from App from stdin (keyboard) Steven Vacca
@ 2002-02-15 15:31 ` Wolfgang Denk
0 siblings, 0 replies; 5+ messages in thread
From: Wolfgang Denk @ 2002-02-15 15:31 UTC (permalink / raw)
To: svacca@valcom.com; +Cc: LinuxEmbeddedMailList (E-mail)
In message <01C1B608.E92871A0.svacca@valcom.com> you wrote:
>
> App and Linux kernel 2.2.13 running on mpc860T.
>
> I would like to know the best way, from inside my GNU C App,
> to sense if there's a keyboard key pressed. Something
man select
man termios (search for VMIN and VTIME)
Wolfgang Denk
--
Software Engineering: Embedded and Realtime Systems, Embedded Linux
Phone: (+49)-8142-4596-87 Fax: (+49)-8142-4596-88 Email: wd@denx.de
See us @ Embedded Systems Nuremberg, Feb 19-21, Hall 12 K01 (with TQ)
** Sent via the linuxppc-embedded mail list. See http://lists.linuxppc.org/
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: Unbuffered char reads from App from stdin (keyboard)
@ 2002-02-15 18:39 Steven Vacca
2002-02-15 18:41 ` Rick Hunnicutt
0 siblings, 1 reply; 5+ messages in thread
From: Steven Vacca @ 2002-02-15 18:39 UTC (permalink / raw)
To: LinuxEmbeddedMailList (E-mail)
Mark,
Using some of your unbuf_getch() code inside a while (1)
loop, fgetc() still blocks until a '\n', then, with each iteration
of the loop, 1 char is returned from the fgetc() until all chars are
returned, then fgetc() blocks again.
Is there a way to get chars without blocking and waiting for
the '\n'?
Thanks,
ShutEyeThinkin
//*************************************************************************
***********
Mark's code:
Hi,
I found some examples on groups.google.com that I modified
to suite my needs. Try something like these:
int
unbuf_getch()
{
FILE *input;
int selected;
struct termios initial_settings;
struct termios new_settings;
if (!isatty(fileno(stdout))) {
fprintf(stderr,"You are not a terminal, OK.\n");
}
input = fopen("/dev/tty", "r");
if(!input) {
fprintf(stderr, "Unable to open /dev/tty\n");
exit(1);
}
tcgetattr(fileno(input),&initial_settings);
new_settings = initial_settings;
new_settings.c_lflag &= ~ICANON;
new_settings.c_lflag &= ~ECHO;
new_settings.c_cc[VMIN] = 1;
new_settings.c_cc[VTIME] = 0;
new_settings.c_lflag &= ~ISIG;
if(tcsetattr(fileno(input), TCSANOW, &new_settings) != 0) {
fprintf(stderr,"could not set attributes\n");
}
selected = fgetc(input);
tcsetattr(fileno(input),TCSANOW,&initial_settings);
fclose(input);
return selected;
}
int
kbhit()
{
int ret, c;
fd_set read_file_descr;
struct timeval timeout;
int debug_flag;
/* this could be a global */
debug_flag = 0;
/* this macro initializes the file descriptor read_file_descr to to be
the empty set */
FD_ZERO(&read_file_descr);
/* this macro adds fileno(stdin) to the file descriptor read_file_descr
*/
FD_SET(fileno(stdin), &read_file_descr);
timeout.tv_sec = 0;
timeout.tv_usec = 100;
/* int FD_SETSIZE macro is maximum number of filedescriptors that fd_set
can hold */
/* function select waits for specified filedescr. to have a signal */
/* last argument struct timeval *timeout */
ret = select(1, &read_file_descr, NULL, NULL, &timeout);
switch( ret ) /* 0 is timeout, -1 error (in errno), 1 = data */
{
case -1:
if( debug_flag )
fprintf(stdout, "select returned -1 error\n");
ret = 0;
break;
case 0:
if( debug_flag )
fprintf(stdout, "select returned 0 timeout\n");
ret = 0;
break;
case 1:
if( debug_flag )
fprintf(stdout, "SELECT returned=%d input\n", ret);
ret = 1;
break;
default:
if( debug_flag )
fprintf(stdout, "select returned=%d invalid\n", ret);
break;
}
/* test if user has data. this'll eat the first non-CR keys pressed */
if( FD_ISSET(fileno(stdin), &read_file_descr) )
{
c = getc(stdin);
if( debug_flag )
fprintf(stdout, "USER KEY=%d\n", c);
FD_CLR(fileno(stdin), &read_file_descr);
}
return ret;
}
Mark
--
[root@hjinc mclayton] /sbin/insmod stddisclaimer.o
> -----Original Message-----
> From: Steven Vacca [mailto:svacca@valcom.com]
> Sent: Friday, February 15, 2002 10:10 AM
> To: LinuxEmbeddedMailList (E-mail)
> Subject: Unbuffered char reads from App from stdin (keyboard)
>
>
>
> App and Linux kernel 2.2.13 running on mpc860T.
>
> I would like to know the best way, from inside my GNU C App,
> to sense if there's a keyboard key pressed. Something
> similar to kbhit() in Microsoft C. I would like to have a particular
> thread execute a loop continuously and only when a key
> is present, do a getc() or getchar(), or gets(), and the like.
>
> What is the best way to accomplish this?
> Is there a way to make stdin unbuffered?
>
> Thanks,
>
> ShutEyeThinkin
>
>
** Sent via the linuxppc-embedded mail list. See http://lists.linuxppc.org/
^ permalink raw reply [flat|nested] 5+ messages in thread* RE: Unbuffered char reads from App from stdin (keyboard)
2002-02-15 18:39 Steven Vacca
@ 2002-02-15 18:41 ` Rick Hunnicutt
0 siblings, 0 replies; 5+ messages in thread
From: Rick Hunnicutt @ 2002-02-15 18:41 UTC (permalink / raw)
To: svacca, 'LinuxEmbeddedMailList (E-mail)'
Just do a read on STD_IN (fd of 0) of one char...
#define STD_IN 0
char ch;
read(STD_IN, &ch, 1)
> -----Original Message-----
> From: owner-linuxppc-embedded@lists.linuxppc.org
> [mailto:owner-linuxppc-embedded@lists.linuxppc.org]On Behalf Of Steven
> Vacca
> Sent: Friday, February 15, 2002 10:39 AM
> To: LinuxEmbeddedMailList (E-mail)
> Subject: RE: Unbuffered char reads from App from stdin (keyboard)
>
>
>
> Mark,
>
> Using some of your unbuf_getch() code inside a while (1)
> loop, fgetc() still blocks until a '\n', then, with each iteration
> of the loop, 1 char is returned from the fgetc() until all chars are
> returned, then fgetc() blocks again.
>
> Is there a way to get chars without blocking and waiting for
> the '\n'?
>
> Thanks,
>
> ShutEyeThinkin
>
>
>
>
>
> //************************************************************
> *************
> ***********
> Mark's code:
>
> Hi,
>
> I found some examples on groups.google.com that I modified
> to suite my needs. Try something like these:
>
> int
> unbuf_getch()
> {
> FILE *input;
> int selected;
> struct termios initial_settings;
> struct termios new_settings;
>
> if (!isatty(fileno(stdout))) {
> fprintf(stderr,"You are not a terminal, OK.\n");
> }
>
> input = fopen("/dev/tty", "r");
> if(!input) {
> fprintf(stderr, "Unable to open /dev/tty\n");
> exit(1);
> }
>
> tcgetattr(fileno(input),&initial_settings);
> new_settings = initial_settings;
> new_settings.c_lflag &= ~ICANON;
> new_settings.c_lflag &= ~ECHO;
> new_settings.c_cc[VMIN] = 1;
> new_settings.c_cc[VTIME] = 0;
> new_settings.c_lflag &= ~ISIG;
> if(tcsetattr(fileno(input), TCSANOW, &new_settings) != 0) {
> fprintf(stderr,"could not set attributes\n");
> }
> selected = fgetc(input);
> tcsetattr(fileno(input),TCSANOW,&initial_settings);
>
> fclose(input);
> return selected;
> }
>
> int
> kbhit()
> {
> int ret, c;
> fd_set read_file_descr;
> struct timeval timeout;
> int debug_flag;
>
> /* this could be a global */
> debug_flag = 0;
>
> /* this macro initializes the file descriptor
> read_file_descr to to be
> the empty set */
> FD_ZERO(&read_file_descr);
>
> /* this macro adds fileno(stdin) to the file descriptor
> read_file_descr
> */
> FD_SET(fileno(stdin), &read_file_descr);
>
> timeout.tv_sec = 0;
> timeout.tv_usec = 100;
> /* int FD_SETSIZE macro is maximum number of
> filedescriptors that fd_set
> can hold */
> /* function select waits for specified filedescr. to have
> a signal */
> /* last argument struct timeval *timeout */
> ret = select(1, &read_file_descr, NULL, NULL, &timeout);
> switch( ret ) /* 0 is timeout, -1 error (in errno), 1 = data */
> {
> case -1:
> if( debug_flag )
> fprintf(stdout, "select returned -1 error\n");
> ret = 0;
> break;
> case 0:
> if( debug_flag )
> fprintf(stdout, "select returned 0 timeout\n");
> ret = 0;
> break;
> case 1:
> if( debug_flag )
> fprintf(stdout, "SELECT returned=%d input\n", ret);
> ret = 1;
> break;
> default:
> if( debug_flag )
> fprintf(stdout, "select returned=%d invalid\n", ret);
> break;
> }
>
> /* test if user has data. this'll eat the first non-CR
> keys pressed */
> if( FD_ISSET(fileno(stdin), &read_file_descr) )
> {
> c = getc(stdin);
> if( debug_flag )
> fprintf(stdout, "USER KEY=%d\n", c);
> FD_CLR(fileno(stdin), &read_file_descr);
> }
>
> return ret;
> }
>
> Mark
> --
> [root@hjinc mclayton] /sbin/insmod stddisclaimer.o
>
>
> > -----Original Message-----
> > From: Steven Vacca [mailto:svacca@valcom.com]
> > Sent: Friday, February 15, 2002 10:10 AM
> > To: LinuxEmbeddedMailList (E-mail)
> > Subject: Unbuffered char reads from App from stdin (keyboard)
> >
> >
> >
> > App and Linux kernel 2.2.13 running on mpc860T.
> >
> > I would like to know the best way, from inside my GNU C App,
> > to sense if there's a keyboard key pressed. Something
> > similar to kbhit() in Microsoft C. I would like to have a
> particular
> > thread execute a loop continuously and only when a key
> > is present, do a getc() or getchar(), or gets(), and the like.
> >
> > What is the best way to accomplish this?
> > Is there a way to make stdin unbuffered?
> >
> > Thanks,
> >
> > ShutEyeThinkin
> >
> >
>
** Sent via the linuxppc-embedded mail list. See http://lists.linuxppc.org/
^ permalink raw reply [flat|nested] 5+ messages in thread
* RE: Unbuffered char reads from App from stdin (keyboard)
@ 2002-02-18 18:28 Steven Vacca
0 siblings, 0 replies; 5+ messages in thread
From: Steven Vacca @ 2002-02-18 18:28 UTC (permalink / raw)
To: 'Steven Vacca', LinuxEmbeddedMailList (E-mail)
FYI (For Y'all's Info):
Here's what I've done.
I wanted to check if 1 or more keys have been pressed, and if so,
get just the next available key in the buffer.
I then wanted to process that key before echoing it to the
terminal, if at all, for nth-degree of control.
- I used termios to disable console Cannonical mode and Echoes.
- I used fcntl() to make stdin Non_Blocking, since, if there's no
key in the buffer, read() will block.
- I then use:
read(0,&key,1);
to read just the next key, without blocking when there is no key
in the buffer.
ShutEyeThinkin
-----Original Message-----
From: Steven Vacca [SMTP:svacca@valcom.com]
Sent: Friday, February 15, 2002 10:10 AM
To: LinuxEmbeddedMailList (E-mail)
Subject: Unbuffered char reads from App from stdin (keyboard)
App and Linux kernel 2.2.13 running on mpc860T.
I would like to know the best way, from inside my GNU C App,
to sense if there's a keyboard key pressed. Something
similar to kbhit() in Microsoft C. I would like to have a particular
thread execute a loop continuously and only when a key
is present, do a getc() or getchar(), or gets(), and the like.
What is the best way to accomplish this?
Is there a way to make stdin unbuffered?
Thanks,
ShutEyeThinkin
** Sent via the linuxppc-embedded mail list. See http://lists.linuxppc.org/
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2002-02-18 18:28 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-02-15 15:09 Unbuffered char reads from App from stdin (keyboard) Steven Vacca
2002-02-15 15:31 ` Wolfgang Denk
-- strict thread matches above, loose matches on Subject: below --
2002-02-15 18:39 Steven Vacca
2002-02-15 18:41 ` Rick Hunnicutt
2002-02-18 18:28 Steven Vacca
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).