Subject: RE: questions about keyboard

From: "Xu, Jiang" <[email-masked]>
Date: Thu, 15 Apr 2004 14:08:54 -0600

Thanks for the reply, I did some testing and found some interesting things:

1.  Everytime I push the key on the keyboard, I can see something out from
/dev/input/event0 by "cat /dev/input/event0".
However, I don't see a directory named "proc/input", did I miss configure
something in the kernel?
2.  read() does work and is a blocking read.  However, if I use select, then
it does not work.
Select() never detects the state change.
Here is the sample code I am using:
{
   int test_fd = -1;
   fd_set rfds;
   struct timeval tv;
  
   tv.tv_sec = 1;
   tv.tv_usec = 0;
   test_fd = open("/dev/input/event0", O_RDONLY); 
   if( test_fd < 0 )
     exit(0);
   
   while( 1 )
   {	
	FD_ZERO(&rfds);
	FD_SET(test_fd, &rfds);
      retval = select( 1, &rfds, NULL, NULL, &tv );
      if( retval )
         printf("\nDetects something....");
   }
}

What could be wrong?

Thanks

John

-----Original Message-----
From: Jan-Benedict Glaw [mailto:[email-masked]] 
Sent: Thursday, April 15, 2004 2:36 AM
To: Linux/MIPS Development
Subject: Re: questions about keyboard

On Wed, 2004-04-14 14:56:20 -0600, Xu, Jiang <[email-masked]> wrote
in message <[email-masked]>:
> 2.  In the application, how can I know which input/event# the usb 
> keyboard connects to?

You don't. You can

	- Hope that your keyboard is the one and only device...
	- Read /proc/bus/input/devices - there should be a "kbd" handler
	  in the "H: " section
	- select() on all event* devices and just only process
	  keypresses generated from "normal" keys.

> 3.  Is there some reference documents about how to read things from 
> input/event# ? I mean such as how to read key event?

I don't think there's really good docu available, but it's really simple.
Just open all devices, select() until there's data available (or just call a
blocking read() on it). Something like this should work, but you'd better
add error checking to the open() call...

#include <linux/input.h>

ssize_t ret;
struct input_event my_input;
int fd;

fd = open ("/dev/input/event0", O_RDONLY);
for (;;) {
	ret = read (fd, &my_input, sizeof (my_input));
	if (ret != sizeof (my_input)
		break;

	if (my_input.type != EV_KEY)
		continue;
	/* my_input.code and my_input.type now contain the key and
	   press/release state; refer to the #defines in linux/input.h
	   for the mapping .code -> ASCII value */
}
close (fd);

-- 
   Jan-Benedict Glaw       [email-masked]    . +49-172-7608481
   "Eine Freie Meinung in  einem Freien Kopf    | Gegen Zensur | Gegen Krieg
    fuer einen Freien Staat voll Freier Bürger" | im Internet! |   im Irak!
   ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM |
TCPA));

---

From: Jan-Benedict Glaw <[email-masked]>
Date: Thu, 15 Apr 2004 22:22:18 +0200

On Thu, 2004-04-15 14:08:54 -0600, Xu, Jiang <[email-masked]>
> However, I don't see a directory named "proc/input", did I miss configure

Maybe your kernel isn't configured with CONFIG_PROC_FS=y ?
Maybe it's not mounted?

> Here is the sample code I am using:

No need to initialize - you're assigning a value before accessing it.

>    tv.tv_usec = 0;

tv_* need to be set before *every* select () invocation, not only once.

>       retval = select( 1, &rfds, NULL, NULL, &tv );

That's wrong. The "1" should be "fd + 1".

>          printf("\nDetects something....");

A negative retval would also be != 0 ...

> What could be wrong?

Most probably it's the hardcoded "1" with the select. That is, select
only looks at all fd's which are smaller than one, so only fd=0 would be
testes, but this one isn't in the set, so...

MfG, JBG

-- 
   Jan-Benedict Glaw       [email-masked]    . +49-172-7608481
   "Eine Freie Meinung in  einem Freien Kopf    | Gegen Zensur | Gegen Krieg
    fuer einen Freien Staat voll Freier Bürger" | im Internet! |   im Irak!
   ret = do_actions((curr | FREE_SPEECH) & ~(NEW_COPYRIGHT_LAW | DRM | TCPA));