[xiph-cvs] cvs commit: mgm/modules/linux battery

Monty xiphmont at xiph.org
Sun Apr 22 14:50:06 PDT 2001



xiphmont    01/04/22 14:50:06

  Modified:    modules/linux battery
  Log:
  Added battery module support via pmud for PowerMacs (G3/Wallstreet and
  later) running Linux.  Of *course* the bloody interfaces aren't the
  same for PC and Mac.  This is Linux.
  
  Should deal correctly with signle/dual batteries.  Hopefully I didn't
  break PC apm support.
  
  (Didn't implement for pre G3 powerbooks; the interface is different
  and I didn't think I'd have a good enough chance of getting it right
  without a machine to test on)
  
  Monty

Revision  Changes    Path
1.7       +159 -41   mgm/modules/linux/battery

Index: battery
===================================================================
RCS file: /usr/local/cvsroot/mgm/modules/linux/battery,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- battery	2000/04/12 00:43:56	1.6
+++ battery	2001/04/22 21:50:05	1.7
@@ -8,7 +8,8 @@
 # the object hash)
 
 package MGMmodule::battery;
-use vars qw($state $graph $widget $xpath);
+use Socket;
+use vars qw(@state $lstate $graph $widget $xpath $openp $batteries);
 
 sub module_init{
     my$this=shift;
@@ -19,6 +20,12 @@
     my$test=$this->read_proc;
     if(!defined($test)){
         $toplevel->optionAdd("$xclass.active",        'false',21);      
+    }else{
+	if($test=~/,/){
+	    $batteries=2;
+	}else{
+	    $batteries=1;
+	}
     }
     $toplevel->optionAdd("$xclass.order",201,21);      
     $this;
@@ -34,9 +41,18 @@
     $toplevel->optionAdd("$xpath.order",201,21); # status group
     $toplevel->optionAdd("$xpath.scalerefresh",10000,21); # 10s
     $toplevel->optionAdd("$xpath.scalereturn" ,1,21);     # don't need hyst
-    $toplevel->optionAdd("$xpath*label", "battery",21);
-    $toplevel->optionAdd("$xpath*lowlabel", "battery low",21);
-    $toplevel->optionAdd("$xpath.scalewidadj", 80,21);  # narrower
+
+    if($batteries==1){
+	$toplevel->optionAdd("$xpath*label", "battery",21);
+	$toplevel->optionAdd("$xpath*lowlabel", "battery low",21);
+    }else{
+	for(my$i=0;$i<$batteries;$i++){
+	    $toplevel->optionAdd("$xpath*$i.label", "battery$i",21);
+	    $toplevel->optionAdd("$xpath*$i.lowlabel", "battery$i low",21);
+	}
+    }
+
+    $toplevel->optionAdd("$xpath.scalewidadj", 80*$batteries,21);  # narrower
     $toplevel->optionAdd("$xpath.scalelenadj", 100,21);
 
     my$fg=&main::moption($this,"litforeground");
@@ -46,7 +62,7 @@
     $toplevel->optionAdd("$xpath*lowbackground", '#ff4040',21);
     $toplevel->optionAdd("$xpath*lowforeground", '#ffffff',21);
 
-    my($minx,$miny)=&MGM::Graph::calcxysize($this,100,'%',1);
+    my($minx,$miny)=&MGM::Graph::calcxysize($this,100,'%',$batteries);
     $toplevel->optionAdd("$xpath.minx",        $minx,21);      
     $toplevel->optionAdd("$xpath.miny",        $miny,21);      
     $this;
@@ -55,19 +71,97 @@
 sub module_run{
     my$this=shift;
     
-    $graph=MGM::Graph->new($this,num=>1,fixed=>1,rangesetting=>100,
+    $graph=MGM::Graph->new($this,num=>$batteries,fixed=>1,rangesetting=>100,
                            rangecurrent=>100,minscale=>0,
                            prompt=>'%');
 
     # color change hack
-    $state=0;
+    @state=map{0}(1...$batteries);
+    $lstate=0;
     $this->module_update;
     $graph->{"widget"};        # must return the widget
 }
 
+my @pmulist=("Unknown",
+	     "Unknown",
+	     "Unknown",
+	     "Unknown",
+	     "Unknown",
+	     "Unknown",
+	     "Unknown",
+	     "Unknown",
+	     "Unknown",
+	     "2400/3400/3500",
+	     "G3/Wallstreet",
+	     "G3/Lombard",
+	     "G4/NewWorld/iBook",
+	     "newer than Titanium");
+
+# Mac style power management
 sub read_proc{
+    my $percent;
+    my $line;
+
+    # get a connection to pmud if we don't have one
+    if(!$openp){
+	my $iaddr = inet_aton('localhost');
+	my $port = 879;
+	my $paddr = sockaddr_in($port,$iaddr);
+	
+	my $proto = getprotobyname('tcp');
+	
+	if(socket(B_FD, PF_INET, SOCK_STREAM, $proto)){
+	    if(connect(B_FD, $paddr)){
+		# pmud gives back the raw power status; the status is different
+		# for different powerbooks (grumble, grumble)
+		sysread B_FD,$_,1024;
+		if(m/^\S+\s+\S+\s+(\S+)/){
+		  SWITCH:{
+		      if($1>=10 && $1<=12){
+			  $openp=$1;
+			  last SWITCH;
+		      }
+		      print "PowerMac PMU version $1 (".
+			  $pmulist[$1].
+			      ") not supported yet.\n";
+		      close B_FD;
+		  }
+		} 
+	    }
+	}
+    }
+    
+    if($openp){
+	syswrite B_FD,"\n",1;
+	if($openp>=10 && $openp<=12){
+	    sysread B_FD,$line,1024;
+	    $line=~m/^S\s+\{([^\}]*)\}\s+\{([^\}]*)\}/;
+	    my$l=$1;
+	    my$r=$2;
+	    
+	    my($lac,$lcharge,$lbatt,$lcurrent,$lvoltage)=
+		split ' ',$l;
+	    my($rac,$rcharge,$rbatt,$rcurrent,$rvoltage)=
+		split ' ',$r;
+	    
+	    $percent= ($lcharge*1000.0 / $lbatt)/10.
+		if(defined $lcurrent);
+	    $percent.=","
+		if(defined ($percent) && defined ($rcurrent));
+	    $percent.= ($rcharge*1000.0 / $rbatt)/10.
+		if(defined $rcurrent);
+	    
+	}
+    }
+    $percent;
+}
+
+# PC style power management.  Of course they're not the same
+# interface.  This is linux.
+sub read_proc_apm{
     my$this=shift;
     my$percent;
+
     if(open(PROC,"/proc/apm")){
         sysread PROC,$_,1024;
         if(m/^\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+([^ \%]+)\%/){
@@ -81,54 +175,78 @@
 sub module_update{ 
     my$this=shift;
     my$toplevel=$this->{"toplevel"};
-    
-    my$percent=$this->read_proc;
-    if(defined($percent)){
+    my$percent;
+    my$maxpercent=-1;
+    my$i=0;
+    my at percentlist=split ",",$this->read_proc;
+    foreach $percent (@percentlist){
         # apm, for some reason, seems to occasionally return -1/1.
         # guard against that
-	if($percent<=1){
-	    $percent=$this->read_proc;
+	if($percent>=0){
+	    $maxpercent=$percent if($maxpercent<$percent);
         }
 
-	if($percent>=0){
-	    $graph->set($percent);
-	    if($percent<30){
-		# below 30%, it would be nice to unfix the scale so 
-		#the  user can see		
-		if($percent<8){
-		    if($state!=2){
-			$graph->
-			    barconfigure(0,'aforeXr'=>'lowforeground',
-					 'abackXr'=>'lowbackground',
-					 'labelXr'=>'lowlabel');
-			$graph->configure(fixed=>0,rangesetting=>8);
-			$state=2;
-		    }
-		}else{
-		    if($state!=1){
-			$graph->
-			    barconfigure(0,'aforeXr'=>'midforeground',
-					 'abackXr'=>'midbackground',
-					 'labelXr'=>'label');
-			$graph->configure(fixed=>0,rangesetting=>32);
-			$state=1;
-		    }
+	if($percent<30){
+	    if($percent<8){
+		if($state[$i]!=2){
+		    $graph->
+			barconfigure($i,'aforeXr'=>'lowforeground',
+				     'abackXr'=>'lowbackground',
+				     'labelXr'=>'lowlabel');
+		    $state[$i]=2;
                 }
             }else{
-		if($state!=0){
+		if($state[$i]!=1){
                     $graph->
-			barconfigure(0,'aforeXr'=>'litforeground',
-				     'abackXr'=>'litbackground',
-					     'labelXr'=>'label');
-		    $graph->configure(fixed=>1,rangesetting=>100);
-		    $state=0;
+			barconfigure($i,'aforeXr'=>'midforeground',
+				     'abackXr'=>'midbackground',
+				     'labelXr'=>'label');
+		    $state[$i]=1;
                 }
             }
+	}else{
+	    if($state[$i]!=0){
+		$graph->
+		    barconfigure(0,'aforeXr'=>'litforeground',
+				 'abackXr'=>'litbackground',
+				 'labelXr'=>'label');
+		$state[$i]=0;
+	    }
+	}
+	
+	$i++;
+    }
+    $percent=$maxpercent;
+    if($percent>=0){
+	
+	$graph->set(@percentlist);
+	
+	if($percent<30){
+	    # below 30%, it would be nice to unfix the scale so 
+	    # the user can see		
+	    if($percent<8){
+		if($lstate!=2){
+		    $graph->configure(fixed=>0,rangesetting=>8);
+		    $lstate=2;
+		}
+	    }else{
+		if($lstate!=1){
+		    $graph->configure(fixed=>0,rangesetting=>32);
+		    $lstate=1;
+		}
+	    }
+	}else{
+	    if($lstate!=0){
+		$graph->configure(fixed=>1,rangesetting=>100);
+		$lstate=0;
+	    }
         }
     }
 }
 
 sub destroy{
+    close B_FD if($openp);
+    $openp=0;
     undef $xpath;
 }
 

--- >8 ----
List archives:  http://www.xiph.org/archives/
Ogg project homepage: http://www.xiph.org/ogg/
To unsubscribe from this list, send a message to 'cvs-request at xiph.org'
containing only the word 'unsubscribe' in the body.  No subject is needed.
Unsubscribe messages sent to the list will be ignored/filtered.



More information about the commits mailing list