Guest User

raw.c

a guest
Sep 1st, 2011
453
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <errno.h>
  6. #include <fcntl.h>
  7. #include <dirent.h>
  8. #include <linux/input.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <sys/select.h>
  12. #include <sys/time.h>
  13. #include <termios.h>
  14. #include <signal.h>
  15.  
  16. void handler (int sig)
  17. {
  18.   printf ("\nexiting...(%d)\n", sig);
  19.   exit (0);
  20. }
  21.  
  22. void perror_exit (char *error)
  23. {
  24.   perror (error);
  25.   handler (9);
  26. }
  27.  
  28. int main (int argc, char *argv[])
  29. {
  30.   struct input_event ev[64];
  31.   int fd, rd, value, size = sizeof (struct input_event);
  32.   char name[256] = "Unknown";
  33.   char *device = NULL;
  34.  
  35.   //Setup check
  36.   if (argv[1] == NULL){
  37.       printf("Please specify (on the command line) the path to the dev event interface device\n");
  38.       exit (0);
  39.     }
  40.  
  41.   if ((getuid ()) != 0)
  42.     printf ("You are not root! This may not work...\n");
  43.  
  44.   if (argc > 1)
  45.     device = argv[1];
  46.  
  47.   //Open Device
  48.   if ((fd = open (device, O_RDONLY)) == -1)
  49.     printf ("%s is not a vaild device.\n", device);
  50.  
  51.   //Print Device Name
  52.   ioctl (fd, EVIOCGNAME (sizeof (name)), name);
  53.   printf ("Reading From : %s (%s)\n", device, name);
  54.  
  55.   while (1){
  56.       if ((rd = read (fd, ev, size * 64)) < size)
  57.           perror_exit ("read()");      
  58.  
  59.       value = ev[0].value;
  60.  
  61.       if (value != ' ' && ev[1].value == 1 && ev[1].type == 1){ // Only read the key press event
  62.       printf ("Code[%d]\n", (ev[1].code));
  63.       }
  64.   }
  65.  
  66.   return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment