Advertisement
metalx1000

GCC C Code for Reading Button presses on USB Headphones

Oct 24th, 2017
706
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.52 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.  *
  7.  * This program is free software: you can redistribute it and/or modify  
  8.  * it under the terms of the GNU General Public License as published by  
  9.  * the Free Software Foundation, version 3.
  10.  *
  11.  * This program is distributed in the hope that it will be useful, but
  12.  * WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14.  * General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18.  */
  19.  
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <fcntl.h>
  23. #include <errno.h>
  24. #include <linux/input.h>
  25. #include <string.h>
  26. #include <stdio.h>
  27.  
  28. int main(void){
  29.   //get input device
  30.   const char *dev = "/dev/input/by-id/usb-C-Media_Electronics_Inc._USB_Audio_Device-event-if03";
  31.   struct input_event ev;
  32.   ssize_t n;
  33.   int fd;
  34.  
  35.   //open device for reading
  36.   fd = open(dev, O_RDONLY);
  37.   //exit on error opening device
  38.   if (fd == -1) {
  39.     fprintf(stderr, "Cannot open %s: %s.\n", dev, strerror(errno));
  40.     return -1;
  41.   }
  42.   while (1) {
  43.     n = read(fd, &ev, sizeof ev);
  44.     if (ev.value == 1){
  45.       if ((int)ev.code == 115){
  46.         printf("Volume Up\n");
  47.       }else if ((int)ev.code == 114){
  48.         printf("Volume Down\n");
  49.       }
  50.     }
  51.   }
  52.  
  53.   return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement