Lines 27-43
Link Here
|
27 |
|
27 |
|
28 |
static const char APMFILENAME[] = "/proc/apm"; |
28 |
static const char APMFILENAME[] = "/proc/apm"; |
29 |
static const char ACPIBATTERYDIR[] = "/proc/acpi/battery"; |
29 |
static const char ACPIBATTERYDIR[] = "/proc/acpi/battery"; |
|
|
30 |
static const char SYSPOWERDIR[] = "/sys/class/power_supply"; |
30 |
|
31 |
|
31 |
BtryMeter::BtryMeter( XOSView *parent ) |
32 |
BtryMeter::BtryMeter( XOSView *parent ) |
32 |
: FieldMeter( parent, 2, "BTRY", "AVAIL/USED", 1, 1, 0 ){ |
33 |
: FieldMeter( parent, 2, "BTRY", "AVAIL/USED", 1, 1, 0 ){ |
33 |
|
34 |
|
34 |
// find out ONCE whether to use ACPI or APM |
35 |
// find out ONCE whether to use ACPI, APM or sysfs |
35 |
use_acpi = use_apm = false; |
36 |
use_acpi = use_apm = use_syspower = false; |
36 |
if ( has_apm() ) { |
37 |
if ( has_apm() ) { |
37 |
use_apm=true; use_acpi=false; |
38 |
use_apm=true; use_acpi=false; use_syspower=false; |
38 |
} |
39 |
} |
39 |
if ( has_acpi() ) { |
40 |
if ( has_acpi() ) { |
40 |
use_acpi=true; use_apm=false; |
41 |
use_acpi=true; use_apm=false; use_syspower=false; |
|
|
42 |
} |
43 |
if ( has_syspower() ) { |
44 |
use_acpi=false; use_apm=false; use_syspower=true; |
41 |
} |
45 |
} |
42 |
|
46 |
|
43 |
old_apm_battery_state = apm_battery_state = 0xFF; |
47 |
old_apm_battery_state = apm_battery_state = 0xFF; |
Lines 87-93
Link Here
|
87 |
return false; |
90 |
return false; |
88 |
} |
91 |
} |
89 |
if ( S_ISDIR(stbuf.st_mode) ) { |
92 |
if ( S_ISDIR(stbuf.st_mode) ) { |
90 |
XOSDEBUG("exists and is a DIR.\n"); |
93 |
XOSDEBUG("%s exists and is a DIR.\n", ACPIBATTERYDIR); |
91 |
} else { |
94 |
} else { |
92 |
XOSDEBUG("no ACPI dir\n"); |
95 |
XOSDEBUG("no ACPI dir\n"); |
93 |
return false; |
96 |
return false; |
Lines 98-103
Link Here
|
98 |
|
101 |
|
99 |
} |
102 |
} |
100 |
|
103 |
|
|
|
104 |
// determine if /sys/class/power_supply exists and is a DIR |
105 |
// (XXX: too lazy - no tests for actual readability is done) |
106 |
bool BtryMeter::has_syspower( void ){ |
107 |
|
108 |
struct stat stbuf; |
109 |
|
110 |
if ( stat(SYSPOWERDIR, &stbuf) != 0 ) { |
111 |
XOSDEBUG("has_syspower(): stat failed: %d\n",errno); |
112 |
return false; |
113 |
} |
114 |
if ( S_ISDIR(stbuf.st_mode) ) { |
115 |
XOSDEBUG("%s exists and is a DIR.\n", SYSPOWERDIR); |
116 |
} else { |
117 |
XOSDEBUG("no /sys/class/power_supply dir\n"); |
118 |
return false; |
119 |
} |
120 |
|
121 |
// declare syspower as usable |
122 |
return true; |
123 |
|
124 |
} |
101 |
|
125 |
|
102 |
void BtryMeter::checkResources( void ){ |
126 |
void BtryMeter::checkResources( void ){ |
103 |
FieldMeter::checkResources(); |
127 |
FieldMeter::checkResources(); |
Lines 220-227
Link Here
|
220 |
|
244 |
|
221 |
void BtryMeter::getpwrinfo( void ){ |
245 |
void BtryMeter::getpwrinfo( void ){ |
222 |
|
246 |
|
223 |
if ( use_acpi ) { |
247 |
if ( use_acpi || use_syspower ) { |
224 |
getacpiinfo(); return; |
248 |
getacpi_or_sys_info(); return; |
225 |
} |
249 |
} |
226 |
if ( use_apm ) { |
250 |
if ( use_apm ) { |
227 |
getapminfo(); return; |
251 |
getapminfo(); return; |
Lines 330-340
Link Here
|
330 |
// but munging it into something usefull is ugly |
354 |
// but munging it into something usefull is ugly |
331 |
// esp. as you can have more than one battery ... |
355 |
// esp. as you can have more than one battery ... |
332 |
|
356 |
|
333 |
bool BtryMeter::getacpiinfo( void ){ |
357 |
bool BtryMeter::getacpi_or_sys_info( void ){ |
|
|
358 |
|
359 |
DIR *dir = NULL; |
360 |
std::string abs_battery_dir; |
361 |
|
362 |
if (use_acpi) { |
363 |
abs_battery_dir = ACPIBATTERYDIR; |
364 |
} else { |
365 |
abs_battery_dir = SYSPOWERDIR; |
366 |
} |
334 |
|
367 |
|
335 |
DIR *dir = opendir(ACPIBATTERYDIR); |
368 |
dir = opendir(abs_battery_dir.c_str()); |
336 |
if (dir==NULL) { |
369 |
if (dir==NULL) { |
337 |
XOSDEBUG("ACPI: Cannot open directory : %s\n", ACPIBATTERYDIR); |
370 |
XOSDEBUG("ACPI/SYS: Cannot open directory : %s\n", abs_battery_dir.c_str()); |
338 |
return false; |
371 |
return false; |
339 |
} |
372 |
} |
340 |
|
373 |
|
Lines 351-357
Link Here
|
351 |
|
384 |
|
352 |
acpi_charge_state=0; // assume charged |
385 |
acpi_charge_state=0; // assume charged |
353 |
|
386 |
|
354 |
std::string abs_battery_dir = ACPIBATTERYDIR; |
|
|
355 |
for (struct dirent *dirent; (dirent = readdir(dir)) != NULL; ) { |
387 |
for (struct dirent *dirent; (dirent = readdir(dir)) != NULL; ) { |
356 |
if (strncmp(dirent->d_name, ".", 1) == 0 |
388 |
if (strncmp(dirent->d_name, ".", 1) == 0 |
357 |
|| strncmp(dirent->d_name, "..", 2) == 0) |
389 |
|| strncmp(dirent->d_name, "..", 2) == 0) |
Lines 359-370
Link Here
|
359 |
|
391 |
|
360 |
std::string abs_battery_name = abs_battery_dir + "/" + dirent->d_name; |
392 |
std::string abs_battery_name = abs_battery_dir + "/" + dirent->d_name; |
361 |
|
393 |
|
362 |
XOSDEBUG("ACPI Batt: %s\n", dirent->d_name); |
394 |
XOSDEBUG("ACPI/SYS Batt: %s\n", dirent->d_name); |
363 |
|
395 |
|
364 |
// still can happen that it's not present: |
396 |
// still can happen that it's not present: |
365 |
if ( battery_present( abs_battery_name + "/info" ) ) { |
397 |
if ( (use_acpi && |
366 |
// ok, worth to parse out all the fields |
398 |
(acpi_battery_present(abs_battery_name + "/info" )) && |
367 |
if ( parse_battery( abs_battery_name ) ) { |
399 |
(acpi_parse_battery(abs_battery_name))) || |
|
|
400 |
(use_syspower && |
401 |
(sys_battery_present(abs_battery_name + "/present" )) && |
402 |
(sys_parse_battery(abs_battery_name))) ) { |
368 |
|
403 |
|
369 |
// sum up: |
404 |
// sum up: |
370 |
|
405 |
|
Lines 385-391
Link Here
|
385 |
found = true; // found at least one |
420 |
found = true; // found at least one |
386 |
} |
421 |
} |
387 |
} |
422 |
} |
388 |
} |
|
|
389 |
|
423 |
|
390 |
closedir(dir); |
424 |
closedir(dir); |
391 |
|
425 |
|
Lines 418-424
Link Here
|
418 |
|
452 |
|
419 |
// present yes/no can change anytime ! |
453 |
// present yes/no can change anytime ! |
420 |
// by adding/removing a battery |
454 |
// by adding/removing a battery |
421 |
bool BtryMeter::battery_present(const std::string& filename) |
455 |
bool BtryMeter::acpi_battery_present(const std::string& filename) |
422 |
{ |
456 |
{ |
423 |
std::ifstream loadinfo( filename.c_str() ); |
457 |
std::ifstream loadinfo( filename.c_str() ); |
424 |
|
458 |
|
Lines 439-445
Link Here
|
439 |
return false; |
473 |
return false; |
440 |
} |
474 |
} |
441 |
|
475 |
|
442 |
bool BtryMeter::parse_battery(const std::string& dirname) |
476 |
bool BtryMeter::acpi_parse_battery(const std::string& dirname) |
443 |
{ |
477 |
{ |
444 |
// actually there are THREE files to check: |
478 |
// actually there are THREE files to check: |
445 |
// 'alarm', 'info' and 'state' |
479 |
// 'alarm', 'info' and 'state' |
Lines 535-537
Link Here
|
535 |
|
569 |
|
536 |
return true; |
570 |
return true; |
537 |
} |
571 |
} |
|
|
572 |
|
573 |
// present yes/no can change anytime ! |
574 |
// by adding/removing a battery |
575 |
bool BtryMeter::sys_battery_present(const std::string& filename) |
576 |
{ |
577 |
std::ifstream loadinfo( filename.c_str() ); |
578 |
std::string value; |
579 |
|
580 |
while ( loadinfo.good() ) { |
581 |
|
582 |
value.clear(); |
583 |
loadinfo >> value; |
584 |
|
585 |
if (value == "1") |
586 |
return true; |
587 |
} |
588 |
XOSDEBUG("batt %s not present\n",filename.c_str() ); |
589 |
return false; |
590 |
} |
591 |
|
592 |
bool BtryMeter::sys_parse_battery(const std::string& dirname) |
593 |
{ |
594 |
std::string filename; |
595 |
std::ifstream loadinfo; |
596 |
std::string value; |
597 |
|
598 |
filename = dirname + "/alarm"; |
599 |
loadinfo.open(filename.c_str() ); |
600 |
while ( loadinfo.good() ) { |
601 |
value.clear(); |
602 |
loadinfo >> value; |
603 |
//XOSDEBUG("alarm (%s): v=\"%s\"\n", filename.c_str(), value.c_str() ); |
604 |
battery.alarm = atoi(value.c_str()); |
605 |
break; |
606 |
} |
607 |
loadinfo.close(); |
608 |
loadinfo.clear(); |
609 |
|
610 |
filename = dirname + "/energy_full_design"; |
611 |
loadinfo.open(filename.c_str() ); |
612 |
while ( loadinfo.good() ) { |
613 |
value.clear(); |
614 |
loadinfo >> value; |
615 |
//XOSDEBUG("design_capacity (%s): v=\"%s\"\n", filename.c_str(), value.c_str() ); |
616 |
battery.design_capacity = atoi(value.c_str()); |
617 |
break; |
618 |
} |
619 |
loadinfo.close(); |
620 |
loadinfo.clear(); |
621 |
|
622 |
filename = dirname + "/energy_full"; |
623 |
loadinfo.open(filename.c_str() ); |
624 |
while ( loadinfo.good() ) { |
625 |
value.clear(); |
626 |
loadinfo >> value; |
627 |
//XOSDEBUG("last_full_capacity (%s): v=\"%s\"\n", filename.c_str(), value.c_str() ); |
628 |
battery.last_full_capacity = atoi(value.c_str()); |
629 |
break; |
630 |
} |
631 |
loadinfo.close(); |
632 |
loadinfo.clear(); |
633 |
|
634 |
filename = dirname + "/energy_now"; |
635 |
loadinfo.open(filename.c_str() ); |
636 |
while ( loadinfo.good() ) { |
637 |
value.clear(); |
638 |
loadinfo >> value; |
639 |
//XOSDEBUG("remaining_capacity (%s): v=\"%s\"\n", filename.c_str(), value.c_str() ); |
640 |
battery.remaining_capacity = atoi(value.c_str()); |
641 |
break; |
642 |
} |
643 |
loadinfo.close(); |
644 |
loadinfo.clear(); |
645 |
|
646 |
filename = dirname + "/status"; |
647 |
loadinfo.open(filename.c_str() ); |
648 |
while ( loadinfo.good() ) { |
649 |
value.clear(); |
650 |
loadinfo >> value; |
651 |
//XOSDEBUG("status (%s): v=\"%s\"\n", filename.c_str(), value.c_str() ); |
652 |
|
653 |
if ( value == "Full" ) |
654 |
battery.charging_state=0; |
655 |
if ( value == "Discharging" ) |
656 |
battery.charging_state=-1; |
657 |
if ( value == "Charging" ) |
658 |
battery.charging_state=1; |
659 |
break; |
660 |
} |
661 |
loadinfo.close(); |
662 |
loadinfo.clear(); |
663 |
|
664 |
return true; |
665 |
} |