2008-09-23 06:21:28 +00:00
|
|
|
/*
|
2011-12-26 21:35:57 +00:00
|
|
|
htop - BatteryMeter.c
|
|
|
|
(C) 2004-2011 Hisham H. Muhammad
|
|
|
|
Released under the GNU GPL, see the COPYING file
|
|
|
|
in the source distribution for its full text.
|
2008-09-23 06:21:28 +00:00
|
|
|
|
2011-12-26 21:35:57 +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"
|
2011-12-26 21:35:57 +00:00
|
|
|
|
2014-11-27 23:02:52 +00:00
|
|
|
#include "Platform.h"
|
2008-09-23 06:21:28 +00:00
|
|
|
#include "ProcessList.h"
|
|
|
|
#include "CRT.h"
|
|
|
|
#include "String.h"
|
|
|
|
|
2011-12-26 21:35:57 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2009-04-27 21:48:09 +00:00
|
|
|
/*{
|
2011-12-26 21:35:57 +00:00
|
|
|
#include "Meter.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 void BatteryMeter_setValues(Meter * this, char *buffer, int len) {
|
2014-11-27 23:02:52 +00:00
|
|
|
ACPresence isOnAC;
|
|
|
|
double percent;
|
|
|
|
|
|
|
|
Platform_getBatteryLevel(&percent, &isOnAC);
|
|
|
|
|
|
|
|
if (percent == -1) {
|
|
|
|
this->values[0] = 0;
|
|
|
|
snprintf(buffer, len, "n/a");
|
|
|
|
return;
|
2009-04-27 21:48:09 +00:00
|
|
|
}
|
|
|
|
|
2008-09-23 06:21:28 +00:00
|
|
|
this->values[0] = percent;
|
|
|
|
|
2010-02-25 01:43:18 +00:00
|
|
|
const char *onAcText, *onBatteryText, *unknownText;
|
2009-04-27 21:48:09 +00:00
|
|
|
|
|
|
|
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)";
|
|
|
|
}
|
|
|
|
|
2014-11-27 23:02:52 +00:00
|
|
|
if (isOnAC == AC_PRESENT) {
|
2008-09-23 06:21:28 +00:00
|
|
|
snprintf(buffer, len, onAcText, percent);
|
2014-11-27 23:02:52 +00:00
|
|
|
} else if (isOnAC == AC_ABSENT) {
|
2009-04-27 21:48:09 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2012-12-05 15:12:20 +00:00
|
|
|
MeterClass BatteryMeter_class = {
|
|
|
|
.super = {
|
|
|
|
.extends = Class(Meter),
|
|
|
|
.delete = Meter_delete
|
|
|
|
},
|
2009-04-27 21:48:09 +00:00
|
|
|
.setValues = BatteryMeter_setValues,
|
2012-12-05 15:12:20 +00:00
|
|
|
.defaultMode = TEXT_METERMODE,
|
2008-09-23 06:21:28 +00:00
|
|
|
.total = 100.0,
|
|
|
|
.attributes = BatteryMeter_attributes,
|
|
|
|
.name = "Battery",
|
|
|
|
.uiName = "Battery",
|
|
|
|
.caption = "Battery: "
|
|
|
|
};
|