Advertisement
metalx1000

GCC C code disable hid input and run commands instead

Oct 25th, 2017
698
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.90 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2017 Kris Occhipint.
  3.  * http://filmsbykris.com
  4.  *
  5.  * Detects if Volume Buttons are being pressed on SADES USB Headphones
  6.  * Default actions are disabled using system call of xinput for Xorg
  7.  *
  8.  * This program is free software: you can redistribute it and/or modify  
  9.  * it under the terms of the GNU General Public License as published by  
  10.  * the Free Software Foundation, version 3.
  11.  *
  12.  * This program is distributed in the hope that it will be useful, but
  13.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  14.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15.  * General Public License for more details.
  16.  *
  17.  * You should have received a copy of the GNU General Public License
  18.  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19.  */
  20.  
  21. #include <stdlib.h>
  22. #include <unistd.h>
  23. #include <fcntl.h>
  24. #include <errno.h>
  25. #include <linux/input.h>
  26. #include <string.h>
  27. #include <stdio.h>
  28.  
  29. int main(void){
  30.   //get input device
  31.   const char *dev = "/dev/input/by-id/usb-C-Media_Electronics_Inc._USB_Audio_Device-event-if03";
  32.   struct input_event ev;
  33.   ssize_t n;
  34.   int fd;
  35.  
  36.   //Disable the device's normal function in Xorg
  37.   system("xinput --disable $(xinput|grep 'C-Media Electronics'|cut -d\\= -f2|cut -f1)");
  38.   //open device for reading
  39.   fd = open(dev, O_RDONLY);
  40.   //exit on error opening device
  41.   if (fd == -1) {
  42.     fprintf(stderr, "Cannot open %s: %s.\n", dev, strerror(errno));
  43.     return -1;
  44.   }
  45.   while (1) {
  46.     n = read(fd, &ev, sizeof ev);
  47.     if (ev.value == 1){
  48.       if ((int)ev.code == 115){
  49.         printf("Opening HTOP\n");
  50.         system("htop");
  51.       }else if ((int)ev.code == 114){
  52.         printf("Current Free Memory\n");
  53.         system("free -h");
  54.       }else if ((int)ev.code == 113){
  55.         printf("sending keys\n");
  56.         system("xte 'str Hello World'");
  57.       }
  58.     }
  59.   }
  60.  
  61.   return 0;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement