#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/errno.h>
#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Output.H>
void box0cb(void* output) {
size_t size;
int t_dK, t_C, t_F; //Default is deciKelvin see SYSCTL_ADD_PROC(9)
char buf[128] = {""};
size_t buflen = sizeof(buf);
size = sizeof t_dK;
if (0 == (sysctlbyname("dev.cpu.0.temperature", &t_dK, &size, NULL, 0))) {
t_C = (t_dK - 2730) / 10;
t_F = (t_dK * 1.8 / 10) - 459.67;
printf("%d dK, %d C, %d F\n", t_dK, t_C, t_F);
snprintf(buf, buflen, "%d F", t_F);
} else {
strerror_r(errno, buf, buflen);
}
((Fl_Output*)output)->value(buf);
Fl::repeat_timeout(5.0, box0cb, output);
}
void box1cb(void* output) {
size_t size;
int t_dK, t_C, t_F; //Default is deciKelvin see SYSCTL_ADD_PROC(9)
char buf[128] = {""};
size_t buflen = sizeof(buf);
size = sizeof t_dK;
if (0 == (sysctlbyname("dev.cpu.1.temperature", &t_dK, &size, NULL, 0))) {
t_C = (t_dK - 2730) / 10;
t_F = (t_dK * 1.8 / 10) - 459.67;
printf("%d dK, %d C, %d F\n", t_dK, t_C, t_F);
snprintf(buf, buflen, "%d F", t_F);
} else {
strerror_r(errno, buf, buflen);
}
((Fl_Output*)output)->value(buf);
Fl::repeat_timeout(5.0, box1cb, output);
}
void box2cb(void* output) {
size_t size;
int t_dK, t_C, t_F; //Default is deciKelvin see SYSCTL_ADD_PROC(9)
char buf[128] = {""};
size_t buflen = sizeof(buf);
size = sizeof t_dK;
if (0 == (sysctlbyname("dev.cpu.2.temperature", &t_dK, &size, NULL, 0))) {
t_C = (t_dK - 2730) / 10;
t_F = (t_dK * 1.8 / 10) - 459.67;
printf("%d dK, %d C, %d F\n", t_dK, t_C, t_F);
snprintf(buf, buflen, "%d F", t_F);
} else {
strerror_r(errno, buf, buflen);
}
((Fl_Output*)output)->value(buf);
Fl::repeat_timeout(5.0, box2cb, output);
}
void box3cb(void* output) {
size_t size;
int t_dK, t_C, t_F; //Default is deciKelvin see SYSCTL_ADD_PROC(9)
char buf[128] = {""};
size_t buflen = sizeof(buf);
size = sizeof t_dK;
if (0 == (sysctlbyname("dev.cpu.3.temperature", &t_dK, &size, NULL, 0))) {
t_C = (t_dK - 2730) / 10;
t_F = (t_dK * 1.8 / 10) - 459.67;
printf("%d dK, %d C, %d F\n", t_dK, t_C, t_F);
snprintf(buf, buflen, "%d F", t_F);
} else {
strerror_r(errno, buf, buflen);
}
((Fl_Output*)output)->value(buf);
Fl::repeat_timeout(5.0, box3cb, output);
}
int main(int argc, char ** argv) {
Fl_Window *window = new Fl_Window(250,420, "CPU Temperature");
Fl_Output *box0 = new Fl_Output(100, 050, 70, 30, "CPU0");
box0->labelsize(20);
Fl_Output *box1 = new Fl_Output(100, 150, 70, 30, "CPU1");
box1->labelsize(20);
Fl_Output *box2 = new Fl_Output(100, 250, 70, 30, "CPU2");
box2->labelsize(20);
Fl_Output *box3 = new Fl_Output(100, 350, 70, 30, "CPU3");
box3->labelsize(20);
window->end();
window->show(argc, argv);
Fl::add_timeout(5.0, box0cb, box0);
Fl::add_timeout(5.0, box1cb, box1);
Fl::add_timeout(5.0, box2cb, box2);
Fl::add_timeout(5.0, box3cb, box3);
return Fl::run();
}