Temperature Monitor app: honest zero-sensor state + XTempHistory.clear()

Single-widget monitor app (XTempHistory, 80 C threshold) reading
/sys/class/thermal + /sys/class/hwmon every 2 s. hot_dallas has no
thermal sensors, so the app shows the widget empty state (no data,
no fake readings) with a title that says so. Adds additive
XTempHistory::clear() and an empty-state legend.
This commit is contained in:
Muse committed 2026-12-05 20:40:33 +00:00
1 parent e474894bae
commit f275d07075
5 files changed
+359 -7

No files matched your search

@@ -0,0 +1,18 @@
CXX ?= g++
CXXFLAGS ?= -std=c++17 -O2 -Wall -Wextra
# One -I per widget dir: each widget header includes "core/xframe_core.h"
# from its own component directory.
INCLUDES := -I../../../widgets/components/XTempHistory
LDFLAGS := -lX11
all: temperature-monitor
temperature-monitor: main.cpp
$(CXX) $(CXXFLAGS) $(INCLUDES) main.cpp -o temperature-monitor $(LDFLAGS)
clean:
rm -f temperature-monitor
.PHONY: all clean
@@ -0,0 +1,38 @@
# Temperature Monitor — monitor (C)
A btop-style temperature monitor in one 1024x768 window, built only
from LightGUI XFrame monitor widgets:
- **XTempHistory** — temperature history of the first thermal zone,
with an 80 C threshold line (readings above it draw red)
Live data comes from `/sys/class/thermal/thermal_zone*/temp` and
`/sys/class/hwmon/hwmon*/temp*_input`, re-scanned every 2 s. On a
machine with sensors the history fills with live readings; on this
machine (hot_dallas, a VPS with no thermal sensors at all — only
cooling devices under `/sys/class/thermal/`, no `thermal_zone*/temp`
files, an empty `/sys/class/hwmon/`) the app shows the widget's
honest empty state: an empty graph, "no data" in the legend, and a
title that says no thermal zones were found. No mock or demo
temperatures: the seeded demo ramp the widget preloads is dropped at
startup with `XTempHistory::clear()`.
## Build
```sh
make
```
## Run
```sh
./temperature-monitor
```
Esc, `q`, or closing the window quits.
## Source
- App: `examples/monitor/temperature-monitor/main.cpp`
- Widget: `widgets/components/XTempHistory`
- Repo: https://github.com/cnmcdee/lightgui
@@ -0,0 +1,278 @@
// Temperature Monitor - monitor app (Stage 2, btop-widget project).
//
// One 1024x768 window: a single XTempHistory widget showing the first
// thermal zone's temperature history (80 C threshold line), re-scanned
// every 2 s from /sys/class/thermal/thermal_zone*/temp and
// /sys/class/hwmon/hwmon*/temp*_input.
//
// On this machine (hot_dallas, a VPS) there are NO thermal sensors at
// all: /sys/class/thermal/ holds only cooling devices, there are no
// thermal_zone*/temp files, /sys/class/hwmon/ is empty and `sensors`
// is not installed. So the app shows the widget's honest empty state
// ("no data", no line, no fake 0.0 C reading) and says so in the
// title. No mock or demo temperatures, ever: the widget's seeded demo
// ramp is dropped at startup with XTempHistory::clear(). If sensors
// ever appear (e.g. on a machine that has them) the rescan picks them
// up and the history starts filling with live readings.
//
// Build: make
// Run: ./temperature-monitor (Esc / q / window-close quits)
#include "XTempHistory.h"
#include <X11/Xlib.h>
#include <X11/keysym.h>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <dirent.h>
#include <fstream>
#include <string>
#include <sys/select.h>
#include <sys/time.h>
#include <unistd.h>
#include <vector>
namespace
{
struct Zone
{
std::string name; // thermal_zone type, or hwmon name + label
std::string path; // .../temp file, millidegrees Celsius
};
std::string read_first_line(const std::string& path)
{
std::ifstream in(path);
std::string line;
if (in && std::getline(in, line))
{
while (!line.empty() && (line.back() == '\n' || line.back() == '\r' ||
line.back() == ' ' || line.back() == '\t'))
line.pop_back();
return line;
}
return "";
}
bool read_temp_c(const std::string& path, double& out)
{
std::string s = read_first_line(path);
if (s.empty()) return false;
char* end = nullptr;
long milli = std::strtol(s.c_str(), &end, 10);
if (end == s.c_str() || *end != '\0') return false;
out = (double)milli / 1000.0;
return true;
}
// All temperature inputs the kernel exposes, thermal zones first.
std::vector<Zone> scan_zones()
{
std::vector<Zone> zones;
DIR* thermal = opendir("/sys/class/thermal");
if (thermal)
{
struct dirent* ent;
while ((ent = readdir(thermal)) != nullptr)
{
if (std::strncmp(ent->d_name, "thermal_zone", 12) != 0) continue;
std::string base = std::string("/sys/class/thermal/") + ent->d_name;
std::string temp_path = base + "/temp";
double probe = 0.0;
if (!read_temp_c(temp_path, probe)) continue;
std::string type = read_first_line(base + "/type");
Zone z;
z.name = type.empty() ? ent->d_name : type;
z.path = temp_path;
zones.push_back(z);
}
closedir(thermal);
}
DIR* hwmon = opendir("/sys/class/hwmon");
if (hwmon)
{
struct dirent* ent;
while ((ent = readdir(hwmon)) != nullptr)
{
if (std::strncmp(ent->d_name, "hwmon", 5) != 0) continue;
std::string base = std::string("/sys/class/hwmon/") + ent->d_name;
std::string chip = read_first_line(base + "/name");
if (chip.empty()) chip = ent->d_name;
DIR* dir = opendir(base.c_str());
if (!dir) continue;
struct dirent* f;
while ((f = readdir(dir)) != nullptr)
{
std::string fn = f->d_name;
if (fn.rfind("temp", 0) != 0) continue;
if (fn.size() < 10 || fn.compare(fn.size() - 6, 6, "_input") != 0)
continue;
std::string temp_path = base + "/" + fn;
double probe = 0.0;
if (!read_temp_c(temp_path, probe)) continue;
std::string label = read_first_line(
base + "/" + fn.substr(0, fn.size() - 6) + "_label");
Zone z;
z.name = label.empty() ? chip : chip + " " + label;
z.path = temp_path;
zones.push_back(z);
}
closedir(dir);
}
closedir(hwmon);
}
return zones;
}
} // namespace
int main()
{
Display* display = XOpenDisplay(nullptr);
if (!display) return 1;
int screen = DefaultScreen(display);
Window window = XCreateSimpleWindow(display, RootWindow(display, screen),
100, 100, 1024, 768, 1,
BlackPixel(display, screen),
WhitePixel(display, screen));
XStoreName(display, window, "Temperature Monitor - monitor");
XSelectInput(display, window,
ExposureMask | KeyPressMask | StructureNotifyMask |
ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False);
XSetWMProtocols(display, window, &deleteWindowMessage, 1);
XMapWindow(display, window);
GC gc = DefaultGC(display, screen);
SStyle_Manager styles(display, window, screen, gc);
styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120},
Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1,
"-misc-fixed-medium-r-normal--13-*");
styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22},
Srgb{200, 200, 200}, Srgb{240, 240, 240}, 6, 1,
"-misc-fixed-medium-r-normal--13-*");
styles.set_active_style_byName("default");
XTempHistory history(display, screen, window, gc, &styles, 16, 16, 992, 736);
history.set_text("Temperature history");
history.set_threshold(80.0);
// Drop the widget's seeded demo ramp: with no live source the
// honest state is an empty graph, not a fake warm-up curve.
history.clear();
const std::string no_zones_title =
"Temperature history -- no thermal zones found";
auto redraw = [&]() {
XClearWindow(display, window);
history.draw();
XFlush(display);
};
// First scan before the first paint, so the title already tells
// the truth on screen one.
std::vector<Zone> zones = scan_zones();
bool had_zones = false;
if (zones.empty())
{
history.set_text(no_zones_title);
}
else
{
had_zones = true;
history.set_text("Temperature history -- " + zones[0].name);
double t = 0.0;
if (read_temp_c(zones[0].path, t)) history.push(t);
}
redraw();
int fd = ConnectionNumber(display);
bool running = true;
while (running)
{
fd_set rfds;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
struct timeval tv;
tv.tv_sec = 2;
tv.tv_usec = 0;
int ready = select(fd + 1, &rfds, nullptr, nullptr, &tv);
if (ready > 0)
{
while (XPending(display))
{
XEvent event;
XNextEvent(display, &event);
bool handled = false;
switch (event.type)
{
case Expose:
handled = true;
break;
case ButtonPress:
handled = history.on_button_press(event.xbutton.x,
event.xbutton.y,
event.xbutton.button);
break;
case ButtonRelease:
handled = history.on_button_release(event.xbutton.x,
event.xbutton.y,
event.xbutton.button);
break;
case MotionNotify:
handled = history.on_motion(event.xmotion.x, event.xmotion.y);
break;
case KeyPress:
{
char buffer[32];
KeySym key = 0;
int n = XLookupString(&event.xkey, buffer,
(int)sizeof(buffer) - 1, &key, nullptr);
buffer[n] = 0;
if (key == XK_Escape || (n == 1 && buffer[0] == 'q'))
running = false;
handled = history.on_key(key, buffer);
break;
}
case ClientMessage:
if ((Atom)event.xclient.data.l[0] == deleteWindowMessage)
running = false;
break;
}
if (handled) redraw();
if (!running) break;
}
}
else if (ready == 0)
{
// 2 s tick: re-scan sysfs (sensors can appear on other
// machines) and push the first zone's live reading.
zones = scan_zones();
if (zones.empty())
{
if (had_zones)
{
had_zones = false;
history.clear();
history.set_text(no_zones_title);
}
}
else
{
if (!had_zones)
{
had_zones = true;
history.set_text("Temperature history -- " + zones[0].name);
}
double t = 0.0;
if (read_temp_c(zones[0].path, t)) history.push(t);
}
redraw();
}
}
XCloseDisplay(display);
return 0;
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

+25 -7
View File
@@ -119,6 +119,15 @@ public:
temps_.push_back(temp_c);
while ((int)temps_.size() > capacity_) temps_.erase(temps_.begin());
}
// Drop every sample (and nothing else): the widget then shows its
// honest empty state ("no data") instead of the demo ramp the
// constructor preloads. A host app with no live source yet (e.g.
// a machine with no thermal zones) calls this at startup; later
// push() calls fill the series again as usual.
void clear()
{
temps_.clear();
}
void set_threshold(double t) { threshold_ = t; }
double get_threshold() { return threshold_; }
void set_y_max(double v) { y_max_ = v > 0.0 ? v : 100.0; }
@@ -198,20 +207,29 @@ public:
}
// Legend: current (big, coloured by state) + min / max.
double mn = 0.0, mx = 0.0;
if (!temps_.empty())
// Empty (no samples pushed, e.g. no thermal zones found):
// say so honestly instead of a 0.0 C reading that never
// happened. The threshold line and scale still draw.
if (temps_.empty())
{
mn = mx = temps_[0];
std::string nd = "no data";
draw_text_color(x2_ - 8 - xf::text_width(st, nd), y1_ + title_h + 14, nd, Srgb{100, 100, 100});
std::string mm = "threshold " + fmt1(threshold_) + "C";
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + title_h + 14, mm);
}
else
{
double mn = temps_[0], mx = temps_[0];
for (double v : temps_)
{
mn = std::min(mn, v);
mx = std::max(mx, v);
}
std::string cur = "now " + fmt1(current()) + "C";
draw_text_color(x2_ - 8 - xf::text_width(st, cur), y1_ + title_h + 14, cur, over_temp() ? hot_color_ : ok_color_);
std::string mm = "min " + fmt1(mn) + "C max " + fmt1(mx) + "C threshold " + fmt1(threshold_) + "C";
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + title_h + 14, mm);
}
std::string cur = "now " + fmt1(current()) + "C";
draw_text_color(x2_ - 8 - xf::text_width(st, cur), y1_ + title_h + 14, cur, over_temp() ? hot_color_ : ok_color_);
std::string mm = "min " + fmt1(mn) + "C max " + fmt1(mx) + "C threshold " + fmt1(threshold_) + "C";
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + title_h + 14, mm);
XSetForeground(display_, gc_, col(axis_color_));
XDrawRectangle(display_, window_, gc_, px, py, pw, ph);