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.
243 lines
7.2 KiB
C++
243 lines
7.2 KiB
C++
#pragma once
|
|
|
|
#include "core/xframe_core.h"
|
|
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstdio>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
// region XCoreGrid
|
|
//
|
|
// A grid of mini history graphs, one per CPU core (Task Manager per-core
|
|
// view): each cell shows its core label, current % and a scrolling line
|
|
// of recent samples. Raw X11, same as the rest of the XFrame set.
|
|
//
|
|
class XCoreGrid
|
|
{
|
|
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 cores_ = 8;
|
|
int capacity_ = 60;
|
|
std::vector<std::vector<double>> history_;
|
|
std::vector<double> current_;
|
|
|
|
Srgb white_color_{255, 255, 255};
|
|
Srgb grid_color_{225, 225, 225};
|
|
Srgb axis_color_{150, 150, 150};
|
|
|
|
unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); }
|
|
|
|
static Srgb color_for(double pct)
|
|
{
|
|
if (pct < 50.0) return Srgb{0, 170, 70};
|
|
if (pct < 80.0) return Srgb{200, 170, 0};
|
|
return Srgb{210, 40, 30};
|
|
}
|
|
|
|
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 ensure_cores()
|
|
{
|
|
if ((int)history_.size() != cores_)
|
|
{
|
|
history_.assign(cores_, std::vector<double>());
|
|
current_.assign(cores_, 0.0);
|
|
}
|
|
}
|
|
|
|
public:
|
|
XCoreGrid(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_ = "XCoreGrid";
|
|
text_ = "CPU cores";
|
|
ensure_cores();
|
|
|
|
// Preload a demo: 8 cores, seeded sine + noise at different phases.
|
|
unsigned int seed = 424242;
|
|
auto rnd = [&]() {
|
|
seed = seed * 1103515245 + 12345;
|
|
return ((seed >> 16) & 0x7fff) / 32767.0;
|
|
};
|
|
for (int i = 0; i < capacity_; ++i)
|
|
{
|
|
double t = (double)i / capacity_;
|
|
for (int c = 0; c < cores_; ++c)
|
|
{
|
|
double v = 45.0 + 38.0 * std::sin(t * 7.0 + c * 1.3) + (rnd() - 0.5) * 22.0;
|
|
push(c, std::max(0.0, std::min(100.0, v)));
|
|
}
|
|
}
|
|
}
|
|
|
|
~XCoreGrid() {}
|
|
|
|
// 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 Core API
|
|
void set_core_count(int n)
|
|
{
|
|
cores_ = n > 0 ? n : 1;
|
|
ensure_cores();
|
|
}
|
|
int core_count() { return cores_; }
|
|
void push(int core, double pct)
|
|
{
|
|
if (core < 0 || core >= cores_) return;
|
|
ensure_cores();
|
|
pct = std::max(0.0, std::min(100.0, pct));
|
|
history_[core].push_back(pct);
|
|
while ((int)history_[core].size() > capacity_) history_[core].erase(history_[core].begin());
|
|
current_[core] = pct;
|
|
}
|
|
void push_all(const std::vector<double>& pcts)
|
|
{
|
|
for (size_t i = 0; i < pcts.size() && (int)i < cores_; ++i) push((int)i, pcts[i]);
|
|
}
|
|
double core_value(int core)
|
|
{
|
|
if (core < 0 || core >= cores_) return 0.0;
|
|
return current_[core];
|
|
}
|
|
// 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);
|
|
ensure_cores();
|
|
|
|
const int title_h = 20;
|
|
int cols = (int)std::ceil(std::sqrt((double)cores_));
|
|
int rows = (cores_ + cols - 1) / cols;
|
|
int cell_w = (w - 8) / cols;
|
|
int cell_h = (h - title_h - 8) / rows;
|
|
|
|
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
|
|
|
|
for (int c = 0; c < cores_; ++c)
|
|
{
|
|
int cx = x1_ + 4 + (c % cols) * cell_w;
|
|
int cy = y1_ + title_h + 4 + (c / cols) * cell_h;
|
|
int cw = cell_w - 4;
|
|
int ch = cell_h - 4;
|
|
// Cell frame.
|
|
XSetForeground(display_, gc_, col(axis_color_));
|
|
XDrawRectangle(display_, window_, gc_, cx, cy, cw, ch);
|
|
// Label + current %.
|
|
char label[32];
|
|
std::snprintf(label, sizeof(label), "C%d %.0f%%", c, current_[c]);
|
|
draw_text_color(cx + 5, cy + 14, label, color_for(current_[c]));
|
|
// Mini history graph.
|
|
int gx = cx + 4;
|
|
int gy = cy + 18;
|
|
int gw = std::max(10, cw - 8);
|
|
int gh = std::max(6, ch - 22);
|
|
XSetForeground(display_, gc_, col(grid_color_));
|
|
XDrawLine(display_, window_, gc_, gx, gy + gh / 2, gx + gw, gy + gh / 2);
|
|
auto& hist = history_[c];
|
|
if (hist.size() >= 2)
|
|
{
|
|
std::vector<XPoint> pts;
|
|
int n = (int)hist.size();
|
|
for (int i = 0; i < n; ++i)
|
|
{
|
|
int px = gx + (int)((double)i / (capacity_ - 1) * gw);
|
|
int py = gy + gh - (int)(hist[i] / 100.0 * gh);
|
|
pts.push_back(XPoint{(short)px, (short)py});
|
|
}
|
|
XSetForeground(display_, gc_, col(color_for(current_[c])));
|
|
xf::set_line_width(display_, gc_, 2);
|
|
XDrawLines(display_, window_, gc_, pts.data(), (int)pts.size(), CoordModeOrigin);
|
|
xf::set_line_width(display_, gc_, 1);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
};
|