File: battery

package info (click to toggle)
mgm 1.1.cvs.20020221-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,724 kB
  • ctags: 33
  • sloc: perl: 768; makefile: 32
file content (443 lines) | stat: -rw-r--r-- 10,640 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
# -*-Perl-*-

# instances allowed: one 

# (single instance modules would be silly to use more than one of
# anyway, so we use package local storage.  This is faster and places
# less artificial load on the machine than doing everything through
# the object hash)

package MGMmodule::battery;
use IO::Seekable;
use Socket;
use vars qw(@percent @battstatus @gstate $ac $lstate $graph $widget 
	    $xpath $openp $openf $opena $batteries $ac $active $lastbat);

sub module_init{
    my$this=shift;
    my$xclass=$this->{"xclass"};
    my$toplevel=$this->{"toplevel"};

    $active=0;
    $openp=0;
    $openf=0;
    $opena=0;
    $this->read_proc;
    if($active==0){
	$toplevel->optionAdd("$xclass.active",        'false',21);      
    }
    $toplevel->optionAdd("$xclass.order",201,21);      
    $this;
}

sub module_instance{
    my$this=shift;
    my$toplevel=$this->{"toplevel"};
    return undef if(defined($xpath));
    $xpath=$this->{"xpath"};

    $lastbat=$batteries;

    # modify defaults
    $toplevel->optionAdd("$xpath.order",201,21); # status group
    $toplevel->optionAdd("$xpath.scalerefresh",4000,21);
    $toplevel->optionAdd("$xpath.scalereturn" ,1,21);     # don't need hyst

    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);
	}
    }

    $toplevel->optionAdd("$xpath.scalewidadj", 80*$batteries,21);  # narrower
    $toplevel->optionAdd("$xpath.scalelenadj", 100,21);

    my$fg=&main::moption($this,"litforeground");
    $toplevel->optionAdd("$xpath*midforeground", $fg,21);
    $toplevel->optionAdd("$xpath*litbackground", '#60c060',21);
    $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);      
    $toplevel->optionAdd("$xpath.miny",        $miny,21);      
    $this;
}

sub module_run{
    my$this=shift;
    
    $graph=MGM::Graph->new($this,num=>$batteries,fixed=>1,rangesetting=>100,
			   rangecurrent=>100,minscale=>0,
			   prompt=>'%');

    # color change hack
    @gstate=map{0}(1...$batteries);
    $lstate=0;
    $this->module_update;
    $widget=$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");

sub read_proc{
    my$test;
    my$this=shift;

    if($opena){return $this->read_proc_apm;}
    if($openp || $openf){return $this->read_proc_mac;}

    $test=$this->read_proc_apm;
    if(defined($test)){return $test;}
    $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');
	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){
			  $active=1;
			  $openp=$1;
			  my$foo="\n";
			  syswrite B_FD,$foo,1;
			  last SWITCH;
		      }
		      print "PowerMac PMU version $1 (".
			  $pmulist[$1].
			      ") not supported yet.\n";
		      close B_FD;
		  }
		} 
	    }
	}
    }
    
    if($openp){
	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;
	    
	    $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;
	}
    }
}

# 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;
	$opena=1;
	sysread PROC,$_,1024;
	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;
    }
    $percent;
}

sub module_update{ 
    my$this=shift;
    my$toplevel=$this->{"toplevel"};
    my$percent;
    my$maxpercent=-1;
    my$i=0;

    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($val>=0){
	    $maxpercent=$val if($maxpercent<$val);
	}

	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'=>'chargeforeground',
				     'abackXr'=>'chargebackground',
				     'labelXr'=>'chargelabel');
		    $gstate[$i]=4;
		}
	    }else{
		if($gstate[$i]!=5){
		    $graph->
			barconfigure($i,'aforeXr'=>'acforeground',
				     'abackXr'=>'acbackground',
				     'labelXr'=>'fulllabel');
		    $gstate[$i]=5;
		}
	    }
	}else{
	    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++;
	}
    }
    
    
    $percent=$maxpercent;
    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($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);
    close PROC if($openf);
    $openp=0;
    $openf=0;
    undef $xpath;
}

sub reconfig{
    &main::reinstance() if($widget->optionGet("reconfig","") eq 'true');
}

sub module_place_p{
    $batteries;
}


bless {};