Files
lightgui/widgets/components/XProcessList/XProcessList.h
T
Muse 8c295dbbdf XFrame monitor widgets: XProcessList, XLoadBadge, XMonitorButton, XMonitorGrid, XSysBox
Live process table from /proc (sortable columns, in-cell CPU% bars,
colour-coded rows, refresh()), load-average badge (1/5/15 min,
threshold colours, uptime from /proc), monitor button bound to a
process (/proc scan), systemd unit (systemctl is-active) or TCP port
(connect to localhost) with poll()/update() and click-for-details,
grid panel of monitor buttons with one shared poll_all(), composite
resource box (title + big number + history + usage bar). Header-only
C++, demo mains (polling loop for the live three), examples, Xvfb
screenshots.
2026-12-05 16:49:29 +00:00

371 lines
12 KiB
C++

#pragma once
#include "core/xframe_core.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);
const int title_h = 20;
const int header_h = 18;
const int row_h = 16;
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%"};
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
std::string count = std::to_string(rows_.size()) + " procs";
xf::draw_text_at(display_, window_, gc_, st, x2_ - 8 - xf::text_width(st, count), y1_ + 15, count);
// Header row.
int hy = y1_ + title_h;
XSetForeground(display_, gc_, col(header_color_));
XFillRectangle(display_, window_, gc_, ix, hy, iw, header_h);
for (int c = 0; c < 5; ++c)
{
std::string head = heads[c];
if (c == sort_col_) head += sort_desc_ ? " v" : " ^";
xf::draw_text_at(display_, window_, gc_, st, xs[c] + 2, hy + 13, head);
}
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;
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);
}
xf::draw_text_at(display_, window_, gc_, st, xs[0] + 2, ry + 12, std::to_string(r.pid));
std::string prog = r.name.substr(0, 28);
xf::draw_text_at(display_, window_, gc_, st, xs[1] + 2, ry + 12, prog);
xf::draw_text_at(display_, window_, gc_, st, xs[2] + 2, ry + 12, r.user.substr(0, 10));
xf::draw_text_at(display_, window_, gc_, st, xs[3] + 2, ry + 12, fmt_mem(r.mem_mb));
// In-cell CPU% bar behind the number.
int bar_x = xs[4] + 2;
int bar_w = xs[5] - bar_x - 4;
int fill = (int)(bar_w * std::min(100.0, r.cpu) / 100.0);
if (fill > 0)
{
XSetForeground(display_, gc_, col(r.cpu >= 50.0 ? Srgb{210, 40, 30} : bar_color_));
XFillRectangle(display_, window_, gc_, bar_x, ry + 3, fill, row_h - 6);
}
draw_text_color(bar_x + 2, ry + 12, fmt1(r.cpu), Srgb{20, 20, 20});
++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 = 20;
const int header_h = 18;
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;
}
};