File indexing completed on 2022-09-27 14:35:03

0001 #!/bin/sh
0002 #################################################################################
0003 # kmixremote - control kmix from a script.
0004 #
0005 # Set volume
0006 # Get volume
0007 # Mute
0008 #################################################################################
0009 
0010 usage()
0011 {
0012   echo "Usage:"
0013   echo "List mixers    #   $0 list"
0014   echo "List controls  #   $0 list <mixer>"
0015   echo "Get Volume     #   $0 get  [--master | <mixer> <control>]"
0016   echo "Set Volume     #   $0 set  [--master | <mixer> <control>] <0..100>"
0017   echo "Mute/Unmute    #   $0 mute [--master | <mixer> <control>] true|false"
0018   echo
0019 }
0020 
0021 exit_with_error()
0022 {
0023   echo "Error: $1"
0024   echo
0025   usage
0026   exit 1
0027 }
0028 
0029 # Prints the mixer DBUS ID's on the console. leaving out the "/Mixers/" prefix
0030 listMixers()
0031 {
0032   qdbus org.kde.kmix /Mixers org.freedesktop.DBus.Properties.Get org.kde.KMix.MixSet mixers | cut -f3 -d/
0033   errorCode=$?
0034   if test $errorCode != 0; then
0035     echo "Error $errorCode listing mixers. KMix is not running."
0036   fi
0037 }
0038 
0039 # Prints the mixer control DBUS ID's of the given mixer on the console. leaving out the "/Mixers/" prefix
0040 listControls()
0041 {
0042   qdbus org.kde.kmix $1 org.freedesktop.DBus.Properties.Get org.kde.KMix.Mixer controls  | cut -f4 -d/
0043   errorCode=$?
0044   if test $errorCode != 0; then
0045     echo "Error $errorCode listing controls. KMix is not running."
0046   fi
0047 }
0048 
0049 command=""
0050 
0051 if ! type qdbus >/dev/null  2>&1 ; then
0052   exit_with_error "$0 requires qdbus, but it cannot be found. Please install or check \$PATH"
0053 fi
0054 
0055 # Read args
0056 while true; do
0057   arg=$1
0058   if test -z "$arg"; then
0059     break
0060   fi
0061   shift
0062   if test "x--master" = "x$arg"; then
0063     mixer=`qdbus org.kde.kmix /Mixers org.kde.KMix.MixSet.currentMasterMixer`
0064     control=`qdbus org.kde.kmix /Mixers org.kde.KMix.MixSet.currentMasterControl`
0065   elif test "x--help" = "x$arg" -o "x-h" = "x$arg"; then
0066     usage
0067     exit 0
0068   else
0069     # If not a specific option, then interpret as standad args, in this order: command, mixer, control and genericArg
0070     if test -z "$command"; then
0071       command=$arg
0072     elif test -z "$mixer"; then
0073       mixer="${arg}"
0074     elif test -z "$control"; then
0075       control=$arg
0076     elif test -z "$genericArg"; then
0077       genericArg=$arg
0078     else
0079       exit_with_error "Too many aguments"
0080     fi
0081   fi
0082   #echo $arg
0083 done
0084 
0085 
0086 if test -z "$command"; then
0087   usage
0088   echo "<mixer>   - The mixer to use. Select one from the following list:"
0089   echo "-----------------------------------------------------------------"
0090   listMixers
0091   exit 0
0092 elif test "xlist" = "x$command"; then
0093   if test -z "$mixer"; then
0094     listMixers
0095   else
0096     # List controls
0097     listControls "/Mixers/${mixer}"
0098   fi
0099   exit 0
0100 fi
0101 
0102 # All following commands require a mixer
0103 if test -z "$mixer"; then
0104   exit_with_error "<mixer> argument missing"
0105 fi
0106 if test -z "$control"; then
0107     exit_with_error "<control> argument missing"
0108 fi
0109 
0110 # All following commands require a mixer and a control
0111 
0112 targetControl="/Mixers/${mixer}/${control}"
0113 #echo "ARGS: $command $targetControl $genericArg"
0114 
0115 # --- EXECUTE PHASE --------------------------------------------------------------------------------------------------
0116 if test "xget" = "x$command"; then
0117   # GET
0118   qdbus org.kde.kmix $targetControl org.freedesktop.DBus.Properties.Get org.kde.KMix.Control volume
0119 elif test "xset" = "x$command"; then
0120   # SET
0121   qdbus org.kde.kmix $targetControl org.freedesktop.DBus.Properties.Set org.kde.KMix.Control volume $genericArg
0122 elif test "xmute" = "x$command"; then
0123   # MUTE
0124   qdbus org.kde.kmix $targetControl org.freedesktop.DBus.Properties.Set org.kde.KMix.Control mute $genericArg
0125 else
0126   exit_with_error "No such command '$command'"
0127 fi
0128 
0129 exit 0