dotfiles/.config/spectrwm/cbar.c

133 lines
3.2 KiB
C
Raw Normal View History

2022-12-05 22:26:33 +01:00
#include <stdio.h>
#include <unistd.h>
#include <wchar.h>
#include <stdlib.h>
2022-12-05 22:40:28 +01:00
#include <string.h>
2022-12-05 22:26:33 +01:00
#include <fcntl.h>
#include <sys/time.h>
#include <sys/sysctl.h>
#include <sys/ioctl.h>
#include <sys/sensors.h>
2022-12-07 23:28:01 +01:00
#include <sndio.h>
2022-12-05 22:40:28 +01:00
#include <sys/types.h>
#include <sys/param.h>
2022-12-05 22:26:33 +01:00
#include <machine/apmvar.h>
2022-12-07 23:28:01 +01:00
static int battery_percent = 23;
static int cpu_temp = 23;
static int fan_speed = 23;
static int cpu_base_speed = 23;
static int cpu_avg_speed = 23;
static int volume = 23;
2022-12-08 13:35:55 +01:00
//void update_volume(void *arg, unsigned addr, unsigned val) {
2022-12-07 23:28:01 +01:00
// printf("volume update\n");
//
// }
//
// void register_volume_callback() {
// struct sioctl_hdl *hdl;
//
// hdl = sioctl_open(SIO_DEVANY, SIOCTL_READ, 0);
// sioctl_onval(hdl, update_volume, NULL);
// sioctl_close(hdl);
// }
void update_cpu_base_speed() {
size_t slen = sizeof(cpu_base_speed);
int mib[5] = { CTL_HW, HW_CPUSPEED }; // Lenovo x1g10
if (sysctl(mib, 2, &cpu_base_speed, &slen, NULL, 0) == -1) {
cpu_base_speed = -1;
2022-12-05 22:40:28 +01:00
}
2022-12-07 23:28:01 +01:00
}
2022-12-05 22:40:28 +01:00
2022-12-07 23:28:01 +01:00
void update_cpu_avg_speed() {
struct sensor sensor;
size_t slen = sizeof(sensor);
int i;
for (i = 0; i < 12; i++) {
int mib[5] = { CTL_HW, HW_SENSORS, 0, SENSOR_FREQ, 0 }; // Lenovo x1g10
if (sysctl(mib, 5, &sensor, &slen, NULL, 0) != -1) {
cpu_avg_speed += ( sensor.value / 1000000 / 1000000 );
2022-12-05 22:40:28 +01:00
}
}
2022-12-07 23:28:01 +01:00
cpu_avg_speed = cpu_avg_speed / i;
2022-12-05 22:40:28 +01:00
}
2022-12-05 22:26:33 +01:00
void update_fan_speed() {
struct sensor sensor;
size_t slen = sizeof(sensor);
2022-12-07 23:28:01 +01:00
// int mib[5] = { CTL_HW, HW_SENSORS, 5, SENSOR_FANRPM, 0 }; // Lenovo x230
int mib[5] = { CTL_HW, HW_SENSORS, 12, SENSOR_FANRPM, 0 }; // Lenovo x1g10
2022-12-05 22:40:28 +01:00
if (sysctl(mib, 5, &sensor, &slen, NULL, 0) != -1) {
2022-12-05 22:26:33 +01:00
fan_speed = sensor.value;
2022-12-05 22:40:28 +01:00
return;
}
2022-12-05 22:26:33 +01:00
fan_speed = -1;
}
void update_cpu_temp() {
struct sensor sensor;
size_t slen = sizeof(sensor);
2022-12-07 23:28:01 +01:00
//int mib[5] = { CTL_HW, HW_SENSORS, 0, SENSOR_TEMP, 0 }; // cpu0.temp0 (x230)
int mib[5] = { CTL_HW, HW_SENSORS, 12, SENSOR_TEMP, 0 }; // acpitz0.temp0 (x1)
2022-12-05 22:40:28 +01:00
if (sysctl(mib, 5, &sensor, &slen, NULL, 0) != -1) {
2022-12-05 22:26:33 +01:00
cpu_temp = (sensor.value - 273150000) / 1000000.0;
2022-12-05 22:40:28 +01:00
return;
}
2022-12-05 22:26:33 +01:00
cpu_temp = -1;
}
void update_battery() {
int fd;
struct apm_power_info pi;
if ((fd = open("/dev/apm", O_RDONLY)) == -1 ||
ioctl(fd, APM_IOC_GETPOWER, &pi) == -1 ||
2022-12-05 22:40:28 +01:00
close(fd) == -1) {
2022-12-05 22:26:33 +01:00
battery_percent = -1;
2022-12-05 22:40:28 +01:00
return;
}
2022-12-05 22:26:33 +01:00
if (pi.battery_state == APM_BATT_UNKNOWN ||
2022-12-05 22:40:28 +01:00
pi.battery_state == APM_BATTERY_ABSENT) {
2022-12-05 22:26:33 +01:00
battery_percent = -1;
2022-12-05 22:40:28 +01:00
return;
}
2022-12-05 22:26:33 +01:00
battery_percent = pi.battery_life;
}
int main(int argc, const char *argv[])
{
2022-12-07 23:28:01 +01:00
//register_volume_callback();
2022-12-05 22:26:33 +01:00
while(1) {
2022-12-05 22:40:28 +01:00
// XXX can update at different intervals
2022-12-05 22:26:33 +01:00
update_battery();
update_cpu_temp();
2022-12-07 23:28:01 +01:00
update_cpu_avg_speed();
update_cpu_base_speed();
2022-12-05 22:26:33 +01:00
update_fan_speed();
2022-12-05 22:40:28 +01:00
2022-12-07 23:28:01 +01:00
wprintf(L"%s %d%% %s %dC %s %4dRPM %s %4dMhz (~%4dMhz) %s %d %s\n",
2022-12-05 22:26:33 +01:00
"", battery_percent,
" ", cpu_temp,
" ", fan_speed,
" ", cpu_base_speed, cpu_avg_speed,
2022-12-07 23:28:01 +01:00
" ", volume,
2022-12-05 22:26:33 +01:00
" ");
sleep(1);
}
return 0;
}