diff --git a/src/common.cc b/src/common.cc index 8a8b3c31..37d6dd77 100644 --- a/src/common.cc +++ b/src/common.cc @@ -638,6 +638,11 @@ void print_battery_time(struct text_object *obj, char *p, get_battery_stuff(p, p_max_size, obj->data.s, BATTERY_TIME); } +void battery_power_draw(struct text_object *obj, char *p, + unsigned int p_max_size) { + get_battery_power_draw(p, p_max_size, obj->data.s); +} + uint8_t battery_percentage(struct text_object *obj) { return get_battery_perct(obj->data.s); } diff --git a/src/common.h b/src/common.h index 6f4ff8f3..4d42702c 100644 --- a/src/common.h +++ b/src/common.h @@ -85,6 +85,7 @@ void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size, void get_acpi_fan(char *, size_t); void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item); int get_battery_perct(const char *bat); +void get_battery_power_draw(char *buffer, unsigned int n, const char *bat); double get_battery_perct_bar(struct text_object *); void get_battery_short_status(char *buf, unsigned int n, const char *bat); @@ -162,6 +163,7 @@ void print_acpiacadapter(struct text_object *, char *, unsigned int); void print_battery(struct text_object *, char *, unsigned int); void print_battery_time(struct text_object *, char *, unsigned int); uint8_t battery_percentage(struct text_object *); +void battery_power_draw(struct text_object *, char *, unsigned int); void print_battery_short(struct text_object *, char *, unsigned int); void print_battery_status(struct text_object *, char *, unsigned int); #endif /* !__OpenBSD__ */ diff --git a/src/core.cc b/src/core.cc index 60b8777d..7b1a3f79 100644 --- a/src/core.cc +++ b/src/core.cc @@ -586,6 +586,18 @@ struct text_object *construct_text_object(char *s, const char *arg, long line, obj->data.s = strndup(bat, text_buffer_size.get(*state)); obj->callbacks.percentage = &battery_percentage; obj->callbacks.free = &gen_free_opaque; + END OBJ(battery_power_draw, nullptr) char bat[64]; + + if (arg != nullptr) { + sscanf(arg, "%63s", bat); + } else { + strncpy(bat, "BAT0", 5); + } + obj->data.s = strndup(bat, text_buffer_size.get(*state)); + obj->callbacks.print = &battery_power_draw; + obj->callbacks.free = &gen_free_opaque; + + END OBJ(battery_bar, nullptr) char bat[64]; arg = scan_bar(obj, arg, 100); diff --git a/src/linux.cc b/src/linux.cc index 19109d57..564a7a37 100644 --- a/src/linux.cc +++ b/src/linux.cc @@ -2351,6 +2351,33 @@ void get_battery_short_status(char *buffer, unsigned int n, const char *bat) { // Otherwise, don't shorten. } +void get_battery_power_draw(char *buffer, unsigned int n, const char *bat) { + static int reported = 0; + char current_now_path[256], voltage_now_path[256], current_now_val[256], voltage_now_val[256]; + char *ptr; + long current_now, voltage_now; + FILE *current_now_file; + FILE *voltage_now_file; + double result; + + snprintf(current_now_path, 255, SYSFS_BATTERY_BASE_PATH "/%s/current_now", bat); + snprintf(voltage_now_path, 255, SYSFS_BATTERY_BASE_PATH "/%s/voltage_now", bat); + + current_now_file = open_file(current_now_path, &reported); + voltage_now_file = open_file(voltage_now_path, &reported); + + if (current_now_file != nullptr && voltage_now_file != nullptr) { + fgets(current_now_val, 256, current_now_file); + fgets(voltage_now_val, 256, voltage_now_file); + + current_now = strtol(current_now_val, &ptr, 10); + voltage_now = strtol(voltage_now_val, &ptr, 10); + + result = (double)(current_now*voltage_now)/(double)1000000000000; + snprintf(buffer, n, "%.1f", result); + } +} + int _get_battery_perct(const char *bat) { static int reported = 0; int idx;