From e0990f3e7d3113918a9eb773087e158ce2c74c2d Mon Sep 17 00:00:00 2001 From: txpaper Date: Thu, 3 May 2018 14:34:48 +0200 Subject: [PATCH] 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 --- doc/config_settings.xml | 9 +++++++++ src/conky.cc | 27 +++++++++++++++++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/doc/config_settings.xml b/doc/config_settings.xml index c72c370c..4ccc8972 100644 --- a/doc/config_settings.xml +++ b/doc/config_settings.xml @@ -1029,6 +1029,15 @@ Update interval when running on batterypower + + + + + + + One or more batteries to check in order to use update_interval_on_battery (comma separated, BAT0 default) + + diff --git a/src/conky.cc b/src/conky.cc index f10d08fb..9b2c55ca 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -210,6 +210,8 @@ conky::range_config_setting update_interval("update_interval", 0.0, std::numeric_limits::infinity(), 3.0, true); conky::range_config_setting update_interval_on_battery("update_interval_on_battery", 0.0, std::numeric_limits::infinity(), NOBATTERY, true); +conky::simple_config_setting 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