next: need to be able to setup device.

Then query at later time. Else too many queries.
This commit is contained in:
Alastair Poole 2021-05-17 08:58:23 +01:00
parent 5ce95f8180
commit b580266dc0
7 changed files with 166 additions and 328 deletions

View File

@ -1,6 +1,6 @@
#include "next/machine.h"
#include <Ecore.h>
static Eina_List *cores = NULL;
static Eina_List *batteries = NULL;
static Eina_List *sensors = NULL;
static Eina_List *network_interfaces = NULL;
@ -9,10 +9,18 @@ int
main(int argc, char **argv)
{
Eina_List *l;
Cpu_Core **cores;
Battery *bat;
Sensor *sensor;
Network_Interface *iface;
ecore_init();
int ncpu = 0;
cores = system_cpu_usage_delayed_get(&ncpu, 1000000);
for (int i = 0; i < ncpu; i++)
printf("core %i = %1.2f%\n", cores[i]->id, cores[i]->percent);
batteries = batteries_find();
EINA_LIST_FOREACH(batteries, l, bat)
{
@ -22,6 +30,23 @@ main(int argc, char **argv)
EINA_LIST_FREE(batteries, bat)
battery_free(bat);
printf("POWER %i\n", power_ac());
sensors = sensors_find();
EINA_LIST_FREE(sensors, sensor)
{
if (sensor_check(sensor))
printf("sensor %s = %1.2f\n", sensor->name, sensor->value);
sensor_free(sensor);
}
network_interfaces = network_interfaces_find();
EINA_LIST_FREE(network_interfaces, iface)
{
printf("name => %s => %i %i\n", iface->name, iface->total_in, iface->total_out);
free(iface);
}
ecore_shutdown();
return 0;

View File

@ -1,12 +1,6 @@
#ifndef MACHINE_H
#define MACHINE_H
/* All functions and data types implementing these APIs have no additional
* system dependencies deliberately.
*
* See machine.c and the files includes in machine/ sub directory.
*/
#include <Eina.h>
#include <stdint.h>
#include <stdbool.h>
@ -16,9 +10,9 @@ typedef struct
unsigned long total;
unsigned long idle;
float percent;
} cpu_core_t;
int id;
} Cpu_Core;
// Will anyone have more than 8 vdrm/video card devices?
#define MEM_VIDEO_CARD_MAX 8
typedef struct
@ -54,7 +48,7 @@ typedef struct
#endif
double value;
bool invalid;
} sensor_t;
} Sensor;
typedef struct
{
@ -76,17 +70,23 @@ typedef struct
#if defined(__OpenBSD__)
int mibs[5];
#endif
} power_t;
} AC_Adapter;
typedef struct
{
char name[255];
struct
{
uint64_t in;
uint64_t out;
} xfer;
} net_iface_t;
char name[255];
uint64_t total_in;
uint64_t total_out;
uint64_t peak_in;
uint64_t peak_out;
uint64_t in;
uint64_t out;
} Network_Interface;
Eina_Bool
power_ac(void);
Eina_List *
batteries_find(void);
@ -97,8 +97,17 @@ battery_free(Battery *bat);
void
battery_check(Battery *bat);
Eina_List *
sensors_find(void);
void
sensor_free(Sensor *sensor);
Eina_Bool
sensor_check(Sensor *sensor);
Eina_List *
network_interfaces_find(void);
int
system_cpu_online_count_get(void);
@ -106,13 +115,13 @@ system_cpu_online_count_get(void);
int
system_cpu_count_get(void);
cpu_core_t **
Cpu_Core **
system_cpu_usage_get(int *ncpu);
cpu_core_t **
Cpu_Core **
system_cpu_usage_delayed_get(int *ncpu, int usecs);
cpu_core_t **
Cpu_Core **
system_cpu_state_get(int *ncpu);
int
@ -136,25 +145,4 @@ system_cpu_topology_get(int *ids, int ncpus);
void
system_memory_usage_get(meminfo_t *memory);
sensor_t **
system_sensors_thermal_get(int *count);
void
system_sensors_thermal_free(sensor_t **sensors, int count);
int
system_sensor_thermal_get(sensor_t *sensor);
void
system_sensor_thermal_free(sensor_t *sensor);
void
system_power_state_get(power_t *power);
void
system_power_state_free(power_t *power);
net_iface_t **
system_network_ifaces_get(int *n);
#endif

View File

@ -1,19 +1,3 @@
/*
* Copyright (c) 2018 Alastair Roy Poole <netstar@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#if defined(__OpenBSD__)
# define CPU_STATES 6
#else
@ -88,12 +72,12 @@ system_cpu_online_count_get(void)
}
static void
_cpu_state_get(cpu_core_t **cores, int ncpu)
_cpu_state_get(Cpu_Core **cores, int ncpu)
{
int diff_total, diff_idle;
double ratio, percent;
unsigned long total, idle, used;
cpu_core_t *core;
Cpu_Core *core;
#if defined(__FreeBSD__) || defined(__DragonFly__)
size_t size;
int i, j;
@ -131,6 +115,7 @@ _cpu_state_get(cpu_core_t **cores, int ncpu)
core->percent = percent;
core->total = total;
core->idle = idle;
core->id = i;
}
#elif defined(__OpenBSD__)
static struct cpustats cpu_times[CPU_STATES];
@ -170,6 +155,7 @@ _cpu_state_get(cpu_core_t **cores, int ncpu)
core->percent = percent;
core->total = total;
core->idle = idle;
core->id = i;
}
#elif defined(__linux__)
char *buf, name[128];
@ -208,6 +194,7 @@ _cpu_state_get(cpu_core_t **cores, int ncpu)
core->percent = percent;
core->total = total;
core->idle = idle;
core->id = i;
}
}
free(buf);
@ -249,38 +236,43 @@ _cpu_state_get(cpu_core_t **cores, int ncpu)
core->percent = percent;
core->total = total;
core->idle = idle;
core->id = i;
}
#endif
}
cpu_core_t **
Cpu_Core **
system_cpu_state_get(int *ncpu)
{
cpu_core_t **cores;
Cpu_Core **cores;
int i;
*ncpu = cpu_count();
cores = malloc((*ncpu) * sizeof(cpu_core_t *));
cores = malloc((*ncpu) * sizeof(Cpu_Core *));
for (i = 0; i < *ncpu; i++)
cores[i] = calloc(1, sizeof(cpu_core_t));
cores[i] = calloc(1, sizeof(Cpu_Core));
_cpu_state_get(cores, *ncpu);
return cores;
}
cpu_core_t **
Cpu_Core **
system_cpu_usage_delayed_get(int *ncpu, int usecs)
{
cpu_core_t **cores;
Cpu_Core **cores;
int i;
*ncpu = cpu_count();
cores = malloc((*ncpu) * sizeof(cpu_core_t *));
cores = malloc((*ncpu) * sizeof(Cpu_Core *));
if (!cores) return NULL;
for (i = 0; i < *ncpu; i++)
cores[i] = calloc(1, sizeof(cpu_core_t));
{
cores[i] = calloc(1, sizeof(Cpu_Core));
if (!cores[i]) return NULL;
}
_cpu_state_get(cores, *ncpu);
usleep(usecs);
@ -289,7 +281,7 @@ system_cpu_usage_delayed_get(int *ncpu, int usecs)
return cores;
}
cpu_core_t **
Cpu_Core **
system_cpu_usage_get(int *ncpu)
{
return system_cpu_usage_delayed_get(ncpu, 1000000);
@ -330,7 +322,8 @@ _cpu_n_temperature_read(int n)
#if defined(__linux__)
typedef struct _thermal_drv {
typedef struct _thermal_drv
{
const char *name;
void (*init)(void);
char min;

View File

@ -1,18 +1,3 @@
/*
* Copyright (c) 2018 Alastair Roy Poole <netstar@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <stdarg.h>
@ -45,7 +30,7 @@ file_contents(const char *path)
}
fclose(f);
if (buf[len - 1] == '\n')
if ((buf[len - 1] == '\n') || (buf[len -1] == '\r'))
buf[len - 1] = 0;
else
buf[len] = 0;
@ -53,22 +38,6 @@ file_contents(const char *path)
return buf;
}
void
strimmer(char *s)
{
char *cp = s;
while (*cp)
{
if ((*cp == '\r') || (*cp == '\n'))
{
*cp = '\0';
return;
}
cp++;
}
}
#if defined(__FreeBSD__) || defined(__DragonFly__)
static long int
_sysctlfromname(const char *name, void *mib, int depth, size_t *len)

View File

@ -1,18 +1,3 @@
/*
* Copyright (c) 2018 Alastair Roy Poole <netstar@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#if defined(__linux__)
static unsigned long

View File

@ -1,21 +1,5 @@
/*
* Copyright (c) 2018 Alastair Roy Poole <netstar@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#if defined(__MacOS__) || defined(__FreeBSD__) || defined(__DragonFly__)
static net_iface_t **
static Eina_List *
_freebsd_generic_network_status(int *n)
{
struct ifmibdata *ifmd;
@ -30,7 +14,7 @@ _freebsd_generic_network_status(int *n)
if (!ifmd)
return NULL;
net_iface_t **ifaces = NULL;
Eina_List *list = NULL;
for (i = 1; i <= count; i++) {
int mib[] = {
@ -39,27 +23,25 @@ _freebsd_generic_network_status(int *n)
len = sizeof(*ifmd);
if (sysctl(mib, 6, ifmd, &len, NULL, 0) < 0) continue;
net_iface_t *iface = malloc(sizeof(net_iface_t));
Network_Interface *iface = calloc(1, sizeof(Network_Interface));
if (iface)
{
snprintf(iface->name, sizeof(iface->name), "%s",
ifmd->ifmd_name);
iface->xfer.in = ifmd->ifmd_data.ifi_ibytes;
iface->xfer.out = ifmd->ifmd_data.ifi_obytes;
void *t = realloc(ifaces, (1 + 1 + *n) * sizeof(net_iface_t *));
ifaces = t;
ifaces[(*n)++] = iface;
iface->total_in = ifmd->ifmd_data.ifi_ibytes;
iface->total_out = ifmd->ifmd_data.ifi_obytes;
list = eina_list_append(list, iface);
}
}
free(ifmd);
return ifaces;
return list;
}
#endif
#if defined(__OpenBSD__)
static net_iface_t **
static Eina_List *
_openbsd_generic_network_status(int *n)
{
struct ifaddrs *interfaces, *ifa;
@ -71,7 +53,7 @@ _openbsd_generic_network_status(int *n)
if (sock < 0)
return NULL;
net_iface_t **ifaces = NULL;
Eina_List *list = NULL;
for (ifa = interfaces; ifa; ifa = ifa->ifa_next) {
struct ifreq ifreq;
@ -92,38 +74,35 @@ _openbsd_generic_network_status(int *n)
if (!LINK_STATE_IS_UP(ifi->ifi_link_state))
continue;
net_iface_t *iface = malloc(sizeof(net_iface_t));
Network_Interface *iface = calloc(1, sizeof(Network_Interface));
if (iface)
{
snprintf(iface->name, sizeof(iface->name), "%s",
ifa->ifa_name);
iface->xfer.in = ifi->ifi_ibytes;
iface->xfer.out = ifi->ifi_obytes;
void *t = realloc(ifaces, (1 + 1 + *n) * sizeof(net_iface_t *));
ifaces = t;
ifaces[(*n)++] = iface;
iface->total_in = ifi->ifi_ibytes;
iface->total_out = ifi->ifi_obytes;
list = eina_list_append(list, iface);
}
}
close(sock);
return ifaces;
return list;
}
#endif
#if defined(__linux__)
static net_iface_t **
_linux_generic_network_status(int *n)
static Eina_List *
_linux_generic_network_status(void)
{
FILE *f;
char buf[4096], name[128];
unsigned long int tmp_in, tmp_out, dummy;
Eina_List *list = NULL;
f = fopen("/proc/net/dev", "r");
if (!f) return NULL;
net_iface_t **ifaces = NULL;
while (fgets(buf, sizeof(buf), f))
{
if (17 == sscanf(buf, "%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu "
@ -132,37 +111,34 @@ _linux_generic_network_status(int *n)
&tmp_out, &dummy, &dummy, &dummy, &dummy, &dummy,
&dummy, &dummy))
{
net_iface_t *iface = malloc(sizeof(net_iface_t));
Network_Interface *iface = calloc(1, sizeof(Network_Interface));
if (iface)
{
name[strlen(name)-1] = '\0';
snprintf(iface->name, sizeof(iface->name), "%s", name);
iface->xfer.in = tmp_in;
iface->xfer.out = tmp_out;
void *t = realloc(ifaces, (1 + 1 + *n) * sizeof(net_iface_t *));
ifaces = t;
ifaces[(*n)++] = iface;
iface->total_in = tmp_in;
iface->total_out = tmp_out;
list = eina_list_append(list, iface);
}
}
}
fclose(f);
return ifaces;
return list;
}
#endif
net_iface_t **
system_network_ifaces_get(int *n)
Eina_List *
network_interfaces_find(void)
{
*n = 0;
#if defined(__linux__)
return _linux_generic_network_status(n);
return _linux_generic_network_status();
#elif defined(__MacOS__) || defined(__FreeBSD__) || defined(__DragonFly__)
return _freebsd_generic_network_status(n);
return _freebsd_generic_network_status();
#elif defined(__OpenBSD__)
return _openbsd_generic_network_status(n);
return _openbsd_generic_network_status();
#else
return NULL;
#endif

View File

@ -1,21 +1,5 @@
/*
* Copyright (c) 2018 Alastair Roy Poole <netstar@gmail.com>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
void
system_sensor_thermal_free(sensor_t *sensor)
sensor_free(Sensor *sensor)
{
if (sensor->name)
free(sensor->name);
@ -28,20 +12,8 @@ system_sensor_thermal_free(sensor_t *sensor)
free(sensor);
}
void
system_sensors_thermal_free(sensor_t **sensors, int count)
{
for (int i = 0; i < count; i++)
{
sensor_t *sensor = sensors[i];
system_sensor_thermal_free(sensor);
}
if (sensors)
free(sensors);
}
int
system_sensor_thermal_get(sensor_t *sensor)
Eina_Bool
sensor_check(Sensor *sensor)
{
#if defined(__linux__)
char *d = file_contents(sensor->path);
@ -75,13 +47,12 @@ system_sensor_thermal_get(sensor_t *sensor)
return 0;
}
sensor_t **
system_sensors_thermal_get(int *sensor_count)
Eina_List *
sensors_find(void)
{
sensor_t **sensors = NULL;
*sensor_count = 0;
Eina_List *sensors = NULL;
#if defined(__OpenBSD__)
sensor_t *sensor;
Sensor *sensor;
int mibs[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 };
int devn, n;
struct sensor snsr;
@ -114,26 +85,31 @@ system_sensors_thermal_get(int *sensor_count)
if (snsr.type != SENSOR_TEMP)
continue;
void *t = realloc(sensors, (1 + *sensor_count) * sizeof(sensor_t *));
sensors = t;
sensors[(*sensor_count)++] = sensor = calloc(1, sizeof(sensor_t));
sensor->name = strdup(snsrdev.xname);
sensor->value = (snsr.value - 273150000) / 1000000.0; // (uK -> C)
memcpy(sensor->mibs, &mibs, sizeof(mibs));
sensor = calloc(1, sizeof(Sensor));
if (sensor)
{
sensor->name = strdup(snsrdev.xname);
sensor->value = (snsr.value - 273150000) / 1000000.0; // (uK -> C)
memcpy(sensor->mibs, &mibs, sizeof(mibs));
sensors = eina_list_append(sensors, sensor);
}
}
#elif defined(__FreeBSD__) || defined(__DragonFly__)
sensor_t *sensor;
Sensor *sensor;
int value;
char buf[256];
size_t len = sizeof(value);
if ((sysctlbyname("hw.acpi.thermal.tz0.temperature", &value, &len, NULL, 0)) != -1)
{
void *t = realloc(sensors, (1 + *sensor_count) * sizeof(sensor_t *));
sensors = t;
sensors[(*sensor_count)++] = sensor = calloc(1, sizeof(sensor_t));
sensor->name = strdup("hw.acpi.thermal.tz0.temperature");
sensor->value = (float) (value - 2732) / 10;
sensor = calloc(1, sizeof(Sensor));
if (sensor)
{
sensor->name = strdup("hw.acpi.thermal.tz0.temperature");
sensor->value = (float) (value - 2732) / 10;
sensors = eina_list_append(sensors, sensor);
}
}
int n = system_cpu_count_get();
@ -144,16 +120,17 @@ system_sensors_thermal_get(int *sensor_count)
snprintf(buf, sizeof(buf), "dev.cpu.%i.temperature", i);
if ((sysctlbyname(buf, &value, &len, NULL, 0)) != -1)
{
void *t = realloc(sensors, (1 + *sensor_count) * sizeof(sensor_t *));
sensors = t;
sensors[(*sensor_count)++] = sensor = calloc(1, sizeof(sensor_t));
sensor->name = strdup(buf);
sensor->value = (float) (value - 2732) / 10;
sensor = calloc(1, sizeof(Sensor));
if (sensor)
{
sensor->name = strdup(buf);
sensor->value = (float) (value - 2732) / 10;
sensors = eina_list_append(sensors, sensor);
}
}
}
#elif defined(__linux__)
sensor_t *sensor;
Sensor *sensor;
DIR *dir;
struct dirent *dh;
char buf[4096];
@ -198,29 +175,26 @@ system_sensors_thermal_get(int *sensor_count)
continue;
}
void *t = realloc(sensors, (1 + (*sensor_count)) * sizeof(sensor_t *));
sensors = t;
sensors[(*sensor_count)++] = sensor = calloc(1, sizeof(sensor_t));
snprintf(buf, sizeof(buf), "%s/name", link);
sensor->name = file_contents(buf);
if (sensor->name)
strimmer(sensor->name);
snprintf(buf, sizeof(buf), "%s/temp%d_label", link, id);
sensor->child_name = file_contents(buf);
if (sensor->child_name)
strimmer(sensor->child_name);
snprintf(buf, sizeof(buf), "%s/temp%d_input", link, id);
sensor->path = strdup(buf);
char *d = file_contents(buf);
if (d)
sensor = calloc(1, sizeof(Sensor));
if (sensor)
{
sensor->value = atoi(d);
if (sensor->value) sensor->value /= 1000;
free(d);
}
snprintf(buf, sizeof(buf), "%s/name", link);
sensor->name = file_contents(buf);
snprintf(buf, sizeof(buf), "%s/temp%d_label", link, id);
sensor->child_name = file_contents(buf);
snprintf(buf, sizeof(buf), "%s/temp%d_input", link, id);
sensor->path = strdup(buf);
char *d = file_contents(buf);
if (d)
{
sensor->value = atoi(d);
if (sensor->value) sensor->value /= 1000;
free(d);
}
sensors = eina_list_append(sensors, sensor);
}
seen[idx++] = id;
}
free(names[i]);
@ -315,7 +289,6 @@ batteries_find(void)
free(names);
#endif
puts("AYE");
return list;
}
@ -328,35 +301,6 @@ battery_free(Battery *bat)
free(bat);
}
static int
_power_battery_count_get(power_t *power)
{
#if defined(__OpenBSD__)
#elif defined(__FreeBSD__) || defined(__DragonFly__)
int n_units, fd;
char name[256];
fd = open("/dev/acpi", O_RDONLY);
if (fd != -1)
{
if (ioctl(fd, ACPIIO_BATT_GET_UNITS, &n_units) != -1)
power->battery_count = n_units;
close(fd);
}
power->batteries = malloc(power->battery_count * sizeof(bat_t **));
for (int i = 0; i < power->battery_count; i++) {
power->batteries[i] = calloc(1, sizeof(bat_t));
snprintf(name, sizeof(name), "hw.acpi.battery.%i", i);
power->batteries[i]->name = strdup(name);
power->batteries[i]->present = true;
}
#elif defined(__linux__)
#endif
return 0;
}
void
battery_check(Battery *bat)
{
@ -405,7 +349,7 @@ battery_check(Battery *bat)
snprintf(path, sizeof(path), "/sys/class/power_supply/%s", bat->name);
if ((stat(path, &st) < 0) || (!S_ISDIR(st.st_mode)))
return;
return;
link = realpath(path, NULL);
if (!link) return;
@ -478,58 +422,20 @@ done:
bat->percent = 100 * (charge_full / charge_current);
}
static void
_battery_state_get(power_t *power)
Eina_Bool
power_ac(void)
{
#if defined(__OpenBSD__)
#elif defined(__FreeBSD__) || defined(__DragonFly__)
int fd, i;
union acpi_battery_ioctl_arg battio;
if ((fd = open("/dev/acpi", O_RDONLY)) == -1) return;
for (i = 0; i < power->battery_count; i++) {
battio.unit = i;
if (ioctl(fd, ACPIIO_BATT_GET_BIX, &battio) != -1)
{
if (battio.bif.lfcap == 0)
power->batteries[i]->charge_full = battio.bif.dcap;
else
power->batteries[i]->charge_full = battio.bif.lfcap;
}
power->batteries[i]->vendor = strdup(battio.bix.oeminfo);
power->batteries[i]->model = strdup(battio.bix.model);
battio.unit = i;
if (ioctl(fd, ACPIIO_BATT_GET_BST, &battio) != -1)
power->batteries[i]->charge_current = battio.bst.cap;
if (battio.bst.state == ACPI_BATT_STAT_NOT_PRESENT)
power->batteries[i]->present = false;
}
close(fd);
#elif defined(__linux__)
#endif
}
void
system_power_state_get(power_t *power)
{
memset(power, 0, sizeof(power_t));
Eina_Bool have_ac = 0;
#if defined(__OpenBSD__)
struct sensor snsr;
size_t slen = sizeof(struct sensor);
#endif
if (!_power_battery_count_get(power))
return;
#if defined(__OpenBSD__)
power->mibs[3] = 9;
power->mibs[4] = 0;
if (sysctl(power->mibs, 5, &snsr, &slen, NULL, 0) != -1)
power->have_ac = (int)snsr.value;
have_ac = (int)snsr.value;
#elif defined(__FreeBSD__) || defined(__DragonFly__)
int val, fd;
@ -537,22 +443,18 @@ system_power_state_get(power_t *power)
if (fd != -1)
{
if (ioctl(fd, ACPIIO_ACAD_GET_STATUS, &val) != -1)
power->have_ac = val;
have_ac = val;
close(fd);
}
#elif defined(__linux__)
char *buf = file_contents("/sys/class/power_supply/AC/online");
if (buf)
{
power->have_ac = atoi(buf);
have_ac = atoi(buf);
free(buf);
}
#endif
_battery_state_get(power);
return have_ac;
}
void
system_power_state_free(power_t *power)
{
}