diff options
author | Simon Quigley <tsimonq2@ubuntu.com> | 2017-06-19 14:38:48 -0500 |
---|---|---|
committer | Simon Quigley <tsimonq2@ubuntu.com> | 2017-06-19 14:38:48 -0500 |
commit | 4979bb6cbbbe39eb44c32530cd13f86bf44e5d77 (patch) | |
tree | 614372cf2510ad45311ecea088012683635d0b76 /arch/linux/common/battery.h | |
parent | ee6c3f9b4cf08a276c971dee66e1acdb32b3dff9 (diff) |
Import Upstream version 0.5c
Diffstat (limited to 'arch/linux/common/battery.h')
-rw-r--r-- | arch/linux/common/battery.h | 84 |
1 files changed, 78 insertions, 6 deletions
diff --git a/arch/linux/common/battery.h b/arch/linux/common/battery.h index 4a38b842..7dfa5423 100644 --- a/arch/linux/common/battery.h +++ b/arch/linux/common/battery.h @@ -1,6 +1,6 @@ /* * HardInfo - Displays System Information - * Copyright (C) 2003-2006 Leandro A. F. Pereira <leandro@linuxmag.com.br> + * Copyright (C) 2003-2006 Leandro A. F. Pereira <leandro@hardinfo.org> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,6 +18,82 @@ #include <time.h> +const struct { + gchar *key, *name; +} ups_fields[] = { + { "UPS Status", NULL }, + { "STATUS", "Status" }, + { "TIMELEFT", "Time Left" }, + { "LINEV", "Line Voltage" }, + { "LOADPCT", "Load Percent" }, + + { "UPS Battery Information", NULL }, + { "BATTV", "Battery Voltage" }, + { "BCHARGE", "Battery Charge" }, + { "BATTDATE", "Battery Date" }, + + { "UPS Information", NULL }, + { "APCMODEL", "Model" }, + { "FIRMWARE", "Firmware Version" }, + { "SERIALNO", "Serial Number" }, + { "UPSMODE", "UPS Mode" }, + { "CABLE", "Cable" }, + { "UPSNAME", "UPS Name" }, + + { "UPS Nominal Values", NULL }, + { "NOMINV", "Voltage" }, + { "NOMBATTV", "Battery Voltage" }, + { "NOMPOWER", "Power" } +}; + + +static void +__scan_battery_apcupsd(void) +{ + GHashTable *ups_data; + FILE *apcaccess; + char buffer[512], *apcaccess_path; + int i; + + apcaccess_path = find_program("apcaccess"); + + if ((apcaccess = popen(apcaccess_path, "r"))) { + /* first line isn't important */ + if (fgets(buffer, 512, apcaccess)) { + /* allocate the key, value hash table */ + ups_data = g_hash_table_new(g_str_hash, g_str_equal); + + /* read up all the apcaccess' output, saving it in the key, value hash table */ + while (fgets(buffer, 512, apcaccess)) { + buffer[9] = '\0'; + + g_hash_table_insert(ups_data, + g_strdup(g_strstrip(buffer)), + g_strdup(g_strstrip(buffer + 10))); + } + + /* builds the ups info string, respecting the field order as found in ups_fields */ + for (i = 0; i < G_N_ELEMENTS(ups_fields); i++) { + if (!ups_fields[i].name) { + /* there's no name: make a group with the key as its name */ + battery_list = h_strdup_cprintf("[%s]\n", battery_list, ups_fields[i].key); + } else { + /* there's a name: adds a line */ + battery_list = h_strdup_cprintf("%s=%s\n", battery_list, + ups_fields[i].name, + g_hash_table_lookup(ups_data, ups_fields[i].key)); + } + } + + g_hash_table_destroy(ups_data); + } + + pclose(apcaccess); + } + + g_free(apcaccess_path); +} + static void __scan_battery_acpi(void) { @@ -31,11 +107,6 @@ __scan_battery_acpi(void) gchar *state = NULL, *rate = NULL; gchar *remaining = NULL; - if (battery_list) { - g_free(battery_list); - } - battery_list = g_strdup(""); - acpi_path = g_strdup("/proc/acpi/battery"); if (g_file_test(acpi_path, G_FILE_TEST_EXISTS)) { GDir *acpi; @@ -209,6 +280,7 @@ __scan_battery(void) __scan_battery_acpi(); __scan_battery_apm(); + __scan_battery_apcupsd(); if (*battery_list == '\0') { g_free(battery_list); |