JamesMcMinn

Untitled

Jan 5th, 2021
848
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.38 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. muteTimeMS=200
  4.  
  5. echo 0 > .lastKPTime
  6. checkKeyPress() {
  7.   echo "capturing input"
  8.   xinput test-xi2 --root | grep --line-buffered RawKeyPress |
  9.   while read -r line; do
  10.     lastPressTime=`cat .lastKPTime`
  11.     deviceID=`pactl list short sources | grep RUNNING | head -n 1 | cut -f1`
  12.     muted=$(pactl list sources | grep '^[[:space:]]Mute:' | head -n $deviceID | tail -n 1 | xargs | cut -d':' -f2 | xargs)
  13.  
  14.     # Skip if the user has muted themsevles
  15.     if [ "${lastPressTime}" = "0" ] && [ "$muted" = "yes" ]; then
  16.       continue
  17.     fi
  18.  
  19.     echo `date +%s%N | cut -b1-13` > .lastKPTime
  20.  
  21.     if [ $muted = "yes" ]; then
  22.       continue
  23.     fi
  24.  
  25.     pactl set-source-mute $deviceID true
  26.     echo "muted"
  27.   done
  28. }
  29. checkKeyPress &
  30.  
  31. while true
  32. do
  33.   sleep .1
  34.   deviceID=`pactl list short sources | grep RUNNING | head -n 1 | cut -f1`
  35.   muted=$(pactl list sources | grep '^[[:space:]]Mute:' | head -n $deviceID | tail -n 1 | xargs | cut -d':' -f2 | xargs)
  36.   if [ $muted != "yes" ]; then
  37.     continue
  38.   fi
  39.    
  40.   lastPressTime=`cat .lastKPTime`
  41.   if [ "${lastPressTime}" = "0" ]; then
  42.     continue
  43.   fi
  44.  
  45.   currentTimeMS=`date +%s%N | cut -b1-13`
  46.   timeSinceLastKP=`expr $currentTimeMS - $lastPressTime`
  47.   if (( timeSinceLastKP > $muteTimeMS )); then
  48.     pactl set-source-mute $deviceID false
  49.     echo "unmuting"
  50.     echo 0 > .lastKPTime
  51.   fi
  52. done
  53.  
Advertisement
Add Comment
Please, Sign In to add comment