Files
lightgui/widgets/components/XNetGraph/XNetGraph.h
Muse f5b3b73e58 XFrame monitor widgets: XHistoryGraph, XCoreGrid, XNetGraph, XTempHistory
Scrolling multi-series history graph (60s window, cur/min/max/avg
legend), per-core mini-graph grid, dual up/down network graph with
rates + totals, temperature history with threshold line + over-temp
colouring. Header-only C++, demo mains, examples, Xvfb screenshots.
2026-12-05 16:48:16 +00:00

259 lines
8.3 KiB
C++

#pragma once
#include "core/xframe_core.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <string>
#include <vector>
// region XNetGraph
//
// Dual up/down scrolling network graph (btop net box): two overlaid
// series in bytes/s, current rates plus running totals in the legend.
// Raw X11, same as the rest of the XFrame set.
//
class XNetGraph
{
private:
Display* display_;
int screen_;
Window window_;
GC gc_;
SStyle_Manager* styles_;
int x1_, y1_, x2_, y2_;
std::string name_;
std::string text_;
std::string style_name_;
bool visible_ = true;
bool enabled_ = true;
bool focused_ = false;
int capacity_ = 120;
std::vector<double> down_; // bytes/s
std::vector<double> up_; // bytes/s
double down_total_ = 0.0; // bytes
double up_total_ = 0.0; // bytes
double y_max_ = 0.0; // 0 = auto
Srgb white_color_{255, 255, 255};
Srgb grid_color_{225, 225, 225};
Srgb axis_color_{150, 150, 150};
Srgb down_color_{0, 170, 70};
Srgb up_color_{220, 80, 40};
unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); }
static std::string fmt_rate(double bps)
{
char buf[40];
if (bps < 1024.0) std::snprintf(buf, sizeof(buf), "%.0f B/s", bps);
else if (bps < 1024.0 * 1024.0) std::snprintf(buf, sizeof(buf), "%.1f KiB/s", bps / 1024.0);
else std::snprintf(buf, sizeof(buf), "%.2f MiB/s", bps / (1024.0 * 1024.0));
return buf;
}
static std::string fmt_total(double bytes)
{
char buf[40];
if (bytes < 1024.0) std::snprintf(buf, sizeof(buf), "%.0f B", bytes);
else if (bytes < 1024.0 * 1024.0) std::snprintf(buf, sizeof(buf), "%.1f KiB", bytes / 1024.0);
else if (bytes < 1024.0 * 1024.0 * 1024.0) std::snprintf(buf, sizeof(buf), "%.2f MiB", bytes / (1024.0 * 1024.0));
else std::snprintf(buf, sizeof(buf), "%.2f GiB", bytes / (1024.0 * 1024.0 * 1024.0));
return buf;
}
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());
}
void draw_series(const std::vector<double>& data, int px, int py, int pw, int ph, double hi, Srgb color)
{
if (data.size() < 2) return;
std::vector<XPoint> pts;
int n = (int)data.size();
for (int i = 0; i < n; ++i)
{
int cx = px + (int)((double)i / (capacity_ - 1) * pw);
int cy = py + ph - (int)(data[i] / hi * ph);
pts.push_back(XPoint{(short)cx, (short)cy});
}
XSetForeground(display_, gc_, col(color));
xf::set_line_width(display_, gc_, 2);
XDrawLines(display_, window_, gc_, pts.data(), (int)pts.size(), CoordModeOrigin);
xf::set_line_width(display_, gc_, 1);
}
public:
XNetGraph(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_ = "XNetGraph";
text_ = "Network";
// Preload a demo: bursty download, steadier upload (bytes/s).
unsigned int seed = 13579;
auto rnd = [&]() {
seed = seed * 1103515245 + 12345;
return ((seed >> 16) & 0x7fff) / 32767.0;
};
for (int i = 0; i < capacity_; ++i)
{
double t = (double)i / capacity_;
double burst = (std::sin(t * 12.0) > 0.2) ? 1.0 : 0.15;
push(180000.0 * burst * (0.5 + rnd()), 24000.0 * (0.6 + rnd() * 0.7));
}
}
~XNetGraph() {}
// 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 Net API
// Rates are bytes/s; each push counts as a 0.5 s sample for totals.
void push(double down_bps, double up_bps)
{
down_.push_back(std::max(0.0, down_bps));
up_.push_back(std::max(0.0, up_bps));
down_total_ += std::max(0.0, down_bps) * 0.5;
up_total_ += std::max(0.0, up_bps) * 0.5;
while ((int)down_.size() > capacity_) down_.erase(down_.begin());
while ((int)up_.size() > capacity_) up_.erase(up_.begin());
}
double down_rate() { return down_.empty() ? 0.0 : down_.back(); }
double up_rate() { return up_.empty() ? 0.0 : up_.back(); }
double down_total() { return down_total_; }
double up_total() { return up_total_; }
void reset_totals()
{
down_total_ = 0.0;
up_total_ = 0.0;
}
// endregion
void draw()
{
if (!visible_) return;
SStyle st = style();
int w = width();
int h = height();
XSetForeground(display_, gc_, col(white_color_));
XFillRectangle(display_, window_, gc_, x1_, y1_, w, h);
const int title_h = 20;
const int legend_h = 44;
const int axis_w = 52;
const int axis_h = 16;
int px = x1_ + axis_w;
int py = y1_ + title_h + legend_h + 4;
int pw = std::max(10, x2_ - 8 - px);
int ph = std::max(10, y2_ - axis_h - py);
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
double hi = y_max_;
if (hi <= 0.0)
{
hi = 1.0;
for (double v : down_) hi = std::max(hi, v);
for (double v : up_) hi = std::max(hi, v);
hi *= 1.05;
}
// Grid + y labels.
XSetForeground(display_, gc_, col(grid_color_));
for (int g = 0; g <= 4; ++g)
{
int gy = py + ph * g / 4;
XDrawLine(display_, window_, gc_, px, gy, px + pw, gy);
}
for (int g = 0; g <= 4; ++g)
{
double v = hi * (4 - g) / 4.0;
draw_text_color(px - 48, py + ph * g / 4 + 4, fmt_rate(v), Srgb{100, 100, 100});
}
draw_series(down_, px, py, pw, ph, hi, down_color_);
draw_series(up_, px, py, pw, ph, hi, up_color_);
// Legend: current rates + totals.
XSetForeground(display_, gc_, col(down_color_));
XFillRectangle(display_, window_, gc_, x1_ + 8, y1_ + title_h + 6, 18, 4);
xf::draw_text_at(display_, window_, gc_, st, x1_ + 32, y1_ + title_h + 16,
"down " + fmt_rate(down_rate()) + " total " + fmt_total(down_total_));
XSetForeground(display_, gc_, col(up_color_));
XFillRectangle(display_, window_, gc_, x1_ + 8, y1_ + title_h + 24, 18, 4);
xf::draw_text_at(display_, window_, gc_, st, x1_ + 32, y1_ + title_h + 34,
"up " + fmt_rate(up_rate()) + " total " + fmt_total(up_total_));
XSetForeground(display_, gc_, col(axis_color_));
XDrawRectangle(display_, window_, gc_, px, py, pw, ph);
XSetForeground(display_, gc_, st.lineColor_);
XDrawRectangle(display_, window_, gc_, x1_, y1_, w, h);
XFlush(display_);
}
bool on_button_press(int x, int y, unsigned int button)
{
(void)x;
(void)y;
(void)button;
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;
}
};