Guest User

Simple tremor emulator

a guest
Aug 12th, 2014
403
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <linux/input.h>
  5. #include <errno.h>
  6. #include <fcntl.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <sys/time.h>
  10.  
  11. int cleanSTDIN()
  12. {
  13.         while (getchar()!='\n');
  14.         return 1;
  15. }
  16.  
  17. long getInteger(char* msg, long from, long till)
  18. {
  19.         int count = 0;  
  20.         long m    = 0;  
  21.         char c;
  22.         do
  23.         {  
  24.                 printf("\n");
  25.                 printf(msg);
  26.  
  27.         } while (((scanf("%d%c", &m, &c)!=2 || c!='\n') && cleanSTDIN()) || m<from || m>till);
  28.  
  29.         return m;
  30. }
  31. long randomTill(long max)
  32. {
  33.         unsigned long
  34.         numBin  = (unsigned long) max + 1,
  35.         numRand = (unsigned long) RAND_MAX + 1,
  36.         binSize = numRand / numBin,
  37.         defect  = numRand % binSize;
  38.  
  39.         long z;
  40.         while (numRand - defect <= (unsigned long)(z = random()));
  41.         return z/binSize;
  42. }
  43.  
  44. int main()
  45. {
  46.         struct input_event event, event_end;
  47.  
  48.         int  fd = open("/dev/input/event4", O_RDWR);
  49.         long ma = getInteger("Enter max amplitude [points, 0..50]: ", 0, 50);
  50.         long ta = getInteger("Enter max wait time [usecs , 0..200000]: ", 0, 200000);
  51.         if (fd < 0)
  52.         {
  53.                 printf("Mouse access attempt failed:%s\n", strerror(errno));
  54.                 return -1;
  55.         }
  56.         memset(&event, 0, sizeof(event));
  57.         memset(&event, 0, sizeof(event_end));
  58.         gettimeofday(&event.time, NULL);
  59.         event.type = EV_REL;
  60.         gettimeofday(&event_end.time, NULL);
  61.         event_end.type = EV_SYN;
  62.         event_end.code = SYN_REPORT;
  63.         event_end.value = 0;
  64.         while(1)
  65.         {
  66.                 event.code  = rand() % 2 ? REL_X : REL_Y;
  67.                 event.value = (rand() % 2 ? -1 : 1)*randomTill(ma);
  68.                 write(fd, &event, sizeof(event));
  69.                 write(fd, &event_end, sizeof(event_end));
  70.                 usleep(randomTill(ta));
  71.         }
  72.         close(fd);
  73.         return 0;
  74. }
RAW Paste Data