Advertisement
metalx1000

GCC C code for running a command when MIDI key pressed

Oct 24th, 2017
665
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.89 KB | None | 0 0
  1. /*
  2.  * Copyright (c) 2017 Kris Occhipint.
  3.  * http://filmsbykris.com
  4.  *
  5.  * Runs commands when keys are pressed on MIDI device
  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. #include <stdlib.h>
  20. #include <sys/soundcard.h>
  21. #include <fcntl.h>
  22. #include <unistd.h>
  23. #include <stdio.h>
  24.  
  25. //#define  DEVICE  "/dev/sequencer"
  26. #define  DEVICE  "/dev/midi4"
  27.  
  28.  
  29. int main(void) {
  30.   unsigned char input[4];
  31.  
  32.   // open the midi device for reading.
  33.   int seqfd = open(DEVICE, O_RDONLY);
  34.   if (seqfd == -1) {
  35.     printf("Error: cannot open %s\n", DEVICE);
  36.     return 1;
  37.   }
  38.  
  39.   // wait for MIDI input and then run a program when keys are pressed.
  40.   while (1) {
  41.     read(seqfd, &input, sizeof(input));
  42.     //if value of input is greater then 0
  43.     //this is so it doesn't run the programm again
  44.     //when key is released
  45.     if(input[2] > 0){
  46.       //run set programs with defined key is pressed
  47.       if(input[1] == 50){
  48.         system("htop");
  49.       }else if(input[1] == 52){
  50.         system("ifconfig");
  51.       }else if(input[1] == 53){
  52.         system("zenity --info --text 'Hello World!'");
  53.       }else if(input[1] == 55){
  54.         //Sends keys to current focused program
  55.         //xautomation must be installed
  56.         //sudo apt install xautomation
  57.         system("xte 'str This is a TEST...Hello World!'");
  58.       }
  59.     }
  60.   }
  61.  
  62.   return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement