2008-09-23 06:21:28 +00:00
|
|
|
/*
|
|
|
|
htop
|
|
|
|
(C) 2004-2006 Hisham H. Muhammad
|
|
|
|
Released under the GNU GPL, see the COPYING file
|
|
|
|
in the source distribution for its full text.
|
|
|
|
|
2009-04-27 21:48:09 +00:00
|
|
|
This "Meter" written by Ian P. Hands (iphands@gmail.com, ihands@redhat.com).
|
2008-09-23 06:21:28 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "BatteryMeter.h"
|
|
|
|
#include "Meter.h"
|
|
|
|
#include "ProcessList.h"
|
|
|
|
#include "CRT.h"
|
|
|
|
#include "String.h"
|
|
|
|
#include "debug.h"
|
|
|
|
|
2009-04-27 21:48:09 +00:00
|
|
|
/*{
|
|
|
|
|
|
|
|
typedef enum ACPresence_ {
|
|
|
|
AC_ABSENT,
|
|
|
|
AC_PRESENT,
|
|
|
|
AC_ERROR
|
|
|
|
} ACPresence;
|
|
|
|
|
|
|
|
}*/
|
|
|
|
|
2008-09-23 06:21:28 +00:00
|
|
|
int BatteryMeter_attributes[] = {
|
|
|
|
BATTERY
|
|
|
|
};
|
|
|
|
|
2009-04-27 21:48:09 +00:00
|
|
|
static unsigned long int parseUevent(FILE * file, char *key) {
|
|
|
|
char line[100];
|
|
|
|
unsigned long int dValue = 0;
|
|
|
|
|
|
|
|
while (fgets(line, sizeof line, file)) {
|
|
|
|
if (strncmp(line, key, strlen(key)) == 0) {
|
|
|
|
char *value;
|
|
|
|
value = strtok(line, "=");
|
|
|
|
value = strtok(NULL, "=");
|
|
|
|
dValue = atoi(value);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned long int parseBatInfo(const char *fileName, const unsigned short int lineNum, const unsigned short int wordNum) {
|
2008-09-23 06:21:28 +00:00
|
|
|
const DIR *batteryDir;
|
2009-04-27 21:48:09 +00:00
|
|
|
const struct dirent *dirEntries;
|
|
|
|
|
2008-09-23 06:21:28 +00:00
|
|
|
const char batteryPath[] = PROCDIR "/acpi/battery/";
|
|
|
|
batteryDir = opendir(batteryPath);
|
2009-04-27 21:48:09 +00:00
|
|
|
|
2008-09-23 06:21:28 +00:00
|
|
|
if (batteryDir == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
2009-04-27 21:48:09 +00:00
|
|
|
|
|
|
|
char *entryName;
|
|
|
|
typedef struct listLbl {
|
|
|
|
char *content;
|
|
|
|
struct listLbl *next;
|
|
|
|
} list;
|
|
|
|
|
2008-09-23 06:21:28 +00:00
|
|
|
list *myList = NULL;
|
|
|
|
list *newEntry;
|
2009-04-27 21:48:09 +00:00
|
|
|
|
2008-09-23 06:21:28 +00:00
|
|
|
/*
|
2009-04-27 21:48:09 +00:00
|
|
|
Some of this is based off of code found in kismet (they claim it came from gkrellm).
|
|
|
|
Written for multi battery use...
|
|
|
|
*/
|
|
|
|
for (dirEntries = readdir((DIR *) batteryDir); dirEntries; dirEntries = readdir((DIR *) batteryDir)) {
|
|
|
|
entryName = (char *) dirEntries->d_name;
|
|
|
|
|
|
|
|
if (strncmp(entryName, "BAT", 3))
|
2008-09-23 06:21:28 +00:00
|
|
|
continue;
|
2009-04-27 21:48:09 +00:00
|
|
|
|
2008-09-23 06:21:28 +00:00
|
|
|
newEntry = calloc(1, sizeof(list));
|
|
|
|
newEntry->next = myList;
|
2009-04-27 21:48:09 +00:00
|
|
|
newEntry->content = entryName;
|
2008-09-23 06:21:28 +00:00
|
|
|
myList = newEntry;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned long int total = 0;
|
|
|
|
for (newEntry = myList; newEntry; newEntry = newEntry->next) {
|
|
|
|
const char infoPath[30];
|
2009-04-27 21:48:09 +00:00
|
|
|
const FILE *file;
|
|
|
|
char line[50];
|
|
|
|
|
|
|
|
sprintf((char *) infoPath, "%s%s/%s", batteryPath, newEntry->content, fileName);
|
|
|
|
|
2008-09-23 06:21:28 +00:00
|
|
|
if ((file = fopen(infoPath, "r")) == NULL) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-04-27 21:48:09 +00:00
|
|
|
for (unsigned short int i = 0; i < lineNum; i++) {
|
|
|
|
fgets(line, sizeof line, (FILE *) file);
|
2008-09-23 06:21:28 +00:00
|
|
|
}
|
2009-04-27 21:48:09 +00:00
|
|
|
|
|
|
|
fclose((FILE *) file);
|
|
|
|
|
|
|
|
const char *foundNumTmp = String_getToken(line, wordNum);
|
2008-09-23 06:21:28 +00:00
|
|
|
const unsigned long int foundNum = atoi(foundNumTmp);
|
2009-04-27 21:48:09 +00:00
|
|
|
free((char *) foundNumTmp);
|
2008-09-23 06:21:28 +00:00
|
|
|
|
|
|
|
total += foundNum;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(myList);
|
|
|
|
free(newEntry);
|
2009-04-27 21:48:09 +00:00
|
|
|
closedir((DIR *) batteryDir);
|
2008-09-23 06:21:28 +00:00
|
|
|
return total;
|
|
|
|
}
|
|
|
|
|
2009-04-27 21:48:09 +00:00
|
|
|
static ACPresence chkIsOnline() {
|
|
|
|
FILE *file = NULL;
|
|
|
|
ACPresence isOn = AC_ERROR;
|
|
|
|
|
|
|
|
if (access(PROCDIR "/acpi/ac_adapter", F_OK) == 0) {
|
|
|
|
const struct dirent *dirEntries;
|
|
|
|
char *power_supplyPath = PROCDIR "/acpi/ac_adapter";
|
|
|
|
DIR *power_supplyDir = opendir(power_supplyPath);
|
|
|
|
char *entryName;
|
|
|
|
|
|
|
|
if (!power_supplyDir) {
|
|
|
|
closedir(power_supplyDir);
|
|
|
|
return AC_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (dirEntries = readdir((DIR *) power_supplyDir); dirEntries; dirEntries = readdir((DIR *) power_supplyDir)) {
|
|
|
|
entryName = (char *) dirEntries->d_name;
|
|
|
|
|
|
|
|
if (strncmp(entryName, "A", 1)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
char statePath[50];
|
|
|
|
sprintf((char *) statePath, "%s/%s/state", power_supplyPath, entryName);
|
|
|
|
file = fopen(statePath, "r");
|
|
|
|
|
|
|
|
if (!file) {
|
|
|
|
isOn = AC_ERROR;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
char line[100];
|
|
|
|
fgets(line, sizeof line, file);
|
|
|
|
line[sizeof(line) - 1] = '\0';
|
|
|
|
|
|
|
|
if (file) {
|
|
|
|
fclose(file);
|
|
|
|
file = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *isOnline = String_getToken(line, 2);
|
|
|
|
|
|
|
|
if (strcmp(isOnline, "on-line") == 0) {
|
|
|
|
free((char *) isOnline);
|
|
|
|
isOn = AC_PRESENT;
|
|
|
|
// If any AC adapter is being used then stop
|
|
|
|
break;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
isOn = AC_ABSENT;
|
|
|
|
}
|
|
|
|
free((char *) isOnline);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (power_supplyDir)
|
|
|
|
closedir(power_supplyDir);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
char *power_supplyPath = "/sys/class/power_supply";
|
|
|
|
|
|
|
|
if (access("/sys/class/power_supply", F_OK) == 0) {
|
|
|
|
const struct dirent *dirEntries;
|
|
|
|
DIR *power_supplyDir = opendir(power_supplyPath);
|
|
|
|
char *entryName;
|
|
|
|
|
|
|
|
if (!power_supplyDir) {
|
|
|
|
return AC_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (dirEntries = readdir((DIR *) power_supplyDir); dirEntries; dirEntries = readdir((DIR *) power_supplyDir)) {
|
|
|
|
entryName = (char *) dirEntries->d_name;
|
|
|
|
|
|
|
|
if (strncmp(entryName, "A", 1)) {
|
|
|
|
continue;
|
|
|
|
}
|
2008-09-23 06:21:28 +00:00
|
|
|
|
2009-04-27 21:48:09 +00:00
|
|
|
char onlinePath[50];
|
|
|
|
sprintf((char *) onlinePath, "%s/%s/online", power_supplyPath, entryName);
|
|
|
|
file = fopen(onlinePath, "r");
|
2008-09-23 06:21:28 +00:00
|
|
|
|
2009-04-27 21:48:09 +00:00
|
|
|
if (!file) {
|
|
|
|
isOn = AC_ERROR;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
isOn = (fgetc(file) - '0');
|
|
|
|
|
|
|
|
if (file) {
|
|
|
|
fclose(file);
|
|
|
|
file = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isOn == AC_PRESENT) {
|
|
|
|
// If any AC adapter is being used then stop
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (power_supplyDir)
|
|
|
|
closedir(power_supplyDir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Just in case :-)
|
|
|
|
if (file)
|
|
|
|
fclose(file);
|
|
|
|
|
|
|
|
return isOn;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double getProcBatData() {
|
2008-09-23 06:21:28 +00:00
|
|
|
const unsigned long int totalFull = parseBatInfo("info", 3, 4);
|
2009-04-27 21:48:09 +00:00
|
|
|
if (totalFull == 0)
|
|
|
|
return 0;
|
|
|
|
|
2008-09-23 06:21:28 +00:00
|
|
|
const unsigned long int totalRemain = parseBatInfo("state", 5, 3);
|
2009-04-27 21:48:09 +00:00
|
|
|
if (totalRemain == 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
double percent = totalFull > 0 ? ((double) totalRemain * 100) / (double) totalFull : 0;
|
|
|
|
|
|
|
|
return percent;
|
|
|
|
}
|
|
|
|
|
|
|
|
static double getSysBatData() {
|
|
|
|
const struct dirent *dirEntries;
|
|
|
|
char *power_supplyPath = "/sys/class/power_supply/";
|
|
|
|
DIR *power_supplyDir = opendir(power_supplyPath);
|
|
|
|
|
|
|
|
|
|
|
|
if (!power_supplyDir) {
|
|
|
|
closedir(power_supplyDir);
|
|
|
|
return 0;
|
2008-09-23 06:21:28 +00:00
|
|
|
}
|
2009-04-27 21:48:09 +00:00
|
|
|
|
|
|
|
char *entryName;
|
|
|
|
|
|
|
|
unsigned long int totalFull = 0;
|
|
|
|
unsigned long int totalRemain = 0;
|
|
|
|
|
|
|
|
for (dirEntries = readdir((DIR *) power_supplyDir); dirEntries; dirEntries = readdir((DIR *) power_supplyDir)) {
|
|
|
|
entryName = (char *) dirEntries->d_name;
|
|
|
|
|
|
|
|
if (strncmp(entryName, "BAT", 3)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char ueventPath[50];
|
|
|
|
|
|
|
|
sprintf((char *) ueventPath, "%s%s/uevent", power_supplyPath, entryName);
|
|
|
|
|
|
|
|
FILE *file;
|
|
|
|
if ((file = fopen(ueventPath, "r")) == NULL) {
|
|
|
|
closedir(power_supplyDir);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
totalFull += parseUevent(file, "POWER_SUPPLY_ENERGY_FULL=");
|
|
|
|
totalRemain += parseUevent(file, "POWER_SUPPLY_ENERGY_NOW=");
|
|
|
|
fclose(file);
|
|
|
|
}
|
|
|
|
|
|
|
|
const double percent = totalFull > 0 ? ((double) totalRemain * 100) / (double) totalFull : 0;
|
|
|
|
closedir(power_supplyDir);
|
|
|
|
return percent;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void BatteryMeter_setValues(Meter * this, char *buffer, int len) {
|
|
|
|
double percent = getProcBatData();
|
|
|
|
if (percent == 0) {
|
|
|
|
percent = getSysBatData();
|
|
|
|
if (percent == 0) {
|
|
|
|
snprintf(buffer, len, "n/a");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-23 06:21:28 +00:00
|
|
|
this->values[0] = percent;
|
|
|
|
|
2009-04-27 21:48:09 +00:00
|
|
|
char *onAcText, *onBatteryText, *unknownText;
|
|
|
|
|
|
|
|
unknownText = "%.1f%%";
|
2008-09-23 06:21:28 +00:00
|
|
|
if (this->mode == TEXT_METERMODE) {
|
|
|
|
onAcText = "%.1f%% (Running on A/C)";
|
|
|
|
onBatteryText = "%.1f%% (Running on battery)";
|
|
|
|
} else {
|
|
|
|
onAcText = "%.1f%%(A/C)";
|
|
|
|
onBatteryText = "%.1f%%(bat)";
|
|
|
|
}
|
|
|
|
|
2009-04-27 21:48:09 +00:00
|
|
|
ACPresence isOnLine = chkIsOnline();
|
|
|
|
|
|
|
|
if (isOnLine == AC_PRESENT) {
|
2008-09-23 06:21:28 +00:00
|
|
|
snprintf(buffer, len, onAcText, percent);
|
2009-04-27 21:48:09 +00:00
|
|
|
} else if (isOnLine == AC_ABSENT) {
|
|
|
|
snprintf(buffer, len, onBatteryText, percent);
|
2008-09-23 06:21:28 +00:00
|
|
|
} else {
|
2009-04-27 21:48:09 +00:00
|
|
|
snprintf(buffer, len, unknownText, percent);
|
2008-09-23 06:21:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MeterType BatteryMeter = {
|
2009-04-27 21:48:09 +00:00
|
|
|
.setValues = BatteryMeter_setValues,
|
2008-09-23 06:21:28 +00:00
|
|
|
.display = NULL,
|
|
|
|
.mode = TEXT_METERMODE,
|
|
|
|
.items = 1,
|
|
|
|
.total = 100.0,
|
|
|
|
.attributes = BatteryMeter_attributes,
|
|
|
|
.name = "Battery",
|
|
|
|
.uiName = "Battery",
|
|
|
|
.caption = "Battery: "
|
|
|
|
};
|