#!/usr/bin/perl # Shamelessly stolen from Kel Modderman , who # perlised my original shell script. use strict; use warnings; use Audio::Mixer; # Must supply an argument. usage() if @ARGV > 2; $ARGV[0] = "show" unless @ARGV; # Get volume (/dev/mixer). my @volume = Audio::Mixer::get_cval('vol'); if ( !@volume ) { die("Unable to obtain current volume\n"); } # Use the greater volume of left and right channels. my $cur = $volume[0] >= $volume[1] ? $volume[0] : $volume[1]; my $vol = $cur; # Process arguments. my $cmd = shift(@ARGV); if ( $cmd eq "incr" ) { $vol += opt_arg(5); $vol = 100 if $vol > 100; } elsif ( $cmd eq "decr" ) { $vol -= opt_arg(5); $vol = 0 if $vol < 0; } elsif ( $cmd eq "mute" ) { $vol = -1; } elsif ( $cmd =~ /^(\d+)%?$/ ) { $vol = $1; $vol = 100 if $vol > 100; } elsif ( $cmd ne "show" ) { die("Invalid option: $cmd\n"); } # No args may remain. usage() if @ARGV; # Toggle mute, if requested. $vol = mute_volume($cur) if $vol < 0; # Set and display the volume. if ( $vol >= 0 ) { if ( Audio::Mixer::set_cval('vol', $vol) != -1 ) { xosd(text => "Volume $vol%", percentage => $vol); } else { xosd(text => "Can't control volume"); } } elsif ( $vol < 0 ) { if ( Audio::Mixer::set_cval('vol', 0) != -1 ) { xosd(text => "Volume Muted"); } else { xosd(text => "Can't control volume"); } } ################ Subroutines ################ # Get a decimal value from @ARGV, if present. sub opt_arg { my ($value) = @_; if ( @ARGV && $ARGV[0] =~ /^(\d+)%?$/ ) { shift(@ARGV); $value = $1; } $value; } sub usage { die("Usage: xbk_aumix {show|mute|incr [N[%]]|decr [N[%]]|N[%}\n"); } sub mute_volume { my $current_vol = shift; my $mutefile = $ENV{HOME}."/.mutevol"; my $mutevol; if ( $current_vol == 0 ) { # muted, restore if ( -r $mutefile ) { # Restore previous volume from $mutefile. open(my $mv, "$mutefile") || die("$mutefile: $!\n"); $mutevol = <$mv>; close(MUTEVOL); unlink($mutefile); } else { # When $mutefile does not exist restore to 30%. $mutevol = 30; } } else { # store $current_vol in $mutefile open(my $mv, ">$mutefile") || die("$mutefile: $!\n"); print {$mv} ($current_vol); close(MUTEVOL); $mutevol = -1; } # Return new value. $mutevol; } sub xosd { my %args = @_; my $trace = 1; my $cmd = "/usr/bin/osd_cat"; my $cmdname = "osd_cat"; # Kill any pending displays. system("killall -q $cmdname > /dev/null 2>&1"); my @cmd = ($cmdname, qw(--pos middle --align center --color green --delay 2 --shadow 1 ), "--font", '-b&h-luxi sans-bold-r-normal--0-480-0-0-p-0-iso8859-1', ); if ( $args{percentage} ) { push(@cmd, "--barmode", "percentage", "--percentage", $args{percentage}); push(@cmd, "--text", $args{text}); warn("+ $cmd @cmd\n") if $trace; system {$cmd} (@cmd); } else { $cmd[0] = $cmd; push(@cmd, "--", "-"); warn("+ |@cmd\n") if $trace; my $xosd; open($xosd, "|-", @cmd); print {$xosd} $args{text}; close($xosd); } } =head1 AUTHOR Johan Vromans Based on a program written by Kel Modderman =head1 COPYRIGHT AND DISCLAIMER This program is Copyright 2004,2005 by Johan Vromans. This program is free software; you can redistribute it and/or modify it under the terms of the Perl Artistic License or the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. If you do not have a copy of the GNU General Public License write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.