Advertisement
Guest User

Untitled

a guest
Aug 10th, 2012
362
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.01 KB | None | 0 0
  1.  
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <linux/input.h>
  8. #include <linux/uinput.h>
  9. #include <stdio.h>
  10. #include <sys/time.h>
  11. #include <sys/types.h>
  12. #include <unistd.h>
  13. /* Globals */
  14. static int uinp_fd = -1;
  15. struct uinput_user_dev uinp;
  16. // uInput device structure
  17. struct input_event event; // Input device structure
  18. /* Setup the uinput device */
  19. int setup_uinput_device()
  20. {
  21.   // Temporary variable
  22.   int i=0;
  23.   // Open the input device
  24.   uinp_fd = open("/dev/uinput", O_WRONLY | O_NDELAY);
  25.   if (uinp_fd == NULL)
  26.     {
  27.       printf("Unable to open /dev/uinput\n");
  28.       return -1;
  29.     }
  30.   memset(&uinp,0,sizeof(uinp)); // Intialize the uInput device to NULL
  31.   strncpy(uinp.name, "PolyVision Touch Screen", UINPUT_MAX_NAME_SIZE);
  32.   uinp.id.version = 4;
  33.   uinp.id.bustype = BUS_USB;
  34.   // Setup the uinput device
  35.   ioctl(uinp_fd, UI_SET_EVBIT, EV_KEY);
  36.   ioctl(uinp_fd, UI_SET_EVBIT, EV_REL);
  37.   ioctl(uinp_fd, UI_SET_RELBIT, REL_X);
  38.   ioctl(uinp_fd, UI_SET_RELBIT, REL_Y);
  39.   for (i=0; i < 256; i++) {
  40.     ioctl(uinp_fd, UI_SET_KEYBIT, i);
  41.   }
  42.   ioctl(uinp_fd, UI_SET_KEYBIT, BTN_MOUSE);
  43.   ioctl(uinp_fd, UI_SET_KEYBIT, BTN_TOUCH);
  44.   ioctl(uinp_fd, UI_SET_KEYBIT, BTN_MOUSE);
  45.   ioctl(uinp_fd, UI_SET_KEYBIT, BTN_LEFT);
  46.   ioctl(uinp_fd, UI_SET_KEYBIT, BTN_MIDDLE);
  47.   ioctl(uinp_fd, UI_SET_KEYBIT, BTN_RIGHT);
  48.   ioctl(uinp_fd, UI_SET_KEYBIT, BTN_FORWARD);
  49.   ioctl(uinp_fd, UI_SET_KEYBIT, BTN_BACK);
  50.   /* Create input device into input sub-system */
  51.   write(uinp_fd, &uinp, sizeof(uinp));
  52.   if (ioctl(uinp_fd, UI_DEV_CREATE))
  53.     {
  54.       printf("Unable to create UINPUT device.");
  55.       return -1;
  56.     }
  57.   return 1;
  58. }
  59.  
  60. void send_click_events( )
  61. {
  62.   // Move pointer to (0,0) location
  63.   memset(&event, 0, sizeof(event));
  64.   gettimeofday(&event.time, NULL);
  65.   event.type = EV_REL;
  66.   event.code = REL_X;
  67.   event.value = 100;
  68.   write(uinp_fd, &event, sizeof(event));
  69.   event.type = EV_REL;
  70.   event.code = REL_Y;
  71.   event.value = 100;
  72.   write(uinp_fd, &event, sizeof(event));
  73.   event.type = EV_SYN;
  74.   event.code = SYN_REPORT;
  75.   event.value = 0;
  76.   write(uinp_fd, &event, sizeof(event));
  77.   // Report BUTTON CLICK - PRESS event
  78.   memset(&event, 0, sizeof(event));
  79.   gettimeofday(&event.time, NULL);
  80.   event.type = EV_KEY;
  81.   event.code = BTN_LEFT;
  82.   event.value = 1;
  83.   write(uinp_fd, &event, sizeof(event));
  84.   event.type = EV_SYN;
  85.   event.code = SYN_REPORT;
  86.   event.value = 0;
  87.   write(uinp_fd, &event, sizeof(event));
  88.   // Report BUTTON CLICK - RELEASE event
  89.   memset(&event, 0, sizeof(event));
  90.   gettimeofday(&event.time, NULL);
  91.   event.type = EV_KEY;
  92.   event.code = BTN_LEFT;
  93.   event.value = 0;
  94.   write(uinp_fd, &event, sizeof(event));
  95.   event.type = EV_SYN;
  96.   event.code = SYN_REPORT;
  97.   event.value = 0;
  98.   write(uinp_fd, &event, sizeof(event));
  99. }
  100.  
  101. void send_a_button()
  102. {
  103.   // Report BUTTON CLICK - PRESS event
  104.   memset(&event, 0, sizeof(event));
  105.   gettimeofday(&event.time, NULL);
  106.   event.type = EV_KEY;
  107.   event.code = KEY_A;
  108.   event.value = 1;
  109.   write(uinp_fd, &event, sizeof(event));
  110.   event.type = EV_SYN;
  111.   event.code = SYN_REPORT;
  112.   event.value = 0;
  113.   write(uinp_fd, &event, sizeof(event));
  114.   // Report BUTTON CLICK - RELEASE event
  115.   memset(&event, 0, sizeof(event));
  116.   gettimeofday(&event.time, NULL);
  117.   event.type = EV_KEY;
  118.   event.code = KEY_A;
  119.   event.value = 0;
  120.   write(uinp_fd, &event, sizeof(event));
  121.   event.type = EV_SYN;
  122.   event.code = SYN_REPORT;
  123.   event.value = 0;
  124.   write(uinp_fd, &event, sizeof(event));
  125. }
  126.  
  127.  
  128. /* This function will open the uInput device. Please make
  129.    sure that you have inserted the uinput.ko into kernel. */
  130. int main()
  131. {
  132.   // Return an error if device not found.
  133.   if (setup_uinput_device() < 0)
  134.     {
  135.       printf("Unable to find uinput device\n");
  136.       return -1;
  137.     }
  138.   send_a_button(); // Send a "A" key
  139.   send_click_events(); // Send mouse event
  140.   /* Destroy the input device */
  141.   ioctl(uinp_fd, UI_DEV_DESTROY);
  142.   /* Close the UINPUT device */
  143.   close(uinp_fd);
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement