Advertisement
xerpi

Linux - Arduino key : Linux part

Jun 22nd, 2013
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.84 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.         XGetInputFocus(display, &winFocus, &revert); //la ventana que tiene el focus
  48.         rd = read(fd, &buff, 1);
  49.         switch(fd)
  50.         {
  51.             case 1:
  52.                 xdo_keysequence_up(myXdo, winFocus, "A",  12000);
  53.                 xdo_keysequence_down(myXdo, winFocus, "A",  12000);
  54.                 break;
  55.             case 2:
  56.                 xdo_keysequence_up(myXdo, winFocus, "B",  12000);
  57.                 xdo_keysequence_down(myXdo, winFocus, "B",  12000);
  58.                 break;
  59.             case 3:
  60.                 xdo_keysequence_up(myXdo, winFocus, "C",  12000);
  61.                 xdo_keysequence_down(myXdo, winFocus, "C",  12000);
  62.                 break;
  63.             default:
  64.                 break;
  65.         }
  66.     }
  67.  
  68.     printf("exiting...\n");
  69.     XCloseDisplay(display);
  70.     xdo_free(myXdo);   
  71.     close_serial(fd);
  72.     return 0;
  73. }
  74.  
  75. int open_serial(char *port)
  76. {
  77.     int fd = open(port, O_RDWR | O_NOCTTY | O_NDELAY);
  78.     if(fd == -1)
  79.     {
  80.         printf("Could not open %s\n", port);
  81.         return 0;
  82.     }
  83.     fcntl(fd, F_SETFL, 0);
  84.     printf("Port %s has been sucessfully opened and %d is the file description\n", port, fd);
  85.     return fd;
  86. }
  87.  
  88. void close_serial(int fd)
  89. {
  90.     close(fd);
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement