Advertisement
Guest User

Untitled

a guest
Jul 27th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.84 KB | None | 0 0
  1. /* Through this source code, you could see the point output architechture of touch device.
  2. You could follow below code to do point parsing. And develop your own app. */
  3.  
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <fcntl.h>
  9. #include <limits.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12.  
  13. #include <linux/input.h>
  14.  
  15. /*Note: If you couldn't build this source code successfully, it may caused by the old kernel issue.
  16. Please enable below define depending on your kernel version */
  17.  
  18. //#define KERNEL_2_6_35_DOWNWARD    /* As kernel <= 2.6.35 */
  19. //#define KERNEL_2_6_32_DOWNWARD    /* As kernel <= 2.6.32 */
  20. //#define KERNEL_2_6_29_DOWNWARD    /* As kernel <= 2.6.29 */
  21.  
  22. #ifdef KERNEL_2_6_35_DOWNWARD
  23. typedef struct tagevent
  24. {
  25.     struct timeval time;
  26.     unsigned short type;
  27.     unsigned short code;
  28.     int value;
  29. } input_event;
  30. #endif
  31.  
  32. void ParseEvent( input_event ev )
  33. {
  34.    if( ev.type == EV_ABS ) {
  35.         switch( ev.code ) {
  36. #ifndef KERNEL_2_6_35_DOWNWARD
  37.          case ABS_MT_SLOT:
  38.             printf("    [ABS_MT_SLOT]           code = %3d, value = %4d\n", ev.code , ev.value);
  39.             break;
  40. #endif
  41. #ifndef KERNEL_2_6_32_DOWNWARD
  42.          case ABS_MT_PRESSURE:
  43.             printf("    [ABS_MT_PRESSURE]       code = %3d, value = %4d\n", ev.code , ev.value);
  44.             break;
  45. #endif
  46. #ifndef KERNEL_2_6_29_DOWNWARD
  47.          case ABS_MT_TRACKING_ID:
  48.             printf("    [ABS_MT_TRACKING_ID]    code = %3d, value = %4d\n", ev.code , ev.value);
  49.             break;
  50.          case ABS_MT_POSITION_X:
  51.             printf("    [ABS_MT_POSITION_X]     code = %3d, value = %4d\n", ev.code , ev.value);
  52.             break;
  53.          case ABS_MT_POSITION_Y:
  54.             printf("    [ABS_MT_POSITION_Y]     code = %3d, value = %4d\n", ev.code , ev.value);
  55.             break;
  56.          case ABS_MT_TOUCH_MAJOR:
  57.             printf("    [ABS_MT_TOUCH_MAJOR]    code = %3d, value = %4d\n", ev.code , ev.value);
  58.             break;
  59.          case ABS_MT_WIDTH_MAJOR:
  60.             printf("    [ABS_MT_WIDTH_MAJOR]    code = %3d, value = %4d\n", ev.code , ev.value);
  61.             break;
  62. #endif
  63.          case ABS_X:
  64.             printf("    [ABS_X]                 code = %3d, value = %4d\n", ev.code , ev.value);
  65.             break;
  66.          case ABS_Y:
  67.             printf("    [ABS_Y]                 code = %3d, value = %4d\n", ev.code , ev.value);
  68.             break;
  69.          case ABS_RX:
  70.             printf("    [ABS_RX]                code = %3d, value = %4d\n", ev.code , ev.value);
  71.             break;
  72.          case ABS_RY:
  73.             printf("    [ABS_RY]                code = %3d, value = %4d\n", ev.code , ev.value);
  74.             break;
  75.          case ABS_PRESSURE:
  76.             printf("    [ABS_PRESSURE]          code = %3d, value = %4d\n", ev.code , ev.value);
  77.             break;
  78.          default:
  79.             break;
  80.         }
  81.     }
  82.    else if( ev.type == EV_KEY ){
  83.       switch( ev.code ){
  84.          case BTN_TOUCH:
  85.             printf("    [BTN_TOUCH]     code = %3d, value = %4d\n", ev.code , ev.value);
  86.             break;
  87.          case BTN_LEFT:
  88.             printf("    [BTN_LEFT]      code = %3d, value = %4d\n", ev.code , ev.value);
  89.             break;
  90.          case BTN_EXTRA:
  91.             printf("    [BTN_EXTRA]     code = %3d, value = %4d\n", ev.code , ev.value);
  92.             break;
  93.          default:
  94.             break;
  95.       }
  96.  
  97.    }
  98.    else if( ev.type == EV_SYN ){
  99.       switch( ev.code ){
  100.          case SYN_REPORT:
  101.             printf("    [SYN_REPORT]        code = %3d, value = %4d\n\n", ev.code , ev.value);
  102.             break;
  103. #ifndef KERNEL_2_6_29_DOWNWARD
  104.          case SYN_MT_REPORT:
  105.             printf("    [SYN_MT_REPORT]     code = %3d, value = %4d\n", ev.code , ev.value);
  106.             break;
  107. #endif
  108.          default:
  109.             break;
  110.       }
  111.     }
  112.    else if( ev.type == EV_MSC ){
  113.       if( ev.code == MSC_SCAN )
  114.          printf("   [MSC_SCAN]      code = %3d, value = %4d\n", ev.code , ev.value);
  115.    }
  116. }
  117.  
  118. int main( int argc, char **argv )
  119. {
  120.     int fd;
  121. //    unsigned char byFlag = 0; /* 0: Point */
  122.     char strPort[64] = { 0 };
  123.     fd_set readfds;
  124.  
  125.     if( argc == 1 || argc > 2 ) {
  126.         printf(" \nUsage:\n \tGetEvent [Port]\n");
  127.         printf("Example:\n \tGetEvent /dev/input/event1\n\n");
  128.         return 0;
  129.     }
  130.  
  131.     sprintf( strPort, "%s", argv[1] );
  132.     printf("Event Port = %s ", strPort);
  133.     fd = open(strPort, O_RDONLY );
  134.     if( fd >= 0 ) {
  135.         printf(" open: ok\n" );
  136.         FD_ZERO( &readfds );
  137.         FD_SET( fd , &readfds );
  138.         while( 1 ) {
  139.             if( select( fd+1, &readfds, NULL, NULL, NULL ) > 0 ) {
  140.                 input_event ev;
  141.                 if( sizeof( input_event ) == read( fd, &ev, sizeof( input_event ) ) ) {
  142.                     ParseEvent( ev );
  143.                 } else {
  144.                     printf(" Nothing read \n" );
  145.                 }
  146.             }
  147.         }
  148.     }
  149.     return 0;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement