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.
220 lines
6.3 KiB
C++
220 lines
6.3 KiB
C++
#pragma once
|
|
|
|
#include "core/xframe_core.h"
|
|
|
|
#include <algorithm>
|
|
#include <cstdio>
|
|
#include <fstream>
|
|
#include <string>
|
|
#include <unistd.h>
|
|
|
|
// region XLoadBadge
|
|
//
|
|
// A load-average badge (1 / 5 / 15 min) with colour thresholds and
|
|
// uptime, in the btop header style. Reads /proc/loadavg + /proc/uptime
|
|
// on refresh(). Raw X11, same as the rest of the XFrame set.
|
|
//
|
|
class XLoadBadge
|
|
{
|
|
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;
|
|
|
|
double load1_ = 0.0;
|
|
double load5_ = 0.0;
|
|
double load15_ = 0.0;
|
|
long uptime_secs_ = 0;
|
|
int ncpu_ = 1;
|
|
|
|
Srgb white_color_{255, 255, 255};
|
|
Srgb axis_color_{150, 150, 150};
|
|
Srgb ok_color_{0, 170, 70};
|
|
Srgb warn_color_{200, 170, 0};
|
|
Srgb crit_color_{210, 40, 30};
|
|
|
|
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), "%.2f", v);
|
|
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());
|
|
}
|
|
|
|
Srgb state_color() const
|
|
{
|
|
double per_core = ncpu_ > 0 ? load1_ / ncpu_ : load1_;
|
|
if (per_core < 0.7) return ok_color_;
|
|
if (per_core < 1.5) return warn_color_;
|
|
return crit_color_;
|
|
}
|
|
|
|
public:
|
|
XLoadBadge(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_ = "XLoadBadge";
|
|
text_ = "Load avg";
|
|
long n = sysconf(_SC_NPROCESSORS_ONLN);
|
|
ncpu_ = n > 0 ? (int)n : 1;
|
|
refresh();
|
|
if (load1_ == 0.0 && load5_ == 0.0 && load15_ == 0.0 && uptime_secs_ == 0)
|
|
{
|
|
// No /proc (non-Linux): demo values so the widget still shows.
|
|
load1_ = 2.09;
|
|
load5_ = 1.85;
|
|
load15_ = 1.60;
|
|
uptime_secs_ = 2 * 86400 + 14 * 3600 + 21 * 60;
|
|
}
|
|
}
|
|
|
|
~XLoadBadge() {}
|
|
|
|
// 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 Load API
|
|
void refresh()
|
|
{
|
|
std::ifstream in("/proc/loadavg");
|
|
if (in) in >> load1_ >> load5_ >> load15_;
|
|
std::ifstream up("/proc/uptime");
|
|
double secs = 0.0;
|
|
if (up && (up >> secs)) uptime_secs_ = (long)secs;
|
|
}
|
|
void set_loads(double l1, double l5, double l15)
|
|
{
|
|
load1_ = l1;
|
|
load5_ = l5;
|
|
load15_ = l15;
|
|
}
|
|
void set_uptime_secs(long s) { uptime_secs_ = s; }
|
|
std::string uptime_string()
|
|
{
|
|
long s = uptime_secs_;
|
|
long d = s / 86400;
|
|
long h = (s % 86400) / 3600;
|
|
long m = (s % 3600) / 60;
|
|
char buf[48];
|
|
if (d > 0) std::snprintf(buf, sizeof(buf), "up %ldd %02ld:%02ld", d, h, m);
|
|
else std::snprintf(buf, sizeof(buf), "up %02ld:%02ld", h, m);
|
|
return buf;
|
|
}
|
|
// 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);
|
|
|
|
Srgb state = state_color();
|
|
// Left status stripe.
|
|
XSetForeground(display_, gc_, col(state));
|
|
XFillRectangle(display_, window_, gc_, x1_, y1_, 8, h);
|
|
|
|
xf::draw_text_at(display_, window_, gc_, st, x1_ + 16, y1_ + 15, text_);
|
|
std::string cpus = std::to_string(ncpu_) + " cores";
|
|
xf::draw_text_at(display_, window_, gc_, st, x2_ - 8 - xf::text_width(st, cpus), y1_ + 15, cpus);
|
|
|
|
// Big load numbers: 1 / 5 / 15 min.
|
|
std::string loads = fmt1(load1_) + " " + fmt1(load5_) + " " + fmt1(load15_);
|
|
int lw = xf::text_width(st, loads);
|
|
draw_text_color(x1_ + (w - lw) / 2, y1_ + 42, loads, state);
|
|
draw_text_color(x1_ + (w - lw) / 2 + 1, y1_ + 42, loads, state);
|
|
|
|
std::string labels = "1 min 5 min 15 min";
|
|
int tw = xf::text_width(st, labels);
|
|
xf::draw_text_at(display_, window_, gc_, st, x1_ + (w - tw) / 2, y1_ + 58, labels);
|
|
|
|
// Uptime line.
|
|
std::string up = uptime_string();
|
|
int uw = xf::text_width(st, up);
|
|
xf::draw_text_at(display_, window_, gc_, st, x1_ + (w - uw) / 2, y2_ - 8, up);
|
|
|
|
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))
|
|
{
|
|
refresh();
|
|
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;
|
|
}
|
|
};
|