fetch core temp with glob support (linux hwmon)

 avatar
unknown
diff
11 days ago
2.7 kB
5
Indexable
---
 Makefile              |  1 +
 components/coretemp.c | 40 ++++++++++++++++++++++++++++++++++++++++
 config.def.h          |  3 +++
 slstatus.c            |  4 ++++
 slstatus.h            |  5 +++++
 5 files changed, 53 insertions(+)
 create mode 100644 components/coretemp.c

diff --git a/Makefile b/Makefile
index 7a18274..742c6d9 100644
--- a/Makefile
+++ b/Makefile
@@ -8,6 +8,7 @@ REQ = util
 COM =\
 	components/battery\
 	components/cat\
+	components/coretemp\
 	components/cpu\
 	components/datetime\
 	components/disk\
diff --git a/components/coretemp.c b/components/coretemp.c
new file mode 100644
index 0000000..778c33f
--- /dev/null
+++ b/components/coretemp.c
@@ -0,0 +1,39 @@
+/* See LICENSE file for copyright and license details. */
+#include <stdint.h>
+#include <string.h>
+#include <stdlib.h>
+#include <glob.h>
+
+#include "../slstatus.h"
+#include "../util.h"
+
+static char *resolved_coretemp = NULL;
+
+#define CORETEMP_PATH "/sys/devices/platform/coretemp.0/hwmon/*/temp1_input"
+
+void
+coretemp_init()
+{
+	glob_t glob_res;
+	if (!glob(CORETEMP_PATH, 0, NULL, &glob_res)) {
+		resolved_coretemp = strdup(glob_res.gl_pathv[0]);
+		globfree(&glob_res);
+	}
+}
+
+const char *
+coretemp(const char *unused)
+{
+	uintmax_t temp;
+
+	if (!resolved_coretemp || pscanf(resolved_coretemp, "%ju", &temp) != 1)
+		return NULL;
+
+	return bprintf("%ju", temp / 1000);
+}
+
+void
+coretemp_cleanup()
+{
+	free(resolved_coretemp);
+}
diff --git a/config.def.h b/config.def.h
index 100093e..788a738 100644
--- a/config.def.h
+++ b/config.def.h
@@ -67,4 +67,5 @@ static const char unknown_str[] = "n/a";
 static const struct arg args[] = {
 	/* function format          argument */
 	{ datetime, "%s",           "%F %T" },
+	{ coretemp, " %s°C",        NULL },
 };
diff --git a/slstatus.c b/slstatus.c
index 16d88fe..7f727dc 100644
--- a/slstatus.c
+++ b/slstatus.c
@@ -72,6 +72,8 @@ main(int argc, char *argv[])
 	if (argc)
 		usage();

+	coretemp_init();
+
 	memset(&act, 0, sizeof(act));
 	act.sa_handler = terminate;
 	sigaction(SIGINT,  &act, NULL);
@@ -125,6 +127,8 @@ main(int argc, char *argv[])
 		}
 	} while (!done);

+	coretemp_cleanup();
+
 	if (!sflag) {
 		XStoreName(dpy, DefaultRootWindow(dpy), NULL);
 		if (XCloseDisplay(dpy) < 0)
diff --git a/slstatus.h b/slstatus.h
index 394281c..7779013 100644
--- a/slstatus.h
+++ b/slstatus.h
@@ -8,6 +8,11 @@ const char *battery_state(const char *);
 /* cat */
 const char *cat(const char *path);

+/* coretemp */
+void coretemp_init(void);
+const char *coretemp(const char *unused);
+void coretemp_cleanup(void);
+
 /* cpu */
 const char *cpu_freq(const char *unused);
 const char *cpu_perc(const char *unused);
--
2.43.0

Editor is loading...
Leave a Comment