All pastes #606921 Raw Edit

bultom

public text v1 · immutable
·published 2007-07-07 06:18 UTC
rendered paste body
#!/usr/bin/perl
# YACAMWUN    : Yet Another Cool Acpi Manager With Unpronounceable Name
# Description : Simple program for managing acpi events
# Authors     : Bultom (also known as Cusanno Thomas bultom@gmail.com)
# Co-author   : Bubble (also known as l'benja )
# Licence     : Under GNU General Public License 2.0 and next
# Usage       : -a to view all statments -A to see acpi info -f to view CPU freq -V to view CPU temperature -b to view battry statment
# Usage       : -C to change CPU frequence -n to NCurse UI -u to TK UI -v for version -h for the help 
# Version     : 1.2
# Year        : 2007

use Term::ANSIColor;
use File::Spec;
use File::Basename;
use Getopt::Std;
use Tk;

#Defining variables
$|="1";
my $freq=(`grep "cpu MHz" /proc/cpuinfo |awk '{print \$4}'`);
my $bat=();
my $bs=();
my $av=();
my $temp=();
my $ac=();
my $fs=();
my $fs2=();
my $cg=();
my $ag=();
my $capmax = ();
my $caprem = ();
my $perbat = ();
my $ver=("1.2");
my $prog = basename $0;
my %options=();

if (-e "/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies") {
    $fs=`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies`; $fs =~ s/ / Hz\n/g;
    $fs2=(`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_frequencies`);
}

if (-e "/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors") {
    $cg=(`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors`); $cg =~s/ / \n/g;
}

if (-e "/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor") {
    $ag=(`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor`);
}

if (-e "/proc/acpi/battery/BAT0/state" ) {
    $bat=(`grep "remain" /proc/acpi/battery/BAT0/state|awk '{print \$3\$4}'`);
    $bs=(`grep "charg" /proc/acpi/battery/BAT0/state | awk '{print \$3}'`);
    $capmax = (`cat /proc/acpi/battery/BAT0/info |grep "design cap" |head -n 1 | awk '{print \$3}'`);
    $caprem = (`cat /proc/acpi/battery/BAT0/state |grep rema|awk '{print \$3}'`);
    $perbat = sprintf "%.2f", $caprem/$capmax*100;
}
if (-e "/proc/acpi/info") {
    $av=(`cat /proc/acpi/info | awk '{print \$2}'`);
}
if (-e "/proc/acpi/thermal_zone/THM/temperature" ) {
    $temp=(`grep "temp"  /proc/acpi/thermal_zone/THM/temperature |awk '{print \$2}'`);
}
if (-e "/proc/acpi/thermal_zone/THRM/temperature") {
    $temp=(`grep "temp"  /proc/acpi/thermal_zone/THRM/temperature |awk '{print \$2}'`);
}
if (-e "/proc/acpi/ac_adapter/AC/state") {
    $ac=(`grep "state" /proc/acpi/ac_adapter/*/state |awk '{print \$2}'`);
}

#cleaning variables
chomp($freq);
chomp($bat);
chomp($temp);
chomp($bs);
chomp($av);
chomp($ac);
chomp($ag);

getopts('aAbCfhnvtu',\%options);

if ($options{v}){
	print "\nThans for using $prog\nVersion $ver\n\n";
	exit;
}
elsif ($options{a}){
          if (-e "/proc/acpi/info") {
          print "Acpi Procfs seems to be installed \n";
          print "Acpi seem to be installed and supported on your computer\n";
          print "Acpi version is $av\n";
          }
          else {
          print "Acpi Procfs isn't installed results can be false \n";
          }
	print "CPU frequency is $freq MHz\n";
	  if (-e "/proc/acpi/thermal_zone/THM/temperature" ) {
	  print "CPU temperature is $temp C\n";
	  }
	  if (-e "/proc/acpi/thermal_zone/THRM/temperature" ) {
	  print "CPU temperature is $temp C\n";
	  }
	  if (-e "/proc/acpi/ac_adapter/AC/state") {
	  print "Ac-Adapter is $ac \n";
	  }
	  else {
	  print "Your system doesn't repport AC adapter state !\n";
	  }
	  if (-e "/proc/acpi/battery/BAT0/state" ) {
	  print "Battery remaining capacity is $bat\n";
	  print "Battery is currently $bs !\n";
	  print "Battery remaining: $perbat %\n";
	  }
	  else {
	  print "It seems that there is no battry plugged or your system don't use one\n";
	  }
	exit;
}
elsif ($options{f}){
	print "CPU frequency is $freq MHz\n";
	exit;
}
elsif ($options{t}){
	print "CPU temperature is $temp C\n";
	exit;
}
elsif ($options{b}){
        if (-e "/proc/acpi/battery/BAT0/state" ) {
	print "Battery remaining capacity is $bat \n";
	print "Battery is currently $bs !\n";
	print "Battery remaining: $perbat %\n";
	}
	else {
	print "It seems that there is no battery plugged or your system doesn't use one\n";
	}
	exit;
}
elsif ($options{A}){
        print "Acpi seem to be installed and supported on your computer\n";
	print "Acpi version is $av\n";
        exit;
}
elsif ($options{C}){
	if (`whoami` !~ /root/) {
	print color("red"),("You are not root , you can't modify CPU frequencies ;)\n"), color("reset");
	exit;
	}
	else {
		if ($ag !~ /userspace/ ) {
		print "CPU Governor is not set to userspace , you can't modify CPU frequencies\n";
		print "Do you want to change CPU governor to userspace ?\n";
		print "Yes\nNo\n";
		my $ch_gov=<STDIN>;
			if ($ch_gov =~ /yes/i ) {
			print "\nSwitching governor to userspace ...\n";
			system("echo userspace > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor");
			print "\nYou can use one of the following frequencies : $fs2\n";
			$freqn=<STDIN>;
			print "\nYou chose to set your CPU frequence to $freqn\n";
			print "Now you have 5 secondes for abort ...\n\n";
			sleep 5;
			system("echo $cf > /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed");
			print "Thank you for using $prog\n";
			exit;
			}
			else {
			print "You can't modify CPU frequencies without setting governor to userspace\n";
			print "Bye !\n";
			exit;
			}
		}
		else {
		print "\nYou can use one of the following frequencies : $fs2\n";
		$freqn=<STDIN>;
		print "\nYou chose to set your CPU frequence to $freqn\n";
		print "Now you have 5 secondes for abort ...\n\n";
		sleep 5;
		system("echo $cf > /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed");	
		print "Thank you for using $prog\n";
		exit;
		}
	}                
}

elsif ($options{h}){
	print "Usage:\n-a to view all statments\n-A to see acpi info\n-f to view CPU freq\n-t to view CPU temperature\n-b to view battry statment\n-C to change CPU frequence\n-n to call NCurse UI \n-u to call TK UI\n-v for version\n-h for this help\n\nAuthor : bultom\n\n";
	exit;
}
###GUI###
elsif ($options{u}){

sub quit
{
  print "Thanks for using $prog\n";
  print "\n";
  exit ( 0 ) ;
}

sub refresh 
{
if ( Exists ( $fille_acpi ) )
    {
    	$fille_acpi ->destroy ( );
    }
    $fille_acpi = $fenetre -> Toplevel ( ) ;
    $fille_acpi -> title ( "Acpi info" ) ;
    $fille_acpi -> raise ( ) ;
    $fille_acpi -> geometry('400x150+400+200');
    $fille_acpi -> resizable(0,0);
    my $freqi = (`grep "cpu MHz" /proc/cpuinfo |awk '{print \$4}'`);
    chomp($freqi);
    my $tempi=(`grep "temp"  /proc/acpi/thermal_zone/*/temperature |awk '{print \$2}'`);
    chomp($tempi);
    my $acpiLabel = $fille_acpi->Label(-text => "Acpi seem to be installed and supported on your computer\nAcpi version is $av\nCPU frequency is $freqi MHz\nCPU temperature is $tempi C\nAc-Adapter is $ac \nBattery remaining capacity is $bat\nBattery is currently $bs !\nBattery remaining: $perbat%\n" )->pack();
    $btn_refresh = $fille_acpi -> Button ( -text => 'Refresh' , -command => \&refresh ) ;
    $btn_refresh -> pack ( ) ;
}

sub infoacpi
{
    if ( Exists ( $fille_acpi ) )
    {
    	$fille_acpi ->destroy ( );
    }
    $fille_acpi = $fenetre -> Toplevel ( ) ;
    $fille_acpi -> title ( "Acpi info" ) ;
    $fille_acpi -> raise ( ) ;
    $fille_acpi -> geometry('400x150+400+200');
    $fille_acpi -> resizable(0,0);
    my $freqi = (`grep "cpu MHz" /proc/cpuinfo |awk '{print \$4}'`);
    chomp($freqi);
    my $tempi=(`grep "temp"  /proc/acpi/thermal_zone/*/temperature |awk '{print \$2}'`);
    chomp($tempi);
    my $acpiLabel = $fille_acpi->Label(-text => "Acpi seem to be installed and supported on your computer\nAcpi version is $av\nCPU frequency is $freqi MHz\nCPU temperature is $tempi C\nAc-Adapter is $ac \nBattery remaining capacity is $bat\nBattery is currently $bs !\nBattery remaining: $perbat%\n" )->pack();
    $btn_refresh = $fille_acpi -> Button ( -text => 'Refresh' , -command => \&refresh ) ;
    $btn_refresh -> pack ( ) ;
}

sub apply
{
    my ( $chg_g ) ;
    $chg_g = $chg_gov -> get ( ) ;
    system("echo $chg_g > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor");
    	if ( Exists ($fille_gov) )
    	{
    	    	$fille_gov -> destroy ( ) ;
    	    	$fille_gov = $fenetre -> Toplevel ( ) ;
    	    	$fille_gov -> title ( "CPU Governor" ) ;
            	$fille_gov -> raise ( ) ;
                my $govi = (`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor`);
                my $govLabel = $fille_gov->Label(-text => "Current CPU Governor is : $govi\nAvailable CPU Governors are : \n$cg")->pack();
                my $chg_g = ( ) ;
		$chg_gov = $fille_gov -> Entry ( -textvariable => \$chg_g ) ;
		$btn_apply = $fille_gov -> Button ( -text => 'Apply' , -command => \&apply ) ;
		$chg_gov -> pack ( ) ;
		$btn_apply-> pack ( ) ;
	}
	if ( Exists ( $fille_acpi ) )
    	{
    		$fille_acpi ->destroy ( );
    		$fille_acpi = $fenetre -> Toplevel ( ) ;
    		$fille_acpi -> title ( "Acpi info" ) ;
    		$fille_acpi -> raise ( ) ;
    		$fille_acpi -> geometry('400x150+400+200');
    		$fille_acpi -> resizable(0,0);
   	 	my $freqi = (`grep "cpu MHz" /proc/cpuinfo |awk '{print \$4}'`);
    		chomp($freqi);
    		my $tempi=(`grep "temp"  /proc/acpi/thermal_zone/*/temperature |awk '{print \$2}'`);
    		chomp($tempi);
    		my $acpiLabel = $fille_acpi->Label(-text => "Acpi seem to be installed and supported on your computer\nAcpi version is $av\nCPU frequency is $freqi MHz\nCPU temperature is $temp C\nAc-Adapter is $ac \nBattery remaining capacity is $bat\nBattery is currently $bs !\nBattery remaining: $perbat%\n" )->pack();
    		$btn_refresh = $fille_acpi -> Button ( -text => 'Refresh' , -command => \&refresh ) ;
    		$btn_refresh -> pack ( ) ;
    	} 	        
}
sub apply2
{
    my ( $chg_f ) ;
    $chg_f = $chg_freq -> get ( ) ;
    system("echo $chg_f > /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed");
    	if ( Exists ($fille_freq) )
    		{
	   	$fille_freq -> destroy ( ) ;
   		}
   		$fille_freq = $fenetre -> Toplevel ( ) ;
   		$fille_freq -> title ( "Adjust CPU frequencie" ) ;
   		$fille_freq -> raise ( ) ;
   		my $freqi = (`grep "cpu MHz" /proc/cpuinfo |awk '{print \$4}'`);
   		chomp($freqi);
   		my $freqLabel = $fille_freq->Label(-text => "Current CPU frequencie is : $freqi MHz\n\nAvailable frequencies for your system are : \n$fs")->pack();
   		$chg_f = ( ) ;
   		$chg_freq = $fille_freq -> Entry ( -textvariable => \$chg_f ) ;
   		$Hz = $fille_freq->Label(-text => 'Hz');
   		$btn_refresh2 = $fille_freq -> Button ( -text => 'Apply' , -command => \&apply2 ) ;
   		$chg_freq -> pack (-side => 'left');
   		$Hz -> pack (-side => 'left');
   		$btn_refresh2 -> pack (-side => 'right' ) ; 
   	if ( Exists ( $fille_acpi ) )
    		{
    		$fille_acpi ->destroy ( );
    		$fille_acpi = $fenetre -> Toplevel ( ) ;
    		$fille_acpi -> title ( "Acpi info" ) ;
    		$fille_acpi -> raise ( ) ;
    		$fille_acpi -> geometry('400x150+400+200');
    		$fille_acpi -> resizable(0,0);
   	 	my $freqi = (`grep "cpu MHz" /proc/cpuinfo |awk '{print \$4}'`);
    		chomp($freqi);
    		my $tempi=(`grep "temp"  /proc/acpi/thermal_zone/*/temperature |awk '{print \$2}'`);
    		chomp($tempi);
    		my $acpiLabel = $fille_acpi->Label(-text => "Acpi seem to be installed and supported on your computer\nAcpi version is $av\nCPU frequency is $freqi MHz\nCPU temperature is $temp C\nAc-Adapter is $ac \nBattery remaining capacity is $bat\nBattery is currently $bs !\nBattery remaining: $perbat%\n" )->pack();
    		$btn_refresh = $fille_acpi -> Button ( -text => 'Refresh' , -command => \&refresh ) ;
    		$btn_refresh -> pack ( ) ;
    	}
}

sub governor
{
    if ( Exists ($fille_gov) )
    {
    $fille_gov -> destroy ( ) ;
    }
    $fille_gov = $fenetre -> Toplevel ( ) ;
    $fille_gov -> title ( "CPU Governor" ) ;
    $fille_gov -> raise ( ) ;
    my $govi = (`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor`);
    my $govLabel = $fille_gov->Label(-text => "Current CPU Governor is : $govi\nAvailable CPU Governors are : \n$cg")->pack();
    my $chg_g = ( ) ;
    $chg_gov = $fille_gov -> Entry ( -textvariable => \$chg_g ) ;
    $btn_apply = $fille_gov -> Button ( -text => 'Apply' , -command => \&apply ) ;
    $chg_gov -> pack ( ) ;
    $btn_apply-> pack ( ) ;
}

sub frequence 
{
   if ( Exists ( $fille_freq ) )
   {
   	$fille_freq -> destroy ( ) ;
   }
   $fille_freq = $fenetre -> Toplevel ( ) ;
   $fille_freq -> title ( "Adjust CPU frequencie" ) ;
   $fille_freq -> raise ( ) ;
   my $freqi = (`grep "cpu MHz" /proc/cpuinfo |awk '{print \$4}'`);
   chomp($freqi);
   my $freqLabel = $fille_freq->Label(-text => "Current CPU frequencie is : $freqi MHz\n\nAvailable frequencies for your system are : \n$fs")->pack();
   $chg_f = ( ) ;
   $chg_freq = $fille_freq -> Entry ( -textvariable => \$chg_f ) ;
   $Hz = $fille_freq->Label(-text => 'Hz');
   
   $btn_refresh2 = $fille_freq -> Button ( -text => 'Apply' , -command => \&apply2 ) ;
   $chg_freq -> pack (-side => 'left');
   $Hz -> pack (-side => 'left');
   $btn_refresh2 -> pack (-side => 'right' ) ; 
}
sub about
{
    if ( Exists ( $fille_about ) )
    {
        $fille_about -> destroy ( ) ;
    }
    $fille_about = $fenetre -> Toplevel ( ) ;
    $fille_about -> title ( "About" ) ;
    $fille_about -> raise ( ) ;
    $fille_about -> geometry ('+808+200');
    my $mainLabel = $fille_about->Label(-text => "Yacamwun version $ver\n\n Copyright bultom 2006-2007\n\nSoftware under licence GPL-2\n\n")->pack();
}

    $fenetre = new MainWindow ( ) ;
    $fenetre -> title ( "Yacamwun" ) ;
    $fenetre ->minsize('250','30');
    $fenetre ->maxsize('250','30');
    $fenetre -> geometry('250x30+400+141');
    $barre_menu = $fenetre -> Frame ( -relief => 'groove' , -borderwidth => 2 ) ;
    $menu_fichier = $barre_menu -> Menubutton (
                                -text => 'File' ,
                                -tearoff => 0 ,
                                -menuitems => [
                                    [ 'command' => 'Quit',
                                -command => \&quit ] ,
				 ]
                            ) ;
    $menu_acpi = $barre_menu    -> Menubutton (
                                -text => 'Acpi' ,
                                -tearoff => 0 ,
                                -menuitems => [
				[ 'command' => 'Acpi info' ,
				-command => \&infoacpi
				] ,
				[ 'command' => 'Adjust CPU governor (need root privilege)' ,
				-command => \&governor
				] ,
				[ 'command' => 'Adjust CPU Frequencie (need root privilege)' ,
				-command => \&frequence
				] ,
                                ]
                            ) ;
    $menu_apropos = $barre_menu -> Menubutton (
                                -text => 'About' ,
                                -tearoff => 0 ,
                                -menuitems => [
				[ 'command' => 'About' ,
				-command => \&about 
				] ,
                                ]
                            ) ;


    $menu_fichier -> pack ( -side => 'left' ) ;
    $menu_acpi -> pack ( -side => 'left' ) ;
    $menu_apropos -> pack ( -side => 'right' ) ;
    $barre_menu -> pack ( -side => 'top' , -anchor => 'n' , -expand => 1 , -fill => 'x' ) ;
MainLoop ( ) ;
exit ;
}
###/GUI###
elsif ($options{n}){
	print "\n";


#Cleaning variables
chomp($freq);
chomp($bat);
chomp($temp);
chomp($bs);
chomp($av);
chomp($ac);

#cleaning messy terminals ^_^
system("clear");

print "\n";

#Saying Welcome ^_^
print "Welcome , and thank you for using $prog version $ver \n\n\n";


sleep 3;
system("clear");

#Warning noobs :x

print color("red"),("*************************************************\n"), color("reset");
print color("red"),("*                                               *\n"), color("reset");
print color("red"),("*                                               *\n"), color("reset");
print color("red"),("*You Will Need Root Access for Setting Frequency*\n"), color("reset");
print color("red"),("*                                               *\n"), color("reset");
print color("red"),("*                                               *\n"), color("reset");
print color("red"),("*************************************************\n\n\n"), color("reset");

print " Please select what you want to do ...\n";

print " 1) See your computer state\n";
print " 2) Interact with acpi \n";
$choice = <STDIN>;

	if ( $choice =~ /1/ ){
#User want to see his computer state , let's show it to him
		if (-e "/proc/acpi/info") {
                        print "Acpi Procfs seems to be installed and supported on your computer\n"; 
	                print "Acpi version is $av\n";
	                print "CPU frequency is $freq MHz\n";
	                print "CPU temperature is $temp C\n";
	                print "Ac-Adapter is $ac \n"; 
	                print "Battery remaining capacity is $bat\n";
	                print "Battery is currently $bs !\n";
		}
	      else {
	                print "Acpi Procfs isn't installed results can be false \n";
	        }
	        if (-e "/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors") {
	        print "CPU Gouvernors are active good\n";
	        if (`cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors` =~ /userspace/) {
	        print "Userspace CPU Gouvernor is present , you can change cpu frequency in 'Interact with acpi'  ...\n\n";
	         }
	        }
	                                       
	       else {
	       die "CPU Gouvernors are not active \n";
	       }
	print "Thanks for using $prog\n";
	
	                                                
	} elsif  ( $choice =~ /2/){
#User want to play with acpi , let's have some fun ^_^
	print color("red"),("Now checking root privilege , you will not able to change cpu frequency if you don't have them\n\n"), color("reset");
	if (`whoami` =~ /root/) {
	        print color("yellow"),("You have root privilege you can pass through\n\n"), color("reset");
	}
	        
	else {
	        die "You don't have root privilege , you can't continue\n\n";
	}
	        
	print "Current CPU Gouvernor is : $ag\n";
	print "These are available governors : \n$cg\n";
	print color("blue"),("To adjust frequencie by hand you need to set governor to userspace\n"),color("reset");
	print "\nDo you want to set governor to userspace ? (you must type YES if you are ok)\n";
		print " 1) Yes\n";
		print " 2) No \n";		
		$choice2 = <STDIN>;
		chomp $choice2;
			if ( $choice2 =~ /yes/i){
				print "Setting governor to Userspace ...\n";
				system("echo userspace > /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor");
			}
			
			                                 	
	print "Enter your selected frequency here , currently it's $freq , you can choose one these:"; print "\n$fs";
	       $cf = <STDIN>;
	       chomp $cf;
	       print "You decided to modify cpu frequency to $cf .\n\n\n";
	       print "Now you have 5 secondes for abort ...\n\n";
	       sleep 5;
	       system("echo $cf > /sys/devices/system/cpu/cpu0/cpufreq/scaling_setspeed");
	       my $freq2=(`grep "cpu MHz" /proc/cpuinfo |awk '{print \$4}'`);
	       chomp $freq2;
	       print "Please wait will refreshing CPU frequency\n";
	       sleep 3;
	       print "CPU frequency is now $freq2 MHz\n";
	                                                                                     
	       print "Thanks for using $prog\n";
	
	
	                                                                                                        
	} else {
	die "you must answer by 1 or 2\n";
	}
}
else {
print "Usage:\n-a to view all statments\n-A to see acpi info\n-f to view CPU freq\n-t to view CPU temperature\n-b to view battry statment\n-C to change CPU frequencie\n-n to call NCurse UI \n-u to call TK UI\n-v for version\n-h for this help\n\nAuthor : bultom\n\n";
exit;
}