commit 7c47d03e8c364214598b5e3df0135d51526e23a3
parent fcba7d6205b16c3099c3a30cf5bc0ef9dadd188b
Author: bsandro <brian.drosan@gmail.com>
Date: Mon, 2 Aug 2021 01:15:47 +0300
Displaying CPU temp sensor
Diffstat:
3 files changed, 23 insertions(+), 86 deletions(-)
diff --git a/dwmstatus-temperature.c b/dwmstatus-temperature.c
@@ -1,15 +0,0 @@
-/*
- * gettemperature("/sys/class/hwmon/hwmon0/device", "temp1_input");
- */
-
-char *
-gettemperature(char *base, char *sensor)
-{
- char *co;
-
- co = readfile(base, sensor);
- if (co == NULL)
- return smprintf("");
- return smprintf("%02.0f°C", atof(co) / 1000);
-}
-
diff --git a/dwmstatus.c b/dwmstatus.c
@@ -14,7 +14,9 @@
#include <time.h>
#include <sys/types.h>
#include <sys/wait.h>
-
+#include <sys/sysctl.h>
+#include <sys/sensors.h>
+#include <errno.h>
#include <X11/Xlib.h>
char *tzcyprus = "Asia/Nicosia";
@@ -205,25 +207,34 @@ getbattery(char *base)
}
char *
-gettemperature(char *base, char *sensor)
+gettemperature()
{
- char *co;
+ int mib[5];
+ struct sensor temp;
+ size_t len;
- co = readfile(base, sensor);
- if (co == NULL)
- return smprintf("");
- return smprintf("%02.0f°C", atof(co) / 1000);
+ mib[0] = CTL_HW;
+ mib[1] = HW_SENSORS;
+ mib[2] = 0; // bcmtmon0
+ mib[3] = SENSOR_TEMP;
+ mib[4] = 0; // temp0
+
+ len = sizeof(temp);
+
+ if (sysctl(mib, 5, &temp, &len, NULL, 0) == -1) {
+ return smprintf("sysctl error: %s\n", strerror(errno));
+ }
+
+ return smprintf("%.2f°C", (double)(temp.value - 273150000) / 1000000.0);
}
int
main(void)
{
char *status;
- //char *avgs;
char *tmmoscow;
char *tmcyprus;
char *cpu_temp;
- int cpu_clock;
if (!(dpy = XOpenDisplay(NULL))) {
fprintf(stderr, "dwmstatus: cannot open display.\n");
@@ -231,17 +242,13 @@ main(void)
}
for (;;sleep(5)) {
- //avgs = loadavg();
tmmoscow = mktimes("%H:%M", tzmoscow);
tmcyprus = mktimes("%a %F %R", tzcyprus);
- //cpu_temp = execproc("vcgencmd measure_temp");
- //cpu_clock = cpufreq();
- status = smprintf("MSK(%s) %s",
- /*cpu_temp, cpu_clock,*/ tmmoscow, tmcyprus);
+ cpu_temp = gettemperature();
+ status = smprintf("CPUTEMP: %s MSK(%s) %s", cpu_temp, tmmoscow, tmcyprus);
setstatus(status);
- //free(cpu_temp);
- //free(avgs);
+ free(cpu_temp);
free(tmmoscow);
free(tmcyprus);
free(status);
diff --git a/new-acpi-battery.c b/new-acpi-battery.c
@@ -1,55 +0,0 @@
-char *
-readfile(char *base, char *file)
-{
- char *path, line[513];
- FILE *fd;
-
- memset(line, 0, sizeof(line));
-
- path = smprintf("%s/%s", base, file);
- fd = fopen(path, "r");
- if (fd == NULL) {
- perror("fopen");
- exit(1);
- }
- free(path);
-
- if (fgets(line, sizeof(line)-1, fd) == NULL) {
- perror("fgets");
- exit(1);
- }
- fclose(fd);
-
- return smprintf("%s", line);
-}
-
-char *
-getbattery(char *base)
-{
- char *co;
- int descap, remcap;
-
- descap = -1;
- remcap = -1;
-
- co = readfile(base, "present");
- if (co[0] != '1') {
- free(co);
- return smprintf("not present");
- }
- free(co);
-
- co = readfile(base, "charge_full_design");
- sscanf(co, "%d", &descap);
- free(co);
-
- co = readfile(base, "charge_now");
- sscanf(co, "%d", &remcap);
- free(co);
-
- if (remcap < 0 || descap < 0)
- return smprintf("invalid");
-
- return smprintf("%.0f", ((float)remcap / (float)descap) * 100);
-}
-