1
0
mirror of https://github.com/Llewellynvdm/conky.git synced 2024-12-26 12:27:52 +00:00

Added detect_battery setting (multiple batteries) to fix #190 (#482)

* Fix #190:
 * Added function `is_on_battery()` to correct set update interval
 * Added setting `detect_battery` to correct select one or more batteries to check

* Added `detect_battery` to doc/config_settings.xml
This commit is contained in:
txpaper 2018-05-03 14:34:48 +02:00 committed by Brenden Matthews
parent 1410bac6aa
commit e0990f3e7d
2 changed files with 30 additions and 6 deletions

View File

@ -1029,6 +1029,15 @@
<listitem>Update interval when running on batterypower
<para /></listitem>
</varlistentry>
<varlistentry>
<term>
<command>
<option>detect_battery</option>
</command>
</term>
<listitem>One or more batteries to check in order to use update_interval_on_battery (comma separated, BAT0 default)
<para /></listitem>
</varlistentry>
<varlistentry>
<term>
<command>

View File

@ -210,6 +210,8 @@ conky::range_config_setting<double> update_interval("update_interval", 0.0,
std::numeric_limits<double>::infinity(), 3.0, true);
conky::range_config_setting<double> update_interval_on_battery("update_interval_on_battery", 0.0,
std::numeric_limits<double>::infinity(), NOBATTERY, true);
conky::simple_config_setting<std::string> detect_battery("detect_battery",
std::string("BAT0"), false);
static bool on_battery = false;
double active_update_interval()
@ -2054,6 +2056,24 @@ static void update_text(void)
int inotify_fd = -1;
#endif
bool is_on_battery() { //checks if at least one battery specified in "detect_battery" is discharging
char buf[64];
std::string detect_battery_str;
std::string str_buf={""};
detect_battery_str.assign(detect_battery.get(*state));
detect_battery_str+=',';
for(std::string::size_type i = 0; i < detect_battery_str.size(); i++){ //parse using ',' as delimiter
if( (detect_battery_str[i] != ',') && (detect_battery_str[i] != ' ') ) str_buf+=detect_battery_str[i];
if( (detect_battery_str[i] == ',') && !str_buf.empty() ){
get_battery_short_status(buf, 64, str_buf.c_str());
if(buf[0] == 'D') return true;
str_buf="";
}
}
return false;
}
static void main_loop(void)
{
int terminate = 0;
@ -2100,12 +2120,7 @@ static void main_loop(void)
info.looped = 0;
while (terminate == 0
&& (total_run_times.get(*state) == 0 || info.looped < total_run_times.get(*state))) {
if(update_interval_on_battery.get(*state) != NOBATTERY) {
char buf[64];
get_battery_short_status(buf, 64, "BAT0");
on_battery = (buf[0] == 'D');
}
if( (update_interval_on_battery.get(*state) != NOBATTERY)) on_battery = is_on_battery();
info.looped++;
#ifdef SIGNAL_BLOCKING