Advertisement
xerpi

arduino fake linux keyboard

Aug 21st, 2012
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.63 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <fcntl.h>
  4. #include <errno.h>
  5. #include <termios.h>
  6. #include <unistd.h>
  7. #include <xdo.h>
  8. #include <X11/Xlib.h>
  9. #include <X11/keysym.h>
  10.  
  11. int open_serial(char *port);
  12. void close_serial(int fd);
  13.  
  14. int main()
  15. {
  16.     sleep(1); //wait 1 second
  17.     printf("creating xdo...\n");
  18.     xdo_t* myXdo = xdo_new(NULL);
  19.     printf("xdo created...\n");
  20.    
  21.     printf("getting display...\n");
  22.     // Obtain the X11 display.
  23.         Display *display = XOpenDisplay(0);
  24.         if(display == NULL)
  25.         {
  26.             printf("could not open display, exiting...\n");
  27.             return -1;
  28.         }
  29.            
  30.     printf("getting root window...\n");
  31.     // Window focus
  32.         int revert;
  33.         Window winFocus;
  34.  
  35.     int fd = open_serial("/dev/ttyACM0");
  36.     if(fd == 0)
  37.     {
  38.         printf("serial error, exiting...\n");
  39.         return -1;
  40.     }
  41.     char buff;
  42.     unsigned int rd;
  43.    
  44.    
  45.     while(1)
  46.     {
  47.         rd = read(fd, &buff, 1);
  48.         if(rd > 0)
  49.         {
  50.             printf("Read: %c\n", buff);
  51.             XGetInputFocus(display, &winFocus, &revert);
  52.             switch(rd)
  53.             {
  54.                 case 'A':
  55.                     xdo_keysequence_up(myXdo, winFocus, "A",  12000);
  56.                     break;
  57.                 case 'B':
  58.                     xdo_keysequence_up(myXdo, winFocus, "B",  12000);
  59.                     break;
  60.                 default:
  61.                     break;
  62.             }
  63.         }
  64.     }
  65.    
  66.     printf("exiting...\n");
  67.     XCloseDisplay(display);
  68.     xdo_free(myXdo);   
  69.     close_serial(fd);
  70.     return 0;
  71. }
  72.  
  73. int open_serial(char *port)
  74. {
  75.     int fd = open(port, O_RDWR | O_NOTTY | O_NDELAY);
  76.     if(fd == -1)
  77.     {
  78.         printf("Could not open %s\n", port);
  79.         return 0;
  80.     }
  81.     fcntl(fd, F_SETFL, 0);
  82.     printf("Port %s has been sucessfully opened and %d is the file description\n", port, fd);
  83.     return fd;
  84. }
  85.  
  86. void close_serial(int fd)
  87. {
  88.     close(fd);
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement