(platform_linux.c) Refactors

This commit is contained in:
twinaphex 2015-09-14 06:56:32 +02:00
parent 33d4612716
commit ad1b433d57

View File

@ -37,18 +37,10 @@ static const char *proc_acpi_sys_ac_adapter_path= "/sys/class/power_supply/ACAD"
static const char *proc_acpi_sys_battery_path= "/sys/class/power_supply";
static const char *proc_acpi_ac_adapter_path = "/proc/acpi/ac_adapter";
static int open_acpi_file(const char *base, const char *node, const char *key)
{
char path[1024];
snprintf(path, sizeof(path), "%s/%s/%s", base, node, key);
return open(path, O_RDONLY);
}
static bool load_acpi_file(const char *base, const char *node, const char *key,
char *buf, size_t buflen)
static bool load_power_file(const char *path, char *buf, size_t buflen)
{
ssize_t br = 0;
const int fd = open_acpi_file(base, node, key);
const int fd = open(path, O_RDONLY);
if (fd == -1)
return false;
br = read(fd, buf, buflen-1);
@ -114,6 +106,7 @@ check_proc_acpi_battery(const char * node, bool * have_battery,
bool * charging, int *seconds, int *percent)
{
const char *base = proc_acpi_battery_path;
char path[1024];
char info[1024] = {0};
char state[1024] = {0};
char *ptr = NULL;
@ -126,9 +119,13 @@ check_proc_acpi_battery(const char * node, bool * have_battery,
int secs = -1;
int pct = -1;
if (!load_acpi_file(base, node, "state", state, sizeof (state)))
snprintf(path, sizeof(path), "%s/%s/%s", base, node, "state");
if (!load_power_file(path, state, sizeof (state)))
return;
else if (!load_acpi_file(base, node, "info", info, sizeof (info)))
snprintf(path, sizeof(path), "%s/%s/%s", base, node, "info");
if (!load_power_file(path, info, sizeof (info)))
return;
ptr = &state[0];
@ -223,9 +220,8 @@ check_proc_acpi_sys_battery(const char * node, bool * have_battery,
bool * charging, int *seconds, int *percent)
{
unsigned capacity;
char path[1024], info[1024], state[1024];
const char *base = proc_acpi_sys_battery_path;
char info[1024] = {0};
char state[1024] = {0};
char *ptr = NULL;
char *key = NULL;
char *val = NULL;
@ -239,7 +235,8 @@ check_proc_acpi_sys_battery(const char * node, bool * have_battery,
if (!strstr(node, "BAT"))
return;
if (!load_acpi_file(base, node, "status", state, sizeof (state)))
snprintf(path, sizeof(path), "%s/%s/%s", base, node, "status");
if (!load_power_file(path, state, sizeof (state)))
return;
if (strstr(state, "Discharging"))
@ -247,7 +244,8 @@ check_proc_acpi_sys_battery(const char * node, bool * have_battery,
else if (strstr(state, "Full"))
*have_battery = true;
if (!load_acpi_file(base, node, "capacity", state, sizeof (state)))
snprintf(path, sizeof(path), "%s/%s/%s", base, node, "capacity");
if (!load_power_file(path, state, sizeof (state)))
return;
capacity = atoi(state);
@ -259,38 +257,41 @@ check_proc_acpi_sys_battery(const char * node, bool * have_battery,
static void
check_proc_acpi_ac_adapter(const char * node, bool *have_ac)
{
const char *base = proc_acpi_ac_adapter_path;
char state[256] = {0};
char *ptr = NULL;
char *key = NULL;
char *val = NULL;
char path[1024];
const char *base = proc_acpi_ac_adapter_path;
char state[256] = {0};
char *ptr = NULL;
char *key = NULL;
char *val = NULL;
if (!load_acpi_file(base, node, "state", state, sizeof (state)))
return;
snprintf(path, sizeof(path), "%s/%s/%s", base, node, "state");
if (!load_power_file(path, state, sizeof (state)))
return;
ptr = &state[0];
while (make_proc_acpi_key_val(&ptr, &key, &val))
{
uint32_t key_hash = djb2_calculate(key);
uint32_t val_hash = djb2_calculate(val);
ptr = &state[0];
while (make_proc_acpi_key_val(&ptr, &key, &val))
{
uint32_t key_hash = djb2_calculate(key);
uint32_t val_hash = djb2_calculate(val);
if (key_hash == ACPI_KEY_STATE &&
val_hash == ACPI_VAL_ONLINE)
*have_ac = true;
}
if (key_hash == ACPI_KEY_STATE &&
val_hash == ACPI_VAL_ONLINE)
*have_ac = true;
}
}
static void
check_proc_acpi_sys_ac_adapter(const char * node, bool *have_ac)
{
char state[256];
const char *base = proc_acpi_sys_ac_adapter_path;
char state[256], path[1024];
const char *base = proc_acpi_sys_ac_adapter_path;
if (!load_acpi_file(base, node, "online", state, sizeof (state)))
return;
snprintf(path, sizeof(path), "%s/%s/%s", base, node, "online");
if (!load_power_file(path, state, sizeof (state)))
return;
if (strstr(state, "1"))
*have_ac = true;
if (strstr(state, "1"))
*have_ac = true;
}
static bool next_string(char **_ptr, char **_str)
@ -327,27 +328,19 @@ static bool frontend_linux_powerstate_check_apm(
enum frontend_powerstate *state,
int *seconds, int *percent)
{
ssize_t br;
char buf[128], *ptr;
int ac_status = 0;
int battery_status = 0;
int battery_flag = 0;
int battery_percent = 0;
int battery_time = 0;
const int fd = open(proc_apm_path, O_RDONLY);
char buf[128] = {0};
char *ptr = &buf[0];
char *str = NULL;
if (fd == -1)
return false; /* can't use this interface. */
br = read(fd, buf, sizeof (buf) - 1);
close(fd);
if (br < 0)
if (!load_power_file(proc_apm_path, buf, sizeof(buf)))
return false;
buf[br] = '\0'; /* null-terminate the string. */
ptr = &buf[0];
if (!next_string(&ptr, &str)) /* driver version */
return false;
if (!next_string(&ptr, &str)) /* BIOS version */