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 "next/machine.h"
#include <Ecore.h>
static Eina_List *cores = NULL;
static Eina_List *batteries = NULL; static Eina_List *batteries = NULL;
static Eina_List *sensors = NULL; static Eina_List *sensors = NULL;
static Eina_List *network_interfaces = NULL; static Eina_List *network_interfaces = NULL;
@ -9,10 +9,18 @@ int
main(int argc, char **argv) main(int argc, char **argv)
{ {
Eina_List *l; Eina_List *l;
Cpu_Core **cores;
Battery *bat; Battery *bat;
Sensor *sensor;
Network_Interface *iface;
ecore_init(); 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(); batteries = batteries_find();
EINA_LIST_FOREACH(batteries, l, bat) EINA_LIST_FOREACH(batteries, l, bat)
{ {
@ -22,6 +30,23 @@ main(int argc, char **argv)
EINA_LIST_FREE(batteries, bat) EINA_LIST_FREE(batteries, bat)
battery_free(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(); ecore_shutdown();
return 0; return 0;

View File

@ -1,12 +1,6 @@
#ifndef MACHINE_H #ifndef MACHINE_H
#define 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 <Eina.h>
#include <stdint.h> #include <stdint.h>
#include <stdbool.h> #include <stdbool.h>
@ -16,9 +10,9 @@ typedef struct
unsigned long total; unsigned long total;
unsigned long idle; unsigned long idle;
float percent; 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 #define MEM_VIDEO_CARD_MAX 8
typedef struct typedef struct
@ -54,7 +48,7 @@ typedef struct
#endif #endif
double value; double value;
bool invalid; bool invalid;
} sensor_t; } Sensor;
typedef struct typedef struct
{ {
@ -76,17 +70,23 @@ typedef struct
#if defined(__OpenBSD__) #if defined(__OpenBSD__)
int mibs[5]; int mibs[5];
#endif #endif
} power_t; } AC_Adapter;
typedef struct typedef struct
{ {
char name[255]; char name[255];
struct uint64_t total_in;
{ uint64_t total_out;
uint64_t in;
uint64_t out; uint64_t peak_in;
} xfer; uint64_t peak_out;
} net_iface_t;
uint64_t in;
uint64_t out;
} Network_Interface;
Eina_Bool
power_ac(void);
Eina_List * Eina_List *
batteries_find(void); batteries_find(void);
@ -97,8 +97,17 @@ battery_free(Battery *bat);
void void
battery_check(Battery *bat); 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 int
system_cpu_online_count_get(void); system_cpu_online_count_get(void);
@ -106,13 +115,13 @@ system_cpu_online_count_get(void);
int int
system_cpu_count_get(void); system_cpu_count_get(void);
cpu_core_t ** Cpu_Core **
system_cpu_usage_get(int *ncpu); system_cpu_usage_get(int *ncpu);
cpu_core_t ** Cpu_Core **
system_cpu_usage_delayed_get(int *ncpu, int usecs); system_cpu_usage_delayed_get(int *ncpu, int usecs);
cpu_core_t ** Cpu_Core **
system_cpu_state_get(int *ncpu); system_cpu_state_get(int *ncpu);
int int
@ -136,25 +145,4 @@ system_cpu_topology_get(int *ids, int ncpus);
void void
system_memory_usage_get(meminfo_t *memory); 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 #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__) #if defined(__OpenBSD__)
# define CPU_STATES 6 # define CPU_STATES 6
#else #else
@ -88,12 +72,12 @@ system_cpu_online_count_get(void)
} }
static 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; int diff_total, diff_idle;
double ratio, percent; double ratio, percent;
unsigned long total, idle, used; unsigned long total, idle, used;
cpu_core_t *core; Cpu_Core *core;
#if defined(__FreeBSD__) || defined(__DragonFly__) #if defined(__FreeBSD__) || defined(__DragonFly__)
size_t size; size_t size;
int i, j; int i, j;
@ -131,6 +115,7 @@ _cpu_state_get(cpu_core_t **cores, int ncpu)
core->percent = percent; core->percent = percent;
core->total = total; core->total = total;
core->idle = idle; core->idle = idle;
core->id = i;
} }
#elif defined(__OpenBSD__) #elif defined(__OpenBSD__)
static struct cpustats cpu_times[CPU_STATES]; static struct cpustats cpu_times[CPU_STATES];
@ -170,6 +155,7 @@ _cpu_state_get(cpu_core_t **cores, int ncpu)
core->percent = percent; core->percent = percent;
core->total = total; core->total = total;
core->idle = idle; core->idle = idle;
core->id = i;
} }
#elif defined(__linux__) #elif defined(__linux__)
char *buf, name[128]; char *buf, name[128];
@ -208,6 +194,7 @@ _cpu_state_get(cpu_core_t **cores, int ncpu)
core->percent = percent; core->percent = percent;
core->total = total; core->total = total;
core->idle = idle; core->idle = idle;
core->id = i;
} }
} }
free(buf); free(buf);
@ -249,38 +236,43 @@ _cpu_state_get(cpu_core_t **cores, int ncpu)
core->percent = percent; core->percent = percent;
core->total = total; core->total = total;
core->idle = idle; core->idle = idle;
core->id = i;
} }
#endif #endif
} }
cpu_core_t ** Cpu_Core **
system_cpu_state_get(int *ncpu) system_cpu_state_get(int *ncpu)
{ {
cpu_core_t **cores; Cpu_Core **cores;
int i; int i;
*ncpu = cpu_count(); *ncpu = cpu_count();
cores = malloc((*ncpu) * sizeof(cpu_core_t *)); cores = malloc((*ncpu) * sizeof(Cpu_Core *));
for (i = 0; i < *ncpu; i++) 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); _cpu_state_get(cores, *ncpu);
return cores; return cores;
} }
cpu_core_t ** Cpu_Core **
system_cpu_usage_delayed_get(int *ncpu, int usecs) system_cpu_usage_delayed_get(int *ncpu, int usecs)
{ {
cpu_core_t **cores; Cpu_Core **cores;
int i; int i;
*ncpu = cpu_count(); *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++) 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); _cpu_state_get(cores, *ncpu);
usleep(usecs); usleep(usecs);
@ -289,7 +281,7 @@ system_cpu_usage_delayed_get(int *ncpu, int usecs)
return cores; return cores;
} }
cpu_core_t ** Cpu_Core **
system_cpu_usage_get(int *ncpu) system_cpu_usage_get(int *ncpu)
{ {
return system_cpu_usage_delayed_get(ncpu, 1000000); return system_cpu_usage_delayed_get(ncpu, 1000000);
@ -330,7 +322,8 @@ _cpu_n_temperature_read(int n)
#if defined(__linux__) #if defined(__linux__)
typedef struct _thermal_drv { typedef struct _thermal_drv
{
const char *name; const char *name;
void (*init)(void); void (*init)(void);
char min; 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> #include <stdarg.h>
@ -45,7 +30,7 @@ file_contents(const char *path)
} }
fclose(f); fclose(f);
if (buf[len - 1] == '\n') if ((buf[len - 1] == '\n') || (buf[len -1] == '\r'))
buf[len - 1] = 0; buf[len - 1] = 0;
else else
buf[len] = 0; buf[len] = 0;
@ -53,22 +38,6 @@ file_contents(const char *path)
return buf; 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__) #if defined(__FreeBSD__) || defined(__DragonFly__)
static long int static long int
_sysctlfromname(const char *name, void *mib, int depth, size_t *len) _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__) #if defined(__linux__)
static unsigned long 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__) #if defined(__MacOS__) || defined(__FreeBSD__) || defined(__DragonFly__)
static net_iface_t ** static Eina_List *
_freebsd_generic_network_status(int *n) _freebsd_generic_network_status(int *n)
{ {
struct ifmibdata *ifmd; struct ifmibdata *ifmd;
@ -30,7 +14,7 @@ _freebsd_generic_network_status(int *n)
if (!ifmd) if (!ifmd)
return NULL; return NULL;
net_iface_t **ifaces = NULL; Eina_List *list = NULL;
for (i = 1; i <= count; i++) { for (i = 1; i <= count; i++) {
int mib[] = { int mib[] = {
@ -39,27 +23,25 @@ _freebsd_generic_network_status(int *n)
len = sizeof(*ifmd); len = sizeof(*ifmd);
if (sysctl(mib, 6, ifmd, &len, NULL, 0) < 0) continue; 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) if (iface)
{ {
snprintf(iface->name, sizeof(iface->name), "%s", snprintf(iface->name, sizeof(iface->name), "%s",
ifmd->ifmd_name); ifmd->ifmd_name);
iface->xfer.in = ifmd->ifmd_data.ifi_ibytes; iface->total_in = ifmd->ifmd_data.ifi_ibytes;
iface->xfer.out = ifmd->ifmd_data.ifi_obytes; iface->total_out = ifmd->ifmd_data.ifi_obytes;
void *t = realloc(ifaces, (1 + 1 + *n) * sizeof(net_iface_t *)); list = eina_list_append(list, iface);
ifaces = t;
ifaces[(*n)++] = iface;
} }
} }
free(ifmd); free(ifmd);
return ifaces; return list;
} }
#endif #endif
#if defined(__OpenBSD__) #if defined(__OpenBSD__)
static net_iface_t ** static Eina_List *
_openbsd_generic_network_status(int *n) _openbsd_generic_network_status(int *n)
{ {
struct ifaddrs *interfaces, *ifa; struct ifaddrs *interfaces, *ifa;
@ -71,7 +53,7 @@ _openbsd_generic_network_status(int *n)
if (sock < 0) if (sock < 0)
return NULL; return NULL;
net_iface_t **ifaces = NULL; Eina_List *list = NULL;
for (ifa = interfaces; ifa; ifa = ifa->ifa_next) { for (ifa = interfaces; ifa; ifa = ifa->ifa_next) {
struct ifreq ifreq; struct ifreq ifreq;
@ -92,38 +74,35 @@ _openbsd_generic_network_status(int *n)
if (!LINK_STATE_IS_UP(ifi->ifi_link_state)) if (!LINK_STATE_IS_UP(ifi->ifi_link_state))
continue; continue;
net_iface_t *iface = malloc(sizeof(net_iface_t)); Network_Interface *iface = calloc(1, sizeof(Network_Interface));
if (iface) if (iface)
{ {
snprintf(iface->name, sizeof(iface->name), "%s", snprintf(iface->name, sizeof(iface->name), "%s",
ifa->ifa_name); ifa->ifa_name);
iface->xfer.in = ifi->ifi_ibytes; iface->total_in = ifi->ifi_ibytes;
iface->xfer.out = ifi->ifi_obytes; iface->total_out = ifi->ifi_obytes;
void *t = realloc(ifaces, (1 + 1 + *n) * sizeof(net_iface_t *)); list = eina_list_append(list, iface);
ifaces = t;
ifaces[(*n)++] = iface;
} }
} }
close(sock); close(sock);
return ifaces; return list;
} }
#endif #endif
#if defined(__linux__) #if defined(__linux__)
static net_iface_t ** static Eina_List *
_linux_generic_network_status(int *n) _linux_generic_network_status(void)
{ {
FILE *f; FILE *f;
char buf[4096], name[128]; char buf[4096], name[128];
unsigned long int tmp_in, tmp_out, dummy; unsigned long int tmp_in, tmp_out, dummy;
Eina_List *list = NULL;
f = fopen("/proc/net/dev", "r"); f = fopen("/proc/net/dev", "r");
if (!f) return NULL; if (!f) return NULL;
net_iface_t **ifaces = NULL;
while (fgets(buf, sizeof(buf), f)) while (fgets(buf, sizeof(buf), f))
{ {
if (17 == sscanf(buf, "%s %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu " 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, &tmp_out, &dummy, &dummy, &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) if (iface)
{ {
name[strlen(name)-1] = '\0'; name[strlen(name)-1] = '\0';
snprintf(iface->name, sizeof(iface->name), "%s", name); snprintf(iface->name, sizeof(iface->name), "%s", name);
iface->xfer.in = tmp_in; iface->total_in = tmp_in;
iface->xfer.out = tmp_out; iface->total_out = tmp_out;
void *t = realloc(ifaces, (1 + 1 + *n) * sizeof(net_iface_t *)); list = eina_list_append(list, iface);
ifaces = t;
ifaces[(*n)++] = iface;
} }
} }
} }
fclose(f); fclose(f);
return ifaces; return list;
} }
#endif #endif
net_iface_t ** Eina_List *
system_network_ifaces_get(int *n) network_interfaces_find(void)
{ {
*n = 0;
#if defined(__linux__) #if defined(__linux__)
return _linux_generic_network_status(n); return _linux_generic_network_status();
#elif defined(__MacOS__) || defined(__FreeBSD__) || defined(__DragonFly__) #elif defined(__MacOS__) || defined(__FreeBSD__) || defined(__DragonFly__)
return _freebsd_generic_network_status(n); return _freebsd_generic_network_status();
#elif defined(__OpenBSD__) #elif defined(__OpenBSD__)
return _openbsd_generic_network_status(n); return _openbsd_generic_network_status();
#else #else
return NULL; return NULL;
#endif #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 void
system_sensor_thermal_free(sensor_t *sensor) sensor_free(Sensor *sensor)
{ {
if (sensor->name) if (sensor->name)
free(sensor->name); free(sensor->name);
@ -28,20 +12,8 @@ system_sensor_thermal_free(sensor_t *sensor)
free(sensor); free(sensor);
} }
void Eina_Bool
system_sensors_thermal_free(sensor_t **sensors, int count) sensor_check(Sensor *sensor)
{
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)
{ {
#if defined(__linux__) #if defined(__linux__)
char *d = file_contents(sensor->path); char *d = file_contents(sensor->path);
@ -75,13 +47,12 @@ system_sensor_thermal_get(sensor_t *sensor)
return 0; return 0;
} }
sensor_t ** Eina_List *
system_sensors_thermal_get(int *sensor_count) sensors_find(void)
{ {
sensor_t **sensors = NULL; Eina_List *sensors = NULL;
*sensor_count = 0;
#if defined(__OpenBSD__) #if defined(__OpenBSD__)
sensor_t *sensor; Sensor *sensor;
int mibs[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 }; int mibs[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 };
int devn, n; int devn, n;
struct sensor snsr; struct sensor snsr;
@ -114,26 +85,31 @@ system_sensors_thermal_get(int *sensor_count)
if (snsr.type != SENSOR_TEMP) if (snsr.type != SENSOR_TEMP)
continue; continue;
void *t = realloc(sensors, (1 + *sensor_count) * sizeof(sensor_t *)); sensor = calloc(1, sizeof(Sensor));
sensors = t; if (sensor)
sensors[(*sensor_count)++] = sensor = calloc(1, sizeof(sensor_t)); {
sensor->name = strdup(snsrdev.xname); sensor->name = strdup(snsrdev.xname);
sensor->value = (snsr.value - 273150000) / 1000000.0; // (uK -> C) sensor->value = (snsr.value - 273150000) / 1000000.0; // (uK -> C)
memcpy(sensor->mibs, &mibs, sizeof(mibs)); memcpy(sensor->mibs, &mibs, sizeof(mibs));
sensors = eina_list_append(sensors, sensor);
}
} }
#elif defined(__FreeBSD__) || defined(__DragonFly__) #elif defined(__FreeBSD__) || defined(__DragonFly__)
sensor_t *sensor; Sensor *sensor;
int value; int value;
char buf[256]; char buf[256];
size_t len = sizeof(value); size_t len = sizeof(value);
if ((sysctlbyname("hw.acpi.thermal.tz0.temperature", &value, &len, NULL, 0)) != -1) if ((sysctlbyname("hw.acpi.thermal.tz0.temperature", &value, &len, NULL, 0)) != -1)
{ {
void *t = realloc(sensors, (1 + *sensor_count) * sizeof(sensor_t *)); sensor = calloc(1, sizeof(Sensor));
sensors = t; if (sensor)
sensors[(*sensor_count)++] = sensor = calloc(1, sizeof(sensor_t)); {
sensor->name = strdup("hw.acpi.thermal.tz0.temperature"); sensor->name = strdup("hw.acpi.thermal.tz0.temperature");
sensor->value = (float) (value - 2732) / 10; sensor->value = (float) (value - 2732) / 10;
sensors = eina_list_append(sensors, sensor);
}
} }
int n = system_cpu_count_get(); 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); snprintf(buf, sizeof(buf), "dev.cpu.%i.temperature", i);
if ((sysctlbyname(buf, &value, &len, NULL, 0)) != -1) if ((sysctlbyname(buf, &value, &len, NULL, 0)) != -1)
{ {
void *t = realloc(sensors, (1 + *sensor_count) * sizeof(sensor_t *)); sensor = calloc(1, sizeof(Sensor));
sensors = t; if (sensor)
sensors[(*sensor_count)++] = sensor = calloc(1, sizeof(sensor_t)); {
sensor->name = strdup(buf); sensor->name = strdup(buf);
sensor->value = (float) (value - 2732) / 10; sensor->value = (float) (value - 2732) / 10;
sensors = eina_list_append(sensors, sensor);
}
} }
} }
#elif defined(__linux__) #elif defined(__linux__)
sensor_t *sensor; Sensor *sensor;
DIR *dir; DIR *dir;
struct dirent *dh; struct dirent *dh;
char buf[4096]; char buf[4096];
@ -198,29 +175,26 @@ system_sensors_thermal_get(int *sensor_count)
continue; continue;
} }
void *t = realloc(sensors, (1 + (*sensor_count)) * sizeof(sensor_t *)); sensor = calloc(1, sizeof(Sensor));
sensors = t; if (sensor)
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->value = atoi(d); snprintf(buf, sizeof(buf), "%s/name", link);
if (sensor->value) sensor->value /= 1000; sensor->name = file_contents(buf);
free(d);
} 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; seen[idx++] = id;
} }
free(names[i]); free(names[i]);
@ -315,7 +289,6 @@ batteries_find(void)
free(names); free(names);
#endif #endif
puts("AYE");
return list; return list;
} }
@ -328,35 +301,6 @@ battery_free(Battery *bat)
free(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 void
battery_check(Battery *bat) battery_check(Battery *bat)
{ {
@ -405,7 +349,7 @@ battery_check(Battery *bat)
snprintf(path, sizeof(path), "/sys/class/power_supply/%s", bat->name); snprintf(path, sizeof(path), "/sys/class/power_supply/%s", bat->name);
if ((stat(path, &st) < 0) || (!S_ISDIR(st.st_mode))) if ((stat(path, &st) < 0) || (!S_ISDIR(st.st_mode)))
return; return;
link = realpath(path, NULL); link = realpath(path, NULL);
if (!link) return; if (!link) return;
@ -478,58 +422,20 @@ done:
bat->percent = 100 * (charge_full / charge_current); bat->percent = 100 * (charge_full / charge_current);
} }
static void
_battery_state_get(power_t *power) Eina_Bool
power_ac(void)
{ {
#if defined(__OpenBSD__) Eina_Bool have_ac = 0;
#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));
#if defined(__OpenBSD__) #if defined(__OpenBSD__)
struct sensor snsr; struct sensor snsr;
size_t slen = sizeof(struct sensor); size_t slen = sizeof(struct sensor);
#endif
if (!_power_battery_count_get(power))
return;
#if defined(__OpenBSD__)
power->mibs[3] = 9; power->mibs[3] = 9;
power->mibs[4] = 0; power->mibs[4] = 0;
if (sysctl(power->mibs, 5, &snsr, &slen, NULL, 0) != -1) 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__) #elif defined(__FreeBSD__) || defined(__DragonFly__)
int val, fd; int val, fd;
@ -537,22 +443,18 @@ system_power_state_get(power_t *power)
if (fd != -1) if (fd != -1)
{ {
if (ioctl(fd, ACPIIO_ACAD_GET_STATUS, &val) != -1) if (ioctl(fd, ACPIIO_ACAD_GET_STATUS, &val) != -1)
power->have_ac = val; have_ac = val;
close(fd); close(fd);
} }
#elif defined(__linux__) #elif defined(__linux__)
char *buf = file_contents("/sys/class/power_supply/AC/online"); char *buf = file_contents("/sys/class/power_supply/AC/online");
if (buf) if (buf)
{ {
power->have_ac = atoi(buf); have_ac = atoi(buf);
free(buf); free(buf);
} }
#endif #endif
_battery_state_get(power); return have_ac;
} }
void
system_power_state_free(power_t *power)
{
}