Advertisement
Guest User

Untitled

a guest
Oct 6th, 2011
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.00 KB | None | 0 0
  1. /*
  2.     Quick hack to get key codes and key state from kernel input subsystem
  3.  
  4.     Copyright (c) 2006 Frantisek Dufka <[email protected]>
  5.  
  6.     This program is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2 of the License, or
  9.     (at your option) any later version.
  10.  
  11.     This program is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with this program; if not, write to the Free Software
  18.     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
  19.    
  20. */
  21.  
  22. #include <sys/types.h>
  23. #include <sys/stat.h>
  24. #include <fcntl.h>
  25.  
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30.  
  31. #include <sys/poll.h>
  32. #include <linux/input.h>
  33.  
  34. static inline int test_bit(int nr, const volatile unsigned long *p)
  35. {
  36.     return (p[nr >> 5] >> (nr & 31)) & 1UL;
  37. }
  38.  
  39. static int fd;
  40. int command;
  41. int timeout;            //miliseconds
  42.  
  43. void keywait(int down)
  44. {
  45.     struct pollfd inpollfd;
  46.     struct input_event ev;
  47.     int err, rb;
  48.     int done = 0;
  49.     memset(&inpollfd, 0, sizeof(inpollfd));
  50.  
  51.     inpollfd.fd = fd;
  52.     inpollfd.events = POLLIN;
  53.     while (!done) {
  54.         err = poll(&inpollfd, 1, timeout);
  55.         if (err < 1) {
  56.             done = 1;
  57.             continue;
  58.         }
  59.         rb = read(fd, &ev, sizeof(struct input_event));
  60.         if (rb < sizeof(struct input_event))
  61.             done = 1;
  62.         else {
  63.             if (ev.type == EV_KEY && ev.value == down) {
  64.                 printf("%d\n", ev.code);
  65.                 done = 1;
  66.             }
  67.         }
  68.     }
  69. }
  70.  
  71. void keydownwait()
  72. {
  73.     keywait(1);
  74. }
  75.  
  76. void keyupwait()
  77. {
  78.     keywait(0);
  79. }
  80.  
  81. void keystate()
  82. {
  83.     unsigned long key_b[KEY_MAX / 8 + 1];
  84.     int yalv;
  85.     if (!fd)
  86.         return;
  87.     memset(key_b, 0, sizeof(key_b));
  88.     ioctl(fd, EVIOCGKEY(sizeof(key_b)), key_b);
  89.  
  90.     for (yalv = 0; yalv < KEY_MAX; yalv++) {
  91.         if (test_bit(yalv, key_b)) {
  92.             /* the bit is set in the key state */
  93.             printf("%d\n", yalv);
  94.         }
  95.     }
  96. }
  97.  
  98. void get_ts_state()
  99. {
  100.     struct pollfd inpollfd;
  101.     struct input_event ev;
  102.     struct input_absinfo ts_abs;
  103.     int err, rb;
  104.     int done = 0;
  105.  
  106.     memset(&ts_abs, 0, sizeof(ts_abs));
  107.     memset(&inpollfd, 0, sizeof(inpollfd));
  108.  
  109.     inpollfd.fd = fd;
  110.     inpollfd.events = POLLIN;
  111.     while (!done) {
  112.         err = poll(&inpollfd, 1, timeout);
  113.         if (err < 1) {
  114.             done = 1;
  115.             continue;
  116.         }
  117.         rb = read(fd, &ev, sizeof(struct input_event));
  118.         if (rb < sizeof(struct input_event))
  119.             done = 1;
  120.         else {
  121.             if (ev.type == EV_KEY) {
  122.                 done = 1;
  123.             }
  124.             if (fd) {
  125.                 ioctl(fd, EVIOCGABS(0), &ts_abs);
  126.                 printf("value = %lu\n", ts_abs.value);
  127.             }
  128.         }
  129.     }
  130. }
  131.  
  132. void help()
  133. {
  134.     printf("usage: evkey -s|-p|-u|-d -t timeout eventdevice\n");
  135. }
  136.  
  137. int main(int argc, char *argv[])
  138. {
  139.  
  140.     int c;
  141.     int morethanone = 0;
  142.  
  143.     while (1) {
  144.         int this_option_optind = optind ? optind : 1;
  145.         c = getopt(argc, argv, "spdut:h");
  146.         if (c == -1)
  147.             break;
  148.  
  149.         switch (c) {
  150.         case 's':
  151.         case 'd':
  152.         case 'u':
  153.         case 'p':
  154.             if (!command)
  155.                 command = c;
  156.             else
  157.                 morethanone = c;
  158.             break;
  159.  
  160.         case 't':
  161.             timeout = atoi(optarg);
  162.             break;
  163.  
  164.         case '?':
  165.             break;
  166.  
  167.         case 'h':
  168.             help();
  169.             break;
  170.  
  171.         default:
  172.             printf("?? getopt returned character code 0%o ??\n", c);
  173.         }
  174.     }
  175.  
  176.     if (!timeout)
  177.         timeout = 100;
  178.  
  179.     if (!command) {
  180.         fprintf(stderr, "one of s,u,d options is needed\n");
  181.         exit(0);
  182.     }
  183.     if (morethanone) {
  184.         fprintf(stderr, "only one of s,u,d options is allowed\n");
  185.         exit(0);
  186.     }
  187.  
  188.     if (optind >= argc) {
  189.         fprintf(stderr, "missing input device name\n");
  190.         exit(0);
  191.     }
  192.     if ((fd = open(argv[optind], O_RDONLY)) < 0) {
  193.         perror("evdev open");
  194.     }
  195.     switch (command) {
  196.     case 'd':
  197.         keydownwait();
  198.         break;
  199.     case 'u':
  200.         keyupwait();
  201.         break;
  202.     case 's':
  203.         keystate();
  204.     case 'p':
  205.         get_ts_state();
  206.         break;
  207.     }
  208. }
  209.  
  210.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement