[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-devel
Subject:    [cross-post] My contribution: a volume cli utility
From:       Andy Pieters <andy () vlaamse-kern ! com>
Date:       2005-05-29 11:56:13
Message-ID: 200505291356.20985.andy () vlaamse-kern ! com
[Download RAW message or body]

[Attachment #2 (multipart/signed)]

[Attachment #4 (multipart/mixed)]


Hi all

I made a little utility that alows you to bind shortcut keys to volume up, 
volume down, volume mute, and volume unmute.

It does this by executing dcop calls to Kmix

I hope this will be of use to someone.

With kind regards

Example usage:

volume --up 
 increases volume with 10%
volume --down 20
 decreases volume with 20%
volume --volume
 prints current volume level
volume --volume 55
 sets volume level to 55%
volume --mute 
 saves volume level to temporary file and set volume to 0
volume --unmute
 loads volume level from temporary file and set volume (defaults to 55%)

-- 
Registered Linux User Number 379093
-- --BEGIN GEEK CODE BLOCK-----
Version: 3.1
GAT/O/>E$ d-(---)>+ s:(+)>: a--(-)>? C++++$(+++) UL++++>++++$ P-(+)>++
L+++>++++$ E---(-)@ W+++>+++$ !N@ o? !K? W--(---) !O !M- V-- PS++(+++)
PE--(-) Y+ PGP++(+++) t+(++) 5-- X++ R*(+)@ !tv b-() DI(+) D+(+++) G(+)
e>++++$@ h++(*) r-->++ y--()>++++
-- ---END GEEK CODE BLOCK------
--
Check out these few php utilities that I released
 under the GPL2 and that are meant for use with a 
 php cli binary:
 
 http://www.vlaamse-kern.com/sas/
--

--

["volume" (application/x-shellscript)]

#!/bin/bash
# Access Kmix volume trough scripting

EXITCODE_HELP=5
EXITCODE_ERROR=1
PROG_NAME='volume'
PROG_AUTHOR='Straight-A-Software'
PROG_EMAIL='x_terminat_or_3@yahoo.fr'
PROG_VERSION='2.0'
BASE=`basename $0`
TEMPFILE="$HOME/.volume.sh"

function end
{
 unset -f end error shorthelp info help morehelp dcopinit getvolume setvolume
 unset -f loadvolume mute unmute volume up down
 unset -v PROG_NAME PROG_VERSION PROG_AUTHOR BASE PROG_EMAIL XVALUE XVOLUME TEMPFILE VOLUME 
 #default to exitcode 0
 EXITCODE=0
 if [ $# -gt 0 ]; then
  if [ -n $1 ]; then
   EXITCODE=$1
  fi
 fi
 if [ $# -gt 1 ]; then
  if [ -n "$2" ]; then
   echo "$2"
  fi
 fi
 exit $EXITCODE
}

function error
{
 if [ $# -gt 0 ]; then
  end $EXITCODE_ERROR "$1"
 fi
 end $EXITCODE_ERROR
}

function shorthelp
{
 #this function is a trademark of Straight-A-Software
 #used in conjunction with 'myscripts' utility
 echo "Access Kmix volume trough cli"
 end $EXITCODE_HELP
}

function info
{
 echo "$PROG_NAME v$PROG_VERSION (c) $PROG_AUTHOR Released under the GPL2"
 echo "$PROG_EMAIL"
 echo ""
}

function help
{
 info
 echo "Access Kmix's volume trough command line interface."
 echo "Get, set, save volume"
 echo
 echo "Usage: $BASE --help | --shorthelp | --morehelp --version --info"
 echo "       $BASE --mute | --unmute | --up [xx] | --down [xx] | --volume [xxx]"
 echo ""
 end $EXITCODE_HELP
}

function morehelp
{
 info
 echo "Note: this program depends on Kmixer.  Install it with the KdeMultimedia "
 echo "package."
 echo ""
 echo "When you --mute, it will try to save a temporary file in your home directory"
 echo "to save the mastervolume so that when you use --unmute, the volume level will"
 echo "be at the same level as before."
 echo ""
 echo "If no temporary file is found when using --unmute, then the volume level will "
 echo "be set to 55%"
 echo ""
 echo "The name of the temporary file is ~.volumelevel.sh"
 echo "This file is removed after using --unmute"
 echo ""
 echo "When using --up, or --down, it defaults to 10% change unless specified otherwise"
 echo
 end $EXITCODE_HELP
}

function dcopinit
{
 type dcop >/dev/null 2>&1
 if [ $? -gt 0 ]; then
  error "Error dcop not found!"
 fi
 #dcop found, try to call it
 dcop >/dev/null 2>&1
 if [ $? -gt 0 ]; then
  error "Dcop cannot start!"
 fi
 #try kmix
 dcop "kmix" >/dev/null 2>&1
 if [ $? -gt 1 ]; then
  #kmix is NOT running
  type dcopstart > /dev/null 2>&1
  if [ $? -gt 0 ]; then
   #cannot use dcopstart, try to start it manually
   type kmix >/dev/null 2>&1
   if [ $? -gt 0 ]; then
    error "Cannot find Kmix!"
   fi
   kmix >/dev/null 2>&1
   if [ $? -gt 0 ]; then
    error "Cannot start Kmix!"
   fi
  else
   dcopstart kmix >/dev/null 2>&1
   if [ $? -gt 0 ]; then
    error "Dcop server cannot start Kmix!"
   fi
  fi
  sleep .5 #alow kmix to start
 fi
 #see if kmix has a mastervolume
 dcop kmix Mixer0 masterVolume >/dev/null 2>&1
 if [ $? -gt 0 ]; then
  error "Kmixer does not have a mastervolume!"
 fi
}

function getvolume
{
 temp=$(dcop kmix Mixer0 masterVolume 2>&1)
 if [ $? -eq 0 ]; then
  XVOLUME=$temp
  return
 fi
 error "Problem retrieving volume!"
}

function setvolume
{
 VOLUME=55 #default
 if [ $# -gt 0 ]; then
   if [ $1 -ge 0 ]; then
    VOLUME=$1
   fi
   if [ $1 -gt 100 ]; then
    VOLUME=100
   fi
 fi
 dcop kmix Mixer0 setMasterVolume $VOLUME >/dev/null 2>&1
 if [ $? -gt 0 ]; then
  error "Problem setting volume!"
 fi
}

function savevolume
{
 getvolume
 echo "echo '$XVOLUME'" >"$TEMPFILE" 2>&1
 if [ -f "$TEMPFILE" ]; then
   chmod +x "$TEMPFILE" 2>&1
 fi
}

function loadvolume
{
 XVOLUME=55
 if [ -x "$TEMPFILE" ]; then
  XVOLUME=$("$TEMPFILE")
  if [ $XVOLUME -lt 0 ]; then
   XVOLUME=0
  elif [ $XVOLUME -gt 100 ]; then
   XVOLUME=100
  fi
  rm -rf "$TEMPFILE" > /dev/null 2>&1
 fi
}

function mute
{
 savevolume
 setvolume 0
 end
}

function unmute
{
 loadvolume
 setvolume $XVOLUME
 end
}

function volume
{
 SET=0
 if [ $# -gt 0 ]; then
  if [ -n $1 ]; then
   if [ $1 -gt -1 ]; then
    SET=1
   fi
  fi
 fi
 if [ $SET -eq 1 ]; then
  XVOLUME=$1
  if [ $XVOLUME -gt 100 ]; then
   XVOLUME=100
  fi
  setvolume $XVOLUME
  end
 fi
 getvolume
 echo $XVOLUME
 end
}

function up
{
 INC=10
 if [ $# -gt 0 ]; then
  if [ $1 -gt 0 ]; then
   INC=$1
  fi
 fi
 getvolume
 INC=$[$XVOLUME + $INC]
 if [ $INC -gt 100 ]; then
  INC=100
 fi
 setvolume $INC
 end
}

function down
{
 DEC=10
 if [ $# -gt 0 ]; then
  if [ $1 -gt 0 ]; then
   DEC=$1
  fi
 fi
 getvolume
 DEC=$[$XVOLUME - $DEC]
 if [ $DEC -lt 0 ]; then
  DEC=0
 fi
 setvolume $DEC
 end
}

#process command line arguments
if [ $# -eq 0 ]; then
 help
fi

XVALUE=-1

if [ $# -gt 1 ]; then
 if [ -n $2 ]; then
  XVALUE=$2
 fi
fi
  
case $1 in 
   --volume)
     dcopinit
     volume $XVALUE
     ;;
   --up)
     dcopinit
     up $XVALUE
     ;;
   --down)
     dcopinit
     down $XVALUE
     ;;
   --mute)
     dcopinit
     mute
     ;;
   --unmute)
     dcopinit
     unmute
     ;;
   --help)
     help
     ;;
   --shorthelp|--info)
     shorthelp
     ;;
   --morehelp)
     morehelp
     ;;
   --version)
     info
     end $EXITCODE_HELP
     ;;
esac

help
[Attachment #8 (application/pgp-signature)]

>> Visit http://mail.kde.org/mailman/listinfo/kde-devel#unsub to unsubscribe <<


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic