- Graphs (XHistoryGraph/XNetGraph/XTempHistory): dynamic left margin from widest Y label (fixes Y labels overflowing the plot), legend wrapped to two lines when too wide, legend on top, 5 Y labels, humanised units in NetGraph, TempHistory on the PY layout (now top-right, min/max/avg bottom, threshold label above the line). Top/bottom Y labels hang clear of the title and the strip below the plot. - XTachometer: numerals 7.5% of dial diameter bold just inside the tick ring, RPM bold centred in-dial, readout enlarged below the hub, redline numerals in red. - All 14 audit widgets: font sizes derived from widget dimensions per the proportions audit (titles 16px bold, hero values 24-48px bold, body/legend 13-16px, min 11px); CoreGrid 4x2 for 8 cores; UsageBar labels outside the bar; StackBar block centred with grown bar; VerticalBar value inside the bar; ArcGauge centre 40-48px + load sub-label + endpoints; DonutGauge centre 36-44px, ring 8-10% of diameter, duplicate title dropped; ProcessList 13px rows, colour-coded CPU bars after the number; LoadBadge centred value block with 1/5/15 sub-labels; MonitorButton scaled label/dot/status + detail line; MonitorGrid ceil(sqrt(n)) columns; SysBox hero 28-32px + max + bar %. - New xframe::scaled_font() helper in InstData.h (scalable misc-fixed XLFD, cached per size/bold) + font_text_width/draw_text_font. - Deviations from the audit spec: UsageBar label capped at 40px (the literal 0.45x rule gives 209px in the 464px-tall demo bar); SysBox hero uses 10% of height clamped 20-32px (spec 6-7% conflicts with the audited-correct PY value of 10.3%). All 15 widget demos rebuild clean and were screenshot-verified.
356 lines
12 KiB
C++
356 lines
12 KiB
C++
#pragma once
|
|
|
|
#include "core/xframe_core.h"
|
|
#include "../InstData.h"
|
|
|
|
#include <algorithm>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <dirent.h>
|
|
#include <string>
|
|
#include <sys/socket.h>
|
|
#include <sys/types.h>
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
#include <unistd.h>
|
|
|
|
// region XMonitorButton
|
|
//
|
|
// A button that IS a live monitor: bound to a process running (by name),
|
|
// a systemd unit active (systemctl is-active), or a TCP port open.
|
|
// Glows green when up, red when down, dim when unknown; label + status
|
|
// text; polls on a timer via poll(); click polls now and shows details.
|
|
// Checks use stdlib / syscalls only. Raw X11, same as the XFrame set.
|
|
//
|
|
class XMonitorButton
|
|
{
|
|
public:
|
|
enum Kind
|
|
{
|
|
Process = 0, // target = process name (comm)
|
|
Unit = 1, // target = systemd unit, e.g. "sshd.service"
|
|
Port = 2 // target = TCP port number, e.g. "22"
|
|
};
|
|
enum MonitorStatus
|
|
{
|
|
MonUnknown = 0,
|
|
MonUp = 1,
|
|
MonDown = 2
|
|
};
|
|
|
|
private:
|
|
Display* display_;
|
|
int screen_;
|
|
Window window_;
|
|
GC gc_;
|
|
SStyle_Manager* styles_;
|
|
int x1_, y1_, x2_, y2_;
|
|
std::string name_;
|
|
std::string text_; // label
|
|
std::string style_name_;
|
|
bool visible_ = true;
|
|
bool enabled_ = true;
|
|
bool focused_ = false;
|
|
|
|
Kind kind_ = Port;
|
|
std::string target_ = "22";
|
|
MonitorStatus status_ = MonUnknown;
|
|
std::string details_;
|
|
bool show_details_ = false;
|
|
|
|
Srgb white_color_{255, 255, 255};
|
|
Srgb up_color_{0, 170, 70};
|
|
Srgb down_color_{210, 40, 30};
|
|
Srgb unknown_color_{150, 150, 150};
|
|
|
|
unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); }
|
|
|
|
void draw_text_color(int x, int baseline_y, const std::string& s, Srgb color)
|
|
{
|
|
SStyle st = style();
|
|
if (!st.font_ || s.empty()) return;
|
|
XSetFont(display_, gc_, st.font_->fid);
|
|
XSetForeground(display_, gc_, col(color));
|
|
XDrawString(display_, window_, gc_, x, baseline_y, s.c_str(), (int)s.size());
|
|
}
|
|
|
|
bool check_process(const std::string& name, int& found_pid)
|
|
{
|
|
DIR* dir = opendir("/proc");
|
|
if (!dir) return false;
|
|
bool found = false;
|
|
struct dirent* ent;
|
|
while ((ent = readdir(dir)) != nullptr)
|
|
{
|
|
if (ent->d_name[0] < '0' || ent->d_name[0] > '9') continue;
|
|
std::string comm_path = std::string("/proc/") + ent->d_name + "/comm";
|
|
FILE* f = std::fopen(comm_path.c_str(), "r");
|
|
if (!f) continue;
|
|
char buf[256];
|
|
if (std::fgets(buf, sizeof(buf), f))
|
|
{
|
|
buf[std::strcspn(buf, "\n")] = 0;
|
|
if (name == buf)
|
|
{
|
|
found = true;
|
|
found_pid = std::atoi(ent->d_name);
|
|
std::fclose(f);
|
|
break;
|
|
}
|
|
}
|
|
std::fclose(f);
|
|
}
|
|
closedir(dir);
|
|
return found;
|
|
}
|
|
|
|
bool check_unit(const std::string& unit, std::string& out)
|
|
{
|
|
// Only safe unit-name characters reach the shell.
|
|
for (char c : unit)
|
|
{
|
|
bool ok = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
|
|
(c >= '0' && c <= '9') || c == '@' || c == '.' || c == '_' || c == '-';
|
|
if (!ok) return false;
|
|
}
|
|
std::string cmd = "systemctl is-active " + unit + " 2>/dev/null";
|
|
FILE* p = popen(cmd.c_str(), "r");
|
|
if (!p) return false;
|
|
char buf[128];
|
|
bool active = false;
|
|
if (std::fgets(buf, sizeof(buf), p))
|
|
{
|
|
buf[std::strcspn(buf, "\n")] = 0;
|
|
out = buf;
|
|
active = (out == "active");
|
|
}
|
|
pclose(p);
|
|
return active;
|
|
}
|
|
|
|
bool check_port(int port)
|
|
{
|
|
int fd = socket(AF_INET, SOCK_STREAM, 0);
|
|
if (fd < 0) return false;
|
|
struct timeval tv;
|
|
tv.tv_sec = 1;
|
|
tv.tv_usec = 0;
|
|
setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv));
|
|
struct sockaddr_in addr;
|
|
std::memset(&addr, 0, sizeof(addr));
|
|
addr.sin_family = AF_INET;
|
|
addr.sin_port = htons((uint16_t)port);
|
|
addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
|
|
bool open = connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == 0;
|
|
close(fd);
|
|
return open;
|
|
}
|
|
|
|
public:
|
|
XMonitorButton(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles, int x, int y, int width, int height)
|
|
{
|
|
display_ = display;
|
|
screen_ = screen;
|
|
window_ = window;
|
|
gc_ = gc;
|
|
styles_ = styles;
|
|
x1_ = x;
|
|
y1_ = y;
|
|
x2_ = x + width;
|
|
y2_ = y + height;
|
|
name_ = "XMonitorButton";
|
|
text_ = "sshd :22";
|
|
poll();
|
|
}
|
|
|
|
~XMonitorButton() {}
|
|
|
|
// region Standard plumbing
|
|
void set_bounds(int x, int y, int width, int height)
|
|
{
|
|
x1_ = x;
|
|
y1_ = y;
|
|
x2_ = x + width;
|
|
y2_ = y + height;
|
|
}
|
|
int width() { return x2_ - x1_; }
|
|
int height() { return y2_ - y1_; }
|
|
bool contains(int x, int y)
|
|
{
|
|
return visible_ && x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_;
|
|
}
|
|
void set_visible(bool v) { visible_ = v; }
|
|
bool get_visible() { return visible_; }
|
|
void set_enabled(bool e) { enabled_ = e; }
|
|
bool get_enabled() { return enabled_; }
|
|
void set_style_name(std::string s) { style_name_ = s; }
|
|
void set_text(std::string t) { text_ = t; }
|
|
std::string get_text() { return text_; }
|
|
SStyle style() { return styles_->resolve(style_name_); }
|
|
// endregion
|
|
|
|
// region Monitor API
|
|
void set_kind(Kind k) { kind_ = k; }
|
|
Kind get_kind() { return kind_; }
|
|
void set_target(const std::string& t) { target_ = t; }
|
|
std::string get_target() { return target_; }
|
|
MonitorStatus status() { return status_; }
|
|
std::string status_text()
|
|
{
|
|
switch (status_)
|
|
{
|
|
case MonUp: return "UP";
|
|
case MonDown: return "DOWN";
|
|
default: return "UNKNOWN";
|
|
}
|
|
}
|
|
std::string details() { return details_; }
|
|
|
|
// Run one check now; call on a timer for live monitoring.
|
|
void poll()
|
|
{
|
|
bool ok = false;
|
|
if (kind_ == Process)
|
|
{
|
|
int pid = 0;
|
|
ok = check_process(target_, pid);
|
|
details_ = ok ? ("pid " + std::to_string(pid)) : "not running";
|
|
}
|
|
else if (kind_ == Unit)
|
|
{
|
|
std::string out;
|
|
ok = check_unit(target_, out);
|
|
details_ = out.empty() ? "no result" : out;
|
|
}
|
|
else
|
|
{
|
|
int port = std::atoi(target_.c_str());
|
|
if (port <= 0 || port > 65535)
|
|
{
|
|
status_ = MonUnknown;
|
|
details_ = "bad port";
|
|
return;
|
|
}
|
|
ok = check_port(port);
|
|
details_ = ok ? "open" : "closed";
|
|
}
|
|
status_ = ok ? MonUp : MonDown;
|
|
}
|
|
// Backwards-compatible name: update() polls once.
|
|
void update() { poll(); }
|
|
// endregion
|
|
|
|
void draw()
|
|
{
|
|
if (!visible_) return;
|
|
SStyle st = style();
|
|
int w = width();
|
|
int h = height();
|
|
Srgb state = status_ == MonUp ? up_color_ : (status_ == MonDown ? down_color_ : unknown_color_);
|
|
|
|
// Scaled content (Dec 5, 2026 proportions audit): label 20-24 px
|
|
// bold, dot 24-32 px, status 20 px bold, sub-label (details_:
|
|
// "open" / "pid N" / ...) under the label. The content block is
|
|
// vertically centred. Sizes derive from the button's own height
|
|
// (one rule serves the standalone button and the grid cells,
|
|
// whose demo heights are 480 px and ~180 px).
|
|
int label_px = xframe::clamp_font_px(0.10 * h, 13, 24);
|
|
int dot_d = xframe::clamp_font_px(0.10 * h, 12, 30);
|
|
int status_px = xframe::clamp_font_px(0.09 * h, 12, 20);
|
|
int detail_px = xframe::clamp_font_px(0.06 * h, 11, 14);
|
|
XFontStruct* label_font = xframe::scaled_font(display_, label_px, true);
|
|
if (!label_font) label_font = st.font_;
|
|
XFontStruct* status_font = xframe::scaled_font(display_, status_px, true);
|
|
if (!status_font) status_font = st.font_;
|
|
XFontStruct* detail_font = xframe::scaled_font(display_, detail_px, false);
|
|
if (!detail_font) detail_font = st.font_;
|
|
int label_asc = label_font ? label_font->ascent : 11;
|
|
int detail_asc = detail_font ? detail_font->ascent : 11;
|
|
|
|
// Body: white, thick status-coloured border = the glow.
|
|
XSetForeground(display_, gc_, col(white_color_));
|
|
XFillRectangle(display_, window_, gc_, x1_, y1_, w, h);
|
|
XSetForeground(display_, gc_, col(state));
|
|
xf::set_line_width(display_, gc_, 3);
|
|
XDrawRectangle(display_, window_, gc_, x1_ + 1, y1_ + 1, w - 2, h - 2);
|
|
xf::set_line_width(display_, gc_, 1);
|
|
|
|
int block_h = label_px + 6 + detail_px;
|
|
int block_top = y1_ + std::max(4, (h - block_h) / 2);
|
|
int label_base = block_top + label_asc;
|
|
int detail_base = block_top + label_px + 6 + detail_asc;
|
|
|
|
// Status dot, centred on the content block.
|
|
int dot_r = dot_d / 2;
|
|
int dot_cx = x1_ + 8 + dot_r;
|
|
int dot_cy = block_top + block_h / 2;
|
|
XSetForeground(display_, gc_, col(state));
|
|
XFillArc(display_, window_, gc_, dot_cx - dot_r, dot_cy - dot_r, 2 * dot_r, 2 * dot_r, 0, 360 * 64);
|
|
|
|
// Label + status text on the first line.
|
|
std::string stxt = status_text();
|
|
int sw = xframe::font_text_width(status_font, stxt);
|
|
int label_x = dot_cx + dot_r + 10;
|
|
int label_avail = std::max(10, x2_ - 10 - sw - 8 - label_x);
|
|
std::string label = text_;
|
|
int label_w = xframe::font_text_width(label_font, label);
|
|
if (label_w > label_avail)
|
|
{
|
|
int cw = label_font ? label_font->max_bounds.width : 8;
|
|
int keep = std::max(0, label_avail / std::max(1, cw) - 3);
|
|
label = label.substr(0, (size_t)keep) + "...";
|
|
}
|
|
xframe::draw_text_font(display_, window_, gc_, label_font, label_x, label_base,
|
|
label, col(Srgb{20, 20, 20}));
|
|
xframe::draw_text_font(display_, window_, gc_, status_font, x2_ - 10 - sw,
|
|
label_base, stxt, col(state));
|
|
|
|
// Sub-label under the label (always shown now, not click-gated).
|
|
std::string detail = details_;
|
|
int detail_avail = std::max(10, x2_ - 10 - label_x);
|
|
int detail_w = xframe::font_text_width(detail_font, detail);
|
|
if (detail_w > detail_avail)
|
|
{
|
|
int cw = detail_font ? detail_font->max_bounds.width : 8;
|
|
int keep = std::max(0, detail_avail / std::max(1, cw) - 3);
|
|
detail = detail.substr(0, (size_t)keep) + "...";
|
|
}
|
|
xframe::draw_text_font(display_, window_, gc_, detail_font, label_x, detail_base,
|
|
detail, col(Srgb{100, 100, 100}));
|
|
|
|
XFlush(display_);
|
|
}
|
|
|
|
bool on_button_press(int x, int y, unsigned int button)
|
|
{
|
|
if (button == 1 && contains(x, y))
|
|
{
|
|
// The detail sub-label is always shown now (Dec 5, 2026
|
|
// proportions audit); a click just re-polls.
|
|
poll();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
bool on_button_release(int x, int y, unsigned int button)
|
|
{
|
|
(void)x;
|
|
(void)y;
|
|
(void)button;
|
|
return false;
|
|
}
|
|
bool on_motion(int x, int y)
|
|
{
|
|
(void)x;
|
|
(void)y;
|
|
return false;
|
|
}
|
|
bool on_key(KeySym key, const char* str)
|
|
{
|
|
(void)key;
|
|
(void)str;
|
|
return false;
|
|
}
|
|
};
|