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

Monty xiphmont at xiph.org
Wed Feb 20 15:07:03 PST 2002



xiphmont    02/02/20 15:07:03

  Modified:    .        mgm
               lib      widget
               modules/linux 00helper battery
  Log:
  new modules:
  
  wireless (Linux):
    display wireless ethernet signal strength, one bar per WiFi card
    installed.  Properly installs/uninstalls meter on change of card
    availability
  
  new features:
  
  battery: now sees multiple batteries using Powermac PMU interface.
    Old interface has one bar per battery installed; new 2.4.x /proc/pmu
    interface knows how many battery bays in given machine, MGM can
    report 'no battery' for empty bays.
  
    reports 'charging' or 'ac power' in bar graph when plugged in, along
    with color change.
  
  fixes:
    Latest 2.4 kernels broke CPUstat on PowerMac due to gratuitous
    interface output change.  Now reads old and new format.
  
  Monty

Revision  Changes    Path
1.32      +15 -4     mgm/mgm

Index: mgm
===================================================================
RCS file: /usr/local/cvsroot/mgm/mgm,v
retrieving revision 1.31
retrieving revision 1.32
diff -u -r1.31 -r1.32
--- mgm	1999/09/30 21:39:46	1.31
+++ mgm	2002/02/20 23:07:01	1.32
@@ -306,7 +306,7 @@
     foreach my $key (@ordered){
         my $mod=$instances{$key};
 
-	if(defined($mod)){
+	if(defined($mod) && &mplace($mod)){
             
             my $active=&moption($mod,"active");
             my $pad=&moption($mod,"widgetpad");
@@ -319,7 +319,7 @@
                     $ladj=&max(1,&max($ladj,&moption($mod,"scalelenadj")));
                     $wadj+=&max(1,&moption($mod,"scalewidadj")); 
                 }
-		
+
                 if($stack eq 'vertical'){
                     my$tempx=&moption($mod,'minx')+$pad*2;
                     $minx=$tempx if ($minx<$tempx);
@@ -368,7 +368,7 @@
     my$pos=0;
     foreach my $key (@ordered){
         my $mod=$instances{$key};
-	if(defined($mod)){
+	if(defined($mod) && mplace($mod)){
             my $pad=&moption($mod,"widgetpad");
             my $justify=&moption($mod,"scalejustify");
             my$x;
@@ -471,7 +471,9 @@
             my$ret=$mod->{'widget'};
             if(defined($ret)){
                 $ret->place('-x'=>$mod->{'placex'},
-			    '-y'=>$mod->{'placey'},-anchor=>'nw');
+			    '-y'=>$mod->{'placey'},-anchor=>'nw')
+		    if(mplace($mod));
+
                 my$refresh=$ret->optionGet("scalerefresh","");
                 $ret->repeat($refresh,sub{$mod->module_update})
                     if($refresh && ref($mod)->can('module_update'));
@@ -574,4 +576,13 @@
 sub moption{
     my($mod,$option)=@_;
     $mod->{"widget"}->optionGet($option,"");
+}
+
+sub mplace{
+    my($mod)=@_;
+    if(ref($mod)->can('module_place_p')){
+	$mod->module_place_p();
+    }else{
+	1;
+    }
 }

<p><p>1.24      +176 -164  mgm/lib/widget

Index: widget
===================================================================
RCS file: /usr/local/cvsroot/mgm/lib/widget,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- widget	2000/04/12 00:43:56	1.23
+++ widget	2002/02/20 23:07:02	1.24
@@ -65,6 +65,7 @@
     $this->{"width"}=$width;
     $this->{"height"}=$height;
     $this->{"num"}=$num;
+    $this->{"value"}=0;
 
     $this->drawit;
     $this;
@@ -84,6 +85,7 @@
     $this->{'above'}->delete('xpmlabel');
     
     $this->drawit;
+    $this->set($this->{'value'});
 }
 
 sub drawit{
@@ -215,6 +217,8 @@
 sub set{
     my$bar=shift;
     my$per=shift;
+
+    $bar->{'value'}=$per;
     my $orient=$main::orient;
     if($orient eq 'vertical'){
         $bar->{'above'}->
@@ -318,40 +322,42 @@
     my($p,$scaleset,$prompt)=@_;
     my $orient=$main::orient;
     my $mult;
-
-    ($scaleset,$mult)=&scalemod($scaleset);
-    my$label;
-
-    my($textw,$textl)=&promptsize("$mult$prompt",$scaleset);
 
-    if($orient eq 'vertical'){
-	$label=$p->{"widget"}->Canvas(Name=>'scalerange',
-				      -borderwidth=>0,
-				      highlightthickness=>0,
-				      width=>$textw,
-				      height=>$textl,
-				      relief=>'flat')->
-					  place('-x'=>0,'-y'=>2,-anchor=>'nw');
-	$label->createImage(0,0,
-			    -image=>$prompts{"$mult$prompt"},-anchor=>'nw');
-	$label->createImage(0,$prompts{"$mult$prompt"}->height,
-			    -image=>$numbers{"$scaleset"},-anchor=>'nw');
-    }else{
-	$label=$p->{"widget"}->Canvas(Name=>'scalerange',
-				      -borderwidth=>0,
-				      highlightthickness=>0,
-				      width=>$textl,
-				      height=>$textw,
-				      relief=>'flat')->
-					  place(-relx=>1.0,
-						'-x'=>-2,'-y'=>0,
-						-anchor=>'ne');
-	$label->createImage($numbers{"$scaleset"}->width,0,
-			    -image=>$prompts{"$mult$prompt"},-anchor=>'nw');
-	$label->createImage(0,0,
-			    -image=>$numbers{"$scaleset"},-anchor=>'nw');
+    if($p->{'scalep'}){
+	($scaleset,$mult)=&scalemod($scaleset);
+	my$label;
+	
+	my($textw,$textl)=&promptsize("$mult$prompt",$scaleset);
+	
+	if($orient eq 'vertical'){
+	    $label=$p->{"widget"}->Canvas(Name=>'scalerange',
+					  -borderwidth=>0,
+					  highlightthickness=>0,
+					  width=>$textw,
+					  height=>$textl,
+					  relief=>'flat')->
+					      place('-x'=>0,'-y'=>2,-anchor=>'nw');
+	    $label->createImage(0,0,
+				-image=>$prompts{"$mult$prompt"},-anchor=>'nw');
+	    $label->createImage(0,$prompts{"$mult$prompt"}->height,
+				-image=>$numbers{"$scaleset"},-anchor=>'nw');
+	}else{
+	    $label=$p->{"widget"}->Canvas(Name=>'scalerange',
+					  -borderwidth=>0,
+					  highlightthickness=>0,
+					  width=>$textl,
+					  height=>$textw,
+					  relief=>'flat')->
+					      place(-relx=>1.0,
+						    '-x'=>-2,'-y'=>0,
+						    -anchor=>'ne');
+	    $label->createImage($numbers{"$scaleset"}->width,0,
+				-image=>$prompts{"$mult$prompt"},-anchor=>'nw');
+	    $label->createImage(0,0,
+				-image=>$numbers{"$scaleset"},-anchor=>'nw');
+	}
+	$label;
     }
-    $label;
 }
 
 sub new{
@@ -427,74 +433,77 @@
     
     # a 'graph' consists of a scale, and <n> bars under central control
 
-    my $name=$this->{"name"};
     my $num=$this->{"num"};
-    my $prompt=$this->{"prompt"};
-    my $fixed=$this->{"fixed"};
-
-    my $height=$this->{"height"};
-    my $width=$this->{"width"};
-    my $widget=$this->{"widget"};
-
-    $widget->configure(-borderwidth=>0,
-		       highlightthickness=>0,
-		       width=>$width,
-		       height=>$height,
-		       relief=>'flat');
-
-    my $rangesetting=$this->{"rangesetting"};
-    my($dummy,$dummy2,$scalew)=&calcsize($this,$rangesetting,$prompt,$num);
-    $this->{"scalew"}=$scalew;
-    $this->{"numlabel"}=$this->promptlabel($rangesetting,$prompt);
- 
-    my $orient=$main::orient;
-    if($orient eq 'vertical'){
-	$this->drawvscale if($scalew);
+    if($num){
 
-	$this->{"barlength"}=$height;
-	my$barwidth=$width-$scalew;
-	my$frame=$this->{"barframe"}=
-	    $widget->Frame(Name=>'bar',
-			   -width=>$barwidth,
-			   -height=>$height,
-			   -borderwidth=>0)->
-			       place('-x'=>$scalew,
-				     '-y'=>0,
-				     '-anchor'=>'nw');
-	
-	$barwidth/=$num;
-	$barwidth=int($barwidth);
-	my$x=0;
-	for(my$i=0;$i<$num;$i++){
-	    $this->{$i}=MGM::Bar->new($frame,$barwidth-1,
-				      $height,
-				      $i)->place('-x'=>$x,'-y'=>0,
-						 '-anchor'=>'nw');
-	    $x+=$barwidth;
-	}
-    }else{
-	$this->drawhscale if($scalew);
-
-	$this->{"barlength"}=$width;
-	my$barwidth=$height-$scalew;
-	my$frame=$this->{"barframe"}=
-	    $widget->Frame(Name=>'bar',
-			   -width=>$width,
-			   -height=>$barwidth,
-			   -borderwidth=>0)->
-			       place('-y'=>$scalew,
-				     '-x'=>0,
-				     -anchor=>'nw');
-	
-	$barwidth/=$num;
-	$barwidth=int($barwidth);
-	my$y=0;
-	for(my$i=0;$i<$num;$i++){
-	    $this->{$i}=MGM::Bar->new($frame,$width,
-				      $barwidth-1,
-				      $i)->place('-x'=>0,'-y'=>$y,
-						 -anchor=>'nw');
-	    $y+=$barwidth;
+	my $name=$this->{"name"};
+	my $prompt=$this->{"prompt"};
+	my $fixed=$this->{"fixed"};
+	
+	my $height=$this->{"height"};
+	my $width=$this->{"width"};
+	my $widget=$this->{"widget"};
+
+	$widget->configure(-borderwidth=>0,
+			   highlightthickness=>0,
+			   width=>$width,
+			   height=>$height,
+			   relief=>'flat');
+	
+	my $rangesetting=$this->{"rangesetting"};
+	my($dummy,$dummy2,$scalew)=&calcsize($this,$rangesetting,$prompt,$num);
+	$this->{"scalew"}=$scalew;
+	$this->{"numlabel"}=$this->promptlabel($rangesetting,$prompt);
+	
+	my $orient=$main::orient;
+	if($orient eq 'vertical'){
+	    $this->drawvscale if($scalew);
+	    
+	    $this->{"barlength"}=$height;
+	    my$barwidth=$width-$scalew;
+	    my$frame=$this->{"barframe"}=
+		$widget->Frame(Name=>'bar',
+			       -width=>$barwidth,
+			       -height=>$height,
+			       -borderwidth=>0)->
+				   place('-x'=>$scalew,
+					 '-y'=>0,
+					 '-anchor'=>'nw');
+	    
+	    $barwidth/=$num;
+	    $barwidth=int($barwidth);
+	    my$x=0;
+	    for(my$i=0;$i<$num;$i++){
+		$this->{$i}=MGM::Bar->new($frame,$barwidth-1,
+					  $height,
+					  $i)->place('-x'=>$x,'-y'=>0,
+						     '-anchor'=>'nw');
+		$x+=$barwidth;
+	    }
+	}else{
+	    $this->drawhscale if($scalew);
+	    
+	    $this->{"barlength"}=$width;
+	    my$barwidth=$height-$scalew;
+	    my$frame=$this->{"barframe"}=
+		$widget->Frame(Name=>'bar',
+			       -width=>$width,
+			       -height=>$barwidth,
+			       -borderwidth=>0)->
+				   place('-y'=>$scalew,
+					 '-x'=>0,
+					 -anchor=>'nw');
+	    
+	    $barwidth/=$num;
+	    $barwidth=int($barwidth);
+	    my$y=0;
+	    for(my$i=0;$i<$num;$i++){
+		$this->{$i}=MGM::Bar->new($frame,$width,
+					  $barwidth-1,
+					  $i)->place('-x'=>0,'-y'=>$y,
+						     -anchor=>'nw');
+		$y+=$barwidth;
+	    }
         }
     }
 }
@@ -694,6 +703,7 @@
 
 sub _animate{
     my $this=shift;
+
     $scalep=$this->{"scalep"};
     $scrollp=$this->{"scroll"};
     
@@ -745,83 +755,85 @@
 sub set{
     my$this=shift;
     my$num=$this->{"num"};
-
-    my$length=$this->{"barlength"};
 
-    # length is in barlength
-    # current scale setting in rangecurrent
-    # scale 'goal' in rangesetting (may smooth-scroll)
-
-    if($#_>=0){
-	# we have new values incoming.  We need to do full maneuvers
-
-	if($#_!=$num-1){
-	    # might as well.  prevents spurious error printing when 
-	    # something unexpected happens to a module (like a CPU
-	    # disappearing ;-)
-
-	    push @_,$#_+1,map{0}(1..$num); #lazy
-	    $#_=$num-1;
-	}
-
-	# find the largest value for scale adjustment
-	my$metermax=0;
-	map{$metermax=$_ if ($metermax<$_)}(@_, @{$this->{"meter"}},
-					    $this->{"scalemax"});
-	$this->{"scalemax"}=$metermax;
-	my$fixed=$this->{"fixed"};
-	
-	# raise the scale range?
-	my$redrawflag=0;
-	if($fixed==0 && $metermax>$this->{"rangesetting"}){
-	    splice @{$this->{"meter"}},0,$#_+1, at _;
-	    while($metermax>$this->{"rangesetting"}){
-		if($this->{"rangesetting"} == 0){
-		    $this->{"rangesetting"}=1;
-		}else{
-		    $this->{"rangesetting"}*=2;
-		}
-		$this->{"scaletimer"}=0; # reset the timer
-		$this->{"scalemax"}=$metermax;
-		$this->_newscale();
-	    }
-	}else{
-	    # lower the scale range?
-	    my$scaletimer=$this->{"scaletimer"}+1;
-	    if($fixed==0 && $scaletimer>=$this->{"scalereturn"} &&
-	       $this->{"rangesetting"}>$this->{"minscale"} && 
-	       $metermax*$this->{"scalethresh"}<$this->{"rangesetting"} &&
-	       $this->{"rangesetting"}==$this->{"rangecurrent"}){
+    if($num){
+	my$length=$this->{"barlength"};
+	
+	# length is in barlength
+	# current scale setting in rangecurrent
+	# scale 'goal' in rangesetting (may smooth-scroll)
+	
+	if($#_>=0){
+	    # we have new values incoming.  We need to do full maneuvers
+	    
+	    if($#_!=$num-1){
+		# might as well.  prevents spurious error printing when 
+		# something unexpected happens to a module (like a CPU
+		# disappearing ;-)
                 
-		$this->{"rangesetting"}=int($this->{"rangesetting"}/2);
-		$this->{"scaletimer"}=0;
-		$this->{"scalemax"}=0;
+		push @_,$#_+1,map{0}(1..$num); #lazy
+		$#_=$num-1;
+	    }
+	    
+	    # find the largest value for scale adjustment
+	    my$metermax=0;
+	    map{$metermax=$_ if ($metermax<$_)}(@_, @{$this->{"meter"}},
+						$this->{"scalemax"});
+	    $this->{"scalemax"}=$metermax;
+	    my$fixed=$this->{"fixed"};
+	    
+	    # raise the scale range?
+	    my$redrawflag=0;
+	    if($fixed==0 && $metermax>$this->{"rangesetting"}){
                 splice @{$this->{"meter"}},0,$#_+1, at _;
-		$this->_newscale();
-		
-	    }else{
-		my$meter=$this->{"meter"};
-		for(my$i=0;defined(my$val=shift);$i++){
-		    if($meter->[$i]!=$val){
-			$meter->[$i]=$val;
-			$val*=$length/$this->{"rangecurrent"};
-			$this->{$i}->set($val);
+		while($metermax>$this->{"rangesetting"}){
+		    if($this->{"rangesetting"} == 0){
+			$this->{"rangesetting"}=1;
+		    }else{
+			$this->{"rangesetting"}*=2;
                     }
+		    $this->{"scaletimer"}=0; # reset the timer
+		    $this->{"scalemax"}=$metermax;
+		    $this->_newscale();
                 }
-		
-		if($scaletimer>=$this->{"scalereturn"}){
+	    }else{
+		# lower the scale range?
+		my$scaletimer=$this->{"scaletimer"}+1;
+		if($fixed==0 && $scaletimer>=$this->{"scalereturn"} &&
+		   $this->{"rangesetting"}>$this->{"minscale"} && 
+		   $metermax*$this->{"scalethresh"}<$this->{"rangesetting"} &&
+		   $this->{"rangesetting"}==$this->{"rangecurrent"}){
+		    
+		    $this->{"rangesetting"}=int($this->{"rangesetting"}/2);
                     $this->{"scaletimer"}=0;
                     $this->{"scalemax"}=0;
+		    splice @{$this->{"meter"}},0,$#_+1, at _;
+		    $this->_newscale();
+		    
                 }else{
-		    $this->{"scaletimer"}=$scaletimer;
-		    $this->{"scalemax"}=$metermax;
+		    my$meter=$this->{"meter"};
+		    for(my$i=0;defined(my$val=shift);$i++){
+			if($meter->[$i]!=$val){
+			    $meter->[$i]=$val;
+			    $val*=$length/$this->{"rangecurrent"};
+			    $this->{$i}->set($val);
+			}
+		    }
+		    
+		    if($scaletimer>=$this->{"scalereturn"}){
+			$this->{"scaletimer"}=0;
+			$this->{"scalemax"}=0;
+		    }else{
+			$this->{"scaletimer"}=$scaletimer;
+			$this->{"scalemax"}=$metermax;
+		    }
                 }
             }
+	    
+	}else{
+	    # just scale animation.  No reconfig
+	    $this->_animate();
         }
-
-    }else{
-	# just scale animation.  No reconfig
-	$this->_animate();
     }
 }
 

<p><p>1.4       +26 -5     mgm/modules/linux/00helper

Index: 00helper
===================================================================
RCS file: /usr/local/cvsroot/mgm/modules/linux/00helper,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- 00helper	1999/09/28 20:23:14	1.3
+++ 00helper	2002/02/20 23:07:02	1.4
@@ -1,7 +1,8 @@
 # -*-Perl-*-
 
-# this module is a hack; it isn't a widget builder.  It sits in the background
-# and reads /proc/stat for other modules to reduce opens/closes.
+# this module is a hack; it isn't a widget builder.  It sits in the
+# background and reads /proc/stat, /proc/net/dev and
+# /proc/net/wireless for other modules to reduce opens/closes.
 
 # It defines an init method that installs the /proc/stat listener,
 # then marks itself 'inactive' so that mgm doesn't try to call _run,
@@ -11,7 +12,7 @@
 
 
 package MGMmodule::helperST;
-use vars qw(%proc %net $lastmod $active $active_if);
+use vars qw(%proc %net %wi $lastmod $active $active_if $wi_count $wi_active);
 use IO::Seekable;
 
 sub module_init{
@@ -25,7 +26,11 @@
     
     $lastmod=0;
     $active=0;
+    $wi_active=0;
     if(open(PROC,"/proc/stat")){
+	if(open(WI,"/proc/net/wireless")){
+	    $wi_active=1;
+	}
         if(open(NET,"/proc/net/dev")){
             $active=1;
             $this->module_update;
@@ -43,18 +48,34 @@
 sub module_update{
     my$data;
     my$netd;
+    my$wid;
     sysseek PROC, 0, SEEK_SET;
     sysseek NET, 0, SEEK_SET;
     sysread NET,$netd,1024;
     sysread PROC,$data,4096;
-
+    if($wi_active){
+	sysseek WI, 0, SEEK_SET;
+	sysread WI,$wid,4096;
+	$wi_count=0;
+	undef %wi;
+	map{
+	    my$pos=rindex $_, ":";
+	    if($pos>0){
+		my$key=substr $_,0,$pos;
+		$key=~s/^\s*(\S+)\s*/$1/;
+		$wi{$key}=substr $_, $pos+1;
+		$wi_count++;
+	    }
+	} split "\n", $wid;
+    }
+	
     $active_if=0;
     undef%net;
     map{
         my$pos=rindex $_, ":";
         if($pos>0){
             my$key=substr $_,0,$pos;
-	    $key=~s/^\s*(\S+)/$1/;
+	    $key=~s/^\s*(\S+)\s*/$1/;
             $net{$key}=substr $_, $pos+1;
             $active_if++;
         }

<p><p>1.9       +219 -50   mgm/modules/linux/battery

Index: battery
===================================================================
RCS file: /usr/local/cvsroot/mgm/modules/linux/battery,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- battery	2001/04/23 17:34:50	1.8
+++ battery	2002/02/20 23:07:02	1.9
@@ -8,24 +8,20 @@
 # the object hash)
 
 package MGMmodule::battery;
+use IO::Seekable;
 use Socket;
-use vars qw(@state $lstate $graph $widget $xpath $openp $batteries);
+use vars qw(@percent @battstatus @gstate $ac $lstate $graph $widget 
+	    $xpath $openp $openf $batteries $ac $active $lastbat);
 
 sub module_init{
     my$this=shift;
     my$xclass=$this->{"xclass"};
     my$toplevel=$this->{"toplevel"};
 
-
-    my$test=$this->read_proc;
-    if(!defined($test)){
+    $active=0;
+    $this->read_proc;
+    if($active==0){
         $toplevel->optionAdd("$xclass.active",        'false',21);      
-    }else{
-	if($test=~/,/){
-	    $batteries=2;
-	}else{
-	    $batteries=1;
-	}
     }
     $toplevel->optionAdd("$xclass.order",201,21);      
     $this;
@@ -37,18 +33,26 @@
     return undef if(defined($xpath));
     $xpath=$this->{"xpath"};
 
+    $lastbat=$batteries;
+
     # modify defaults
     $toplevel->optionAdd("$xpath.order",201,21); # status group
-    $toplevel->optionAdd("$xpath.scalerefresh",10000,21); # 10s
+    $toplevel->optionAdd("$xpath.scalerefresh",4000,21);
     $toplevel->optionAdd("$xpath.scalereturn" ,1,21);     # don't need hyst
 
-    if($batteries==1){
+    if($batteries<=1){
         $toplevel->optionAdd("$xpath*label", "battery",21);
         $toplevel->optionAdd("$xpath*lowlabel", "battery low",21);
+	$toplevel->optionAdd("$xpath*nonelabel", "no battery",21);
+	$toplevel->optionAdd("$xpath*chargelabel", "charging",21);
+	$toplevel->optionAdd("$xpath*fulllabel", "ac power",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*$i.nonelabel", "no battery$i",21);
+	    $toplevel->optionAdd("$xpath*$i.chargelabel", "charging $i",21);
+	    $toplevel->optionAdd("$xpath*$i.fulllabel", "ac power",21);
         }
     }
 
@@ -61,6 +65,10 @@
     $toplevel->optionAdd("$xpath*midbackground", '#d0d060',21);
     $toplevel->optionAdd("$xpath*lowbackground", '#ff4040',21);
     $toplevel->optionAdd("$xpath*lowforeground", '#ffffff',21);
+    $toplevel->optionAdd("$xpath*acbackground", '#808080',21);
+    $toplevel->optionAdd("$xpath*acforeground", '#000000',21);
+    $toplevel->optionAdd("$xpath*chargebackground", '#74ade7',21);
+    $toplevel->optionAdd("$xpath*chargeforeground", '#000000',21);
 
     my($minx,$miny)=&MGM::Graph::calcxysize($this,100,'%',$batteries);
     $toplevel->optionAdd("$xpath.minx",        $minx,21);      
@@ -76,10 +84,10 @@
                            prompt=>'%');
 
     # color change hack
-    @state=map{0}(1...$batteries);
+    @gstate=map{0}(1...$batteries);
     $lstate=0;
     $this->module_update;
-    $graph->{"widget"};        # must return the widget
+    $widget=$graph->{"widget"};        # must return the widget
 }
 
 my @pmulist=("Unknown",
@@ -101,17 +109,71 @@
     my$test;
     my$this=shift;
 
-    if($openp){return $this->read_proc_mac;}
+    if($openp || $openf){return $this->read_proc_mac;}
     $test=$this->read_proc_apm;
     if(defined($test)){return $test;}
-    return $this->read_proc_mac;
+    $this->read_proc_mac;
 }
 
 # Mac style power management
 sub read_proc_mac{
     my $percent;
     my $line;
+    my $data;
+
+    # recent 2.4 has gone to a /proc entry for pmu values.  Use that
+    # if we can.
+    if(!$openf && !$openp){
+	if(open(PROC,"/proc/pmu/info")){
+	    $openf=1;
+	}
+    }
+
+    if($openf){
+	my %hash;
+	
+	sysseek PROC, 0, SEEK_SET;
+	sysread PROC,$data,4096;
+
+	map{
+	    my$pos=rindex $_, ":";
+	    if($pos>0){
+		my$key=substr $_,0,$pos;
+		$key=~s/^\s*(\S+)\s(\S*)\s*/$1$2/;
+		$hash{$key}=substr $_, $pos+1;
+	    }
+	} split "\n", $data;
+	$ac=int($hash{'ACPower'});
+	$batteries=int($hash{'Batterycount'});
+	
+	for(my$i=0;$i<$batteries;$i++){
+	    if(open(FPROC,"/proc/pmu/battery_$i")){
+		sysread FPROC,$data,4096;
+		map{
+		    my$pos=rindex $_, ":";
+		    if($pos>0){
+			my$key=substr $_,0,$pos;
+			$key=~s/^\s*(\S+)\s*/$1/;
+			$hash{$key}=substr $_, $pos+1;
+		    }
+		} split "\n", $data;
+		close FPROC;
+		if($hash{'max_charge'}>0){
+		    $percent[$i]=$hash{'charge'}*100./$hash{'max_charge'}
+		}else{
+		    $percent[$i]=0;
+		}
 
+		$battstatus[$i]=0;
+		$battstatus[$i]=1 if($hash{'current'}>0);
+		$battstatus[$i]=-1 if($hash{'flags'}==0);
+		$active=1;
+	    }
+	}
+	return;
+    }
+  
+
     # get a connection to pmud if we don't have one
     if(!$openp){
         my $iaddr = inet_aton('localhost');
@@ -128,6 +190,7 @@
                 if(m/^\S+\s+\S+\s+(\S+)/){
                   SWITCH:{
                       if($1>=10 && $1<=12){
+			  $active=1;
                           $openp=$1;
                           my$foo="\n";
                           syswrite B_FD,$foo,1;
@@ -155,30 +218,91 @@
             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);
+	    $ac=0;
+	    $ac=1 if($lac>=100);
             
+	    $batteries=0;
+	    if(defined($lcurrent)){
+		$percent[$batteries]=($lcharge / $lbatt)*100.;
+		$battstatus[$batteries]=0;
+		$battstatus[$batteries]=1 if($lcurrent>0);
+		
+		$batteries++;
+	    }
+	    if(defined($rcurrent)){
+		$percent[$batteries]=($rcharge / $rbatt)*100.;
+		$battstatus[$batteries]=0;
+		$battstatus[$batteries]=1 if($rcurrent>0);
+		
+		$batteries++;
+	    }
+
             my$foo="\n";
             syswrite B_FD,$foo,1;
         }
     }
-    $percent;
 }
 
 # PC style power management.  Of course they're not the same
 # interface.  This is linux.
+# /proc/apm only reports one line for all batteries
 sub read_proc_apm{
     my$this=shift;
     my$percent;
 
+
+    # Arguments, with symbols from linux/apm_bios.h
+    #0) Linux driver version (this will change if format changes)
+    #1) APM BIOS Version.  Usually 1.0 or 1.1.
+    #2) APM flags from APM Installation Check (0x00):
+    #      bit 0: APM_16_BIT_SUPPORT
+    #      bit 1: APM_32_BIT_SUPPORT
+    #      bit 2: APM_IDLE_SLOWS_CLOCK
+    #      bit 3: APM_BIOS_DISABLED
+    #      bit 4: APM_BIOS_DISENGAGED
+    #3) AC line status
+    #      0x00: Off-line
+    #      0x01: On-line
+    #      0x02: On backup power (APM BIOS 1.1 only)
+    #      0xff: Unknown
+    #4) Battery status
+    #      0x00: High
+    #      0x01: Low
+    #      0x02: Critical
+    #      0x03: Charging
+    #      0xff: Unknown
+    #5) Battery flag
+    #      bit 0: High
+    #      bit 1: Low
+    #      bit 2: Critical
+    #      bit 3: Charging
+    #      bit 7: No system battery
+    #      0xff: Unknown
+    #6) Remaining battery life (percentage of charge):
+    #      0-100: valid
+    #      -1: Unknown
+    #7) Remaining battery life (time units):
+    #      Number of remaining minutes or seconds
+    #      -1: Unknown
+    #   8) min = minutes; sec = seconds */
+
+
+    $batteries=0;
     if(open(PROC,"/proc/apm")){
+	$active=1;
         sysread PROC,$_,1024;
-	if(m/^\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+\S+\s+([^ \%]+)\%/){
-	    $percent=$1;
+	if(m/^\S+\s+\S+\s+\S+\s+(\S+)\s+(\S+)\s+\S+\s+([^ \%]+)\%/){
+	    if($1 eq '0xff'){
+		$batteries=0;
+	    }else{
+		$ac=1;
+		$ac=0 if($1 eq '0x00');
+		$batteries=1;
+		$battstatus[0]=0;
+		$battstatus[0]=1 if($2 eq '0x03');
+		$battstatus[0]=-1 if($2 eq '0xff');
+		$percent[0]=$3;
+	    }
         }
         close PROC;
     }
@@ -191,48 +315,83 @@
     my$percent;
     my$maxpercent=-1;
     my$i=0;
-    my at percentlist=split ",",$this->read_proc;
-    foreach $percent (@percentlist){
+
+    read_proc($this);
+    return &reconfig if($lastbat!=$batteries);
+
+    for($i=0;$i<$batteries;$i++){
+	my$val=$percent[$i];
         # apm, for some reason, seems to occasionally return -1/1.
         # guard against that
-	if($percent>=0){
-	    $maxpercent=$percent if($maxpercent<$percent);
+	if($val>=0){
+	    $maxpercent=$val if($maxpercent<$val);
         }
 
-	if($percent<30){
-	    if($percent<8){
-		if($state[$i]!=2){
+	if($battstatus[$i]<0){
+	    if($gstate[$i]!=6){
+		$graph->
+		    barconfigure($i,'aforeXr'=>'acforeground',
+				 'abackXr'=>'acbackground',
+				 'labelXr'=>'nonelabel');
+		$gstate[$i]=6;
+	    }
+	    
+	}elsif($ac){
+	    if($battstatus[$i]==1){
+		if($gstate[$i]!=4){
                     $graph->
-			barconfigure($i,'aforeXr'=>'lowforeground',
-				     'abackXr'=>'lowbackground',
-				     'labelXr'=>'lowlabel');
-		    $state[$i]=2;
+			barconfigure($i,'aforeXr'=>'chargeforeground',
+				     'abackXr'=>'chargebackground',
+				     'labelXr'=>'chargelabel');
+		    $gstate[$i]=4;
                 }
             }else{
-		if($state[$i]!=1){
+		if($gstate[$i]!=5){
                     $graph->
-			barconfigure($i,'aforeXr'=>'midforeground',
-				     'abackXr'=>'midbackground',
-				     'labelXr'=>'label');
-		    $state[$i]=1;
+			barconfigure($i,'aforeXr'=>'acforeground',
+				     'abackXr'=>'acbackground',
+				     'labelXr'=>'fulllabel');
+		    $gstate[$i]=5;
                 }
             }
         }else{
-	    if($state[$i]!=0){
-		$graph->
-		    barconfigure(0,'aforeXr'=>'litforeground',
-				 'abackXr'=>'litbackground',
-				 'labelXr'=>'label');
-		$state[$i]=0;
+	    if($val<30){
+		if($val<8){
+		    if($gstate[$i]!=2){
+			$graph->
+			    barconfigure($i,'aforeXr'=>'lowforeground',
+					 'abackXr'=>'lowbackground',
+					 'labelXr'=>'lowlabel');
+			$gstate[$i]=2;
+		    }
+		}else{
+		    if($gstate[$i]!=1){
+			$graph->
+			    barconfigure($i,'aforeXr'=>'midforeground',
+					 'abackXr'=>'midbackground',
+					 'labelXr'=>'label');
+			$gstate[$i]=1;
+		    }
+		}
+	    }else{
+		if($gstate[$i]!=0){
+		    $graph->
+			barconfigure($i,'aforeXr'=>'litforeground',
+				     'abackXr'=>'litbackground',
+				     'labelXr'=>'label');
+		    $gstate[$i]=0;
+		}
             }
+	    
+	    $i++;
         }
-	
-	$i++;
     }
+    
+    
     $percent=$maxpercent;
     if($percent>=0){
         
-	$graph->set(@percentlist);
+	$graph->set(@percent);
         
         if($percent<30){
             # below 30%, it would be nice to unfix the scale so 
@@ -259,9 +418,19 @@
 
 sub destroy{
     close B_FD if($openp);
+    close PROC if($openf);
     $openp=0;
     undef $xpath;
 }
+
+sub reconfig{
+    &main::reinstance() if($widget->optionGet("reconfig","") eq 'true');
+}
+
+sub module_place_p{
+    $batteries;
+}
+
 
 bless {};
 

<p><p><p>--- >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