- 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.
413 lines
15 KiB
C++
413 lines
15 KiB
C++
#pragma once
|
|
|
|
#include "core/xframe_core.h"
|
|
#include "../InstData.h"
|
|
|
|
#include <algorithm>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <dirent.h>
|
|
#include <fstream>
|
|
#include <pwd.h>
|
|
#include <string>
|
|
#include <sys/types.h>
|
|
#include <unistd.h>
|
|
#include <vector>
|
|
|
|
// region XProcessList
|
|
//
|
|
// A live process table (btop proc box): sortable columns, an in-cell
|
|
// CPU% bar, colour-coded rows, auto-refresh from /proc. Click a column
|
|
// header to sort by it; click again to reverse. Raw X11, same as the
|
|
// rest of the XFrame set.
|
|
//
|
|
class XProcessList
|
|
{
|
|
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;
|
|
|
|
struct Row
|
|
{
|
|
int pid = 0;
|
|
std::string name;
|
|
std::string user;
|
|
double mem_mb = 0.0;
|
|
double cpu = 0.0; // lifetime average % of one core
|
|
};
|
|
|
|
std::vector<Row> rows_;
|
|
int sort_col_ = 4; // 0 pid, 1 name, 2 user, 3 mem, 4 cpu
|
|
bool sort_desc_ = true;
|
|
int max_rows_ = 40;
|
|
|
|
Srgb white_color_{255, 255, 255};
|
|
Srgb header_color_{235, 235, 235};
|
|
Srgb axis_color_{150, 150, 150};
|
|
Srgb bar_color_{0, 150, 60};
|
|
Srgb hot_row_{255, 235, 235};
|
|
|
|
unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); }
|
|
|
|
static std::string fmt1(double v)
|
|
{
|
|
char buf[32];
|
|
std::snprintf(buf, sizeof(buf), "%.1f", v);
|
|
return buf;
|
|
}
|
|
|
|
static std::string fmt_mem(double mb)
|
|
{
|
|
char buf[32];
|
|
if (mb >= 1024.0) std::snprintf(buf, sizeof(buf), "%.1fG", mb / 1024.0);
|
|
else if (mb >= 1.0) std::snprintf(buf, sizeof(buf), "%.0fM", mb);
|
|
else std::snprintf(buf, sizeof(buf), "%.0fK", mb * 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 sort_rows()
|
|
{
|
|
std::sort(rows_.begin(), rows_.end(), [&](const Row& a, const Row& b) {
|
|
int cmp = 0;
|
|
switch (sort_col_)
|
|
{
|
|
case 0: cmp = a.pid < b.pid ? -1 : (a.pid > b.pid ? 1 : 0); break;
|
|
case 1: cmp = a.name.compare(b.name); break;
|
|
case 2: cmp = a.user.compare(b.user); break;
|
|
case 3: cmp = a.mem_mb < b.mem_mb ? -1 : (a.mem_mb > b.mem_mb ? 1 : 0); break;
|
|
default: cmp = a.cpu < b.cpu ? -1 : (a.cpu > b.cpu ? 1 : 0); break;
|
|
}
|
|
return sort_desc_ ? cmp > 0 : cmp < 0;
|
|
});
|
|
}
|
|
|
|
// Column x-offsets (fractions of the inner width) for Pid/Program/User/MemB/Cpu%.
|
|
void col_xs(int inner_x, int inner_w, int xs[6]) const
|
|
{
|
|
xs[0] = inner_x;
|
|
xs[1] = inner_x + inner_w * 14 / 100;
|
|
xs[2] = inner_x + inner_w * 52 / 100;
|
|
xs[3] = inner_x + inner_w * 68 / 100;
|
|
xs[4] = inner_x + inner_w * 82 / 100;
|
|
xs[5] = inner_x + inner_w;
|
|
}
|
|
|
|
public:
|
|
XProcessList(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_ = "XProcessList";
|
|
text_ = "Processes";
|
|
refresh();
|
|
}
|
|
|
|
~XProcessList() {}
|
|
|
|
// 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 Process API
|
|
// Re-scan /proc and rebuild the table (call on a timer for live data).
|
|
void refresh()
|
|
{
|
|
rows_.clear();
|
|
double uptime = 0.0;
|
|
{
|
|
std::ifstream up("/proc/uptime");
|
|
if (up) up >> uptime;
|
|
}
|
|
long ticks = sysconf(_SC_CLK_TCK);
|
|
long page = sysconf(_SC_PAGESIZE);
|
|
if (ticks <= 0) ticks = 100;
|
|
if (page <= 0) page = 4096;
|
|
DIR* dir = opendir("/proc");
|
|
if (!dir) return;
|
|
struct dirent* ent;
|
|
while ((ent = readdir(dir)) != nullptr)
|
|
{
|
|
if (ent->d_name[0] < '0' || ent->d_name[0] > '9') continue;
|
|
int pid = std::atoi(ent->d_name);
|
|
std::string stat_path = std::string("/proc/") + ent->d_name + "/stat";
|
|
std::ifstream in(stat_path);
|
|
if (!in) continue;
|
|
std::string line;
|
|
std::getline(in, line);
|
|
size_t open = line.find('(');
|
|
size_t close = line.rfind(')');
|
|
if (open == std::string::npos || close == std::string::npos || close <= open) continue;
|
|
Row r;
|
|
r.pid = pid;
|
|
r.name = line.substr(open + 1, close - open - 1);
|
|
// Fields after ")": state ppid pgrp session tty_nr tpgid flags
|
|
// minflt cminflt majflt cmajflt utime stime cutime cstime
|
|
// priority nice num_threads itrealvalue starttime vsize rss.
|
|
unsigned long utime = 0, stime = 0, starttime = 0;
|
|
long rss = 0;
|
|
int scanned = std::sscanf(line.c_str() + close + 1,
|
|
"%*s %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %lu %lu %*d %*d %*d %*d %*d %*d %lu %*u %ld",
|
|
&utime, &stime, &starttime, &rss);
|
|
if (scanned < 4) continue;
|
|
double age = uptime - (double)starttime / ticks;
|
|
if (age > 1.0)
|
|
r.cpu = ((double)(utime + stime) / ticks) / age * 100.0;
|
|
r.mem_mb = (double)rss * page / (1024.0 * 1024.0);
|
|
// User from /proc/<pid>/status Uid line.
|
|
std::string status_path = std::string("/proc/") + ent->d_name + "/status";
|
|
std::ifstream st(status_path);
|
|
std::string key;
|
|
long uid = -1;
|
|
if (st)
|
|
{
|
|
while (st >> key)
|
|
{
|
|
if (key == "Uid:")
|
|
{
|
|
st >> uid;
|
|
break;
|
|
}
|
|
std::string skip;
|
|
std::getline(st, skip);
|
|
}
|
|
}
|
|
if (uid >= 0)
|
|
{
|
|
struct passwd* pw = getpwuid((uid_t)uid);
|
|
r.user = pw ? pw->pw_name : std::to_string(uid);
|
|
}
|
|
else
|
|
{
|
|
r.user = "?";
|
|
}
|
|
rows_.push_back(r);
|
|
if ((int)rows_.size() >= max_rows_ * 4) break; // bound the scan
|
|
}
|
|
closedir(dir);
|
|
sort_rows();
|
|
if ((int)rows_.size() > max_rows_) rows_.resize(max_rows_);
|
|
}
|
|
void set_sort_column(int c)
|
|
{
|
|
if (c < 0 || c > 4) return;
|
|
if (c == sort_col_) sort_desc_ = !sort_desc_;
|
|
else
|
|
{
|
|
sort_col_ = c;
|
|
sort_desc_ = (c == 3 || c == 4);
|
|
}
|
|
sort_rows();
|
|
}
|
|
int sort_column() { return sort_col_; }
|
|
int row_count() { return (int)rows_.size(); }
|
|
void set_max_rows(int n) { max_rows_ = n > 0 ? n : 10; }
|
|
// 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);
|
|
|
|
// Scaled fonts (Dec 5, 2026 proportions audit): title 16 px bold,
|
|
// "N procs" 13-14 px, header bold 13-14 px, rows 13 px at
|
|
// 22-24 px pitch. CPU bars colour-coded by value (Python scheme:
|
|
// green < 50, amber < 80, red >= 80).
|
|
int title_px = xframe::clamp_font_px(0.033 * h, 11, 16);
|
|
int head_px = xframe::clamp_font_px(0.03 * h, 11, 14);
|
|
const int row_px = 13;
|
|
XFontStruct* title_font = xframe::scaled_font(display_, title_px, true);
|
|
if (!title_font) title_font = st.font_;
|
|
XFontStruct* head_font = xframe::scaled_font(display_, head_px, true);
|
|
if (!head_font) head_font = st.font_;
|
|
XFontStruct* row_font = xframe::scaled_font(display_, row_px, false);
|
|
if (!row_font) row_font = st.font_;
|
|
XFontStruct* count_font = xframe::scaled_font(display_, head_px, false);
|
|
if (!count_font) count_font = st.font_;
|
|
int title_asc = title_font ? title_font->ascent : 11;
|
|
int head_asc = head_font ? head_font->ascent : 11;
|
|
int head_desc = head_font ? head_font->descent : 2;
|
|
int row_asc = row_font ? row_font->ascent : 11;
|
|
int row_desc = row_font ? row_font->descent : 2;
|
|
|
|
const int title_h = title_px + 8;
|
|
const int header_h = head_px + 10;
|
|
const int row_h = 23;
|
|
int ix = x1_ + 6;
|
|
int iw = w - 12;
|
|
int xs[6];
|
|
col_xs(ix, iw, xs);
|
|
const char* heads[5] = {"Pid", "Program", "User", "MemB", "Cpu%"};
|
|
|
|
xframe::draw_text_font(display_, window_, gc_, title_font, x1_ + 8,
|
|
y1_ + 4 + title_asc, text_, col(Srgb{20, 20, 20}));
|
|
std::string count = std::to_string(rows_.size()) + " procs";
|
|
xframe::draw_text_font(display_, window_, gc_, count_font,
|
|
x2_ - 8 - xframe::font_text_width(count_font, count),
|
|
y1_ + 4 + title_asc, count, col(Srgb{20, 20, 20}));
|
|
|
|
// Header row.
|
|
int hy = y1_ + title_h;
|
|
XSetForeground(display_, gc_, col(header_color_));
|
|
XFillRectangle(display_, window_, gc_, ix, hy, iw, header_h);
|
|
int head_base = hy + (header_h + head_asc - head_desc) / 2;
|
|
for (int c = 0; c < 5; ++c)
|
|
{
|
|
std::string head = heads[c];
|
|
if (c == sort_col_) head += sort_desc_ ? " v" : " ^";
|
|
xframe::draw_text_font(display_, window_, gc_, head_font, xs[c] + 2,
|
|
head_base, head, col(Srgb{20, 20, 20}));
|
|
}
|
|
XSetForeground(display_, gc_, col(axis_color_));
|
|
XDrawLine(display_, window_, gc_, ix, hy + header_h, ix + iw, hy + header_h);
|
|
|
|
// Rows.
|
|
int y = hy + header_h;
|
|
int shown = 0;
|
|
int max_fit = (y2_ - 6 - y) / row_h;
|
|
int row_base_off = (row_h + row_asc - row_desc) / 2;
|
|
for (auto& r : rows_)
|
|
{
|
|
if (shown >= max_fit) break;
|
|
int ry = y + shown * row_h;
|
|
if (r.cpu >= 50.0)
|
|
{
|
|
XSetForeground(display_, gc_, col(hot_row_));
|
|
XFillRectangle(display_, window_, gc_, ix, ry, iw, row_h);
|
|
}
|
|
else if (shown % 2 == 1)
|
|
{
|
|
XSetForeground(display_, gc_, col(Srgb{248, 248, 248}));
|
|
XFillRectangle(display_, window_, gc_, ix, ry, iw, row_h);
|
|
}
|
|
xframe::draw_text_font(display_, window_, gc_, row_font, xs[0] + 2,
|
|
ry + row_base_off, std::to_string(r.pid),
|
|
col(Srgb{20, 20, 20}));
|
|
std::string prog = r.name.substr(0, 28);
|
|
xframe::draw_text_font(display_, window_, gc_, row_font, xs[1] + 2,
|
|
ry + row_base_off, prog, col(Srgb{20, 20, 20}));
|
|
xframe::draw_text_font(display_, window_, gc_, row_font, xs[2] + 2,
|
|
ry + row_base_off, r.user.substr(0, 10),
|
|
col(Srgb{20, 20, 20}));
|
|
xframe::draw_text_font(display_, window_, gc_, row_font, xs[3] + 2,
|
|
ry + row_base_off, fmt_mem(r.mem_mb), col(Srgb{20, 20, 20}));
|
|
// In-cell CPU% bar AFTER the number: drawing the bar first put
|
|
// it under the first digit (dark-on-green, e.g. "10.2" lost
|
|
// its "1" behind the bar).
|
|
int bar_x = xs[4] + 2;
|
|
std::string cpu_txt = fmt1(r.cpu);
|
|
int cpu_tw = xframe::font_text_width(row_font, cpu_txt);
|
|
xframe::draw_text_font(display_, window_, gc_, row_font, bar_x + 2,
|
|
ry + row_base_off, cpu_txt, col(Srgb{20, 20, 20}));
|
|
int bar_x2 = bar_x + 4 + cpu_tw;
|
|
int bar_w2 = xs[5] - bar_x2 - 4;
|
|
int fill = (int)(bar_w2 * std::min(100.0, r.cpu) / 100.0);
|
|
if (fill > 0)
|
|
{
|
|
Srgb bar_col = r.cpu >= 80.0 ? Srgb{210, 40, 30}
|
|
: r.cpu >= 50.0 ? Srgb{220, 180, 0} : bar_color_;
|
|
XSetForeground(display_, gc_, col(bar_col));
|
|
XFillRectangle(display_, window_, gc_, bar_x2, ry + (row_h - 12) / 2, fill, 12);
|
|
}
|
|
++shown;
|
|
}
|
|
|
|
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)
|
|
{
|
|
if (button != 1 || !contains(x, y)) return false;
|
|
const int title_h = xframe::clamp_font_px(0.033 * height(), 11, 16) + 8;
|
|
const int header_h = xframe::clamp_font_px(0.03 * height(), 11, 14) + 10;
|
|
int hy = y1_ + title_h;
|
|
if (y >= hy && y < hy + header_h)
|
|
{
|
|
int ix = x1_ + 6;
|
|
int iw = width() - 12;
|
|
int xs[6];
|
|
col_xs(ix, iw, xs);
|
|
for (int c = 0; c < 5; ++c)
|
|
{
|
|
if (x >= xs[c] && x < xs[c + 1])
|
|
{
|
|
set_sort_column(c);
|
|
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;
|
|
}
|
|
};
|