Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1c0756ecfc | ||
|
|
7c4b0a0aef | ||
|
|
f275d07075 | ||
|
|
a6177a7a1a | ||
|
|
74bcee9702 | ||
|
|
3340f29ac4 | ||
|
|
d117e77915 | ||
|
|
fc8db40d0d | ||
|
|
25299f8e24 | ||
|
|
8ba8fc5737 | ||
|
|
e17a7fd4b8 | ||
|
|
82def11311 | ||
|
|
b962038f50 | ||
|
|
f97cfbbf9c | ||
|
|
9bf5a7ce51 | ||
|
|
8693a007c9 | ||
|
|
6339e64501 | ||
|
|
c6edacbe89 | ||
|
|
d6498fb236 | ||
|
|
608ba74f28 |
No files matched your search
@@ -0,0 +1,22 @@
|
||||
CXX ?= g++
|
||||
CXXFLAGS ?= -std=c++17 -O2 -Wall -Wextra
|
||||
|
||||
# One -I per widget dir: each widget header includes "core/xframe_core.h"
|
||||
# from its own component directory.
|
||||
INCLUDES := -I../../../widgets/components/XCoreGrid \
|
||||
-I../../../widgets/components/XArcGauge \
|
||||
-I../../../widgets/components/XHistoryGraph \
|
||||
-I../../../widgets/components/XLoadBadge \
|
||||
-I../../../widgets/components/XUsageBar
|
||||
|
||||
LDFLAGS := -lX11
|
||||
|
||||
all: cpu-dashboard
|
||||
|
||||
cpu-dashboard: main.cpp
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDES) main.cpp -o cpu-dashboard $(LDFLAGS)
|
||||
|
||||
clean:
|
||||
rm -f cpu-dashboard
|
||||
|
||||
.PHONY: all clean
|
||||
@@ -0,0 +1,36 @@
|
||||
# CPU Dashboard — monitor (C)
|
||||
|
||||
A btop-style CPU dashboard in one 1024x768 window, built only from
|
||||
LightGUI XFrame monitor widgets:
|
||||
|
||||
- **XCoreGrid** — per-core mini history graphs (8 cores on hot_dallas)
|
||||
- **XArcGauge** — big total-CPU % gauge (warn 70%, crit 90%)
|
||||
- **XHistoryGraph** — total CPU % over the last 60 s
|
||||
- **XLoadBadge** — load average 1/5/15 min + uptime
|
||||
- **XUsageBar** — total CPU % bar
|
||||
|
||||
Live data comes from `/proc/stat` (per-core busy %) and
|
||||
`/proc/loadavg` (via the badge's own `refresh()`), sampled every
|
||||
500 ms. The seeded demo data each widget preloads is scrolled out at
|
||||
startup by priming the histories with the first live sample.
|
||||
|
||||
## Build
|
||||
|
||||
```sh
|
||||
make
|
||||
```
|
||||
|
||||
## Run
|
||||
|
||||
```sh
|
||||
./cpu-dashboard
|
||||
```
|
||||
|
||||
Esc, `q`, or closing the window quits.
|
||||
|
||||
## Source
|
||||
|
||||
- App: `examples/monitor/cpu-dashboard/main.cpp`
|
||||
- Widgets: `widgets/components/XCoreGrid`, `XArcGauge`,
|
||||
`XHistoryGraph`, `XLoadBadge`, `XUsageBar`
|
||||
- Repo: https://github.com/cnmcdee/lightgui
|
||||
@@ -0,0 +1,291 @@
|
||||
// CPU Dashboard - monitor: a btop-style CPU dashboard built ONLY from
|
||||
// LightGUI XFrame monitor widgets (XCoreGrid, XArcGauge, XHistoryGraph,
|
||||
// XLoadBadge, XUsageBar). Live data from /proc/stat + /proc/loadavg,
|
||||
// refreshed every 500 ms. No raw X11 drawing for UI elements: the app
|
||||
// shell only opens the window and calls each widget's draw().
|
||||
//
|
||||
// Build: make
|
||||
// Run: ./cpu-dashboard (Esc / q / window-close quits)
|
||||
|
||||
#include "XArcGauge.h"
|
||||
#include "XCoreGrid.h"
|
||||
#include "XHistoryGraph.h"
|
||||
#include "XLoadBadge.h"
|
||||
#include "XUsageBar.h"
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/keysym.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <sys/select.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct CpuTimes
|
||||
{
|
||||
long long idle = 0;
|
||||
long long total = 0;
|
||||
};
|
||||
|
||||
bool read_cpu_times(std::vector<CpuTimes>& per_core, CpuTimes& total)
|
||||
{
|
||||
FILE* f = std::fopen("/proc/stat", "r");
|
||||
if (!f) return false;
|
||||
char line[1024];
|
||||
per_core.clear();
|
||||
bool ok = false;
|
||||
while (std::fgets(line, sizeof(line), f))
|
||||
{
|
||||
if (std::strncmp(line, "cpu", 3) != 0) continue;
|
||||
char* p = line + 3;
|
||||
bool is_total = (*p == ' ');
|
||||
if (!is_total && (*p < '0' || *p > '9')) continue;
|
||||
long long nums[16] = {0};
|
||||
int n = std::sscanf(p, "%lld %lld %lld %lld %lld %lld %lld %lld "
|
||||
"%lld %lld %lld %lld %lld %lld %lld %lld",
|
||||
&nums[0], &nums[1], &nums[2], &nums[3],
|
||||
&nums[4], &nums[5], &nums[6], &nums[7],
|
||||
&nums[8], &nums[9], &nums[10], &nums[11],
|
||||
&nums[12], &nums[13], &nums[14], &nums[15]);
|
||||
if (n < 4) continue;
|
||||
CpuTimes t;
|
||||
t.total = 0;
|
||||
for (int i = 0; i < n; ++i) t.total += nums[i];
|
||||
t.idle = nums[3] + (n > 4 ? nums[4] : 0);
|
||||
if (is_total) total = t;
|
||||
else per_core.push_back(t);
|
||||
ok = true;
|
||||
}
|
||||
std::fclose(f);
|
||||
return ok && !per_core.empty();
|
||||
}
|
||||
|
||||
double busy_pct(const CpuTimes& prev, const CpuTimes& now)
|
||||
{
|
||||
long long d_total = now.total - prev.total;
|
||||
long long d_idle = now.idle - prev.idle;
|
||||
if (d_total <= 0) return 0.0;
|
||||
double v = 100.0 * (1.0 - (double)d_idle / (double)d_total);
|
||||
if (v < 0.0) v = 0.0;
|
||||
if (v > 100.0) v = 100.0;
|
||||
return v;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
Display* display = XOpenDisplay(nullptr);
|
||||
if (!display) return 1;
|
||||
int screen = DefaultScreen(display);
|
||||
Window window = XCreateSimpleWindow(display, RootWindow(display, screen),
|
||||
100, 100, 1024, 768, 1,
|
||||
BlackPixel(display, screen),
|
||||
WhitePixel(display, screen));
|
||||
XStoreName(display, window, "CPU Dashboard - monitor");
|
||||
XSelectInput(display, window,
|
||||
ExposureMask | KeyPressMask | StructureNotifyMask |
|
||||
ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
|
||||
Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False);
|
||||
XSetWMProtocols(display, window, &deleteWindowMessage, 1);
|
||||
XMapWindow(display, window);
|
||||
GC gc = DefaultGC(display, screen);
|
||||
SStyle_Manager styles(display, window, screen, gc);
|
||||
styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120},
|
||||
Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1,
|
||||
"-misc-fixed-medium-r-normal--13-*");
|
||||
styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22},
|
||||
Srgb{200, 200, 200}, Srgb{240, 240, 240}, 6, 1,
|
||||
"-misc-fixed-medium-r-normal--13-*");
|
||||
styles.set_active_style_byName("default");
|
||||
|
||||
int ncores = (int)sysconf(_SC_NPROCESSORS_ONLN);
|
||||
if (ncores < 1) ncores = 8;
|
||||
|
||||
XLoadBadge load(display, screen, window, gc, &styles, 16, 16, 320, 84);
|
||||
load.set_text("Load avg");
|
||||
|
||||
XUsageBar total_bar(display, screen, window, gc, &styles, 352, 16, 656, 84);
|
||||
total_bar.set_text("Total CPU");
|
||||
total_bar.set_max(100.0);
|
||||
|
||||
XCoreGrid grid(display, screen, window, gc, &styles, 16, 116, 640, 300);
|
||||
grid.set_text("CPU cores");
|
||||
grid.set_core_count(ncores);
|
||||
|
||||
XArcGauge gauge(display, screen, window, gc, &styles, 672, 116, 336, 300);
|
||||
gauge.set_text("CPU");
|
||||
gauge.set_zones(70.0, 90.0);
|
||||
|
||||
XHistoryGraph history(display, screen, window, gc, &styles, 16, 432, 992, 320);
|
||||
history.set_text("Total CPU history");
|
||||
history.clear_series();
|
||||
history.add_series("CPU", Srgb{0, 170, 70});
|
||||
history.set_y_range(0.0, 100.0);
|
||||
history.set_window_seconds(60);
|
||||
|
||||
auto redraw = [&]() {
|
||||
XClearWindow(display, window);
|
||||
load.draw();
|
||||
total_bar.draw();
|
||||
grid.draw();
|
||||
gauge.draw();
|
||||
history.draw();
|
||||
XFlush(display);
|
||||
};
|
||||
|
||||
// Prime with one live sample so the seeded demo data is fully
|
||||
// scrolled out before the first paint: 60 pushes fill the core
|
||||
// grid (capacity 60), 120 fill the 60 s history window.
|
||||
std::vector<CpuTimes> prev_cores, now_cores;
|
||||
CpuTimes prev_total, now_total;
|
||||
bool have_prev = read_cpu_times(prev_cores, prev_total);
|
||||
if (have_prev)
|
||||
{
|
||||
usleep(100000);
|
||||
if (read_cpu_times(now_cores, now_total) &&
|
||||
now_cores.size() == prev_cores.size())
|
||||
{
|
||||
std::vector<double> per_core;
|
||||
double sum = 0.0;
|
||||
for (size_t i = 0; i < now_cores.size(); ++i)
|
||||
{
|
||||
double v = busy_pct(prev_cores[i], now_cores[i]);
|
||||
per_core.push_back(v);
|
||||
sum += v;
|
||||
}
|
||||
double total = per_core.empty()
|
||||
? 0.0
|
||||
: sum / (double)per_core.size();
|
||||
for (int i = 0; i < 60; ++i) grid.push_all(per_core);
|
||||
for (int i = 0; i < 120; ++i) history.push(0, total);
|
||||
gauge.set_value(total);
|
||||
total_bar.set_value(total);
|
||||
prev_cores = now_cores;
|
||||
prev_total = now_total;
|
||||
}
|
||||
}
|
||||
load.refresh();
|
||||
redraw();
|
||||
|
||||
int fd = ConnectionNumber(display);
|
||||
bool running = true;
|
||||
while (running)
|
||||
{
|
||||
fd_set rfds;
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(fd, &rfds);
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 500000;
|
||||
int ready = select(fd + 1, &rfds, nullptr, nullptr, &tv);
|
||||
if (ready > 0)
|
||||
{
|
||||
while (XPending(display))
|
||||
{
|
||||
XEvent event;
|
||||
XNextEvent(display, &event);
|
||||
bool handled = false;
|
||||
switch (event.type)
|
||||
{
|
||||
case Expose:
|
||||
handled = true;
|
||||
break;
|
||||
case ButtonPress:
|
||||
handled =
|
||||
grid.on_button_press(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button) ||
|
||||
gauge.on_button_press(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button) ||
|
||||
history.on_button_press(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button) ||
|
||||
load.on_button_press(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button) ||
|
||||
total_bar.on_button_press(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button);
|
||||
break;
|
||||
case ButtonRelease:
|
||||
handled =
|
||||
grid.on_button_release(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button) ||
|
||||
gauge.on_button_release(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button) ||
|
||||
history.on_button_release(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button) ||
|
||||
load.on_button_release(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button) ||
|
||||
total_bar.on_button_release(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button);
|
||||
break;
|
||||
case MotionNotify:
|
||||
handled = grid.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
gauge.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
history.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
load.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
total_bar.on_motion(event.xmotion.x, event.xmotion.y);
|
||||
break;
|
||||
case KeyPress:
|
||||
{
|
||||
char buffer[32];
|
||||
KeySym key = 0;
|
||||
int n = XLookupString(&event.xkey, buffer,
|
||||
(int)sizeof(buffer) - 1, &key, nullptr);
|
||||
buffer[n] = 0;
|
||||
if (key == XK_Escape || (n == 1 && buffer[0] == 'q'))
|
||||
running = false;
|
||||
handled = grid.on_key(key, buffer) ||
|
||||
gauge.on_key(key, buffer) ||
|
||||
history.on_key(key, buffer) ||
|
||||
load.on_key(key, buffer) ||
|
||||
total_bar.on_key(key, buffer);
|
||||
break;
|
||||
}
|
||||
case ClientMessage:
|
||||
if ((Atom)event.xclient.data.l[0] == deleteWindowMessage)
|
||||
running = false;
|
||||
break;
|
||||
}
|
||||
if (handled) redraw();
|
||||
if (!running) break;
|
||||
}
|
||||
}
|
||||
else if (ready == 0)
|
||||
{
|
||||
// 500 ms tick: live sample from /proc/stat + /proc/loadavg.
|
||||
if (have_prev && read_cpu_times(now_cores, now_total) &&
|
||||
now_cores.size() == prev_cores.size())
|
||||
{
|
||||
std::vector<double> per_core;
|
||||
double sum = 0.0;
|
||||
for (size_t i = 0; i < now_cores.size(); ++i)
|
||||
{
|
||||
double v = busy_pct(prev_cores[i], now_cores[i]);
|
||||
per_core.push_back(v);
|
||||
sum += v;
|
||||
}
|
||||
double total = per_core.empty()
|
||||
? 0.0
|
||||
: sum / (double)per_core.size();
|
||||
grid.push_all(per_core);
|
||||
history.push(0, total);
|
||||
gauge.set_value(total);
|
||||
total_bar.set_value(total);
|
||||
prev_cores = now_cores;
|
||||
prev_total = now_total;
|
||||
}
|
||||
load.refresh();
|
||||
redraw();
|
||||
}
|
||||
}
|
||||
|
||||
XCloseDisplay(display);
|
||||
return 0;
|
||||
}
|
||||
|
After Width: | Height: | Size: 12 KiB |
@@ -0,0 +1,20 @@
|
||||
CXX ?= g++
|
||||
CXXFLAGS ?= -std=c++17 -O2 -Wall
|
||||
|
||||
ROOT = ../../..
|
||||
INC = -I$(ROOT)/widgets/components/XUsageBar \
|
||||
-I$(ROOT)/widgets/components/XHistoryGraph \
|
||||
-I$(ROOT)/widgets/components/XStackBar
|
||||
|
||||
all: disk-monitor
|
||||
|
||||
disk-monitor: main.cpp
|
||||
$(CXX) $(CXXFLAGS) $(INC) main.cpp -o disk-monitor -lX11
|
||||
|
||||
run: disk-monitor
|
||||
./disk-monitor
|
||||
|
||||
clean:
|
||||
rm -f disk-monitor
|
||||
|
||||
.PHONY: all run clean
|
||||
@@ -0,0 +1,47 @@
|
||||
# Disk Monitor — C (XFrame)
|
||||
|
||||
A live disk monitor in one 1024x768 window: per-mount usage bars,
|
||||
a Used / Free stack bar for the largest mount, and a read/write
|
||||
throughput history graph. All data is live — `/proc/mounts` +
|
||||
`statvfs` for usage, `/proc/diskstats` (sectors differenced once a
|
||||
second) for MB/s. Refreshes every second; Esc / `q` / window-close
|
||||
quits.
|
||||
|
||||
## Widgets used
|
||||
|
||||
| Widget | Role |
|
||||
| --- | --- |
|
||||
| XUsageBar | One per disk mount (`/`, …): usage % with total size |
|
||||
| XStackBar | Largest mount split into Used / Free (GiB) |
|
||||
| XHistoryGraph | Read MB/s + Write MB/s history, 60 s window |
|
||||
|
||||
**Why no XSysBox:** it has no clear-history API and its
|
||||
constructor seeds 60 demo samples, so it cannot show live disk data
|
||||
without the seeded demo leaking into the history. XUsageBar +
|
||||
XHistoryGraph + XStackBar cover the same ground with live setters
|
||||
(`set_value`, `push`, `set_segment`/`clear_segments`).
|
||||
|
||||
## Build & run
|
||||
|
||||
```sh
|
||||
make
|
||||
./disk-monitor
|
||||
```
|
||||
|
||||
Build recipe (same as the widget demo mains): `g++ -std=c++17
|
||||
-I<repo>/widgets/components/XUsageBar
|
||||
-I<repo>/widgets/components/XHistoryGraph
|
||||
-I<repo>/widgets/components/XStackBar main.cpp -lX11`.
|
||||
|
||||
## Screenshot
|
||||
|
||||

|
||||
|
||||
## Source
|
||||
|
||||
- App: `examples/monitor/disk-monitor/main.cpp`
|
||||
- Repo: https://github.com/cnmcdee/lightgui
|
||||
- Rust port: `crates/xframe-demo/examples/monitor_disk_monitor/main.rs`
|
||||
in https://github.com/cnmcdee/lightgui_rust
|
||||
- Python port: `examples/monitor_disk_monitor/main.py`
|
||||
in https://github.com/cnmcdee/lightgui_python
|
||||
@@ -0,0 +1,303 @@
|
||||
// Disk Monitor - monitor (C / XFrame)
|
||||
//
|
||||
// One 1024x768 window: a live XUsageBar per disk mount (from
|
||||
// /proc/mounts + statvfs), an XStackBar splitting the largest mount
|
||||
// into Used / Free (GiB), and an XHistoryGraph of disk read/write
|
||||
// throughput in MB/s (from /proc/diskstats, differenced once a second).
|
||||
//
|
||||
// Widgets used (all from the LightGUI registry, no raw drawing here
|
||||
// beyond the demo-main window shell):
|
||||
// XUsageBar - per-mount usage %
|
||||
// XStackBar - Used / Free split of the largest mount
|
||||
// XHistoryGraph - read / write MB/s history (60 s window)
|
||||
//
|
||||
// XSysBox is deliberately NOT used: it has no clear-history API and
|
||||
// its constructor seeds 60 demo samples, so it cannot show live
|
||||
// disk data without the seeded demo leaking into the history.
|
||||
//
|
||||
// Build: make (see Makefile)
|
||||
// Run: ./disk-monitor (Esc / q / window-close quits)
|
||||
|
||||
#include "XUsageBar.h"
|
||||
#include "XHistoryGraph.h"
|
||||
#include "XStackBar.h"
|
||||
|
||||
#include <cctype>
|
||||
#include <chrono>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <sys/select.h>
|
||||
#include <sys/statvfs.h>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct Mount
|
||||
{
|
||||
std::string dev; // /dev/sda2
|
||||
std::string point; // /
|
||||
std::string base; // sda (diskstats device backing this mount)
|
||||
double total_gib = 0; // GiB
|
||||
double used_gib = 0; // GiB
|
||||
double free_gib = 0; // GiB
|
||||
double pct = 0; // 0..100
|
||||
};
|
||||
|
||||
// Strip a partition suffix: sda2 -> sda, nvme0n1p2 -> nvme0n1.
|
||||
std::string base_device(const std::string& dev)
|
||||
{
|
||||
std::string name = dev;
|
||||
std::size_t slash = name.rfind('/');
|
||||
if (slash != std::string::npos) name = name.substr(slash + 1);
|
||||
while (!name.empty() && std::isdigit((unsigned char)name.back()))
|
||||
name.pop_back();
|
||||
if (!name.empty() && name.back() == 'p' && name.size() >= 2 &&
|
||||
std::isdigit((unsigned char)name[name.size() - 2]))
|
||||
name.pop_back();
|
||||
return name;
|
||||
}
|
||||
|
||||
bool real_fs(const std::string& fs)
|
||||
{
|
||||
return fs == "ext4" || fs == "xfs" || fs == "btrfs";
|
||||
}
|
||||
|
||||
// Live mounts (real on-disk filesystems, max 4), with statvfs sizes.
|
||||
std::vector<Mount> read_mounts()
|
||||
{
|
||||
std::vector<Mount> mounts;
|
||||
FILE* f = std::fopen("/proc/mounts", "r");
|
||||
if (!f) return mounts;
|
||||
char dev[256], point[256], fs[64], opts[256];
|
||||
int a, b;
|
||||
while (std::fscanf(f, "%255s %255s %63s %255s %d %d", dev, point, fs, opts, &a, &b) == 6)
|
||||
{
|
||||
if (!real_fs(fs)) continue;
|
||||
bool dup = false;
|
||||
for (auto& m : mounts)
|
||||
if (m.point == point) dup = true;
|
||||
if (dup) continue;
|
||||
struct statvfs sv;
|
||||
if (statvfs(point, &sv) != 0) continue;
|
||||
Mount m;
|
||||
m.dev = dev;
|
||||
m.point = point;
|
||||
m.base = base_device(dev);
|
||||
double total = (double)sv.f_blocks * (double)sv.f_frsize;
|
||||
double free = (double)sv.f_bfree * (double)sv.f_frsize;
|
||||
m.total_gib = total / 1073741824.0;
|
||||
m.free_gib = free / 1073741824.0;
|
||||
m.used_gib = m.total_gib - m.free_gib;
|
||||
m.pct = m.total_gib > 0 ? m.used_gib / m.total_gib * 100.0 : 0.0;
|
||||
mounts.push_back(m);
|
||||
if (mounts.size() >= 4) break;
|
||||
}
|
||||
std::fclose(f);
|
||||
return mounts;
|
||||
}
|
||||
|
||||
// (read_sectors, write_sectors) summed over the backing disks.
|
||||
bool read_io(const std::vector<Mount>& mounts, unsigned long long& rd, unsigned long long& wr)
|
||||
{
|
||||
rd = 0;
|
||||
wr = 0;
|
||||
FILE* f = std::fopen("/proc/diskstats", "r");
|
||||
if (!f) return false;
|
||||
char line[512];
|
||||
bool ok = false;
|
||||
while (std::fgets(line, sizeof(line), f))
|
||||
{
|
||||
unsigned long long rd_s = 0, wr_s = 0;
|
||||
char name[64];
|
||||
// major minor name reads_completed reads_merged sectors_read ...
|
||||
// sectors_written is field 10.
|
||||
unsigned long long f1, f2, f3, f4, f5, f6, f7, f8, f9;
|
||||
int n = std::sscanf(line, "%llu %llu %63s %llu %llu %llu %llu %llu %llu %llu %llu",
|
||||
&f1, &f2, name, &f3, &f4, &rd_s, &f5, &f6, &f7, &wr_s, &f8);
|
||||
(void)f9;
|
||||
if (n < 10) continue;
|
||||
for (auto& m : mounts)
|
||||
{
|
||||
if (m.base == name)
|
||||
{
|
||||
rd += rd_s;
|
||||
wr += wr_s;
|
||||
ok = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
std::fclose(f);
|
||||
return ok;
|
||||
}
|
||||
|
||||
std::string fmt_gib(double gib)
|
||||
{
|
||||
char buf[32];
|
||||
std::snprintf(buf, sizeof(buf), "%.0f GiB", gib);
|
||||
return buf;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
Display* display = XOpenDisplay(nullptr);
|
||||
if (!display) return 1;
|
||||
int screen = DefaultScreen(display);
|
||||
Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 1024, 768, 1,
|
||||
BlackPixel(display, screen), WhitePixel(display, screen));
|
||||
XStoreName(display, window, "Disk Monitor - monitor");
|
||||
XSelectInput(display, window,
|
||||
ExposureMask | KeyPressMask | StructureNotifyMask | ButtonPressMask |
|
||||
ButtonReleaseMask | PointerMotionMask);
|
||||
Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False);
|
||||
XSetWMProtocols(display, window, &deleteWindowMessage, 1);
|
||||
XMapWindow(display, window);
|
||||
GC gc = DefaultGC(display, screen);
|
||||
SStyle_Manager styles(display, window, screen, gc);
|
||||
styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120}, Srgb{60, 60, 60},
|
||||
Srgb{20, 20, 20}, 6, 1, "-misc-fixed-medium-r-normal--13-*");
|
||||
styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22}, Srgb{200, 200, 200},
|
||||
Srgb{240, 240, 240}, 6, 1, "-misc-fixed-medium-r-normal--13-*");
|
||||
styles.set_active_style_byName("default");
|
||||
|
||||
// Live mount list (fixed at startup; mounts rarely change).
|
||||
std::vector<Mount> mounts = read_mounts();
|
||||
if (mounts.empty())
|
||||
{
|
||||
Mount root;
|
||||
root.dev = "?";
|
||||
root.point = "/";
|
||||
root.base = "";
|
||||
mounts.push_back(root);
|
||||
}
|
||||
const int n = (int)mounts.size();
|
||||
|
||||
// Layout: usage bars on top, stack bar, history graph fills the rest.
|
||||
const int margin = 16;
|
||||
const int bar_h = 44;
|
||||
const int bar_gap = 8;
|
||||
const int stack_h = 170;
|
||||
const int stack_y = margin + n * (bar_h + bar_gap) + 8;
|
||||
const int hist_y = stack_y + stack_h + 8;
|
||||
const int hist_h = 768 - margin - hist_y;
|
||||
const int col_w = 1024 - 2 * margin;
|
||||
|
||||
std::vector<XUsageBar> bars;
|
||||
for (int i = 0; i < n; ++i)
|
||||
{
|
||||
bars.emplace_back(display, screen, window, gc, &styles, margin,
|
||||
margin + i * (bar_h + bar_gap), col_w, bar_h);
|
||||
bars.back().set_text(mounts[i].point + " " + fmt_gib(mounts[i].total_gib));
|
||||
}
|
||||
|
||||
XStackBar stack(display, screen, window, gc, &styles, margin, stack_y, col_w, stack_h);
|
||||
stack.set_text(mounts[0].point + " disk usage");
|
||||
stack.clear_segments();
|
||||
stack.add_segment("Used", mounts[0].used_gib, Srgb{0, 150, 60});
|
||||
stack.add_segment("Free", mounts[0].free_gib, Srgb{200, 200, 200});
|
||||
|
||||
XHistoryGraph hist(display, screen, window, gc, &styles, margin, hist_y, col_w, hist_h);
|
||||
hist.set_text("Disk IO - MB/s (read / write)");
|
||||
hist.clear_series(); // drop the constructor's seeded demo series
|
||||
hist.add_series("Read MB/s", Srgb{0, 170, 70});
|
||||
hist.add_series("Write MB/s", Srgb{0, 100, 200});
|
||||
|
||||
// No XClearWindow here: every widget paints its own white
|
||||
// background first, so a clear would only blank the window and
|
||||
// leave import/screenshot tools catching a half-painted frame
|
||||
// (and XClearWindow itself generates another Expose).
|
||||
auto redraw = [&]() {
|
||||
for (auto& b : bars) b.draw();
|
||||
stack.draw();
|
||||
hist.draw();
|
||||
XFlush(display);
|
||||
};
|
||||
|
||||
unsigned long long prev_rd = 0, prev_wr = 0;
|
||||
bool have_prev = read_io(mounts, prev_rd, prev_wr);
|
||||
auto refresh = [&]() {
|
||||
std::vector<Mount> live = read_mounts();
|
||||
for (int i = 0; i < n && i < (int)live.size(); ++i)
|
||||
{
|
||||
mounts[i].total_gib = live[i].total_gib;
|
||||
mounts[i].used_gib = live[i].used_gib;
|
||||
mounts[i].free_gib = live[i].free_gib;
|
||||
mounts[i].pct = live[i].pct;
|
||||
bars[i].set_value(mounts[i].pct);
|
||||
}
|
||||
stack.clear_segments();
|
||||
stack.add_segment("Used", mounts[0].used_gib, Srgb{0, 150, 60});
|
||||
stack.add_segment("Free", mounts[0].free_gib, Srgb{200, 200, 200});
|
||||
unsigned long long rd = 0, wr = 0;
|
||||
if (read_io(mounts, rd, wr) && have_prev)
|
||||
{
|
||||
// 1 s tick, 512-byte sectors -> MB/s.
|
||||
hist.push(0, (double)(rd - prev_rd) * 512.0 / 1e6);
|
||||
hist.push(1, (double)(wr - prev_wr) * 512.0 / 1e6);
|
||||
}
|
||||
prev_rd = rd;
|
||||
prev_wr = wr;
|
||||
have_prev = true;
|
||||
};
|
||||
|
||||
refresh();
|
||||
redraw();
|
||||
|
||||
bool running = true;
|
||||
auto last_tick = std::chrono::steady_clock::now();
|
||||
int fd = ConnectionNumber(display);
|
||||
while (running)
|
||||
{
|
||||
fd_set rfds;
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(fd, &rfds);
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 500000;
|
||||
int ready = select(fd + 1, &rfds, nullptr, nullptr, &tv);
|
||||
if (ready > 0)
|
||||
{
|
||||
while (XPending(display))
|
||||
{
|
||||
XEvent event;
|
||||
XNextEvent(display, &event);
|
||||
switch (event.type)
|
||||
{
|
||||
case Expose:
|
||||
// Only repaint on the last Expose of a series;
|
||||
// earlier ones are superseded.
|
||||
if (event.xexpose.count == 0) redraw();
|
||||
break;
|
||||
case KeyPress:
|
||||
{
|
||||
char buffer[32];
|
||||
KeySym key = 0;
|
||||
int k = XLookupString(&event.xkey, buffer, (int)sizeof(buffer) - 1, &key, nullptr);
|
||||
buffer[k] = 0;
|
||||
if (key == XK_Escape || buffer[0] == 'q') running = false;
|
||||
break;
|
||||
}
|
||||
case ClientMessage:
|
||||
if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) running = false;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
if (std::chrono::duration_cast<std::chrono::seconds>(now - last_tick).count() >= 1)
|
||||
{
|
||||
last_tick = now;
|
||||
refresh();
|
||||
redraw();
|
||||
}
|
||||
}
|
||||
|
||||
XCloseDisplay(display);
|
||||
return 0;
|
||||
}
|
||||
|
After Width: | Height: | Size: 5.2 KiB |
@@ -0,0 +1,19 @@
|
||||
CXX ?= g++
|
||||
CXXFLAGS ?= -std=c++17 -O2 -Wall -Wextra
|
||||
|
||||
# The five monitor widgets used, each with its own copy of
|
||||
# core/xframe_core.h (byte-identical, include-guarded).
|
||||
WIDGET_DIR = ../../../widgets/components
|
||||
INCLUDES = -I$(WIDGET_DIR)/XStackBar \
|
||||
-I$(WIDGET_DIR)/XHistoryGraph \
|
||||
-I$(WIDGET_DIR)/XUsageBar \
|
||||
-I$(WIDGET_DIR)/XSysBox \
|
||||
-I$(WIDGET_DIR)/XDonutGauge
|
||||
|
||||
memory-monitor: main.cpp
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDES) main.cpp -lX11 -o memory-monitor
|
||||
|
||||
clean:
|
||||
rm -f memory-monitor
|
||||
|
||||
.PHONY: clean
|
||||
@@ -0,0 +1,31 @@
|
||||
# Memory Monitor — C
|
||||
|
||||
A btop-style memory monitor in one 1024x768 window, built from the new
|
||||
XFrame monitor widgets (Stage 2 of the btop-widget project). All data is
|
||||
live from `/proc/meminfo`, refreshed every 500 ms.
|
||||
|
||||
## What it shows
|
||||
|
||||
- **XSysBox "MEMORY"** — big used-% number, mini history graph, usage bar
|
||||
- **XDonutGauge "Memory used" / "Swap used"** — ring gauges (swap is 0%
|
||||
on machines with no swap configured)
|
||||
- **XStackBar** — used / cached / buffers / free in GiB, with legend and
|
||||
total; the title carries the live available figure
|
||||
- **XHistoryGraph** — used % and cached % over the last 60 seconds
|
||||
- **XUsageBar x3** — Memory used, Cached, Swap used
|
||||
|
||||
"Used" follows the `free` convention: total − free − buffers − cached.
|
||||
|
||||
## Build and run
|
||||
|
||||
```sh
|
||||
make
|
||||
./memory-monitor # needs an X display; Xvfb works
|
||||
```
|
||||
|
||||
Quit with `q`, `Esc`, or the window close button.
|
||||
|
||||
## Widgets used
|
||||
|
||||
XSysBox, XDonutGauge, XStackBar, XHistoryGraph, XUsageBar — all from the
|
||||
LightGUI registry (562 widgets: 547 + 15 monitor widgets).
|
||||
@@ -0,0 +1,276 @@
|
||||
// Memory Monitor - monitor (Stage 2, btop-widget project).
|
||||
//
|
||||
// One 1024x768 window of live memory data from /proc/meminfo, refreshed
|
||||
// every 500 ms. Every visible element is one of the new XFrame monitor
|
||||
// widgets: XSysBox (big used %), XDonutGauge x2 (memory / swap used),
|
||||
// XStackBar (used / cached / buffers / free), XHistoryGraph (used % and
|
||||
// cached % over the last 60 s), XUsageBar x3 (memory used / cached /
|
||||
// swap used). No raw X11 drawing for UI elements: the shell only opens
|
||||
// the window, clears it and calls each widget's draw().
|
||||
//
|
||||
// Build: make
|
||||
// Run: ./memory-monitor (needs an X display; Xvfb works)
|
||||
|
||||
#include "XStackBar.h"
|
||||
#include "XHistoryGraph.h"
|
||||
#include "XUsageBar.h"
|
||||
#include "XSysBox.h"
|
||||
#include "XDonutGauge.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <sys/select.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct MemInfo
|
||||
{
|
||||
double total_gib = 0.0;
|
||||
double free_gib = 0.0;
|
||||
double avail_gib = 0.0;
|
||||
double cached_gib = 0.0;
|
||||
double buffers_gib = 0.0;
|
||||
double used_gib = 0.0;
|
||||
double used_pct = 0.0;
|
||||
double cached_pct = 0.0;
|
||||
double swap_total_gib = 0.0;
|
||||
double swap_used_gib = 0.0;
|
||||
double swap_pct = 0.0;
|
||||
};
|
||||
|
||||
// Live memory figures from /proc/meminfo. "Used" follows the `free`
|
||||
// convention: total - free - buffers - cached.
|
||||
bool read_meminfo(MemInfo& out)
|
||||
{
|
||||
FILE* f = std::fopen("/proc/meminfo", "r");
|
||||
if (!f) return false;
|
||||
unsigned long total = 0, free_kb = 0, avail = 0, cached = 0, buffers = 0;
|
||||
unsigned long swap_total = 0, swap_free = 0;
|
||||
char line[256];
|
||||
while (std::fgets(line, sizeof(line), f))
|
||||
{
|
||||
unsigned long v = 0;
|
||||
if (std::sscanf(line, "MemTotal: %lu", &v) == 1) total = v;
|
||||
else if (std::sscanf(line, "MemFree: %lu", &v) == 1) free_kb = v;
|
||||
else if (std::sscanf(line, "MemAvailable: %lu", &v) == 1) avail = v;
|
||||
else if (std::sscanf(line, "Cached: %lu", &v) == 1) cached = v;
|
||||
else if (std::sscanf(line, "Buffers: %lu", &v) == 1) buffers = v;
|
||||
else if (std::sscanf(line, "SwapTotal: %lu", &v) == 1) swap_total = v;
|
||||
else if (std::sscanf(line, "SwapFree: %lu", &v) == 1) swap_free = v;
|
||||
}
|
||||
std::fclose(f);
|
||||
if (total == 0) return false;
|
||||
const double gib = 1024.0 * 1024.0;
|
||||
out.total_gib = total / gib;
|
||||
out.free_gib = free_kb / gib;
|
||||
out.avail_gib = avail / gib;
|
||||
out.cached_gib = cached / gib;
|
||||
out.buffers_gib = buffers / gib;
|
||||
out.used_gib = (total - free_kb - buffers - cached) / gib;
|
||||
if (out.used_gib < 0.0) out.used_gib = 0.0;
|
||||
out.used_pct = out.used_gib / out.total_gib * 100.0;
|
||||
out.cached_pct = out.cached_gib / out.total_gib * 100.0;
|
||||
out.swap_total_gib = swap_total / gib;
|
||||
out.swap_used_gib = (swap_total - swap_free) / gib;
|
||||
if (out.swap_used_gib < 0.0) out.swap_used_gib = 0.0;
|
||||
out.swap_pct = swap_total > 0 ? out.swap_used_gib / out.swap_total_gib * 100.0 : 0.0;
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
Display* display = XOpenDisplay(nullptr);
|
||||
if (!display) return 1;
|
||||
int screen = DefaultScreen(display);
|
||||
Window window = XCreateSimpleWindow(display, RootWindow(display, screen),
|
||||
100, 100, 1024, 768, 1,
|
||||
BlackPixel(display, screen),
|
||||
WhitePixel(display, screen));
|
||||
XStoreName(display, window, "Memory Monitor - monitor");
|
||||
XSelectInput(display, window, ExposureMask | KeyPressMask | StructureNotifyMask |
|
||||
ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
|
||||
Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False);
|
||||
XSetWMProtocols(display, window, &deleteWindowMessage, 1);
|
||||
XMapWindow(display, window);
|
||||
GC gc = DefaultGC(display, screen);
|
||||
SStyle_Manager styles(display, window, screen, gc);
|
||||
styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120},
|
||||
Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1,
|
||||
"-misc-fixed-medium-r-normal--13-*");
|
||||
styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22},
|
||||
Srgb{200, 200, 200}, Srgb{240, 240, 240}, 6, 1,
|
||||
"-misc-fixed-medium-r-normal--13-*");
|
||||
styles.set_active_style_byName("default");
|
||||
|
||||
XSysBox sysbox(display, screen, window, gc, &styles, 16, 16, 500, 190);
|
||||
sysbox.set_text("MEMORY");
|
||||
sysbox.set_unit("%");
|
||||
sysbox.set_max(100.0);
|
||||
|
||||
XDonutGauge donut_mem(display, screen, window, gc, &styles, 532, 16, 230, 190);
|
||||
donut_mem.set_text("Memory used");
|
||||
XDonutGauge donut_swap(display, screen, window, gc, &styles, 778, 16, 230, 190);
|
||||
donut_swap.set_text("Swap used");
|
||||
|
||||
XStackBar stack(display, screen, window, gc, &styles, 16, 222, 992, 170);
|
||||
stack.clear_segments();
|
||||
stack.add_segment("Used", 0.0, Srgb{0, 150, 60});
|
||||
stack.add_segment("Cached", 0.0, Srgb{220, 180, 0});
|
||||
stack.add_segment("Buffers", 0.0, Srgb{0, 120, 200});
|
||||
stack.add_segment("Free", 0.0, Srgb{200, 200, 200});
|
||||
|
||||
XHistoryGraph history(display, screen, window, gc, &styles, 16, 408, 992, 200);
|
||||
history.set_text("Memory history");
|
||||
history.clear_series();
|
||||
history.add_series("Used %", Srgb{0, 170, 70});
|
||||
history.add_series("Cached %", Srgb{0, 100, 200});
|
||||
history.set_y_range(0.0, 100.0);
|
||||
|
||||
XUsageBar bar_mem(display, screen, window, gc, &styles, 16, 624, 992, 36);
|
||||
bar_mem.set_text("Memory used");
|
||||
bar_mem.set_max(100.0);
|
||||
XUsageBar bar_cached(display, screen, window, gc, &styles, 16, 664, 992, 36);
|
||||
bar_cached.set_text("Cached");
|
||||
bar_cached.set_max(100.0);
|
||||
XUsageBar bar_swap(display, screen, window, gc, &styles, 16, 704, 992, 36);
|
||||
bar_swap.set_text("Swap used");
|
||||
bar_swap.set_max(100.0);
|
||||
|
||||
auto redraw = [&]() {
|
||||
XClearWindow(display, window);
|
||||
sysbox.draw();
|
||||
donut_mem.draw();
|
||||
donut_swap.draw();
|
||||
stack.draw();
|
||||
history.draw();
|
||||
bar_mem.draw();
|
||||
bar_cached.draw();
|
||||
bar_swap.draw();
|
||||
XFlush(display);
|
||||
};
|
||||
|
||||
auto tick = [&]() {
|
||||
MemInfo m;
|
||||
if (!read_meminfo(m)) return;
|
||||
sysbox.set_value(m.used_pct);
|
||||
donut_mem.set_value(m.used_pct);
|
||||
donut_swap.set_value(m.swap_pct);
|
||||
stack.set_segment(0, m.used_gib);
|
||||
stack.set_segment(1, m.cached_gib);
|
||||
stack.set_segment(2, m.buffers_gib);
|
||||
stack.set_segment(3, m.free_gib);
|
||||
char title[128];
|
||||
std::snprintf(title, sizeof(title), "Memory - avail %.1f GiB", m.avail_gib);
|
||||
stack.set_text(title);
|
||||
history.push(0, m.used_pct);
|
||||
history.push(1, m.cached_pct);
|
||||
bar_mem.set_value(m.used_pct);
|
||||
bar_cached.set_value(m.cached_pct);
|
||||
bar_swap.set_value(m.swap_pct);
|
||||
redraw();
|
||||
};
|
||||
|
||||
// First live sample, then flush the SysBox's seeded demo history
|
||||
// (60 samples) with the live value so the mini-graph is real data
|
||||
// from the first paint, not the demo sine wave.
|
||||
{
|
||||
MemInfo m;
|
||||
if (read_meminfo(m))
|
||||
{
|
||||
for (int i = 0; i < 60; ++i) sysbox.set_value(m.used_pct);
|
||||
}
|
||||
}
|
||||
tick();
|
||||
|
||||
bool running = true;
|
||||
const int fd = ConnectionNumber(display);
|
||||
while (running)
|
||||
{
|
||||
fd_set rfds;
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(fd, &rfds);
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 500000; // ~500 ms live refresh
|
||||
int ready = select(fd + 1, &rfds, nullptr, nullptr, &tv);
|
||||
if (ready == 0)
|
||||
{
|
||||
tick();
|
||||
continue;
|
||||
}
|
||||
if (ready < 0) continue;
|
||||
while (XPending(display))
|
||||
{
|
||||
XEvent event;
|
||||
XNextEvent(display, &event);
|
||||
bool handled = false;
|
||||
switch (event.type)
|
||||
{
|
||||
case Expose:
|
||||
redraw();
|
||||
break;
|
||||
case ButtonPress:
|
||||
handled = sysbox.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
donut_mem.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
donut_swap.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
stack.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
history.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
bar_mem.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
bar_cached.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
bar_swap.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button);
|
||||
break;
|
||||
case ButtonRelease:
|
||||
handled = sysbox.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
donut_mem.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
donut_swap.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
stack.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
history.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
bar_mem.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
bar_cached.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
bar_swap.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button);
|
||||
break;
|
||||
case MotionNotify:
|
||||
handled = sysbox.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
donut_mem.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
donut_swap.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
stack.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
history.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
bar_mem.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
bar_cached.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
bar_swap.on_motion(event.xmotion.x, event.xmotion.y);
|
||||
break;
|
||||
case KeyPress:
|
||||
{
|
||||
char buffer[32];
|
||||
KeySym key = 0;
|
||||
int n = XLookupString(&event.xkey, buffer, (int)sizeof(buffer) - 1, &key, nullptr);
|
||||
buffer[n] = 0;
|
||||
if (key == XK_Escape || (n == 1 && (buffer[0] == 'q' || buffer[0] == 'Q')))
|
||||
running = false;
|
||||
handled = sysbox.on_key(key, buffer) ||
|
||||
donut_mem.on_key(key, buffer) ||
|
||||
donut_swap.on_key(key, buffer) ||
|
||||
stack.on_key(key, buffer) ||
|
||||
history.on_key(key, buffer) ||
|
||||
bar_mem.on_key(key, buffer) ||
|
||||
bar_cached.on_key(key, buffer) ||
|
||||
bar_swap.on_key(key, buffer);
|
||||
break;
|
||||
}
|
||||
case ClientMessage:
|
||||
if ((Atom)event.xclient.data.l[0] == deleteWindowMessage)
|
||||
running = false;
|
||||
break;
|
||||
}
|
||||
if (handled) redraw();
|
||||
}
|
||||
}
|
||||
|
||||
XCloseDisplay(display);
|
||||
return 0;
|
||||
}
|
||||
|
After Width: | Height: | Size: 8.1 KiB |
@@ -0,0 +1,25 @@
|
||||
# Network Monitor - monitor (C / XFrame)
|
||||
#
|
||||
# Build: make
|
||||
# Run: ./network-monitor (quit: q / Escape / window close)
|
||||
# Clean: make clean
|
||||
|
||||
CXX ?= g++
|
||||
CXXFLAGS ?= -std=c++17 -O2 -Wall
|
||||
|
||||
ROOT := ../../..
|
||||
INCLUDES := -I$(ROOT)/widgets/components/XNetGraph \
|
||||
-I$(ROOT)/widgets/components/XHistoryGraph \
|
||||
-I$(ROOT)/widgets/components/XUsageBar \
|
||||
-I$(ROOT)/widgets/components/XSysBox
|
||||
LIBS := $(shell pkg-config --libs x11)
|
||||
|
||||
all: network-monitor
|
||||
|
||||
network-monitor: main.cpp
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDES) -o $@ main.cpp $(LIBS)
|
||||
|
||||
clean:
|
||||
rm -f network-monitor
|
||||
|
||||
.PHONY: all clean
|
||||
@@ -0,0 +1,45 @@
|
||||
# Network Monitor — monitor (C)
|
||||
|
||||
A live network monitor in one 1024x768 window, built only from the
|
||||
LightGUI XFrame monitor widgets (Stage 2 of the btop-widget project).
|
||||
|
||||
## What it shows
|
||||
|
||||
Live traffic for the main interface (`enp3s0` on hot_dallas, otherwise
|
||||
the first non-loopback interface with traffic), read from
|
||||
`/proc/net/dev` and differenced every 500 ms:
|
||||
|
||||
- **XNetGraph** (top) — up/down graph in bytes/s, current rates plus
|
||||
session totals in the legend.
|
||||
- **XSysBox** (middle, x2) — Download / Upload big-number boxes in
|
||||
KiB/s, each with its own history sparkline and usage bar. The range
|
||||
tracks the session peak (x1.15, floor 100 KiB/s).
|
||||
- **XHistoryGraph** — down/up rate history in KiB/s, 60 s window,
|
||||
current / min / max / average per series.
|
||||
- **XUsageBar** (bottom) — total throughput as a percentage of the
|
||||
link speed (from `/sys/class/net/<iface>/speed`, 1 Gb/s fallback).
|
||||
|
||||
## Live data, not demo data
|
||||
|
||||
The widgets preload seeded demo series. The app flushes them at
|
||||
startup: the first measured rate (two `/proc/net/dev` reads 500 ms
|
||||
apart) is pushed 120x into XNetGraph (which has no clear API) and its
|
||||
totals reset, the history graph is cleared and re-seeded with down/up
|
||||
series, and each XSysBox gets 60 live `set_value()` calls. Everything
|
||||
on screen after that is measured traffic.
|
||||
|
||||
## Build and run
|
||||
|
||||
```sh
|
||||
make # builds ./network-monitor
|
||||
./network-monitor # quit with q, Escape, or window close
|
||||
```
|
||||
|
||||
## Source
|
||||
|
||||
- C: `examples/monitor/network-monitor/main.cpp` in
|
||||
[lightgui](https://github.com/cnmcdee/lightgui)
|
||||
- Rust: `crates/xframe-demo/examples/monitor_network_monitor/main.rs` in
|
||||
[lightgui_rust](https://github.com/cnmcdee/lightgui_rust)
|
||||
- Python: `examples/monitor_network_monitor/main.py` in
|
||||
[lightgui_python](https://github.com/cnmcdee/lightgui_python)
|
||||
@@ -0,0 +1,311 @@
|
||||
// Network Monitor - monitor (Stage 2, btop-widget project).
|
||||
//
|
||||
// One 1024x768 window showing LIVE traffic for the main interface
|
||||
// (enp3s0 on hot_dallas, else the first non-loopback interface with
|
||||
// traffic), read from /proc/net/dev and differenced every 500 ms:
|
||||
//
|
||||
// XNetGraph up/down graph, current rates + session totals
|
||||
// XSysBox x2 Download / Upload big-number boxes (KiB/s + history)
|
||||
// XHistoryGraph rate history, down/up in KiB/s (60 s window)
|
||||
// XUsageBar total throughput as % of the link speed
|
||||
//
|
||||
// Widgets preload seeded demo data; the app flushes it at startup
|
||||
// (XNetGraph has no clear API, so the first measured rate is pushed
|
||||
// 120x to fill its window, then reset_totals(); XHistoryGraph is
|
||||
// cleared and re-seeded with down/up series; each XSysBox gets 60
|
||||
// live set_value() calls). Everything on screen after that is live.
|
||||
//
|
||||
// Build: make (see Makefile) Run: ./network-monitor
|
||||
// Quit: q / Escape / window close.
|
||||
|
||||
#include "XNetGraph.h"
|
||||
#include "XHistoryGraph.h"
|
||||
#include "XUsageBar.h"
|
||||
#include "XSysBox.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <chrono>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <sys/select.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
const char* kTitle = "Network Monitor - monitor";
|
||||
const int kWinW = 1024;
|
||||
const int kWinH = 768;
|
||||
|
||||
// Read rx/tx byte counters for iface from /proc/net/dev.
|
||||
bool read_counters(const std::string& iface, unsigned long long& rx, unsigned long long& tx)
|
||||
{
|
||||
FILE* f = std::fopen("/proc/net/dev", "r");
|
||||
if (!f) return false;
|
||||
char line[512];
|
||||
bool ok = false;
|
||||
while (std::fgets(line, sizeof(line), f))
|
||||
{
|
||||
char* colon = std::strchr(line, ':');
|
||||
if (!colon) continue;
|
||||
std::string name(line, colon);
|
||||
// Trim spaces.
|
||||
size_t b = name.find_first_not_of(" \t");
|
||||
size_t e = name.find_last_not_of(" \t");
|
||||
if (b == std::string::npos) continue;
|
||||
name = name.substr(b, e - b + 1);
|
||||
if (name != iface) continue;
|
||||
unsigned long long v[16] = {0};
|
||||
int n = std::sscanf(colon + 1, "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
|
||||
&v[0], &v[1], &v[2], &v[3], &v[4], &v[5], &v[6], &v[7],
|
||||
&v[8], &v[9], &v[10], &v[11], &v[12], &v[13], &v[14], &v[15]);
|
||||
if (n >= 9)
|
||||
{
|
||||
rx = v[0];
|
||||
tx = v[8];
|
||||
ok = true;
|
||||
}
|
||||
break;
|
||||
}
|
||||
std::fclose(f);
|
||||
return ok;
|
||||
}
|
||||
|
||||
bool iface_exists(const std::string& iface)
|
||||
{
|
||||
unsigned long long rx = 0, tx = 0;
|
||||
return read_counters(iface, rx, tx);
|
||||
}
|
||||
|
||||
// Prefer enp3s0; else the first non-loopback interface with traffic.
|
||||
std::string pick_iface()
|
||||
{
|
||||
if (iface_exists("enp3s0")) return "enp3s0";
|
||||
FILE* f = std::fopen("/proc/net/dev", "r");
|
||||
if (!f) return "enp3s0";
|
||||
char line[512];
|
||||
std::string fallback = "enp3s0";
|
||||
while (std::fgets(line, sizeof(line), f))
|
||||
{
|
||||
char* colon = std::strchr(line, ':');
|
||||
if (!colon) continue;
|
||||
std::string name(line, colon);
|
||||
size_t b = name.find_first_not_of(" \t");
|
||||
size_t e = name.find_last_not_of(" \t");
|
||||
if (b == std::string::npos) continue;
|
||||
name = name.substr(b, e - b + 1);
|
||||
if (name == "lo") continue;
|
||||
unsigned long long v[16] = {0};
|
||||
std::sscanf(colon + 1, "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
|
||||
&v[0], &v[1], &v[2], &v[3], &v[4], &v[5], &v[6], &v[7],
|
||||
&v[8], &v[9], &v[10], &v[11], &v[12], &v[13], &v[14], &v[15]);
|
||||
if (fallback == "enp3s0") fallback = name;
|
||||
if (v[0] > 0 || v[8] > 0) { std::fclose(f); return name; }
|
||||
}
|
||||
std::fclose(f);
|
||||
return fallback;
|
||||
}
|
||||
|
||||
// Link speed in bits/s from /sys/class/net/<iface>/speed (Mbit/s);
|
||||
// unknown/unreadable -> assume 1 Gbit/s.
|
||||
double link_speed_bps(const std::string& iface)
|
||||
{
|
||||
std::string path = "/sys/class/net/" + iface + "/speed";
|
||||
FILE* f = std::fopen(path.c_str(), "r");
|
||||
if (!f) return 1e9;
|
||||
long mbps = 0;
|
||||
if (std::fscanf(f, "%ld", &mbps) != 1 || mbps <= 0) mbps = 1000;
|
||||
std::fclose(f);
|
||||
return (double)mbps * 1e6;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
std::string iface = pick_iface();
|
||||
double link_bps = link_speed_bps(iface);
|
||||
|
||||
Display* display = XOpenDisplay(nullptr);
|
||||
if (!display) return 1;
|
||||
int screen = DefaultScreen(display);
|
||||
Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100,
|
||||
kWinW, kWinH, 1, BlackPixel(display, screen),
|
||||
WhitePixel(display, screen));
|
||||
XStoreName(display, window, kTitle);
|
||||
XSelectInput(display, window, ExposureMask | KeyPressMask | StructureNotifyMask |
|
||||
ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
|
||||
Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False);
|
||||
XSetWMProtocols(display, window, &deleteWindowMessage, 1);
|
||||
XMapWindow(display, window);
|
||||
GC gc = DefaultGC(display, screen);
|
||||
SStyle_Manager styles(display, window, screen, gc);
|
||||
styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120}, Srgb{60, 60, 60},
|
||||
Srgb{20, 20, 20}, 6, 1, "-misc-fixed-medium-r-normal--13-*");
|
||||
styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22}, Srgb{200, 200, 200},
|
||||
Srgb{240, 240, 240}, 6, 1, "-misc-fixed-medium-r-normal--13-*");
|
||||
styles.set_active_style_byName("default");
|
||||
|
||||
XNetGraph net(display, screen, window, gc, &styles, 12, 12, 1000, 300);
|
||||
net.set_text("Network - " + iface);
|
||||
XSysBox down_box(display, screen, window, gc, &styles, 12, 324, 492, 180);
|
||||
down_box.set_text("Download - " + iface);
|
||||
down_box.set_unit(" KiB/s");
|
||||
XSysBox up_box(display, screen, window, gc, &styles, 520, 324, 492, 180);
|
||||
up_box.set_text("Upload - " + iface);
|
||||
up_box.set_unit(" KiB/s");
|
||||
up_box.set_graph_color(Srgb{220, 80, 40});
|
||||
XHistoryGraph hist(display, screen, window, gc, &styles, 12, 516, 1000, 172);
|
||||
hist.set_text("Rate history (KiB/s)");
|
||||
XUsageBar usage(display, screen, window, gc, &styles, 12, 700, 1000, 44);
|
||||
{
|
||||
char buf[96];
|
||||
std::snprintf(buf, sizeof(buf), "Link use - %s (%.0f Gb/s)", iface.c_str(), link_bps / 1e9);
|
||||
usage.set_text(buf);
|
||||
}
|
||||
usage.set_max(100.0);
|
||||
|
||||
auto redraw = [&]() {
|
||||
XClearWindow(display, window);
|
||||
net.draw();
|
||||
down_box.draw();
|
||||
up_box.draw();
|
||||
hist.draw();
|
||||
usage.draw();
|
||||
XFlush(display);
|
||||
};
|
||||
|
||||
// First live rate (two reads 500 ms apart), then flush the widgets'
|
||||
// seeded demo data with it so the screen is live from the start.
|
||||
unsigned long long rx0 = 0, tx0 = 0, rx1 = 0, tx1 = 0;
|
||||
read_counters(iface, rx0, tx0);
|
||||
usleep(500000);
|
||||
read_counters(iface, rx1, tx1);
|
||||
double down_bps = (rx1 >= rx0) ? (double)(rx1 - rx0) / 0.5 : 0.0;
|
||||
double up_bps = (tx1 >= tx0) ? (double)(tx1 - tx0) / 0.5 : 0.0;
|
||||
double down_kb = down_bps / 1024.0;
|
||||
double up_kb = up_bps / 1024.0;
|
||||
double peak_down = std::max(100.0, down_kb * 1.15);
|
||||
double peak_up = std::max(100.0, up_kb * 1.15);
|
||||
|
||||
for (int i = 0; i < 120; ++i) net.push(down_bps, up_bps);
|
||||
net.reset_totals(); // totals restart at session start, not demo-inflated
|
||||
hist.clear_series();
|
||||
hist.add_series("Down KiB/s", Srgb{0, 170, 70});
|
||||
hist.add_series("Up KiB/s", Srgb{220, 80, 40});
|
||||
hist.push(0, down_kb);
|
||||
hist.push(1, up_kb);
|
||||
down_box.set_max(peak_down);
|
||||
up_box.set_max(peak_up);
|
||||
for (int i = 0; i < 60; ++i) { down_box.set_value(down_kb); up_box.set_value(up_kb); }
|
||||
usage.set_value(std::min(100.0, (down_bps + up_bps) / link_bps * 100.0));
|
||||
|
||||
unsigned long long prev_rx = rx1, prev_tx = tx1;
|
||||
auto prev_time = std::chrono::steady_clock::now();
|
||||
redraw();
|
||||
|
||||
int fd = ConnectionNumber(display);
|
||||
bool running = true;
|
||||
auto last_tick = std::chrono::steady_clock::now();
|
||||
while (running)
|
||||
{
|
||||
while (XPending(display))
|
||||
{
|
||||
XEvent event;
|
||||
XNextEvent(display, &event);
|
||||
bool handled = false;
|
||||
switch (event.type)
|
||||
{
|
||||
case Expose:
|
||||
handled = true;
|
||||
break;
|
||||
case ButtonPress:
|
||||
handled = net.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
down_box.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
up_box.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
hist.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
usage.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button);
|
||||
break;
|
||||
case ButtonRelease:
|
||||
handled = net.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
down_box.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
up_box.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
hist.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button) ||
|
||||
usage.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button);
|
||||
break;
|
||||
case MotionNotify:
|
||||
handled = net.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
down_box.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
up_box.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
hist.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
usage.on_motion(event.xmotion.x, event.xmotion.y);
|
||||
break;
|
||||
case KeyPress:
|
||||
{
|
||||
char buffer[32];
|
||||
KeySym key = 0;
|
||||
int n = XLookupString(&event.xkey, buffer, (int)sizeof(buffer) - 1, &key, nullptr);
|
||||
buffer[n] = 0;
|
||||
if (key == XK_Escape || (n == 1 && (buffer[0] == 'q' || buffer[0] == 'Q')))
|
||||
running = false;
|
||||
handled = net.on_key(key, buffer) || down_box.on_key(key, buffer) ||
|
||||
up_box.on_key(key, buffer) || hist.on_key(key, buffer) ||
|
||||
usage.on_key(key, buffer);
|
||||
break;
|
||||
}
|
||||
case ClientMessage:
|
||||
if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) running = false;
|
||||
break;
|
||||
}
|
||||
if (handled) redraw();
|
||||
if (!running) break;
|
||||
}
|
||||
if (!running) break;
|
||||
|
||||
auto now = std::chrono::steady_clock::now();
|
||||
long elapsed_ms = (long)std::chrono::duration_cast<std::chrono::milliseconds>(now - last_tick).count();
|
||||
if (elapsed_ms < 500)
|
||||
{
|
||||
fd_set fds;
|
||||
FD_ZERO(&fds);
|
||||
FD_SET(fd, &fds);
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = (500 - elapsed_ms) * 1000;
|
||||
select(fd + 1, &fds, nullptr, nullptr, &tv);
|
||||
continue;
|
||||
}
|
||||
last_tick = now;
|
||||
|
||||
// Live tick: difference /proc/net/dev over the real elapsed time.
|
||||
unsigned long long rx = 0, tx = 0;
|
||||
if (!read_counters(iface, rx, tx)) continue;
|
||||
double dt = std::chrono::duration<double>(now - prev_time).count();
|
||||
prev_time = now;
|
||||
if (dt <= 0.0) continue;
|
||||
down_bps = (rx >= prev_rx) ? (double)(rx - prev_rx) / dt : 0.0;
|
||||
up_bps = (tx >= prev_tx) ? (double)(tx - prev_tx) / dt : 0.0;
|
||||
prev_rx = rx;
|
||||
prev_tx = tx;
|
||||
down_kb = down_bps / 1024.0;
|
||||
up_kb = up_bps / 1024.0;
|
||||
peak_down = std::max(peak_down, down_kb * 1.15);
|
||||
peak_up = std::max(peak_up, up_kb * 1.15);
|
||||
|
||||
net.push(down_bps, up_bps);
|
||||
hist.push(0, down_kb);
|
||||
hist.push(1, up_kb);
|
||||
down_box.set_max(peak_down);
|
||||
up_box.set_max(peak_up);
|
||||
down_box.set_value(down_kb);
|
||||
up_box.set_value(up_kb);
|
||||
usage.set_value(std::min(100.0, (down_bps + up_bps) / link_bps * 100.0));
|
||||
redraw();
|
||||
}
|
||||
|
||||
XCloseDisplay(display);
|
||||
return 0;
|
||||
}
|
||||
|
After Width: | Height: | Size: 3.8 KiB |
@@ -0,0 +1 @@
|
||||
process-manager
|
||||
@@ -0,0 +1,25 @@
|
||||
# Process Manager - monitor (C++ / XFrame)
|
||||
#
|
||||
# Build:
|
||||
# make # produces ./process-manager
|
||||
# Run (under Xvfb or a real X server):
|
||||
# ./process-manager
|
||||
# Close the window or press Esc to quit.
|
||||
|
||||
ROOT := ../../..
|
||||
CXX := g++
|
||||
CXXFLAGS := -std=c++17 -O2 -Wall -I$(ROOT)/widgets -I$(ROOT)/widgets/components
|
||||
LDFLAGS := -lX11
|
||||
|
||||
all: process-manager
|
||||
|
||||
process-manager: main.cpp \
|
||||
$(ROOT)/widgets/components/XProcessList/XProcessList.h \
|
||||
$(ROOT)/widgets/components/GtkButton/GtkButton.h \
|
||||
$(ROOT)/widgets/components/GtkEntry/GtkEntry.h
|
||||
$(CXX) $(CXXFLAGS) main.cpp $(LDFLAGS) -o $@
|
||||
|
||||
clean:
|
||||
rm -f process-manager
|
||||
|
||||
.PHONY: all clean
|
||||
@@ -0,0 +1,42 @@
|
||||
# Process Manager — monitor (C++)
|
||||
|
||||
A btop-style process manager in one 1024x768 window, built only from
|
||||
LightGUI XFrame widgets: `XProcessList` (live `/proc` table, sortable
|
||||
columns, click a row to select it), `GtkEntry` (filter by name or pid),
|
||||
and three `GtkButton`s — **Terminate**, **Kill**, **Refresh**.
|
||||
|
||||
## What it shows
|
||||
|
||||
- **Processes** — pid, program, user, memory and CPU% for the live
|
||||
processes on this machine, refreshed every 2 seconds. Click a column
|
||||
header to sort by it (click again to reverse); click a body row to
|
||||
select it (highlighted). CPU% is the lifetime average, with an
|
||||
in-cell bar.
|
||||
- **Filter** — type a name or pid substring; the list re-scans `/proc`
|
||||
and shows only matching rows.
|
||||
- **Terminate** — sends `SIGTERM` to the selected process.
|
||||
- **Kill** — sends `SIGKILL` to the selected process.
|
||||
- **Refresh** — re-scans `/proc` immediately.
|
||||
|
||||
Safety: Terminate/Kill act on the selected row's pid only, and refuse
|
||||
pid ≤ 1 and the manager's own pid.
|
||||
|
||||
## Build and run
|
||||
|
||||
```sh
|
||||
make # produces ./process-manager
|
||||
./process-manager # under Xvfb or a real X server; Esc quits
|
||||
```
|
||||
|
||||
## Source
|
||||
|
||||
- App: `examples/monitor/process-manager/main.cpp` in
|
||||
[lightgui](https://github.com/cnmcdee/lightgui)
|
||||
- Widgets: `XProcessList`, `GtkButton`, `GtkEntry` (this app adds
|
||||
row-selection and a name/pid filter to `XProcessList`; additive only,
|
||||
the widget demo is unchanged)
|
||||
|
||||
The same app exists in
|
||||
[Rust](https://github.com/cnmcdee/lightgui_rust) and
|
||||
[Python](https://github.com/cnmcdee/lightgui_python) — see the
|
||||
[walkthrough](https://www.lightgui.com/monitor-process-manager/).
|
||||
@@ -0,0 +1,207 @@
|
||||
// Process Manager - monitor (C++ / XFrame).
|
||||
//
|
||||
// One 1024x768 window: a live XProcessList (/proc, sortable columns,
|
||||
// click a row to select it), a GtkEntry filter (name or pid substring),
|
||||
// and GtkButton Terminate / Kill / Refresh. Terminate sends SIGTERM and
|
||||
// Kill sends SIGKILL to the SELECTED row's pid only -- never anything
|
||||
// else (pid <= 1 and our own pid are refused outright).
|
||||
//
|
||||
// Build: make (see Makefile)
|
||||
// Run under Xvfb (or a real X server); Esc or close the window to quit.
|
||||
|
||||
#include "GtkButton/GtkButton.h"
|
||||
#include "GtkEntry/GtkEntry.h"
|
||||
#include "XProcessList/XProcessList.h"
|
||||
|
||||
#include <csignal>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <ctime>
|
||||
#include <sys/select.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
long now_ms()
|
||||
{
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
|
||||
}
|
||||
|
||||
void redraw(Display* display, Window window, XProcessList& list, GtkEntry& filter,
|
||||
GtkButton& term, GtkButton& kill, GtkButton& refresh)
|
||||
{
|
||||
XClearWindow(display, window);
|
||||
list.draw();
|
||||
filter.draw();
|
||||
term.draw();
|
||||
kill.draw();
|
||||
refresh.draw();
|
||||
XFlush(display);
|
||||
}
|
||||
|
||||
// Send sig to pid; returns false (and does nothing) for pid <= 1,
|
||||
// our own pid, or no selection.
|
||||
bool signal_selected(XProcessList& list, int sig)
|
||||
{
|
||||
int pid = list.selected_pid();
|
||||
if (pid <= 1 || pid == (int)getpid()) return false;
|
||||
return ::kill((pid_t)pid, sig) == 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
Display* display = XOpenDisplay(nullptr);
|
||||
if (!display) return 1;
|
||||
int screen = DefaultScreen(display);
|
||||
Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 1024, 768, 1,
|
||||
BlackPixel(display, screen), WhitePixel(display, screen));
|
||||
XStoreName(display, window, "Process Manager - monitor");
|
||||
XSelectInput(display, window,
|
||||
ExposureMask | KeyPressMask | StructureNotifyMask | ButtonPressMask |
|
||||
ButtonReleaseMask | PointerMotionMask);
|
||||
Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False);
|
||||
XSetWMProtocols(display, window, &deleteWindowMessage, 1);
|
||||
XMapWindow(display, window);
|
||||
GC gc = DefaultGC(display, screen);
|
||||
SStyle_Manager styles(display, window, screen, gc);
|
||||
styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120}, Srgb{60, 60, 60},
|
||||
Srgb{20, 20, 20}, 6, 1, "-misc-fixed-medium-r-normal--13-*");
|
||||
styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22}, Srgb{200, 200, 200},
|
||||
Srgb{240, 240, 240}, 6, 1, "-misc-fixed-medium-r-normal--13-*");
|
||||
styles.set_active_style_byName("default");
|
||||
|
||||
GtkEntry filter(display, screen, window, gc, &styles, 16, 16, 300, 30);
|
||||
filter.set_text(""); // clear the constructor's "Sample text"
|
||||
filter.set_placeholder_text("Filter: name or pid");
|
||||
GtkButton term_btn(display, screen, window, gc, &styles, 336, 16, 120, 30);
|
||||
term_btn.set_text("Terminate");
|
||||
GtkButton kill_btn(display, screen, window, gc, &styles, 466, 16, 120, 30);
|
||||
kill_btn.set_text("Kill");
|
||||
GtkButton refresh_btn(display, screen, window, gc, &styles, 596, 16, 120, 30);
|
||||
refresh_btn.set_text("Refresh");
|
||||
XProcessList list(display, screen, window, gc, &styles, 16, 58, 992, 694);
|
||||
list.set_text("Processes");
|
||||
list.set_max_rows(200);
|
||||
|
||||
term_btn.on_click = [&]() {
|
||||
if (signal_selected(list, SIGTERM)) list.refresh();
|
||||
};
|
||||
kill_btn.on_click = [&]() {
|
||||
if (signal_selected(list, SIGKILL)) list.refresh();
|
||||
};
|
||||
refresh_btn.on_click = [&]() { list.refresh(); };
|
||||
|
||||
redraw(display, window, list, filter, term_btn, kill_btn, refresh_btn);
|
||||
|
||||
bool running = true;
|
||||
long last_poll_ms = now_ms();
|
||||
std::string last_filter;
|
||||
int xfd = ConnectionNumber(display);
|
||||
while (running)
|
||||
{
|
||||
// Wait for X events with a ~500 ms timeout so the 2 s live
|
||||
// refresh tick runs even with no input.
|
||||
fd_set rfds;
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(xfd, &rfds);
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 500000;
|
||||
int ready = select(xfd + 1, &rfds, nullptr, nullptr, &tv);
|
||||
if (ready < 0) break;
|
||||
while (XPending(display) > 0)
|
||||
{
|
||||
XEvent event;
|
||||
XNextEvent(display, &event);
|
||||
bool handled = false;
|
||||
switch (event.type)
|
||||
{
|
||||
case Expose:
|
||||
handled = true;
|
||||
break;
|
||||
case ButtonPress:
|
||||
if (filter.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button))
|
||||
handled = true;
|
||||
else if (term_btn.on_button_press(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button))
|
||||
handled = true;
|
||||
else if (kill_btn.on_button_press(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button))
|
||||
handled = true;
|
||||
else if (refresh_btn.on_button_press(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button))
|
||||
handled = true;
|
||||
else if (list.on_button_press(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button))
|
||||
handled = true;
|
||||
break;
|
||||
case ButtonRelease:
|
||||
if (term_btn.on_button_release(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button))
|
||||
handled = true;
|
||||
else if (kill_btn.on_button_release(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button))
|
||||
handled = true;
|
||||
else if (refresh_btn.on_button_release(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button))
|
||||
handled = true;
|
||||
else if (filter.on_button_release(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button))
|
||||
handled = true;
|
||||
else if (list.on_button_release(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button))
|
||||
handled = true;
|
||||
break;
|
||||
case MotionNotify:
|
||||
handled = filter.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
term_btn.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
kill_btn.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
refresh_btn.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
list.on_motion(event.xmotion.x, event.xmotion.y);
|
||||
break;
|
||||
case KeyPress:
|
||||
{
|
||||
char buffer[32];
|
||||
KeySym key = 0;
|
||||
int n = XLookupString(&event.xkey, buffer, (int)sizeof(buffer) - 1, &key, nullptr);
|
||||
buffer[n] = 0;
|
||||
if (key == XK_Escape)
|
||||
{
|
||||
running = false;
|
||||
break;
|
||||
}
|
||||
handled = filter.on_key(key, buffer);
|
||||
if (handled)
|
||||
{
|
||||
std::string f = filter.get_text();
|
||||
if (f != last_filter)
|
||||
{
|
||||
last_filter = f;
|
||||
list.set_filter(f);
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
case ClientMessage:
|
||||
if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) running = false;
|
||||
break;
|
||||
}
|
||||
if (handled) redraw(display, window, list, filter, term_btn, kill_btn, refresh_btn);
|
||||
}
|
||||
if (now_ms() - last_poll_ms >= 2000)
|
||||
{
|
||||
last_poll_ms = now_ms();
|
||||
list.refresh();
|
||||
redraw(display, window, list, filter, term_btn, kill_btn, refresh_btn);
|
||||
}
|
||||
}
|
||||
|
||||
XCloseDisplay(display);
|
||||
return 0;
|
||||
}
|
||||
|
After Width: | Height: | Size: 12 KiB |
@@ -0,0 +1,28 @@
|
||||
# Service Dashboard (C++) -- XMonitorGrid of live service checks.
|
||||
#
|
||||
# Build: make
|
||||
# Run: DISPLAY=:119 ./service-dashboard (Xvfb or a real X server)
|
||||
# Close the window to quit.
|
||||
#
|
||||
# The include paths mirror the widget demo build line
|
||||
# (widgets/components/XMonitorGrid/CMakeLists.txt + the flat example
|
||||
# in examples/XMonitorGrid.cpp): XMonitorGrid.h includes
|
||||
# "core/xframe_core.h" (widgets/core/) and
|
||||
# "../XMonitorButton/XMonitorGrid.h"'s sibling XMonitorButton.h.
|
||||
|
||||
REPO_ROOT := /root/monitor-wt/service-dashboard/lightgui
|
||||
CXX := g++
|
||||
CXXFLAGS := -std=c++17 -O2 -Wall -Wextra -I$(REPO_ROOT)/widgets/components -I$(REPO_ROOT)/widgets
|
||||
LDLIBS := -lX11
|
||||
|
||||
TARGET := service-dashboard
|
||||
|
||||
all: $(TARGET)
|
||||
|
||||
$(TARGET): main.cpp
|
||||
$(CXX) $(CXXFLAGS) main.cpp $(LDLIBS) -o $@
|
||||
|
||||
clean:
|
||||
rm -f $(TARGET)
|
||||
|
||||
.PHONY: all clean
|
||||
@@ -0,0 +1,59 @@
|
||||
# Service Dashboard — monitor app
|
||||
|
||||
A service/port dashboard in one window (1024×768): a single
|
||||
`XMonitorGrid` of illuminator buttons for the services that actually
|
||||
run on this host (hot_dallas). Each button polls its live check every
|
||||
2 seconds and glows green when up, red when down, dim when unknown.
|
||||
Click a button to toggle its detail line (pid / unit state /
|
||||
open–closed).
|
||||
|
||||
## What it monitors
|
||||
|
||||
| Button | Kind | Check |
|
||||
| --- | --- | --- |
|
||||
| SSH :22 | Port | TCP connect to localhost:22 |
|
||||
| HTTP :80 | Port | TCP connect to localhost:80 (nginx) |
|
||||
| HTTPS :443 | Port | TCP connect to localhost:443 (nginx) |
|
||||
| NPM admin :81 | Port | TCP connect to localhost:81 |
|
||||
| ThinkMelt :2368 | Port | TCP connect to localhost:2368 |
|
||||
| Ghost :5000 | Port | TCP connect to localhost:5000 |
|
||||
| lightgui.com :5009 | Port | TCP connect to localhost:5009 |
|
||||
| Traffic stats :7888 | Port | TCP connect to localhost:7888 (GoAccess) |
|
||||
| dockerd | Process | `/proc/<pid>/comm` == `dockerd` |
|
||||
| containerd | Process | `/proc/<pid>/comm` == `containerd` |
|
||||
| docker.service | Unit | `systemctl is-active docker.service` == `active` |
|
||||
| ssh.service | Unit | `systemctl is-active ssh.service` == `active` |
|
||||
|
||||
No mock or static data: every cell is a live `XMonitorButton` check
|
||||
(`poll()` on a 2 s timer; the C app waits on the X connection with
|
||||
`select()` and a 500 ms timeout).
|
||||
|
||||
## Widgets used
|
||||
|
||||
- `XMonitorGrid` — the panel; one shared `poll_all()` timer, `N/12 up`
|
||||
summary in its title bar.
|
||||
- `XMonitorButton` — one cell per service (kinds Port / Process /
|
||||
Unit, via `set_kind` / `set_target` / `poll()`).
|
||||
|
||||
## Build and run
|
||||
|
||||
```sh
|
||||
# C++
|
||||
make
|
||||
DISPLAY=:119 ./service-dashboard
|
||||
|
||||
# Rust (from the lightgui_rust checkout)
|
||||
cargo run -p xframe-demo --example monitor_service_dashboard
|
||||
|
||||
# Python (from the lightgui_python checkout)
|
||||
PYTHONPATH=/root/monitor-wt/service-dashboard/lightgui_python python3 examples/monitor_service_dashboard/main.py
|
||||
```
|
||||
|
||||
## Source
|
||||
|
||||
- C++: `examples/monitor/service-dashboard/main.cpp` in
|
||||
[lightgui](https://github.com/cnmcdee/lightgui)
|
||||
- Rust: `crates/xframe-demo/examples/monitor_service_dashboard/main.rs`
|
||||
in [lightgui_rust](https://github.com/cnmcdee/lightgui_rust)
|
||||
- Python: `examples/monitor_service_dashboard/main.py` in
|
||||
[lightgui_python](https://github.com/cnmcdee/lightgui_python)
|
||||
@@ -0,0 +1,145 @@
|
||||
// Service Dashboard -- a service/port dashboard in one window (1024x768).
|
||||
//
|
||||
// One XMonitorGrid of illuminator buttons for REAL services on this
|
||||
// host (hot_dallas): TCP ports (sshd, nginx, NPM, ThinkMelt, Ghost
|
||||
// blogs, traffic stats), processes (dockerd, containerd) and systemd
|
||||
// units (docker.service, ssh.service). Each cell polls its live check
|
||||
// every 2 s and glows green when up, red when down; click a cell to
|
||||
// toggle its detail line (pid / active state / open-closed).
|
||||
//
|
||||
// Build: make (or: g++ -std=c++17 -I<repo>/widgets/components
|
||||
// -I<repo>/widgets main.cpp -lX11 -o service-dashboard)
|
||||
// Run under Xvfb (or a real X server); close the window to quit.
|
||||
// Setup mirrors widgets/components/XMonitorGrid/main_XMonitorGrid.cpp.
|
||||
#include "XMonitorGrid/XMonitorGrid.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <ctime>
|
||||
#include <sys/select.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
void add_real_services(XMonitorGrid& grid)
|
||||
{
|
||||
// The grid constructor preloads demo monitors (incl. docker :2375,
|
||||
// which is not a listening port here); drop them and wire the
|
||||
// services that actually run on this host.
|
||||
grid.clear_monitors();
|
||||
grid.set_text("Services -- hot_dallas");
|
||||
grid.set_columns(3);
|
||||
// TCP ports (connect to localhost).
|
||||
grid.add_monitor("SSH :22", XMonitorButton::Port, "22");
|
||||
grid.add_monitor("HTTP :80", XMonitorButton::Port, "80");
|
||||
grid.add_monitor("HTTPS :443", XMonitorButton::Port, "443");
|
||||
grid.add_monitor("NPM admin :81", XMonitorButton::Port, "81");
|
||||
grid.add_monitor("ThinkMelt :2368", XMonitorButton::Port, "2368");
|
||||
grid.add_monitor("Ghost :5000", XMonitorButton::Port, "5000");
|
||||
grid.add_monitor("lightgui.com :5009", XMonitorButton::Port, "5009");
|
||||
grid.add_monitor("Traffic stats :7888", XMonitorButton::Port, "7888");
|
||||
// Processes (matched by /proc/<pid>/comm).
|
||||
grid.add_monitor("dockerd", XMonitorButton::Process, "dockerd");
|
||||
grid.add_monitor("containerd", XMonitorButton::Process, "containerd");
|
||||
// systemd units (systemctl is-active).
|
||||
grid.add_monitor("docker.service", XMonitorButton::Unit, "docker.service");
|
||||
grid.add_monitor("ssh.service", XMonitorButton::Unit, "ssh.service");
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
Display* display = XOpenDisplay(nullptr);
|
||||
if (!display) { return 1; }
|
||||
int screen = DefaultScreen(display);
|
||||
Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 1024, 768, 1, BlackPixel(display, screen), WhitePixel(display, screen));
|
||||
XStoreName(display, window, "Service Dashboard - monitor");
|
||||
XSelectInput(display, window, ExposureMask | KeyPressMask | StructureNotifyMask | ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
|
||||
Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False);
|
||||
XSetWMProtocols(display, window, &deleteWindowMessage, 1);
|
||||
XMapWindow(display, window);
|
||||
GC gc = DefaultGC(display, screen);
|
||||
SStyle_Manager styles(display, window, screen, gc);
|
||||
styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120}, Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1, "-misc-fixed-medium-r-normal--13-*");
|
||||
styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22}, Srgb{200, 200, 200}, Srgb{240, 240, 240}, 6, 1, "-misc-fixed-medium-r-normal--13-*");
|
||||
styles.set_active_style_byName("default");
|
||||
XMonitorGrid grid(display, screen, window, gc, &styles, 16, 16, 992, 736);
|
||||
add_real_services(grid);
|
||||
grid.poll_all();
|
||||
grid.draw();
|
||||
bool running = true;
|
||||
long last_poll_ms = 0;
|
||||
int display_fd = ConnectionNumber(display);
|
||||
while (running)
|
||||
{
|
||||
// Wait up to 500 ms for X events; the timeout is the live
|
||||
// refresh tick (poll every 2 s, redraw on tick).
|
||||
fd_set read_set;
|
||||
FD_ZERO(&read_set);
|
||||
FD_SET(display_fd, &read_set);
|
||||
struct timeval timeout;
|
||||
timeout.tv_sec = 0;
|
||||
timeout.tv_usec = 500000;
|
||||
int ready = select(display_fd + 1, &read_set, nullptr, nullptr, &timeout);
|
||||
if (ready > 0)
|
||||
{
|
||||
while (XPending(display) > 0)
|
||||
{
|
||||
XEvent event;
|
||||
XNextEvent(display, &event);
|
||||
bool handled = false;
|
||||
switch (event.type)
|
||||
{
|
||||
case Expose:
|
||||
handled = true;
|
||||
break;
|
||||
case ButtonPress:
|
||||
handled = grid.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button);
|
||||
break;
|
||||
case ButtonRelease:
|
||||
handled = grid.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button);
|
||||
break;
|
||||
case MotionNotify:
|
||||
handled = grid.on_motion(event.xmotion.x, event.xmotion.y);
|
||||
break;
|
||||
case KeyPress:
|
||||
{
|
||||
char buffer[32];
|
||||
KeySym key = 0;
|
||||
int n = XLookupString(&event.xkey, buffer, (int)sizeof(buffer) - 1, &key, nullptr);
|
||||
buffer[n] = 0;
|
||||
handled = grid.on_key(key, buffer);
|
||||
break;
|
||||
}
|
||||
case ClientMessage:
|
||||
if ((Atom)event.xclient.data.l[0] == deleteWindowMessage)
|
||||
{
|
||||
running = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (handled)
|
||||
{
|
||||
XClearWindow(display, window);
|
||||
grid.draw();
|
||||
XFlush(display);
|
||||
}
|
||||
}
|
||||
}
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
long now_ms = ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
|
||||
if (now_ms - last_poll_ms >= 2000)
|
||||
{
|
||||
last_poll_ms = now_ms;
|
||||
grid.poll_all();
|
||||
XClearWindow(display, window);
|
||||
grid.draw();
|
||||
XFlush(display);
|
||||
}
|
||||
}
|
||||
|
||||
XCloseDisplay(display);
|
||||
return 0;
|
||||
}
|
||||
|
After Width: | Height: | Size: 3.1 KiB |
@@ -0,0 +1,23 @@
|
||||
# System Overview - monitor app (Stage 2, btop-widget project)
|
||||
CXX ?= g++
|
||||
CXXFLAGS ?= -O2 -Wall -Wextra -std=c++17
|
||||
ROOT = ../../..
|
||||
INCLUDES = -I$(ROOT)/widgets/components/XSysBox \
|
||||
-I$(ROOT)/widgets/components/XLoadBadge \
|
||||
-I$(ROOT)/widgets/components/XDonutGauge \
|
||||
-I$(ROOT)/widgets/components/XUsageBar
|
||||
LDLIBS = -lX11
|
||||
|
||||
all: system-overview
|
||||
|
||||
system-overview: main.cpp
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDES) main.cpp $(LDLIBS) -o system-overview
|
||||
|
||||
# Smoke test: build only (the GUI needs an X display; see README).
|
||||
test: all
|
||||
@echo "system-overview: build OK"
|
||||
|
||||
clean:
|
||||
rm -f system-overview
|
||||
|
||||
.PHONY: all test clean
|
||||
@@ -0,0 +1,37 @@
|
||||
# System Overview — monitor app
|
||||
|
||||
A system overview in one 1024x768 window, in the btop style: four
|
||||
`XSysBox` composite boxes (CPU / MEM / NET / DISK), one `XLoadBadge`
|
||||
(live load averages + uptime), two `XDonutGauge`s (CPU, Disk) and three
|
||||
`XUsageBar`s (Memory, Disk, Network). All values are read live from
|
||||
`/proc` (`stat`, `meminfo`, `net/dev`, `loadavg`, `uptime`) and
|
||||
`statvfs("/")`, refreshed every 500 ms. No seeded demo data is shown:
|
||||
the SysBox history graphs are flushed with live ticks before the
|
||||
screenshot is taken (60 samples at 500 ms ≈ 30 s of run time).
|
||||
|
||||
Window title: `System Overview - monitor`.
|
||||
|
||||
## Widgets used
|
||||
|
||||
- `XSysBox` × 4 — CPU, MEM, NET (KB/s), DISK /
|
||||
- `XLoadBadge` × 1 — 1 / 5 / 15 min load averages + uptime
|
||||
- `XDonutGauge` × 2 — CPU, Disk /
|
||||
- `XUsageBar` × 3 — Memory, Disk /, Network KB/s
|
||||
|
||||
## Build and run
|
||||
|
||||
```sh
|
||||
make # builds ./system-overview
|
||||
./system-overview # needs an X display; Esc / q / window-close quits
|
||||
```
|
||||
|
||||
## Source
|
||||
|
||||
- C: `examples/monitor/system-overview/main.cpp` in
|
||||
https://github.com/cnmcdee/lightgui
|
||||
- Rust: `crates/xframe-demo/examples/monitor_system_overview.rs` in
|
||||
https://github.com/cnmcdee/lightgui_rust
|
||||
- Python: `examples/monitor_system_overview/main.py` in
|
||||
https://github.com/cnmcdee/lightgui_python
|
||||
|
||||
Walkthrough: https://www.lightgui.com/monitor-system-overview/
|
||||
@@ -0,0 +1,302 @@
|
||||
// System Overview - monitor app (Stage 2, btop-widget project).
|
||||
//
|
||||
// One 1024x768 window: four XSysBox composites (CPU / MEM / NET / DISK),
|
||||
// one XLoadBadge (live /proc/loadavg + uptime), two XDonutGauges
|
||||
// (CPU, Disk) and two XUsageBars (Memory, Disk). All values are read
|
||||
// live from /proc (stat, meminfo, net/dev, loadavg, uptime) and
|
||||
// statvfs("/") on a 500 ms timer; no mock or static data.
|
||||
//
|
||||
// Build: make Run: ./system-overview (needs an X display)
|
||||
|
||||
#include "XSysBox.h"
|
||||
#include "XLoadBadge.h"
|
||||
#include "XDonutGauge.h"
|
||||
#include "XUsageBar.h"
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/keysym.h>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <sys/select.h>
|
||||
#include <sys/statvfs.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Live data from /proc
|
||||
// ---------------------------------------------------------------------------
|
||||
struct LiveData {
|
||||
double cpu_pct = 0.0;
|
||||
double mem_pct = 0.0;
|
||||
double net_kbps = 0.0; // receive KB/s, all non-loopback interfaces
|
||||
double disk_pct = 0.0; // root filesystem used %
|
||||
double load1 = 0.0, load5 = 0.0, load15 = 0.0;
|
||||
long uptime_secs = 0;
|
||||
};
|
||||
|
||||
static bool read_cpu(double& out)
|
||||
{
|
||||
static unsigned long long prev_idle = 0, prev_total = 0;
|
||||
std::ifstream in("/proc/stat");
|
||||
std::string line;
|
||||
if (!std::getline(in, line)) return false;
|
||||
unsigned long long user, nice, system, idle, iowait, irq, softirq, steal;
|
||||
if (std::sscanf(line.c_str(), "cpu %llu %llu %llu %llu %llu %llu %llu %llu",
|
||||
&user, &nice, &system, &idle, &iowait, &irq, &softirq, &steal) != 8)
|
||||
return false;
|
||||
unsigned long long idle_now = idle + iowait;
|
||||
unsigned long long total_now = user + nice + system + idle + iowait + irq + softirq + steal;
|
||||
if (prev_total == 0) { // first sample: no delta yet
|
||||
prev_idle = idle_now;
|
||||
prev_total = total_now;
|
||||
return false;
|
||||
}
|
||||
unsigned long long d_total = total_now - prev_total;
|
||||
unsigned long long d_idle = idle_now - prev_idle;
|
||||
prev_idle = idle_now;
|
||||
prev_total = total_now;
|
||||
if (d_total == 0) return false;
|
||||
out = 100.0 * (1.0 - (double)d_idle / (double)d_total);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool read_mem(double& out)
|
||||
{
|
||||
std::ifstream in("/proc/meminfo");
|
||||
std::string key;
|
||||
unsigned long long total = 0, avail = 0, value;
|
||||
std::string unit;
|
||||
while (in >> key >> value >> unit) {
|
||||
if (key == "MemTotal:") total = value;
|
||||
else if (key == "MemAvailable:") avail = value;
|
||||
}
|
||||
if (total == 0) return false;
|
||||
out = 100.0 * (1.0 - (double)avail / (double)total);
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool read_net(double& out)
|
||||
{
|
||||
static unsigned long long prev_rx = 0;
|
||||
static struct timeval prev_tv = {0, 0};
|
||||
std::ifstream in("/proc/net/dev");
|
||||
std::string line;
|
||||
unsigned long long rx_total = 0;
|
||||
std::getline(in, line); // headers
|
||||
std::getline(in, line);
|
||||
while (std::getline(in, line)) {
|
||||
char iface[64];
|
||||
unsigned long long rx = 0;
|
||||
if (std::sscanf(line.c_str(), " %63[^:]: %llu", iface, &rx) != 2) continue;
|
||||
if (std::strcmp(iface, "lo") == 0) continue;
|
||||
rx_total += rx;
|
||||
}
|
||||
struct timeval now;
|
||||
gettimeofday(&now, nullptr);
|
||||
if (prev_tv.tv_sec == 0) {
|
||||
prev_rx = rx_total;
|
||||
prev_tv = now;
|
||||
return false;
|
||||
}
|
||||
double dt = (now.tv_sec - prev_tv.tv_sec) +
|
||||
(now.tv_usec - prev_tv.tv_usec) / 1e6;
|
||||
prev_tv = now;
|
||||
if (dt <= 0.0) return false;
|
||||
out = (double)(rx_total - prev_rx) / dt / 1024.0;
|
||||
prev_rx = rx_total;
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool read_disk(double& out)
|
||||
{
|
||||
struct statvfs st;
|
||||
if (statvfs("/", &st) != 0) return false;
|
||||
if (st.f_blocks == 0) return false;
|
||||
out = 100.0 * (1.0 - (double)st.f_bavail / (double)st.f_blocks);
|
||||
return true;
|
||||
}
|
||||
|
||||
static void read_load(LiveData& d)
|
||||
{
|
||||
std::ifstream in("/proc/loadavg");
|
||||
if (in) in >> d.load1 >> d.load5 >> d.load15;
|
||||
std::ifstream up("/proc/uptime");
|
||||
double secs = 0.0;
|
||||
if (up && (up >> secs)) d.uptime_secs = (long)secs;
|
||||
}
|
||||
|
||||
static void sample(LiveData& d)
|
||||
{
|
||||
double v;
|
||||
if (read_cpu(v)) d.cpu_pct = v;
|
||||
if (read_mem(v)) d.mem_pct = v;
|
||||
if (read_net(v)) d.net_kbps = v;
|
||||
if (read_disk(v)) d.disk_pct = v;
|
||||
read_load(d);
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// App
|
||||
// ---------------------------------------------------------------------------
|
||||
int main()
|
||||
{
|
||||
Display* display = XOpenDisplay(nullptr);
|
||||
if (!display) {
|
||||
std::fprintf(stderr, "system-overview: cannot open display\n");
|
||||
return 1;
|
||||
}
|
||||
int screen = DefaultScreen(display);
|
||||
Window window = XCreateSimpleWindow(display, RootWindow(display, screen),
|
||||
100, 100, 1024, 768, 1,
|
||||
BlackPixel(display, screen),
|
||||
WhitePixel(display, screen));
|
||||
XStoreName(display, window, "System Overview - monitor");
|
||||
XSelectInput(display, window, ExposureMask | KeyPressMask | StructureNotifyMask |
|
||||
ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
|
||||
Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False);
|
||||
XSetWMProtocols(display, window, &deleteWindowMessage, 1);
|
||||
XMapWindow(display, window);
|
||||
GC gc = DefaultGC(display, screen);
|
||||
SStyle_Manager styles(display, window, screen, gc);
|
||||
styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120},
|
||||
Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1,
|
||||
"-misc-fixed-medium-r-normal--13-*");
|
||||
styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22},
|
||||
Srgb{200, 200, 200}, Srgb{240, 240, 240}, 6, 1,
|
||||
"-misc-fixed-medium-r-normal--13-*");
|
||||
styles.set_active_style_byName("default");
|
||||
|
||||
// Row 0: load badge across the top.
|
||||
XLoadBadge badge(display, screen, window, gc, &styles, 16, 12, 992, 84);
|
||||
// Rows 1-2: 2x2 grid of system boxes.
|
||||
XSysBox cpu(display, screen, window, gc, &styles, 16, 108, 488, 200);
|
||||
cpu.set_text("CPU");
|
||||
cpu.set_unit("%");
|
||||
cpu.set_max(100.0);
|
||||
XSysBox mem(display, screen, window, gc, &styles, 520, 108, 488, 200);
|
||||
mem.set_text("MEM");
|
||||
mem.set_unit("%");
|
||||
mem.set_max(100.0);
|
||||
XSysBox net(display, screen, window, gc, &styles, 16, 320, 488, 200);
|
||||
net.set_text("NET");
|
||||
net.set_unit(" KB/s");
|
||||
net.set_max(1000.0);
|
||||
net.set_graph_color(Srgb{0, 150, 200});
|
||||
XSysBox disk(display, screen, window, gc, &styles, 520, 320, 488, 200);
|
||||
disk.set_text("DISK /");
|
||||
disk.set_unit("%");
|
||||
disk.set_max(100.0);
|
||||
disk.set_graph_color(Srgb{200, 120, 0});
|
||||
// Row 3: donut gauges + usage bars.
|
||||
XDonutGauge donut_cpu(display, screen, window, gc, &styles, 16, 532, 180, 180);
|
||||
donut_cpu.set_text("CPU");
|
||||
donut_cpu.set_ring_color_rgb(0, 170, 70);
|
||||
XDonutGauge donut_disk(display, screen, window, gc, &styles, 212, 532, 180, 180);
|
||||
donut_disk.set_text("Disk /");
|
||||
donut_disk.set_ring_color_rgb(200, 120, 0);
|
||||
XUsageBar bar_mem(display, screen, window, gc, &styles, 408, 532, 600, 44);
|
||||
bar_mem.set_text("Memory");
|
||||
bar_mem.set_max(100.0);
|
||||
XUsageBar bar_disk(display, screen, window, gc, &styles, 408, 596, 600, 44);
|
||||
bar_disk.set_text("Disk /");
|
||||
bar_disk.set_max(100.0);
|
||||
XUsageBar bar_net(display, screen, window, gc, &styles, 408, 660, 600, 44);
|
||||
bar_net.set_text("Network KB/s");
|
||||
bar_net.set_max(1000.0);
|
||||
|
||||
auto draw_all = [&]() {
|
||||
XClearWindow(display, window);
|
||||
badge.draw();
|
||||
cpu.draw();
|
||||
mem.draw();
|
||||
net.draw();
|
||||
disk.draw();
|
||||
donut_cpu.draw();
|
||||
donut_disk.draw();
|
||||
bar_mem.draw();
|
||||
bar_disk.draw();
|
||||
bar_net.draw();
|
||||
XFlush(display);
|
||||
};
|
||||
|
||||
LiveData data;
|
||||
sample(data); // first live sample before the first paint
|
||||
auto push = [&]() {
|
||||
cpu.set_value(data.cpu_pct);
|
||||
mem.set_value(data.mem_pct);
|
||||
net.set_value(data.net_kbps);
|
||||
disk.set_value(data.disk_pct);
|
||||
badge.set_loads(data.load1, data.load5, data.load15);
|
||||
badge.set_uptime_secs(data.uptime_secs);
|
||||
donut_cpu.set_value(data.cpu_pct);
|
||||
donut_disk.set_value(data.disk_pct);
|
||||
bar_mem.set_value(data.mem_pct);
|
||||
bar_disk.set_value(data.disk_pct);
|
||||
bar_net.set_value(data.net_kbps);
|
||||
};
|
||||
push();
|
||||
draw_all();
|
||||
|
||||
int xfd = ConnectionNumber(display);
|
||||
bool running = true;
|
||||
while (running) {
|
||||
fd_set rfds;
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(xfd, &rfds);
|
||||
struct timeval tv = {0, 500000}; // 500 ms live refresh
|
||||
int n = select(xfd + 1, &rfds, nullptr, nullptr, &tv);
|
||||
if (n == 0) { // timeout: poll /proc, push, redraw
|
||||
sample(data);
|
||||
push();
|
||||
draw_all();
|
||||
continue;
|
||||
}
|
||||
if (n < 0) continue;
|
||||
while (XPending(display)) {
|
||||
XEvent event;
|
||||
XNextEvent(display, &event);
|
||||
bool handled = false;
|
||||
switch (event.type) {
|
||||
case Expose:
|
||||
draw_all();
|
||||
break;
|
||||
case ButtonPress:
|
||||
handled = badge.on_button_press(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button) ||
|
||||
cpu.on_button_press(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button) ||
|
||||
mem.on_button_press(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button) ||
|
||||
net.on_button_press(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button) ||
|
||||
disk.on_button_press(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button);
|
||||
break;
|
||||
case ButtonRelease:
|
||||
break;
|
||||
case MotionNotify:
|
||||
break;
|
||||
case KeyPress: {
|
||||
char buffer[32];
|
||||
KeySym key = 0;
|
||||
int k = XLookupString(&event.xkey, buffer, (int)sizeof(buffer) - 1,
|
||||
&key, nullptr);
|
||||
buffer[k] = 0;
|
||||
if (key == XK_Escape || (k == 1 && (buffer[0] == 'q' || buffer[0] == 'Q')))
|
||||
running = false;
|
||||
break;
|
||||
}
|
||||
case ClientMessage:
|
||||
if ((Atom)event.xclient.data.l[0] == deleteWindowMessage)
|
||||
running = false;
|
||||
break;
|
||||
}
|
||||
if (handled) draw_all();
|
||||
}
|
||||
}
|
||||
|
||||
XCloseDisplay(display);
|
||||
return 0;
|
||||
}
|
||||
|
After Width: | Height: | Size: 8.7 KiB |
@@ -0,0 +1,13 @@
|
||||
CXX ?= g++
|
||||
CXXFLAGS ?= -std=c++17 -O2 -I../../../widgets/components -I../../../widgets
|
||||
LDFLAGS ?= -lX11
|
||||
|
||||
all: task-manager
|
||||
|
||||
task-manager: main.cpp
|
||||
$(CXX) $(CXXFLAGS) main.cpp $(LDFLAGS) -o task-manager
|
||||
|
||||
clean:
|
||||
rm -f task-manager
|
||||
|
||||
.PHONY: all clean
|
||||
@@ -0,0 +1,34 @@
|
||||
# Task Manager — monitor app
|
||||
|
||||
A Windows-style Task Manager clone in one 1024×768 window, built from
|
||||
the LightGUI XFrame monitor widgets (Stage 2 of the btop-widget
|
||||
project).
|
||||
|
||||
## Tabs
|
||||
|
||||
- **Processes** — `XProcessList`: live `/proc` table, columns Pid /
|
||||
Program / User / MemB / Cpu%, click a column header to sort (click
|
||||
again to reverse). Refreshes every 2 s.
|
||||
- **Performance** — `XHistoryGraph` (60 s CPU + memory), `XCoreGrid`
|
||||
(one mini history graph per CPU core), `XUsageBar` (Memory, Disk /,
|
||||
Swap), `XStackBar` (memory Used / Cached / Buffers / Free in GiB),
|
||||
`XNetGraph` (live download / upload bytes/s from `/proc/net/dev`).
|
||||
All graphs refresh every 500 ms from live `/proc` data
|
||||
(`/proc/stat`, `/proc/meminfo`, `/proc/net/dev`, `statvfs`).
|
||||
|
||||
## Build and run
|
||||
|
||||
```sh
|
||||
make
|
||||
DISPLAY=:112 ./task-manager
|
||||
```
|
||||
|
||||
Close the window (or press `q` / Esc) to quit.
|
||||
|
||||
## Widgets used
|
||||
|
||||
`TPageControl`, `XProcessList`, `XHistoryGraph`, `XCoreGrid`,
|
||||
`XUsageBar`, `XStackBar`, `XNetGraph` — all from the LightGUI
|
||||
registry. No raw X11 drawing for UI elements: the only raw X11 here
|
||||
is the window shell and the event loop, as in the per-widget demo
|
||||
mains.
|
||||
@@ -0,0 +1,420 @@
|
||||
// Task Manager - monitor app (Stage 2, btop-widget project).
|
||||
//
|
||||
// A Windows-style Task Manager clone in one 1024x768 window, tabbed
|
||||
// with TPageControl: a Processes tab (XProcessList, live /proc table
|
||||
// with sortable CPU/mem columns) and a Performance tab (XHistoryGraph
|
||||
// 60 s CPU+memory, XCoreGrid per-core grid, XUsageBar memory/disk/swap,
|
||||
// XStackBar memory breakdown, XNetGraph live network rates).
|
||||
//
|
||||
// Every visible UI element is a LightGUI registry widget; the only
|
||||
// raw X11 here is the window shell (create/map/select-input) and the
|
||||
// event loop, exactly like the per-widget demo mains.
|
||||
//
|
||||
// Build: make (g++ -std=c++17, -lX11)
|
||||
// Run: DISPLAY=:112 ./task-manager (or any X server)
|
||||
|
||||
#include "TPageControl/TPageControl.h"
|
||||
#include "XCoreGrid/XCoreGrid.h"
|
||||
#include "XHistoryGraph/XHistoryGraph.h"
|
||||
#include "XNetGraph/XNetGraph.h"
|
||||
#include "XProcessList/XProcessList.h"
|
||||
#include "XStackBar/XStackBar.h"
|
||||
#include "XUsageBar/XUsageBar.h"
|
||||
|
||||
#include <X11/keysym.h>
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <sys/statvfs.h>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct CpuTimes
|
||||
{
|
||||
unsigned long long idle = 0;
|
||||
unsigned long long total = 0;
|
||||
};
|
||||
|
||||
bool read_cpu(CpuTimes& total, std::vector<CpuTimes>& cores)
|
||||
{
|
||||
std::ifstream in("/proc/stat");
|
||||
if (!in) return false;
|
||||
std::string line;
|
||||
cores.clear();
|
||||
bool got_total = false;
|
||||
while (std::getline(in, line))
|
||||
{
|
||||
if (line.compare(0, 3, "cpu") != 0) break;
|
||||
if (line.size() > 3 && line[3] != ' ' && (line[3] < '0' || line[3] > '9')) break;
|
||||
// Fields start after the first space: for "cpu" that is
|
||||
// offset 3, for "cpuN" offset 4 (the core index digit must
|
||||
// not be parsed as the first counter).
|
||||
size_t fields_at = line.find(' ');
|
||||
if (fields_at == std::string::npos) continue;
|
||||
unsigned long long nums[10] = {0};
|
||||
int n = std::sscanf(line.c_str() + fields_at, "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
|
||||
&nums[0], &nums[1], &nums[2], &nums[3], &nums[4],
|
||||
&nums[5], &nums[6], &nums[7], &nums[8], &nums[9]);
|
||||
if (n < 4) continue;
|
||||
CpuTimes t;
|
||||
t.idle = nums[3] + nums[4];
|
||||
for (int i = 0; i < n; ++i) t.total += nums[i];
|
||||
if (line[3] == ' ')
|
||||
{
|
||||
total = t;
|
||||
got_total = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
cores.push_back(t);
|
||||
}
|
||||
}
|
||||
return got_total && !cores.empty();
|
||||
}
|
||||
|
||||
double cpu_pct(const CpuTimes& prev, const CpuTimes& cur)
|
||||
{
|
||||
unsigned long long d_total = cur.total - prev.total;
|
||||
unsigned long long d_idle = cur.idle - prev.idle;
|
||||
if (d_total == 0) return 0.0;
|
||||
double pct = 100.0 * (1.0 - (double)d_idle / (double)d_total);
|
||||
return pct < 0.0 ? 0.0 : (pct > 100.0 ? 100.0 : pct);
|
||||
}
|
||||
|
||||
struct MemInfo
|
||||
{
|
||||
double total_gb = 0.0;
|
||||
double free_gb = 0.0;
|
||||
double buffers_gb = 0.0;
|
||||
double cached_gb = 0.0;
|
||||
double used_gb = 0.0;
|
||||
double used_pct = 0.0;
|
||||
double swap_total_gb = 0.0;
|
||||
double swap_used_pct = 0.0;
|
||||
};
|
||||
|
||||
bool read_mem(MemInfo& m)
|
||||
{
|
||||
std::ifstream in("/proc/meminfo");
|
||||
if (!in) return false;
|
||||
unsigned long long total_kb = 0, free_kb = 0, buf_kb = 0, cache_kb = 0;
|
||||
unsigned long long swap_total_kb = 0, swap_free_kb = 0;
|
||||
std::string key;
|
||||
unsigned long long val = 0;
|
||||
while (in >> key >> val)
|
||||
{
|
||||
if (key == "MemTotal:") total_kb = val;
|
||||
else if (key == "MemFree:") free_kb = val;
|
||||
else if (key == "Buffers:") buf_kb = val;
|
||||
else if (key == "Cached:") cache_kb = val;
|
||||
else if (key == "SwapTotal:") swap_total_kb = val;
|
||||
else if (key == "SwapFree:") swap_free_kb = val;
|
||||
std::string skip;
|
||||
std::getline(in, skip);
|
||||
}
|
||||
if (total_kb == 0) return false;
|
||||
const double gb = 1024.0 * 1024.0;
|
||||
m.total_gb = total_kb / gb;
|
||||
m.free_gb = free_kb / gb;
|
||||
m.buffers_gb = buf_kb / gb;
|
||||
m.cached_gb = cache_kb / gb;
|
||||
m.used_gb = (total_kb - free_kb - buf_kb - cache_kb) / gb;
|
||||
if (m.used_gb < 0.0) m.used_gb = 0.0;
|
||||
m.used_pct = 100.0 * m.used_gb / m.total_gb;
|
||||
m.swap_total_gb = swap_total_kb / gb;
|
||||
m.swap_used_pct = swap_total_kb > 0
|
||||
? 100.0 * (double)(swap_total_kb - swap_free_kb) / (double)swap_total_kb
|
||||
: 0.0;
|
||||
return true;
|
||||
}
|
||||
|
||||
struct NetBytes
|
||||
{
|
||||
unsigned long long rx = 0;
|
||||
unsigned long long tx = 0;
|
||||
};
|
||||
|
||||
bool read_net(NetBytes& n)
|
||||
{
|
||||
std::ifstream in("/proc/net/dev");
|
||||
if (!in) return false;
|
||||
std::string line;
|
||||
std::getline(in, line);
|
||||
std::getline(in, line);
|
||||
n.rx = 0;
|
||||
n.tx = 0;
|
||||
while (std::getline(in, line))
|
||||
{
|
||||
size_t colon = line.find(':');
|
||||
if (colon == std::string::npos) continue;
|
||||
std::string iface = line.substr(0, colon);
|
||||
size_t first = iface.find_first_not_of(" \t");
|
||||
if (first != std::string::npos) iface = iface.substr(first);
|
||||
if (iface == "lo") continue;
|
||||
unsigned long long rx = 0, tx = 0;
|
||||
// rx_bytes is field 1, tx_bytes field 9 after the colon.
|
||||
if (std::sscanf(line.c_str() + colon + 1, "%llu %*u %*u %*u %*u %*u %*u %*u %llu",
|
||||
&rx, &tx) == 2)
|
||||
{
|
||||
n.rx += rx;
|
||||
n.tx += tx;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
double disk_used_pct()
|
||||
{
|
||||
struct statvfs v;
|
||||
if (statvfs("/", &v) != 0 || v.f_blocks == 0) return 0.0;
|
||||
return 100.0 * (double)(v.f_blocks - v.f_bfree) / (double)v.f_blocks;
|
||||
}
|
||||
|
||||
long now_ms()
|
||||
{
|
||||
struct timespec ts;
|
||||
clock_gettime(CLOCK_MONOTONIC, &ts);
|
||||
return (long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
Display* display = XOpenDisplay(nullptr);
|
||||
if (!display) return 1;
|
||||
int screen = DefaultScreen(display);
|
||||
Window window = XCreateSimpleWindow(display, RootWindow(display, screen),
|
||||
100, 100, 1024, 768, 1,
|
||||
BlackPixel(display, screen),
|
||||
WhitePixel(display, screen));
|
||||
XStoreName(display, window, "Task Manager - monitor");
|
||||
XSelectInput(display, window, ExposureMask | KeyPressMask | StructureNotifyMask |
|
||||
ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
|
||||
Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False);
|
||||
XSetWMProtocols(display, window, &deleteWindowMessage, 1);
|
||||
XMapWindow(display, window);
|
||||
GC gc = DefaultGC(display, screen);
|
||||
SStyle_Manager styles(display, window, screen, gc);
|
||||
styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120},
|
||||
Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1,
|
||||
"-misc-fixed-medium-r-normal--13-*");
|
||||
styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22},
|
||||
Srgb{200, 200, 200}, Srgb{240, 240, 240}, 6, 1,
|
||||
"-misc-fixed-medium-r-normal--13-*");
|
||||
styles.set_active_style_byName("default");
|
||||
|
||||
// Tabs first: the page widgets are drawn after it, over its
|
||||
// centred "Page: ..." placeholder label.
|
||||
TPageControl tabs(display, screen, window, gc, &styles, 8, 8, 1008, 752);
|
||||
// The constructor seeds TabSheet1..3; drop them (remove_page keeps
|
||||
// at least one page, so remove two, add ours, then remove the last).
|
||||
tabs.remove_page(2);
|
||||
tabs.remove_page(1);
|
||||
tabs.add_page("Processes");
|
||||
tabs.add_page("Performance");
|
||||
tabs.remove_page(0);
|
||||
|
||||
// Processes page (page body is 11,35 - 1013,757).
|
||||
XProcessList proclist(display, screen, window, gc, &styles, 14, 38, 996, 712);
|
||||
proclist.set_max_rows(40);
|
||||
|
||||
// Performance page.
|
||||
XHistoryGraph hist(display, screen, window, gc, &styles, 14, 38, 630, 300);
|
||||
hist.set_text("CPU + Memory (60 s)");
|
||||
hist.clear_series();
|
||||
hist.add_series("CPU", Srgb{0, 170, 70});
|
||||
hist.add_series("MEM", Srgb{0, 100, 200});
|
||||
hist.set_y_range(0.0, 100.0);
|
||||
hist.set_window_seconds(60);
|
||||
|
||||
XCoreGrid cores(display, screen, window, gc, &styles, 652, 38, 354, 300);
|
||||
cores.set_text("CPU cores");
|
||||
|
||||
XUsageBar membar(display, screen, window, gc, &styles, 14, 346, 640, 34);
|
||||
membar.set_text("Memory");
|
||||
XStackBar memstack(display, screen, window, gc, &styles, 14, 388, 640, 60);
|
||||
memstack.set_text("Memory");
|
||||
memstack.clear_segments();
|
||||
memstack.add_segment("Used", 0.0, Srgb{0, 150, 60});
|
||||
memstack.add_segment("Cached", 0.0, Srgb{220, 180, 0});
|
||||
memstack.add_segment("Buffers", 0.0, Srgb{0, 120, 200});
|
||||
memstack.add_segment("Free", 0.0, Srgb{200, 200, 200});
|
||||
|
||||
XUsageBar diskbar(display, screen, window, gc, &styles, 662, 346, 344, 34);
|
||||
diskbar.set_text("Disk /");
|
||||
XUsageBar swapbar(display, screen, window, gc, &styles, 662, 388, 344, 34);
|
||||
swapbar.set_text("Swap");
|
||||
|
||||
XNetGraph net(display, screen, window, gc, &styles, 14, 456, 992, 294);
|
||||
net.set_text("Network");
|
||||
|
||||
// clear=true wipes the window first (tick + tab switch, so the old
|
||||
// page leaves no stale pixels in the gaps); Expose and sort clicks
|
||||
// repaint only, because XClearWindow itself generates the Expose
|
||||
// that would otherwise retrigger a clear forever.
|
||||
auto redraw = [&](bool clear) {
|
||||
if (clear) XClearWindow(display, window);
|
||||
tabs.draw();
|
||||
if (tabs.get_active_page() == 0)
|
||||
{
|
||||
proclist.draw();
|
||||
}
|
||||
else
|
||||
{
|
||||
hist.draw();
|
||||
cores.draw();
|
||||
membar.draw();
|
||||
memstack.draw();
|
||||
diskbar.draw();
|
||||
swapbar.draw();
|
||||
net.draw();
|
||||
}
|
||||
XFlush(display);
|
||||
};
|
||||
|
||||
// Prime live data: two /proc/stat samples 100 ms apart give the
|
||||
// first real CPU/net rates; the history widgets are then filled
|
||||
// with that live value so no seeded demo data shows.
|
||||
CpuTimes prev_total, cur_total;
|
||||
std::vector<CpuTimes> prev_cores, cur_cores;
|
||||
read_cpu(prev_total, prev_cores);
|
||||
NetBytes prev_net, cur_net;
|
||||
read_net(prev_net);
|
||||
usleep(100000);
|
||||
read_cpu(cur_total, cur_cores);
|
||||
read_net(cur_net);
|
||||
int ncores = (int)cur_cores.size();
|
||||
cores.set_core_count(ncores);
|
||||
std::vector<double> per_core(ncores, 0.0);
|
||||
for (int i = 0; i < ncores; ++i)
|
||||
per_core[i] = cpu_pct(prev_cores[i], cur_cores[i]);
|
||||
double cpu0 = cpu_pct(prev_total, cur_total);
|
||||
MemInfo mem;
|
||||
read_mem(mem);
|
||||
hist.push_all(std::vector<double>{cpu0, mem.used_pct});
|
||||
for (int i = 0; i < 60; ++i) cores.push_all(per_core);
|
||||
double down0 = (double)(cur_net.rx - prev_net.rx) * 10.0;
|
||||
double up0 = (double)(cur_net.tx - prev_net.tx) * 10.0;
|
||||
for (int i = 0; i < 120; ++i) net.push(down0, up0);
|
||||
membar.set_value(mem.used_pct);
|
||||
memstack.set_segment(0, mem.used_gb);
|
||||
memstack.set_segment(1, mem.cached_gb);
|
||||
memstack.set_segment(2, mem.buffers_gb);
|
||||
memstack.set_segment(3, mem.free_gb);
|
||||
diskbar.set_value(disk_used_pct());
|
||||
swapbar.set_value(mem.swap_used_pct);
|
||||
prev_total = cur_total;
|
||||
prev_cores = cur_cores;
|
||||
prev_net = cur_net;
|
||||
|
||||
redraw(true);
|
||||
bool running = true;
|
||||
long last_poll_ms = now_ms();
|
||||
long last_proc_ms = 0;
|
||||
int tick = 0;
|
||||
int last_page = tabs.get_active_page();
|
||||
while (running)
|
||||
{
|
||||
while (XPending(display) > 0)
|
||||
{
|
||||
XEvent event;
|
||||
XNextEvent(display, &event);
|
||||
bool handled = false;
|
||||
bool clear = false;
|
||||
switch (event.type)
|
||||
{
|
||||
case Expose:
|
||||
handled = true;
|
||||
break;
|
||||
case ButtonPress:
|
||||
handled = tabs.on_button_press(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button);
|
||||
if (handled && tabs.get_active_page() != last_page)
|
||||
{
|
||||
last_page = tabs.get_active_page();
|
||||
clear = true;
|
||||
}
|
||||
if (!handled && tabs.get_active_page() == 0)
|
||||
handled = proclist.on_button_press(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button);
|
||||
break;
|
||||
case ButtonRelease:
|
||||
if (tabs.get_active_page() == 0)
|
||||
handled = proclist.on_button_release(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button);
|
||||
break;
|
||||
case MotionNotify:
|
||||
if (tabs.get_active_page() == 0)
|
||||
handled = proclist.on_motion(event.xmotion.x, event.xmotion.y);
|
||||
break;
|
||||
case KeyPress:
|
||||
{
|
||||
char buffer[32];
|
||||
KeySym key = 0;
|
||||
int n = XLookupString(&event.xkey, buffer, (int)sizeof(buffer) - 1, &key, nullptr);
|
||||
buffer[n] = 0;
|
||||
if (key == XK_Escape || (n > 0 && (buffer[0] == 'q' || buffer[0] == 'Q')))
|
||||
running = false;
|
||||
handled = tabs.on_key(key, buffer);
|
||||
break;
|
||||
}
|
||||
case ClientMessage:
|
||||
if ((Atom)event.xclient.data.l[0] == deleteWindowMessage)
|
||||
running = false;
|
||||
break;
|
||||
}
|
||||
if (handled) redraw(clear);
|
||||
}
|
||||
long now = now_ms();
|
||||
if (now - last_poll_ms >= 500)
|
||||
{
|
||||
long dt_ms = now - last_poll_ms;
|
||||
last_poll_ms = now;
|
||||
++tick;
|
||||
read_cpu(cur_total, cur_cores);
|
||||
read_net(cur_net);
|
||||
read_mem(mem);
|
||||
double cpu = cpu_pct(prev_total, cur_total);
|
||||
for (int i = 0; i < ncores && i < (int)cur_cores.size(); ++i)
|
||||
per_core[i] = cpu_pct(prev_cores[i], cur_cores[i]);
|
||||
hist.push_all(std::vector<double>{cpu, mem.used_pct});
|
||||
cores.push_all(per_core);
|
||||
double dt_s = dt_ms / 1000.0;
|
||||
net.push((double)(cur_net.rx - prev_net.rx) / dt_s,
|
||||
(double)(cur_net.tx - prev_net.tx) / dt_s);
|
||||
membar.set_value(mem.used_pct);
|
||||
memstack.set_segment(0, mem.used_gb);
|
||||
memstack.set_segment(1, mem.cached_gb);
|
||||
memstack.set_segment(2, mem.buffers_gb);
|
||||
memstack.set_segment(3, mem.free_gb);
|
||||
diskbar.set_value(disk_used_pct());
|
||||
swapbar.set_value(mem.swap_used_pct);
|
||||
prev_total = cur_total;
|
||||
prev_cores = cur_cores;
|
||||
prev_net = cur_net;
|
||||
if (now - last_proc_ms >= 2000)
|
||||
{
|
||||
last_proc_ms = now;
|
||||
proclist.refresh();
|
||||
}
|
||||
// Data is sampled every 500 ms, but repainting is every
|
||||
// 2 s (every 4th tick): every widget colour goes through
|
||||
// SStyle_Manager::get_color_Srgb, which is one XAllocColor
|
||||
// round trip per call, and a full repaint issues ~500 of
|
||||
// them (~0.5 s wall on this Xvfb). Repainting every tick
|
||||
// would never catch up and screenshots would catch the
|
||||
// paint mid-draw; at 2 s each repaint completes inside
|
||||
// its interval and the graphs still advance live.
|
||||
if (tick % 4 == 0) redraw(true);
|
||||
}
|
||||
usleep(20000);
|
||||
}
|
||||
|
||||
XCloseDisplay(display);
|
||||
return 0;
|
||||
}
|
||||
|
After Width: | Height: | Size: 13 KiB |
|
After Width: | Height: | Size: 11 KiB |
@@ -0,0 +1,18 @@
|
||||
CXX ?= g++
|
||||
CXXFLAGS ?= -std=c++17 -O2 -Wall -Wextra
|
||||
|
||||
# One -I per widget dir: each widget header includes "core/xframe_core.h"
|
||||
# from its own component directory.
|
||||
INCLUDES := -I../../../widgets/components/XTempHistory
|
||||
|
||||
LDFLAGS := -lX11
|
||||
|
||||
all: temperature-monitor
|
||||
|
||||
temperature-monitor: main.cpp
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDES) main.cpp -o temperature-monitor $(LDFLAGS)
|
||||
|
||||
clean:
|
||||
rm -f temperature-monitor
|
||||
|
||||
.PHONY: all clean
|
||||
@@ -0,0 +1,38 @@
|
||||
# Temperature Monitor — monitor (C)
|
||||
|
||||
A btop-style temperature monitor in one 1024x768 window, built only
|
||||
from LightGUI XFrame monitor widgets:
|
||||
|
||||
- **XTempHistory** — temperature history of the first thermal zone,
|
||||
with an 80 C threshold line (readings above it draw red)
|
||||
|
||||
Live data comes from `/sys/class/thermal/thermal_zone*/temp` and
|
||||
`/sys/class/hwmon/hwmon*/temp*_input`, re-scanned every 2 s. On a
|
||||
machine with sensors the history fills with live readings; on this
|
||||
machine (hot_dallas, a VPS with no thermal sensors at all — only
|
||||
cooling devices under `/sys/class/thermal/`, no `thermal_zone*/temp`
|
||||
files, an empty `/sys/class/hwmon/`) the app shows the widget's
|
||||
honest empty state: an empty graph, "no data" in the legend, and a
|
||||
title that says no thermal zones were found. No mock or demo
|
||||
temperatures: the seeded demo ramp the widget preloads is dropped at
|
||||
startup with `XTempHistory::clear()`.
|
||||
|
||||
## Build
|
||||
|
||||
```sh
|
||||
make
|
||||
```
|
||||
|
||||
## Run
|
||||
|
||||
```sh
|
||||
./temperature-monitor
|
||||
```
|
||||
|
||||
Esc, `q`, or closing the window quits.
|
||||
|
||||
## Source
|
||||
|
||||
- App: `examples/monitor/temperature-monitor/main.cpp`
|
||||
- Widget: `widgets/components/XTempHistory`
|
||||
- Repo: https://github.com/cnmcdee/lightgui
|
||||
@@ -0,0 +1,278 @@
|
||||
// Temperature Monitor - monitor app (Stage 2, btop-widget project).
|
||||
//
|
||||
// One 1024x768 window: a single XTempHistory widget showing the first
|
||||
// thermal zone's temperature history (80 C threshold line), re-scanned
|
||||
// every 2 s from /sys/class/thermal/thermal_zone*/temp and
|
||||
// /sys/class/hwmon/hwmon*/temp*_input.
|
||||
//
|
||||
// On this machine (hot_dallas, a VPS) there are NO thermal sensors at
|
||||
// all: /sys/class/thermal/ holds only cooling devices, there are no
|
||||
// thermal_zone*/temp files, /sys/class/hwmon/ is empty and `sensors`
|
||||
// is not installed. So the app shows the widget's honest empty state
|
||||
// ("no data", no line, no fake 0.0 C reading) and says so in the
|
||||
// title. No mock or demo temperatures, ever: the widget's seeded demo
|
||||
// ramp is dropped at startup with XTempHistory::clear(). If sensors
|
||||
// ever appear (e.g. on a machine that has them) the rescan picks them
|
||||
// up and the history starts filling with live readings.
|
||||
//
|
||||
// Build: make
|
||||
// Run: ./temperature-monitor (Esc / q / window-close quits)
|
||||
|
||||
#include "XTempHistory.h"
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/keysym.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <dirent.h>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <sys/select.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct Zone
|
||||
{
|
||||
std::string name; // thermal_zone type, or hwmon name + label
|
||||
std::string path; // .../temp file, millidegrees Celsius
|
||||
};
|
||||
|
||||
std::string read_first_line(const std::string& path)
|
||||
{
|
||||
std::ifstream in(path);
|
||||
std::string line;
|
||||
if (in && std::getline(in, line))
|
||||
{
|
||||
while (!line.empty() && (line.back() == '\n' || line.back() == '\r' ||
|
||||
line.back() == ' ' || line.back() == '\t'))
|
||||
line.pop_back();
|
||||
return line;
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
bool read_temp_c(const std::string& path, double& out)
|
||||
{
|
||||
std::string s = read_first_line(path);
|
||||
if (s.empty()) return false;
|
||||
char* end = nullptr;
|
||||
long milli = std::strtol(s.c_str(), &end, 10);
|
||||
if (end == s.c_str() || *end != '\0') return false;
|
||||
out = (double)milli / 1000.0;
|
||||
return true;
|
||||
}
|
||||
|
||||
// All temperature inputs the kernel exposes, thermal zones first.
|
||||
std::vector<Zone> scan_zones()
|
||||
{
|
||||
std::vector<Zone> zones;
|
||||
DIR* thermal = opendir("/sys/class/thermal");
|
||||
if (thermal)
|
||||
{
|
||||
struct dirent* ent;
|
||||
while ((ent = readdir(thermal)) != nullptr)
|
||||
{
|
||||
if (std::strncmp(ent->d_name, "thermal_zone", 12) != 0) continue;
|
||||
std::string base = std::string("/sys/class/thermal/") + ent->d_name;
|
||||
std::string temp_path = base + "/temp";
|
||||
double probe = 0.0;
|
||||
if (!read_temp_c(temp_path, probe)) continue;
|
||||
std::string type = read_first_line(base + "/type");
|
||||
Zone z;
|
||||
z.name = type.empty() ? ent->d_name : type;
|
||||
z.path = temp_path;
|
||||
zones.push_back(z);
|
||||
}
|
||||
closedir(thermal);
|
||||
}
|
||||
DIR* hwmon = opendir("/sys/class/hwmon");
|
||||
if (hwmon)
|
||||
{
|
||||
struct dirent* ent;
|
||||
while ((ent = readdir(hwmon)) != nullptr)
|
||||
{
|
||||
if (std::strncmp(ent->d_name, "hwmon", 5) != 0) continue;
|
||||
std::string base = std::string("/sys/class/hwmon/") + ent->d_name;
|
||||
std::string chip = read_first_line(base + "/name");
|
||||
if (chip.empty()) chip = ent->d_name;
|
||||
DIR* dir = opendir(base.c_str());
|
||||
if (!dir) continue;
|
||||
struct dirent* f;
|
||||
while ((f = readdir(dir)) != nullptr)
|
||||
{
|
||||
std::string fn = f->d_name;
|
||||
if (fn.rfind("temp", 0) != 0) continue;
|
||||
if (fn.size() < 10 || fn.compare(fn.size() - 6, 6, "_input") != 0)
|
||||
continue;
|
||||
std::string temp_path = base + "/" + fn;
|
||||
double probe = 0.0;
|
||||
if (!read_temp_c(temp_path, probe)) continue;
|
||||
std::string label = read_first_line(
|
||||
base + "/" + fn.substr(0, fn.size() - 6) + "_label");
|
||||
Zone z;
|
||||
z.name = label.empty() ? chip : chip + " " + label;
|
||||
z.path = temp_path;
|
||||
zones.push_back(z);
|
||||
}
|
||||
closedir(dir);
|
||||
}
|
||||
closedir(hwmon);
|
||||
}
|
||||
return zones;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
Display* display = XOpenDisplay(nullptr);
|
||||
if (!display) return 1;
|
||||
int screen = DefaultScreen(display);
|
||||
Window window = XCreateSimpleWindow(display, RootWindow(display, screen),
|
||||
100, 100, 1024, 768, 1,
|
||||
BlackPixel(display, screen),
|
||||
WhitePixel(display, screen));
|
||||
XStoreName(display, window, "Temperature Monitor - monitor");
|
||||
XSelectInput(display, window,
|
||||
ExposureMask | KeyPressMask | StructureNotifyMask |
|
||||
ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
|
||||
Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False);
|
||||
XSetWMProtocols(display, window, &deleteWindowMessage, 1);
|
||||
XMapWindow(display, window);
|
||||
GC gc = DefaultGC(display, screen);
|
||||
SStyle_Manager styles(display, window, screen, gc);
|
||||
styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120},
|
||||
Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1,
|
||||
"-misc-fixed-medium-r-normal--13-*");
|
||||
styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22},
|
||||
Srgb{200, 200, 200}, Srgb{240, 240, 240}, 6, 1,
|
||||
"-misc-fixed-medium-r-normal--13-*");
|
||||
styles.set_active_style_byName("default");
|
||||
|
||||
XTempHistory history(display, screen, window, gc, &styles, 16, 16, 992, 736);
|
||||
history.set_text("Temperature history");
|
||||
history.set_threshold(80.0);
|
||||
// Drop the widget's seeded demo ramp: with no live source the
|
||||
// honest state is an empty graph, not a fake warm-up curve.
|
||||
history.clear();
|
||||
|
||||
const std::string no_zones_title =
|
||||
"Temperature history -- no thermal zones found";
|
||||
|
||||
auto redraw = [&]() {
|
||||
XClearWindow(display, window);
|
||||
history.draw();
|
||||
XFlush(display);
|
||||
};
|
||||
|
||||
// First scan before the first paint, so the title already tells
|
||||
// the truth on screen one.
|
||||
std::vector<Zone> zones = scan_zones();
|
||||
bool had_zones = false;
|
||||
if (zones.empty())
|
||||
{
|
||||
history.set_text(no_zones_title);
|
||||
}
|
||||
else
|
||||
{
|
||||
had_zones = true;
|
||||
history.set_text("Temperature history -- " + zones[0].name);
|
||||
double t = 0.0;
|
||||
if (read_temp_c(zones[0].path, t)) history.push(t);
|
||||
}
|
||||
redraw();
|
||||
|
||||
int fd = ConnectionNumber(display);
|
||||
bool running = true;
|
||||
while (running)
|
||||
{
|
||||
fd_set rfds;
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(fd, &rfds);
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 2;
|
||||
tv.tv_usec = 0;
|
||||
int ready = select(fd + 1, &rfds, nullptr, nullptr, &tv);
|
||||
if (ready > 0)
|
||||
{
|
||||
while (XPending(display))
|
||||
{
|
||||
XEvent event;
|
||||
XNextEvent(display, &event);
|
||||
bool handled = false;
|
||||
switch (event.type)
|
||||
{
|
||||
case Expose:
|
||||
handled = true;
|
||||
break;
|
||||
case ButtonPress:
|
||||
handled = history.on_button_press(event.xbutton.x,
|
||||
event.xbutton.y,
|
||||
event.xbutton.button);
|
||||
break;
|
||||
case ButtonRelease:
|
||||
handled = history.on_button_release(event.xbutton.x,
|
||||
event.xbutton.y,
|
||||
event.xbutton.button);
|
||||
break;
|
||||
case MotionNotify:
|
||||
handled = history.on_motion(event.xmotion.x, event.xmotion.y);
|
||||
break;
|
||||
case KeyPress:
|
||||
{
|
||||
char buffer[32];
|
||||
KeySym key = 0;
|
||||
int n = XLookupString(&event.xkey, buffer,
|
||||
(int)sizeof(buffer) - 1, &key, nullptr);
|
||||
buffer[n] = 0;
|
||||
if (key == XK_Escape || (n == 1 && buffer[0] == 'q'))
|
||||
running = false;
|
||||
handled = history.on_key(key, buffer);
|
||||
break;
|
||||
}
|
||||
case ClientMessage:
|
||||
if ((Atom)event.xclient.data.l[0] == deleteWindowMessage)
|
||||
running = false;
|
||||
break;
|
||||
}
|
||||
if (handled) redraw();
|
||||
if (!running) break;
|
||||
}
|
||||
}
|
||||
else if (ready == 0)
|
||||
{
|
||||
// 2 s tick: re-scan sysfs (sensors can appear on other
|
||||
// machines) and push the first zone's live reading.
|
||||
zones = scan_zones();
|
||||
if (zones.empty())
|
||||
{
|
||||
if (had_zones)
|
||||
{
|
||||
had_zones = false;
|
||||
history.clear();
|
||||
history.set_text(no_zones_title);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!had_zones)
|
||||
{
|
||||
had_zones = true;
|
||||
history.set_text("Temperature history -- " + zones[0].name);
|
||||
}
|
||||
double t = 0.0;
|
||||
if (read_temp_c(zones[0].path, t)) history.push(t);
|
||||
}
|
||||
redraw();
|
||||
}
|
||||
}
|
||||
|
||||
XCloseDisplay(display);
|
||||
return 0;
|
||||
}
|
||||
|
After Width: | Height: | Size: 2.5 KiB |
@@ -7,11 +7,7 @@
|
||||
// Attribution: XFrame.
|
||||
#pragma once
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
|
||||
@@ -79,7 +75,8 @@ inline double inst_demo_value(double min_v, double max_v, unsigned seed = 42) {
|
||||
// runs (same rationale as the SStyle_Manager comment in
|
||||
// core/xframe_core.h). Returns nullptr if no size loads, in which case
|
||||
// callers keep the style's own font.
|
||||
inline XFontStruct* inst_bold_font(Display* display) { if (!display) return nullptr;
|
||||
inline XFontStruct* inst_bold_font(Display* display) {
|
||||
if (!display) return nullptr;
|
||||
static XFontStruct* cached = nullptr;
|
||||
static bool tried = false;
|
||||
if (tried) return cached;
|
||||
@@ -97,82 +94,4 @@ inline XFontStruct* inst_bold_font(Display* display) { if (!display) return n
|
||||
return cached;
|
||||
}
|
||||
|
||||
// Scaled monitor fonts (Dec 5, 2026, monitor proportions audit): the C
|
||||
// monitor widgets derive every font size from the widget's current
|
||||
// dimensions, so they need core fonts at arbitrary pixel sizes, bold and
|
||||
// medium. Probed Dec 5, 2026 on the dallas Xvfb: bitmap bold exists only
|
||||
// at 13/14/15/18 px and bitmap medium only at 13/18/20 px, but the
|
||||
// misc-fixed family is scalable — an XLFD with an explicit pixel size and
|
||||
// wildcards for the remaining fields loads at that size (20/24/28 px
|
||||
// verified for bold; the same pattern works for medium). So try the
|
||||
// scalable XLFD at the requested size first, then bitmap fallbacks.
|
||||
// Loaded fonts are cached per (size, weight) in a process-lifetime table
|
||||
// and never XFreeFont'd (same rationale as inst_bold_font above).
|
||||
// Returns nullptr when nothing loads; callers then keep the style font.
|
||||
inline XFontStruct* scaled_font(Display* display, int px, bool bold)
|
||||
{
|
||||
if (!display) return nullptr;
|
||||
if (px < 8) px = 8;
|
||||
if (px > 64) px = 64;
|
||||
struct Entry
|
||||
{
|
||||
int px;
|
||||
bool bold;
|
||||
XFontStruct* font;
|
||||
};
|
||||
static std::vector<Entry> cache;
|
||||
for (auto& e : cache)
|
||||
if (e.px == px && e.bold == bold) return e.font;
|
||||
XFontStruct* font = nullptr;
|
||||
char name[96];
|
||||
// Scalable XLFD: explicit pixel size, wildcards elsewhere.
|
||||
std::snprintf(name, sizeof(name), "-misc-fixed-%s-r-normal--%d-*-75-75-c-*-iso8859-1",
|
||||
bold ? "bold" : "medium", px);
|
||||
font = XLoadQueryFont(display, name);
|
||||
if (!font)
|
||||
{
|
||||
// Bitmap fallbacks, nearest size first.
|
||||
static const int kBoldFallbacks[] = {18, 20, 15, 14, 13};
|
||||
static const int kMediumFallbacks[] = {20, 18, 13};
|
||||
const int* fallbacks = bold ? kBoldFallbacks : kMediumFallbacks;
|
||||
int n = bold ? 5 : 3;
|
||||
for (int i = 0; i < n && !font; ++i)
|
||||
{
|
||||
std::snprintf(name, sizeof(name), "-misc-fixed-%s-r-normal--%d-*",
|
||||
bold ? "bold" : "medium", fallbacks[i]);
|
||||
font = XLoadQueryFont(display, name);
|
||||
}
|
||||
}
|
||||
if (!font) font = XLoadQueryFont(display, "fixed");
|
||||
if ((int)cache.size() < 24) cache.push_back(Entry{px, bold, font});
|
||||
return font;
|
||||
}
|
||||
|
||||
// Width of `s` in `font` (XTextWidth), or 8 px/char when no font.
|
||||
inline int font_text_width(XFontStruct* font, const std::string& s)
|
||||
{
|
||||
if (!font) return (int)s.size() * 8;
|
||||
return XTextWidth(font, s.c_str(), (int)s.size());
|
||||
}
|
||||
|
||||
// Draw `s` at baseline (x, baseline_y) in `font` and foreground `fg`.
|
||||
// No-op when the font failed to load.
|
||||
inline void draw_text_font(Display* display, Window window, GC gc, XFontStruct* font,
|
||||
int x, int baseline_y, const std::string& s, unsigned long fg)
|
||||
{
|
||||
if (!font || s.empty()) return;
|
||||
XSetFont(display, gc, font->fid);
|
||||
XSetForeground(display, gc, fg);
|
||||
XDrawString(display, window, gc, x, baseline_y, s.c_str(), (int)s.size());
|
||||
}
|
||||
|
||||
// Clamp a derived font size: at least `lo` px, at most `hi` px.
|
||||
inline int clamp_font_px(double px, int lo, int hi)
|
||||
{
|
||||
int v = (int)std::lround(px);
|
||||
if (v < lo) v = lo;
|
||||
if (v > hi) v = hi;
|
||||
return v;
|
||||
}
|
||||
|
||||
} // namespace xframe
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/xframe_core.h"
|
||||
#include "../InstData.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
@@ -32,7 +31,6 @@ private:
|
||||
double value_ = 0.0; // 0..100
|
||||
double warn_ = 70.0; // yellow starts here
|
||||
double crit_ = 90.0; // red starts here
|
||||
std::string unit_ = "load"; // sub-label under the centre value
|
||||
|
||||
Srgb white_color_{255, 255, 255};
|
||||
Srgb axis_color_{150, 150, 150};
|
||||
@@ -111,8 +109,6 @@ public:
|
||||
// region Gauge API
|
||||
void set_value(double v) { value_ = std::max(0.0, std::min(100.0, v)); }
|
||||
double get_value() { return value_; }
|
||||
void set_unit(const std::string& u) { unit_ = u; }
|
||||
std::string get_unit() { return unit_; }
|
||||
void set_zones(double warn, double crit)
|
||||
{
|
||||
warn_ = std::max(0.0, std::min(100.0, warn));
|
||||
@@ -137,26 +133,7 @@ public:
|
||||
double wfrac = warn_ / 100.0;
|
||||
double cfrac = crit_ / 100.0;
|
||||
|
||||
// Scaled fonts (Dec 5, 2026 proportions audit): centre value
|
||||
// 40-48 px bold (9-11% of arc diameter), endpoints 16-18 px,
|
||||
// title 16 px bold, "load" sub-label under the value.
|
||||
int title_px = xframe::clamp_font_px(0.033 * h, 11, 16);
|
||||
int value_px = xframe::clamp_font_px(0.10 * 2 * r, 16, 48);
|
||||
int end_px = xframe::clamp_font_px(0.04 * 2 * r, 11, 18);
|
||||
int sub_px = xframe::clamp_font_px(0.04 * 2 * r, 11, 18);
|
||||
XFontStruct* title_font = xframe::scaled_font(display_, title_px, true);
|
||||
if (!title_font) title_font = st.font_;
|
||||
XFontStruct* value_font = xframe::scaled_font(display_, value_px, true);
|
||||
if (!value_font) value_font = st.font_;
|
||||
XFontStruct* end_font = xframe::scaled_font(display_, end_px, false);
|
||||
if (!end_font) end_font = st.font_;
|
||||
XFontStruct* sub_font = xframe::scaled_font(display_, sub_px, false);
|
||||
if (!sub_font) sub_font = st.font_;
|
||||
int title_asc = title_font ? title_font->ascent : 11;
|
||||
int sub_asc = sub_font ? sub_font->ascent : 11;
|
||||
|
||||
xframe::draw_text_font(display_, window_, gc_, title_font, x1_ + 8,
|
||||
y1_ + 4 + title_asc, text_, col(Srgb{20, 20, 20}));
|
||||
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
|
||||
|
||||
// Track + coloured zones.
|
||||
arc_slice(cx, cy, r, 0.0, 1.0, track_color_, 14);
|
||||
@@ -165,30 +142,17 @@ public:
|
||||
if (frac > cfrac) arc_slice(cx, cy, r, cfrac, frac, crit_color_, 14);
|
||||
|
||||
// End labels 0 / 100.
|
||||
xframe::draw_text_font(display_, window_, gc_, end_font, cx - r - 4, cy + 16,
|
||||
"0", col(Srgb{100, 100, 100}));
|
||||
{
|
||||
std::string end100 = "100";
|
||||
int ew = xframe::font_text_width(end_font, end100);
|
||||
xframe::draw_text_font(display_, window_, gc_, end_font, cx + r + 4 - ew,
|
||||
cy + 16, end100, col(Srgb{100, 100, 100}));
|
||||
}
|
||||
draw_text_color(cx - r - 4, cy + 16, "0", Srgb{100, 100, 100});
|
||||
draw_text_color(cx + r - 12, cy + 16, "100", Srgb{100, 100, 100});
|
||||
|
||||
// Big % in the middle + sub-label under it.
|
||||
// Big % in the middle.
|
||||
char pct[16];
|
||||
std::snprintf(pct, sizeof(pct), "%.0f%%", value_);
|
||||
Srgb pct_color = value_ >= crit_ ? crit_color_ : (value_ >= warn_ ? warn_color_ : ok_color_);
|
||||
int pw = xframe::font_text_width(value_font, pct);
|
||||
int value_base = cy - (int)(r * 0.35);
|
||||
xframe::draw_text_font(display_, window_, gc_, value_font, cx - pw / 2,
|
||||
value_base, pct, col(pct_color));
|
||||
if (!unit_.empty())
|
||||
{
|
||||
int uw = xframe::font_text_width(sub_font, unit_);
|
||||
xframe::draw_text_font(display_, window_, gc_, sub_font, cx - uw / 2,
|
||||
value_base + sub_asc + 10, unit_,
|
||||
col(Srgb{100, 100, 100}));
|
||||
}
|
||||
int pw = xf::text_width(st, pct);
|
||||
// Fake a larger readout by drawing the text twice offset by 1 px.
|
||||
draw_text_color(cx - pw / 2, cy - 8, pct, pct_color);
|
||||
draw_text_color(cx - pw / 2 + 1, cy - 8, pct, pct_color);
|
||||
|
||||
XSetForeground(display_, gc_, st.lineColor_);
|
||||
XDrawRectangle(display_, window_, gc_, x1_, y1_, w, h);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/xframe_core.h"
|
||||
#include "../InstData.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
@@ -163,42 +162,13 @@ public:
|
||||
XFillRectangle(display_, window_, gc_, x1_, y1_, w, h);
|
||||
ensure_cores();
|
||||
|
||||
// Scaled fonts (Dec 5, 2026 proportions audit): title 16 px bold,
|
||||
// cell labels 14-16 px bold (9-11% of cell height).
|
||||
int title_px = xframe::clamp_font_px(0.033 * h, 11, 16);
|
||||
XFontStruct* title_font = xframe::scaled_font(display_, title_px, true);
|
||||
if (!title_font) title_font = st.font_;
|
||||
int title_asc = title_font ? title_font->ascent : 11;
|
||||
|
||||
const int title_h = title_px + 8;
|
||||
// Grid geometry: pick cols/rows with no empty cell when possible
|
||||
// (8 cores -> 4x2, not 3x3 with a hole). Minimise waste among
|
||||
// near-square layouts, keeping rows >= 2 and cols <= 2*rows.
|
||||
const int title_h = 20;
|
||||
int cols = (int)std::ceil(std::sqrt((double)cores_));
|
||||
int rows = (cores_ + cols - 1) / cols;
|
||||
int best_waste = cols * rows - cores_;
|
||||
for (int cc = cols + 1; cc <= cores_; ++cc)
|
||||
{
|
||||
int rr = (cores_ + cc - 1) / cc;
|
||||
if (rr < 2 || cc > 2 * rr) continue;
|
||||
int waste = cc * rr - cores_;
|
||||
if (waste < best_waste)
|
||||
{
|
||||
cols = cc;
|
||||
rows = rr;
|
||||
best_waste = waste;
|
||||
if (waste == 0) break;
|
||||
}
|
||||
}
|
||||
int cell_w = (w - 8) / cols;
|
||||
int cell_h = (h - title_h - 8) / rows;
|
||||
int label_px = xframe::clamp_font_px(0.10 * cell_h, 11, 16);
|
||||
XFontStruct* label_font = xframe::scaled_font(display_, label_px, true);
|
||||
if (!label_font) label_font = st.font_;
|
||||
int label_asc = label_font ? label_font->ascent : 11;
|
||||
|
||||
xframe::draw_text_font(display_, window_, gc_, title_font, x1_ + 8,
|
||||
y1_ + 4 + title_asc, text_, col(Srgb{20, 20, 20}));
|
||||
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
|
||||
|
||||
for (int c = 0; c < cores_; ++c)
|
||||
{
|
||||
@@ -212,13 +182,12 @@ public:
|
||||
// Label + current %.
|
||||
char label[32];
|
||||
std::snprintf(label, sizeof(label), "C%d %.0f%%", c, current_[c]);
|
||||
xframe::draw_text_font(display_, window_, gc_, label_font, cx + 5,
|
||||
cy + 4 + label_asc, label, col(color_for(current_[c])));
|
||||
draw_text_color(cx + 5, cy + 14, label, color_for(current_[c]));
|
||||
// Mini history graph.
|
||||
int gx = cx + 4;
|
||||
int gy = cy + label_px + 10;
|
||||
int gy = cy + 18;
|
||||
int gw = std::max(10, cw - 8);
|
||||
int gh = std::max(6, ch - label_px - 14);
|
||||
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];
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/xframe_core.h"
|
||||
#include "../InstData.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
@@ -104,24 +103,13 @@ public:
|
||||
XSetForeground(display_, gc_, col(white_color_));
|
||||
XFillRectangle(display_, window_, gc_, x1_, y1_, w, h);
|
||||
|
||||
// Scaled fonts (Dec 5, 2026 proportions audit): centre value
|
||||
// 36-44 px bold (9-11% of diameter), centre label 16-18 px,
|
||||
// ring thickness 8-10% of diameter. The top-left title is
|
||||
// dropped: it duplicated the centre label ("Disk" twice).
|
||||
const int title_h = 20;
|
||||
int cx = x1_ + w / 2;
|
||||
int cy = y1_ + h / 2;
|
||||
int r = std::max(16, std::min(w, h) / 2 - 10);
|
||||
int thick = xframe::clamp_font_px(0.09 * 2 * r, 8, 40);
|
||||
// The ring stroke extends thick/2 past r: shrink r so the outer
|
||||
// edge stays inside the frame.
|
||||
r = std::max(16, r - thick / 2);
|
||||
int value_px = xframe::clamp_font_px(0.10 * 2 * r, 16, 44);
|
||||
int label_px = xframe::clamp_font_px(0.045 * 2 * r, 11, 18);
|
||||
XFontStruct* value_font = xframe::scaled_font(display_, value_px, true);
|
||||
if (!value_font) value_font = st.font_;
|
||||
XFontStruct* label_font = xframe::scaled_font(display_, label_px, false);
|
||||
if (!label_font) label_font = st.font_;
|
||||
int label_asc = label_font ? label_font->ascent : 11;
|
||||
int cy = y1_ + title_h + (h - title_h) / 2;
|
||||
int r = std::max(16, std::min(w, h - title_h - 8) / 2 - 10);
|
||||
const int thick = 16;
|
||||
|
||||
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
|
||||
|
||||
// Track ring (full circle) + value arc from the top, clockwise.
|
||||
XSetForeground(display_, gc_, col(track_color_));
|
||||
@@ -133,15 +121,14 @@ public:
|
||||
XDrawArc(display_, window_, gc_, cx - r, cy - r, 2 * r, 2 * r, 90 * 64, -sweep);
|
||||
xf::set_line_width(display_, gc_, 1);
|
||||
|
||||
// % centred, value name under it (the only "Disk" label now).
|
||||
// % centred, label under it.
|
||||
char pct[16];
|
||||
std::snprintf(pct, sizeof(pct), "%.0f%%", value_);
|
||||
int pw = xframe::font_text_width(value_font, pct);
|
||||
xframe::draw_text_font(display_, window_, gc_, value_font, cx - pw / 2, cy - 2,
|
||||
pct, col(ring_color_));
|
||||
int tw = xframe::font_text_width(label_font, text_);
|
||||
xframe::draw_text_font(display_, window_, gc_, label_font, cx - tw / 2,
|
||||
cy + label_asc + 10, text_, col(Srgb{20, 20, 20}));
|
||||
int pw = xf::text_width(st, pct);
|
||||
draw_text_color(cx - pw / 2, cy + 4, pct, ring_color_);
|
||||
draw_text_color(cx - pw / 2 + 1, cy + 4, pct, ring_color_);
|
||||
int tw = xf::text_width(st, text_);
|
||||
xf::draw_text_at(display_, window_, gc_, st, cx - tw / 2, cy + 22, text_);
|
||||
|
||||
XSetForeground(display_, gc_, st.lineColor_);
|
||||
XDrawRectangle(display_, window_, gc_, x1_, y1_, w, h);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/xframe_core.h"
|
||||
#include "../InstData.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
@@ -177,26 +176,19 @@ public:
|
||||
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,
|
||||
// Y labels + legend 14-16 px, time labels 13-14 px, all derived
|
||||
// from widget height (min 11 px). Fall back to the style font
|
||||
// when a scaled font fails to load.
|
||||
int title_px = xframe::clamp_font_px(0.033 * h, 11, 16);
|
||||
int body_px = xframe::clamp_font_px(0.032 * h, 11, 16);
|
||||
int time_px = xframe::clamp_font_px(0.028 * h, 11, 14);
|
||||
XFontStruct* title_font = xframe::scaled_font(display_, title_px, true);
|
||||
if (!title_font) title_font = st.font_;
|
||||
XFontStruct* body_font = xframe::scaled_font(display_, body_px, false);
|
||||
if (!body_font) body_font = st.font_;
|
||||
XFontStruct* time_font = xframe::scaled_font(display_, time_px, false);
|
||||
if (!time_font) time_font = st.font_;
|
||||
int title_asc = title_font ? title_font->ascent : 11;
|
||||
int body_asc = body_font ? body_font->ascent : 11;
|
||||
|
||||
const int title_h = title_px + 8;
|
||||
const int title_h = 20;
|
||||
const int legend_h = 18 * (int)series_.size();
|
||||
const int axis_w = 44;
|
||||
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);
|
||||
|
||||
// Y range first: the widest Y label sets the left margin.
|
||||
// Title.
|
||||
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
|
||||
|
||||
// Y range.
|
||||
double lo = y_min_;
|
||||
double hi = y_max_;
|
||||
if (auto_y_)
|
||||
@@ -209,75 +201,11 @@ public:
|
||||
}
|
||||
if (hi <= lo) hi = lo + 1.0;
|
||||
|
||||
// Dynamic left margin: measure the widest of the 5 Y labels and
|
||||
// put the plot's left edge after it (labels right-aligned,
|
||||
// ending 4 px left of the plot). The old fixed 44 px margin let
|
||||
// labels wider than 40 px run over the axis line.
|
||||
std::string ylabels[5];
|
||||
int axis_w = 8;
|
||||
for (int g = 0; g <= 4; ++g)
|
||||
{
|
||||
double v = lo + (hi - lo) * (4 - g) / 4.0;
|
||||
ylabels[g] = fmt1(v);
|
||||
axis_w = std::max(axis_w, xframe::font_text_width(body_font, ylabels[g]) + 8);
|
||||
}
|
||||
|
||||
// Legend lines first: a series whose full line would run past
|
||||
// the right edge wraps to two lines ("name cur X" /
|
||||
// "min Y max Z avg W"), so the plot height accounts for it.
|
||||
struct LegLine
|
||||
{
|
||||
std::string text;
|
||||
bool swatch;
|
||||
Srgb color{0, 0, 0};
|
||||
};
|
||||
std::vector<LegLine> leg_lines;
|
||||
int leg_avail = std::max(20, x2_ - 8 - (x1_ + 32));
|
||||
for (auto& s : series_)
|
||||
{
|
||||
double cur = 0.0, mn = 0.0, mx = 0.0, sum = 0.0;
|
||||
if (!s.data.empty())
|
||||
{
|
||||
cur = s.data.back();
|
||||
mn = mx = s.data[0];
|
||||
for (double v : s.data)
|
||||
{
|
||||
mn = std::min(mn, v);
|
||||
mx = std::max(mx, v);
|
||||
sum += v;
|
||||
}
|
||||
sum /= (double)s.data.size();
|
||||
}
|
||||
std::string full = s.name + " cur " + fmt1(cur) + " min " + fmt1(mn) +
|
||||
" max " + fmt1(mx) + " avg " + fmt1(sum);
|
||||
if (xframe::font_text_width(body_font, full) <= leg_avail)
|
||||
{
|
||||
leg_lines.push_back(LegLine{full, true, s.color});
|
||||
}
|
||||
else
|
||||
{
|
||||
std::string l1 = s.name + " cur " + fmt1(cur);
|
||||
std::string l2 = "min " + fmt1(mn) + " max " + fmt1(mx) + " avg " + fmt1(sum);
|
||||
leg_lines.push_back(LegLine{l1, true, s.color});
|
||||
leg_lines.push_back(LegLine{l2, false, s.color});
|
||||
}
|
||||
}
|
||||
int line_h = body_px + 8;
|
||||
int legend_h = line_h * (int)leg_lines.size();
|
||||
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);
|
||||
|
||||
// Title.
|
||||
xframe::draw_text_font(display_, window_, gc_, title_font, x1_ + 8,
|
||||
y1_ + 4 + title_asc, text_, col(Srgb{20, 20, 20}));
|
||||
|
||||
auto y_at = [&](double v) {
|
||||
return py + ph - (int)((v - lo) / (hi - lo) * ph);
|
||||
};
|
||||
|
||||
// Grid + y labels (right-aligned, clear of the plot).
|
||||
// Grid + y labels.
|
||||
XSetForeground(display_, gc_, col(grid_color_));
|
||||
for (int g = 0; g <= 4; ++g)
|
||||
{
|
||||
@@ -286,21 +214,13 @@ public:
|
||||
}
|
||||
for (int g = 0; g <= 4; ++g)
|
||||
{
|
||||
int gy = py + ph * g / 4;
|
||||
int lw = xframe::font_text_width(body_font, ylabels[g]);
|
||||
int ybase = (g == 0) ? gy + body_asc
|
||||
: (g == 4) ? gy - 2 : gy + body_asc / 2;
|
||||
xframe::draw_text_font(display_, window_, gc_, body_font, px - 4 - lw,
|
||||
ybase, ylabels[g], col(Srgb{100, 100, 100}));
|
||||
double v = lo + (hi - lo) * (4 - g) / 4.0;
|
||||
draw_text_color(px - 40, py + ph * g / 4 + 4, fmt1(v), Srgb{100, 100, 100});
|
||||
}
|
||||
// Time labels.
|
||||
std::string ago = "-" + std::to_string(window_seconds_) + "s";
|
||||
xframe::draw_text_font(display_, window_, gc_, time_font, px, y2_ - 3, ago,
|
||||
col(Srgb{100, 100, 100}));
|
||||
draw_text_color(px, y2_ - 3, "-" + std::to_string(window_seconds_) + "s", Srgb{100, 100, 100});
|
||||
std::string now_s = "now";
|
||||
xframe::draw_text_font(display_, window_, gc_, time_font,
|
||||
px + pw - xframe::font_text_width(time_font, now_s), y2_ - 3,
|
||||
now_s, col(Srgb{100, 100, 100}));
|
||||
draw_text_color(px + pw - xf::text_width(st, now_s), y2_ - 3, now_s, Srgb{100, 100, 100});
|
||||
|
||||
// Series lines.
|
||||
for (auto& s : series_)
|
||||
@@ -319,20 +239,29 @@ public:
|
||||
xf::set_line_width(display_, gc_, 1);
|
||||
}
|
||||
|
||||
// Legend: swatch + name + current / min / max / avg (wrapped
|
||||
// lines computed above so nothing runs past the right edge).
|
||||
// Legend: swatch + name + current / min / max / avg.
|
||||
int ly = y1_ + title_h + 4;
|
||||
for (auto& ln : leg_lines)
|
||||
for (auto& s : series_)
|
||||
{
|
||||
int base = ly + body_asc;
|
||||
if (ln.swatch)
|
||||
double cur = 0.0, mn = 0.0, mx = 0.0, sum = 0.0;
|
||||
if (!s.data.empty())
|
||||
{
|
||||
XSetForeground(display_, gc_, col(ln.color));
|
||||
XFillRectangle(display_, window_, gc_, x1_ + 8, base - body_asc + 2, 18, 4);
|
||||
cur = s.data.back();
|
||||
mn = mx = s.data[0];
|
||||
for (double v : s.data)
|
||||
{
|
||||
mn = std::min(mn, v);
|
||||
mx = std::max(mx, v);
|
||||
sum += v;
|
||||
}
|
||||
sum /= (double)s.data.size();
|
||||
}
|
||||
xframe::draw_text_font(display_, window_, gc_, body_font, x1_ + 32, base,
|
||||
ln.text, col(Srgb{20, 20, 20}));
|
||||
ly += line_h;
|
||||
XSetForeground(display_, gc_, col(s.color));
|
||||
XFillRectangle(display_, window_, gc_, x1_ + 8, ly - 2, 18, 4);
|
||||
std::string leg = s.name + " cur " + fmt1(cur) + " min " + fmt1(mn) +
|
||||
" max " + fmt1(mx) + " avg " + fmt1(sum);
|
||||
xf::draw_text_at(display_, window_, gc_, st, x1_ + 32, ly + 8, leg);
|
||||
ly += 18;
|
||||
}
|
||||
|
||||
// Plot frame + outer frame.
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/xframe_core.h"
|
||||
#include "../InstData.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
@@ -160,71 +159,29 @@ public:
|
||||
XSetForeground(display_, gc_, col(white_color_));
|
||||
XFillRectangle(display_, window_, gc_, x1_, y1_, w, h);
|
||||
|
||||
// Scaled fonts (Dec 5, 2026 proportions audit): values 24-28 px
|
||||
// bold (5-6% of height), sub-labels 14 px, title + "N cores"
|
||||
// 14-16 px, uptime 14 px. The value block is vertically centred
|
||||
// (no ~350 px empty middle); sub-labels sit under their value.
|
||||
int title_px = xframe::clamp_font_px(0.033 * h, 11, 16);
|
||||
int values_px = xframe::clamp_font_px(0.055 * h, 13, 28);
|
||||
int sub_px = xframe::clamp_font_px(0.03 * h, 11, 14);
|
||||
XFontStruct* title_font = xframe::scaled_font(display_, title_px, true);
|
||||
if (!title_font) title_font = st.font_;
|
||||
XFontStruct* values_font = xframe::scaled_font(display_, values_px, true);
|
||||
if (!values_font) values_font = st.font_;
|
||||
XFontStruct* sub_font = xframe::scaled_font(display_, sub_px, false);
|
||||
if (!sub_font) sub_font = st.font_;
|
||||
int title_asc = title_font ? title_font->ascent : 11;
|
||||
int values_asc = values_font ? values_font->ascent : 11;
|
||||
int sub_asc = sub_font ? sub_font->ascent : 11;
|
||||
|
||||
Srgb state = state_color();
|
||||
// Left status stripe.
|
||||
XSetForeground(display_, gc_, col(state));
|
||||
XFillRectangle(display_, window_, gc_, x1_, y1_, 8, h);
|
||||
|
||||
const int title_h = title_px + 8;
|
||||
xframe::draw_text_font(display_, window_, gc_, title_font, x1_ + 16,
|
||||
y1_ + 4 + title_asc, text_, col(Srgb{20, 20, 20}));
|
||||
xf::draw_text_at(display_, window_, gc_, st, x1_ + 16, y1_ + 15, text_);
|
||||
std::string cpus = std::to_string(ncpu_) + " cores";
|
||||
xframe::draw_text_font(display_, window_, gc_, sub_font,
|
||||
x2_ - 8 - xframe::font_text_width(sub_font, cpus),
|
||||
y1_ + 4 + title_asc, cpus, col(Srgb{20, 20, 20}));
|
||||
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, centred as a block.
|
||||
std::string vstr[3] = {fmt1(load1_), fmt1(load5_), fmt1(load15_)};
|
||||
std::string gap = " ";
|
||||
int gap_w = xframe::font_text_width(values_font, gap);
|
||||
int widths[3];
|
||||
int total_w = -gap_w;
|
||||
for (int i = 0; i < 3; ++i)
|
||||
{
|
||||
widths[i] = xframe::font_text_width(values_font, vstr[i]);
|
||||
total_w += widths[i] + gap_w;
|
||||
}
|
||||
int region_top = y1_ + title_h + 4;
|
||||
int region_bottom = y2_ - sub_px - 12;
|
||||
int block_h = values_px + 6 + sub_px;
|
||||
int block_top = region_top + std::max(0, (region_bottom - region_top - block_h) / 2);
|
||||
int values_base = block_top + values_asc;
|
||||
int sub_base = values_base + 6 + sub_asc;
|
||||
int vx = x1_ + (w - total_w) / 2;
|
||||
const char* sub_labels[3] = {"1 min", "5 min", "15 min"};
|
||||
for (int i = 0; i < 3; ++i)
|
||||
{
|
||||
xframe::draw_text_font(display_, window_, gc_, values_font, vx, values_base,
|
||||
vstr[i], col(state));
|
||||
int sw = xframe::font_text_width(sub_font, sub_labels[i]);
|
||||
xframe::draw_text_font(display_, window_, gc_, sub_font,
|
||||
vx + (widths[i] - sw) / 2, sub_base, sub_labels[i],
|
||||
col(Srgb{100, 100, 100}));
|
||||
vx += widths[i] + gap_w;
|
||||
}
|
||||
// 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 = xframe::font_text_width(sub_font, up);
|
||||
xframe::draw_text_font(display_, window_, gc_, sub_font, x1_ + (w - uw) / 2,
|
||||
y2_ - 10, up, col(Srgb{20, 20, 20}));
|
||||
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);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/xframe_core.h"
|
||||
#include "../InstData.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
@@ -249,25 +248,6 @@ public:
|
||||
int h = height();
|
||||
Srgb state = status_ == MonUp ? up_color_ : (status_ == MonDown ? down_color_ : unknown_color_);
|
||||
|
||||
// Scaled content (Dec 5, 2026 proportions audit): label 20-24 px
|
||||
// bold, dot 24-32 px, status 20 px bold, sub-label (details_:
|
||||
// "open" / "pid N" / ...) under the label. The content block is
|
||||
// vertically centred. Sizes derive from the button's own height
|
||||
// (one rule serves the standalone button and the grid cells,
|
||||
// whose demo heights are 480 px and ~180 px).
|
||||
int label_px = xframe::clamp_font_px(0.10 * h, 13, 24);
|
||||
int dot_d = xframe::clamp_font_px(0.10 * h, 12, 30);
|
||||
int status_px = xframe::clamp_font_px(0.09 * h, 12, 20);
|
||||
int detail_px = xframe::clamp_font_px(0.06 * h, 11, 14);
|
||||
XFontStruct* label_font = xframe::scaled_font(display_, label_px, true);
|
||||
if (!label_font) label_font = st.font_;
|
||||
XFontStruct* status_font = xframe::scaled_font(display_, status_px, true);
|
||||
if (!status_font) status_font = st.font_;
|
||||
XFontStruct* detail_font = xframe::scaled_font(display_, detail_px, false);
|
||||
if (!detail_font) detail_font = st.font_;
|
||||
int label_asc = label_font ? label_font->ascent : 11;
|
||||
int detail_asc = detail_font ? detail_font->ascent : 11;
|
||||
|
||||
// Body: white, thick status-coloured border = the glow.
|
||||
XSetForeground(display_, gc_, col(white_color_));
|
||||
XFillRectangle(display_, window_, gc_, x1_, y1_, w, h);
|
||||
@@ -276,48 +256,27 @@ public:
|
||||
XDrawRectangle(display_, window_, gc_, x1_ + 1, y1_ + 1, w - 2, h - 2);
|
||||
xf::set_line_width(display_, gc_, 1);
|
||||
|
||||
int block_h = label_px + 6 + detail_px;
|
||||
int block_top = y1_ + std::max(4, (h - block_h) / 2);
|
||||
int label_base = block_top + label_asc;
|
||||
int detail_base = block_top + label_px + 6 + detail_asc;
|
||||
|
||||
// Status dot, centred on the content block.
|
||||
int dot_r = dot_d / 2;
|
||||
int dot_cx = x1_ + 8 + dot_r;
|
||||
int dot_cy = block_top + block_h / 2;
|
||||
// Status dot.
|
||||
int dot_r = 7;
|
||||
int dot_cx = x1_ + 16;
|
||||
int dot_cy = y1_ + h / 2 - (show_details_ ? 8 : 0);
|
||||
XSetForeground(display_, gc_, col(state));
|
||||
XFillArc(display_, window_, gc_, dot_cx - dot_r, dot_cy - dot_r, 2 * dot_r, 2 * dot_r, 0, 360 * 64);
|
||||
|
||||
// Label + status text on the first line.
|
||||
// Label + status text.
|
||||
xf::draw_text_at(display_, window_, gc_, st, x1_ + 30, dot_cy + 4, text_);
|
||||
std::string stxt = status_text();
|
||||
int sw = xframe::font_text_width(status_font, stxt);
|
||||
int label_x = dot_cx + dot_r + 10;
|
||||
int label_avail = std::max(10, x2_ - 10 - sw - 8 - label_x);
|
||||
std::string label = text_;
|
||||
int label_w = xframe::font_text_width(label_font, label);
|
||||
if (label_w > label_avail)
|
||||
{
|
||||
int cw = label_font ? label_font->max_bounds.width : 8;
|
||||
int keep = std::max(0, label_avail / std::max(1, cw) - 3);
|
||||
label = label.substr(0, (size_t)keep) + "...";
|
||||
}
|
||||
xframe::draw_text_font(display_, window_, gc_, label_font, label_x, label_base,
|
||||
label, col(Srgb{20, 20, 20}));
|
||||
xframe::draw_text_font(display_, window_, gc_, status_font, x2_ - 10 - sw,
|
||||
label_base, stxt, col(state));
|
||||
int sw = xf::text_width(st, stxt);
|
||||
draw_text_color(x2_ - 10 - sw, dot_cy + 4, stxt, state);
|
||||
draw_text_color(x2_ - 10 - sw + 1, dot_cy + 4, stxt, state);
|
||||
|
||||
// Sub-label under the label (always shown now, not click-gated).
|
||||
std::string detail = details_;
|
||||
int detail_avail = std::max(10, x2_ - 10 - label_x);
|
||||
int detail_w = xframe::font_text_width(detail_font, detail);
|
||||
if (detail_w > detail_avail)
|
||||
if (show_details_)
|
||||
{
|
||||
int cw = detail_font ? detail_font->max_bounds.width : 8;
|
||||
int keep = std::max(0, detail_avail / std::max(1, cw) - 3);
|
||||
detail = detail.substr(0, (size_t)keep) + "...";
|
||||
std::string d = kind_ == Process ? ("process " + target_ + ": " + details_)
|
||||
: kind_ == Unit ? ("unit " + target_ + ": " + details_)
|
||||
: ("port " + target_ + ": " + details_);
|
||||
xf::draw_text_at(display_, window_, gc_, st, x1_ + 30, y1_ + h - 8, d.substr(0, 48));
|
||||
}
|
||||
xframe::draw_text_font(display_, window_, gc_, detail_font, label_x, detail_base,
|
||||
detail, col(Srgb{100, 100, 100}));
|
||||
|
||||
XFlush(display_);
|
||||
}
|
||||
@@ -326,8 +285,7 @@ public:
|
||||
{
|
||||
if (button == 1 && contains(x, y))
|
||||
{
|
||||
// The detail sub-label is always shown now (Dec 5, 2026
|
||||
// proportions audit); a click just re-polls.
|
||||
show_details_ = !show_details_;
|
||||
poll();
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/xframe_core.h"
|
||||
#include "../InstData.h"
|
||||
#include "../XMonitorButton/XMonitorButton.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
@@ -33,7 +31,7 @@ private:
|
||||
bool focused_ = false;
|
||||
|
||||
std::vector<XMonitorButton> monitors_;
|
||||
int columns_ = 0; // 0 = auto: cols = ceil(sqrt(n)) (2x2 for 4)
|
||||
int columns_ = 2;
|
||||
|
||||
Srgb white_color_{255, 255, 255};
|
||||
|
||||
@@ -42,11 +40,9 @@ private:
|
||||
void layout()
|
||||
{
|
||||
if (monitors_.empty()) return;
|
||||
const int title_h = xframe::clamp_font_px(0.033 * height(), 11, 16) + 8;
|
||||
const int title_h = 20;
|
||||
const int gap = 6;
|
||||
int cols = columns_ > 0 ? columns_
|
||||
: (int)std::ceil(std::sqrt((double)monitors_.size()));
|
||||
cols = std::max(1, cols);
|
||||
int cols = std::max(1, columns_);
|
||||
int rows = ((int)monitors_.size() + cols - 1) / cols;
|
||||
int cw = (width() - 8 - gap * (cols - 1)) / cols;
|
||||
int ch = (height() - title_h - 8 - gap * (rows - 1)) / rows;
|
||||
@@ -151,17 +147,9 @@ public:
|
||||
XSetForeground(display_, gc_, col(white_color_));
|
||||
XFillRectangle(display_, window_, gc_, x1_, y1_, w, h);
|
||||
|
||||
// Title + summary 14-16 px bold (Dec 5, 2026 proportions audit).
|
||||
int title_px = xframe::clamp_font_px(0.033 * h, 11, 16);
|
||||
XFontStruct* title_font = xframe::scaled_font(display_, title_px, true);
|
||||
if (!title_font) title_font = st.font_;
|
||||
int title_asc = title_font ? title_font->ascent : 11;
|
||||
xframe::draw_text_font(display_, window_, gc_, title_font, x1_ + 8,
|
||||
y1_ + 4 + title_asc, text_, col(Srgb{20, 20, 20}));
|
||||
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
|
||||
std::string summary = std::to_string(up_count()) + "/" + std::to_string(monitors_.size()) + " up";
|
||||
xframe::draw_text_font(display_, window_, gc_, title_font,
|
||||
x2_ - 8 - xframe::font_text_width(title_font, summary),
|
||||
y1_ + 4 + title_asc, summary, col(Srgb{20, 20, 20}));
|
||||
xf::draw_text_at(display_, window_, gc_, st, x2_ - 8 - xf::text_width(st, summary), y1_ + 15, summary);
|
||||
|
||||
layout();
|
||||
for (auto& m : monitors_) m.draw();
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/xframe_core.h"
|
||||
#include "../InstData.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
@@ -177,18 +176,16 @@ public:
|
||||
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,
|
||||
// Y labels + legend 14-16 px, derived from widget height.
|
||||
int title_px = xframe::clamp_font_px(0.033 * h, 11, 16);
|
||||
int body_px = xframe::clamp_font_px(0.032 * h, 11, 16);
|
||||
XFontStruct* title_font = xframe::scaled_font(display_, title_px, true);
|
||||
if (!title_font) title_font = st.font_;
|
||||
XFontStruct* body_font = xframe::scaled_font(display_, body_px, false);
|
||||
if (!body_font) body_font = st.font_;
|
||||
int title_asc = title_font ? title_font->ascent : 11;
|
||||
int body_asc = body_font ? body_font->ascent : 11;
|
||||
const int title_h = title_px + 8;
|
||||
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)
|
||||
@@ -199,52 +196,7 @@ public:
|
||||
hi *= 1.05;
|
||||
}
|
||||
|
||||
// Dynamic left margin: the widest humanised Y label
|
||||
// ("276.7 KiB/s") sets the plot's left edge; labels are
|
||||
// right-aligned so none runs over the axis line.
|
||||
std::string ylabels[5];
|
||||
int axis_w = 8;
|
||||
for (int g = 0; g <= 4; ++g)
|
||||
{
|
||||
ylabels[g] = fmt_rate(hi * (4 - g) / 4.0);
|
||||
axis_w = std::max(axis_w, xframe::font_text_width(body_font, ylabels[g]) + 8);
|
||||
}
|
||||
|
||||
// Legend lines first: a "down R total T" line that would run
|
||||
// past the right edge splits into rate / total lines.
|
||||
struct LegLine
|
||||
{
|
||||
std::string text;
|
||||
bool swatch;
|
||||
Srgb color{0, 0, 0};
|
||||
};
|
||||
std::vector<LegLine> leg_lines;
|
||||
int leg_avail = std::max(20, x2_ - 8 - (x1_ + 32));
|
||||
auto add_series_leg = [&](const std::string& tag, double rate, double tot, Srgb color) {
|
||||
std::string full = tag + fmt_rate(rate) + " total " + fmt_total(tot);
|
||||
if (xframe::font_text_width(body_font, full) <= leg_avail)
|
||||
{
|
||||
leg_lines.push_back(LegLine{full, true, color});
|
||||
}
|
||||
else
|
||||
{
|
||||
leg_lines.push_back(LegLine{tag + fmt_rate(rate), true, color});
|
||||
leg_lines.push_back(LegLine{"total " + fmt_total(tot), false, color});
|
||||
}
|
||||
};
|
||||
add_series_leg("down ", down_rate(), down_total_, down_color_);
|
||||
add_series_leg("up ", up_rate(), up_total_, up_color_);
|
||||
int line_h = body_px + 8;
|
||||
int legend_h = line_h * (int)leg_lines.size();
|
||||
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);
|
||||
|
||||
xframe::draw_text_font(display_, window_, gc_, title_font, x1_ + 8,
|
||||
y1_ + 4 + title_asc, text_, col(Srgb{20, 20, 20}));
|
||||
|
||||
// Grid + y labels (right-aligned, clear of the plot).
|
||||
// Grid + y labels.
|
||||
XSetForeground(display_, gc_, col(grid_color_));
|
||||
for (int g = 0; g <= 4; ++g)
|
||||
{
|
||||
@@ -253,31 +205,22 @@ public:
|
||||
}
|
||||
for (int g = 0; g <= 4; ++g)
|
||||
{
|
||||
int gy = py + ph * g / 4;
|
||||
int lw = xframe::font_text_width(body_font, ylabels[g]);
|
||||
int ybase = (g == 0) ? gy + body_asc
|
||||
: (g == 4) ? gy - 2 : gy + body_asc / 2;
|
||||
xframe::draw_text_font(display_, window_, gc_, body_font, px - 4 - lw,
|
||||
ybase, ylabels[g], col(Srgb{100, 100, 100}));
|
||||
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 (wrapped lines computed above).
|
||||
int ly = y1_ + title_h + 4;
|
||||
for (auto& ln : leg_lines)
|
||||
{
|
||||
int base = ly + body_asc;
|
||||
if (ln.swatch)
|
||||
{
|
||||
XSetForeground(display_, gc_, col(ln.color));
|
||||
XFillRectangle(display_, window_, gc_, x1_ + 8, base - body_asc + 2, 18, 4);
|
||||
}
|
||||
xframe::draw_text_font(display_, window_, gc_, body_font, x1_ + 32, base,
|
||||
ln.text, col(Srgb{20, 20, 20}));
|
||||
ly += line_h;
|
||||
}
|
||||
// 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);
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/xframe_core.h"
|
||||
#include "../InstData.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
@@ -51,6 +51,8 @@ private:
|
||||
int sort_col_ = 4; // 0 pid, 1 name, 2 user, 3 mem, 4 cpu
|
||||
bool sort_desc_ = true;
|
||||
int max_rows_ = 40;
|
||||
int selected_pid_ = -1; // pid of the selected row (-1 = none)
|
||||
std::string filter_; // substring filter on name/pid (empty = all)
|
||||
|
||||
Srgb white_color_{255, 255, 255};
|
||||
Srgb header_color_{235, 235, 235};
|
||||
@@ -232,6 +234,17 @@ public:
|
||||
}
|
||||
closedir(dir);
|
||||
sort_rows();
|
||||
if (!filter_.empty())
|
||||
{
|
||||
std::string f = filter_;
|
||||
std::transform(f.begin(), f.end(), f.begin(), ::tolower);
|
||||
rows_.erase(std::remove_if(rows_.begin(), rows_.end(), [&](const Row& r) {
|
||||
std::string n = r.name;
|
||||
std::transform(n.begin(), n.end(), n.begin(), ::tolower);
|
||||
return n.find(f) == std::string::npos &&
|
||||
std::to_string(r.pid).find(filter_) == std::string::npos;
|
||||
}), rows_.end());
|
||||
}
|
||||
if ((int)rows_.size() > max_rows_) rows_.resize(max_rows_);
|
||||
}
|
||||
void set_sort_column(int c)
|
||||
@@ -248,6 +261,13 @@ public:
|
||||
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; }
|
||||
// Filter: substring match on process name (case-insensitive) or
|
||||
// pid. Re-scans /proc so the filter applies to live data.
|
||||
void set_filter(const std::string& f) { filter_ = f; refresh(); }
|
||||
std::string get_filter() { return filter_; }
|
||||
// Selection: stored as a pid so a refresh (re-sort) keeps it.
|
||||
int selected_pid() { return selected_pid_; }
|
||||
void set_selected_pid(int pid) { selected_pid_ = pid; }
|
||||
// endregion
|
||||
|
||||
void draw()
|
||||
@@ -259,54 +279,28 @@ public:
|
||||
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;
|
||||
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%"};
|
||||
|
||||
xframe::draw_text_font(display_, window_, gc_, title_font, x1_ + 8,
|
||||
y1_ + 4 + title_asc, text_, col(Srgb{20, 20, 20}));
|
||||
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
|
||||
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}));
|
||||
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);
|
||||
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}));
|
||||
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);
|
||||
@@ -315,7 +309,6 @@ public:
|
||||
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;
|
||||
@@ -330,35 +323,26 @@ public:
|
||||
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}));
|
||||
if (r.pid == selected_pid_)
|
||||
{
|
||||
XSetForeground(display_, gc_, col(Srgb{219, 234, 254}));
|
||||
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);
|
||||
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).
|
||||
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;
|
||||
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);
|
||||
int bar_w = xs[5] - bar_x - 4;
|
||||
int fill = (int)(bar_w * 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);
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -370,8 +354,8 @@ public:
|
||||
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;
|
||||
const int title_h = 20;
|
||||
const int header_h = 18;
|
||||
int hy = y1_ + title_h;
|
||||
if (y >= hy && y < hy + header_h)
|
||||
{
|
||||
@@ -388,6 +372,19 @@ public:
|
||||
}
|
||||
}
|
||||
}
|
||||
// Body click selects the row (by pid, so refresh keeps it).
|
||||
const int row_h = 16;
|
||||
int y0 = hy + header_h;
|
||||
if (y >= y0)
|
||||
{
|
||||
int max_fit = (y2_ - 6 - y0) / row_h;
|
||||
int row = (y - y0) / row_h;
|
||||
if (row >= 0 && row < (int)rows_.size() && row < max_fit)
|
||||
{
|
||||
selected_pid_ = rows_[row].pid;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
bool on_button_release(int x, int y, unsigned int button)
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/xframe_core.h"
|
||||
#include "../InstData.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
@@ -132,44 +131,20 @@ public:
|
||||
XSetForeground(display_, gc_, col(white_color_));
|
||||
XFillRectangle(display_, window_, gc_, x1_, y1_, w, h);
|
||||
|
||||
// Scaled fonts (Dec 5, 2026 proportions audit): title + total
|
||||
// 14-16 px (title bold), legend 14-16 px, bar height ~1.5x
|
||||
// legend text height. The bar+legend block scales with widget
|
||||
// height and is vertically centred (no 80% empty area).
|
||||
int title_px = xframe::clamp_font_px(0.033 * h, 11, 16);
|
||||
int legend_px = xframe::clamp_font_px(0.032 * h, 11, 16);
|
||||
XFontStruct* title_font = xframe::scaled_font(display_, title_px, true);
|
||||
if (!title_font) title_font = st.font_;
|
||||
XFontStruct* legend_font = xframe::scaled_font(display_, legend_px, false);
|
||||
if (!legend_font) legend_font = st.font_;
|
||||
int title_asc = title_font ? title_font->ascent : 11;
|
||||
int legend_asc = legend_font ? legend_font->ascent : 11;
|
||||
int legend_desc = legend_font ? legend_font->descent : 2;
|
||||
|
||||
const int title_h = 20;
|
||||
const int bar_h = 26;
|
||||
double tot = total();
|
||||
const int title_h = title_px + 8;
|
||||
int row_h = (int)std::lround(1.6 * legend_px);
|
||||
int legend_total = row_h * (int)segments_.size();
|
||||
int bar_min = (int)std::lround(1.5 * legend_px);
|
||||
int bar_max = (int)std::lround(0.30 * h);
|
||||
int bar_h = std::max(bar_min, std::min(bar_max, h - title_h - legend_total - 24));
|
||||
bar_h = std::max(8, bar_h);
|
||||
int block_h = title_h + 6 + bar_h + 8 + legend_total;
|
||||
int start_y = y1_ + std::max(4, (h - block_h) / 2);
|
||||
|
||||
xframe::draw_text_font(display_, window_, gc_, title_font, x1_ + 8,
|
||||
start_y + 4 + title_asc, text_, col(Srgb{20, 20, 20}));
|
||||
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
|
||||
if (tot > 0.0)
|
||||
{
|
||||
std::string tot_s = "total " + fmt1(tot) + " GiB";
|
||||
xframe::draw_text_font(display_, window_, gc_, legend_font,
|
||||
x2_ - 8 - xframe::font_text_width(legend_font, tot_s),
|
||||
start_y + 4 + title_asc, tot_s, col(Srgb{20, 20, 20}));
|
||||
xf::draw_text_at(display_, window_, gc_, st, x2_ - 8 - xf::text_width(st, tot_s), y1_ + 15, tot_s);
|
||||
}
|
||||
|
||||
// Stacked bar.
|
||||
int bx = x1_ + 8;
|
||||
int by = start_y + title_h + 6;
|
||||
int by = y1_ + title_h + 4;
|
||||
int bw = std::max(10, w - 16);
|
||||
int x = bx;
|
||||
for (auto& s : segments_)
|
||||
@@ -184,13 +159,11 @@ public:
|
||||
XDrawRectangle(display_, window_, gc_, bx, by, bw, bar_h);
|
||||
|
||||
// Legend: one row per segment — swatch, name, value, share.
|
||||
int ly = by + bar_h + 8;
|
||||
int ly = by + bar_h + 18;
|
||||
for (auto& s : segments_)
|
||||
{
|
||||
int base = ly + (row_h + legend_asc - legend_desc) / 2;
|
||||
XSetForeground(display_, gc_, col(s.color));
|
||||
XFillRectangle(display_, window_, gc_, x1_ + 8, ly + (row_h - legend_px) / 2,
|
||||
legend_px, legend_px);
|
||||
XFillRectangle(display_, window_, gc_, x1_ + 8, ly - 10, 12, 12);
|
||||
std::string line = s.name + " " + fmt1(s.value) + " GiB";
|
||||
if (tot > 0.0)
|
||||
{
|
||||
@@ -198,10 +171,8 @@ public:
|
||||
std::snprintf(share, sizeof(share), " %.0f%%", s.value / tot * 100.0);
|
||||
line += share;
|
||||
}
|
||||
xframe::draw_text_font(display_, window_, gc_, legend_font,
|
||||
x1_ + 8 + legend_px + 8, base, line,
|
||||
col(Srgb{20, 20, 20}));
|
||||
ly += row_h;
|
||||
xf::draw_text_at(display_, window_, gc_, st, x1_ + 28, ly, line);
|
||||
ly += 18;
|
||||
}
|
||||
|
||||
XSetForeground(display_, gc_, st.lineColor_);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/xframe_core.h"
|
||||
#include "../InstData.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
@@ -150,49 +149,22 @@ public:
|
||||
XSetForeground(display_, gc_, col(white_color_));
|
||||
XFillRectangle(display_, window_, gc_, x1_, y1_, w, h);
|
||||
|
||||
// Scaled fonts (Dec 5, 2026 proportions audit): hero value
|
||||
// 28-32 px bold, title 14-16 px bold, "max" 13-14 px, bottom bar
|
||||
// 24-28 px tall with a 14 px label. NOTE on the hero rule: the
|
||||
// spec's 6-7% of height and the Python port's audited-correct
|
||||
// 10.3% conflict, so the hero uses 10% of height clamped to
|
||||
// 20-32 px — 28-32 px at the 480 px demo scale, ~23 px at the
|
||||
// Python port's natural size.
|
||||
int title_px = xframe::clamp_font_px(0.033 * h, 11, 16);
|
||||
int hero_px = xframe::clamp_font_px(0.10 * h, 20, 32);
|
||||
int max_px = xframe::clamp_font_px(0.03 * h, 11, 14);
|
||||
int bar_label_px = xframe::clamp_font_px(0.03 * h, 11, 14);
|
||||
XFontStruct* title_font = xframe::scaled_font(display_, title_px, true);
|
||||
if (!title_font) title_font = st.font_;
|
||||
XFontStruct* hero_font = xframe::scaled_font(display_, hero_px, true);
|
||||
if (!hero_font) hero_font = st.font_;
|
||||
XFontStruct* max_font = xframe::scaled_font(display_, max_px, false);
|
||||
if (!max_font) max_font = st.font_;
|
||||
XFontStruct* bar_font = xframe::scaled_font(display_, bar_label_px, true);
|
||||
if (!bar_font) bar_font = st.font_;
|
||||
int title_asc = title_font ? title_font->ascent : 11;
|
||||
int hero_asc = hero_font ? hero_font->ascent : 11;
|
||||
int bar_asc = bar_font ? bar_font->ascent : 11;
|
||||
int bar_desc = bar_font ? bar_font->descent : 2;
|
||||
|
||||
const int title_h = title_px + 8;
|
||||
const int hero_h = hero_px + 10;
|
||||
const int bar_h = xframe::clamp_font_px(0.055 * h, 20, 28);
|
||||
const int title_h = 20;
|
||||
const int big_h = 30;
|
||||
const int bar_h = 18;
|
||||
int gx = x1_ + 8;
|
||||
int gy = y1_ + title_h + hero_h + 4;
|
||||
int gy = y1_ + title_h + big_h + 4;
|
||||
int gw = std::max(10, w - 16);
|
||||
int gh = std::max(10, y2_ - bar_h - 12 - gy);
|
||||
|
||||
// Title + big number.
|
||||
xframe::draw_text_font(display_, window_, gc_, title_font, x1_ + 8,
|
||||
y1_ + 4 + title_asc, text_, col(Srgb{20, 20, 20}));
|
||||
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
|
||||
std::string big = fmt1(value_) + unit_;
|
||||
xframe::draw_text_font(display_, window_, gc_, hero_font, x1_ + 8,
|
||||
y1_ + title_h + hero_asc + 2, big, col(graph_color_));
|
||||
draw_text_color(x1_ + 8, y1_ + title_h + 22, big, graph_color_);
|
||||
draw_text_color(x1_ + 9, y1_ + title_h + 22, big, graph_color_);
|
||||
// Max at the right of the big number row.
|
||||
std::string mx = "max " + fmt1(max_) + unit_;
|
||||
xframe::draw_text_font(display_, window_, gc_, max_font,
|
||||
x2_ - 8 - xframe::font_text_width(max_font, mx),
|
||||
y1_ + title_h + hero_asc + 2, mx, col(Srgb{100, 100, 100}));
|
||||
xf::draw_text_at(display_, window_, gc_, st, x2_ - 8 - xf::text_width(st, mx), y1_ + title_h + 22, mx);
|
||||
|
||||
// History graph.
|
||||
XSetForeground(display_, gc_, col(grid_color_));
|
||||
@@ -228,10 +200,8 @@ public:
|
||||
XDrawRectangle(display_, window_, gc_, bx, by, bw, bar_h);
|
||||
char pct[16];
|
||||
std::snprintf(pct, sizeof(pct), "%.0f%%", max_ > 0.0 ? value_ / max_ * 100.0 : 0.0);
|
||||
int pw = xframe::font_text_width(bar_font, pct);
|
||||
xframe::draw_text_font(display_, window_, gc_, bar_font, bx + bw - pw - 4,
|
||||
by + (bar_h + bar_asc - bar_desc) / 2, pct,
|
||||
col(Srgb{20, 20, 20}));
|
||||
int pw = xf::text_width(st, pct);
|
||||
xf::draw_text_at(display_, window_, gc_, st, bx + bw - pw - 4, by + 13, pct);
|
||||
|
||||
XSetForeground(display_, gc_, st.lineColor_);
|
||||
XDrawRectangle(display_, window_, gc_, x1_, y1_, w, h);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/xframe_core.h"
|
||||
#include "../InstData.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
@@ -139,18 +138,7 @@ public:
|
||||
double red_frac = max_ > 0.0 ? redline_ / max_ : 1.0;
|
||||
double peak_frac = max_ > 0.0 ? peak_ / max_ : 0.0;
|
||||
|
||||
// Scaled fonts (Dec 5, 2026 tach fix): dial numerals ~7-8% of
|
||||
// the dial diameter, bold, just inside the major tick ring;
|
||||
// "RPM" title slightly smaller, bold; readout legible, bold.
|
||||
int numeral_px = xframe::clamp_font_px(0.075 * 2 * r, 13, 28);
|
||||
int title_px = xframe::clamp_font_px(numeral_px - 4, 13, 24);
|
||||
int readout_px = xframe::clamp_font_px(0.05 * 2 * r, 13, 20);
|
||||
XFontStruct* numeral_font = xframe::scaled_font(display_, numeral_px, true);
|
||||
if (!numeral_font) numeral_font = st.font_;
|
||||
XFontStruct* title_font = xframe::scaled_font(display_, title_px, true);
|
||||
if (!title_font) title_font = st.font_;
|
||||
XFontStruct* readout_font = xframe::scaled_font(display_, readout_px, true);
|
||||
if (!readout_font) readout_font = st.font_;
|
||||
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
|
||||
|
||||
// Dial face.
|
||||
XSetForeground(display_, gc_, col(dial_color_));
|
||||
@@ -186,40 +174,10 @@ public:
|
||||
std::string lab = fmt0(max_ * f / 1000.0);
|
||||
int lx = cx + (int)((r - 26) * std::cos(a));
|
||||
int ly = cy - (int)((r - 26) * std::sin(a));
|
||||
int lw = xframe::font_text_width(numeral_font, lab);
|
||||
// Numerals at/after the redline render in red.
|
||||
Srgb lab_color = (f >= red_frac) ? redline_color_ : Srgb{20, 20, 20};
|
||||
xframe::draw_text_font(display_, window_, gc_, numeral_font,
|
||||
lx - lw / 2,
|
||||
ly + (numeral_font ? numeral_font->ascent : 11) / 2,
|
||||
lab, col(lab_color));
|
||||
xf::draw_text_at(display_, window_, gc_, st, lx - xf::text_width(st, lab) / 2, ly + 4, lab);
|
||||
}
|
||||
}
|
||||
|
||||
// "RPM" title: bold, centred inside the dial below the top
|
||||
// numerals (not a tiny top-left corner label). Its baseline
|
||||
// hangs below the top numeral's bottom edge, so it cannot
|
||||
// collide with the neighbouring numerals on small dials.
|
||||
if (!text_.empty())
|
||||
{
|
||||
int tw = xframe::font_text_width(title_font, text_);
|
||||
int title_base = cy - (r - 22) + (int)(numeral_px * 0.75)
|
||||
+ title_font->ascent + 4;
|
||||
xframe::draw_text_font(display_, window_, gc_, title_font,
|
||||
cx - tw / 2, title_base, text_,
|
||||
col(Srgb{20, 20, 20}));
|
||||
}
|
||||
|
||||
// Current value + peak readout: enlarged, inside the dial below
|
||||
// the hub (not cramped at the bottom edge).
|
||||
{
|
||||
std::string readout = fmt0(value_) + " (peak " + fmt0(peak_) + ")";
|
||||
int rw = xframe::font_text_width(readout_font, readout);
|
||||
xframe::draw_text_font(display_, window_, gc_, readout_font,
|
||||
cx - rw / 2, cy + (int)(r * 0.45), readout,
|
||||
col(Srgb{20, 20, 20}));
|
||||
}
|
||||
|
||||
// Peak-hold tick.
|
||||
{
|
||||
double a = angle_deg(peak_frac) * M_PI / 180.0;
|
||||
@@ -245,6 +203,11 @@ public:
|
||||
XFillArc(display_, window_, gc_, cx - 6, cy - 6, 12, 12, 0, 360 * 64);
|
||||
}
|
||||
|
||||
// Digital readout under the dial.
|
||||
std::string readout = fmt0(value_) + " (peak " + fmt0(peak_) + ")";
|
||||
int rw = xf::text_width(st, readout);
|
||||
xf::draw_text_at(display_, window_, gc_, st, cx - rw / 2, y2_ - 10, readout);
|
||||
|
||||
XSetForeground(display_, gc_, st.lineColor_);
|
||||
XDrawRectangle(display_, window_, gc_, x1_, y1_, w, h);
|
||||
XFlush(display_);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/xframe_core.h"
|
||||
#include "../InstData.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
@@ -120,6 +119,15 @@ public:
|
||||
temps_.push_back(temp_c);
|
||||
while ((int)temps_.size() > capacity_) temps_.erase(temps_.begin());
|
||||
}
|
||||
// Drop every sample (and nothing else): the widget then shows its
|
||||
// honest empty state ("no data") instead of the demo ramp the
|
||||
// constructor preloads. A host app with no live source yet (e.g.
|
||||
// a machine with no thermal zones) calls this at startup; later
|
||||
// push() calls fill the series again as usual.
|
||||
void clear()
|
||||
{
|
||||
temps_.clear();
|
||||
}
|
||||
void set_threshold(double t) { threshold_ = t; }
|
||||
double get_threshold() { return threshold_; }
|
||||
void set_y_max(double v) { y_max_ = v > 0.0 ? v : 100.0; }
|
||||
@@ -139,54 +147,23 @@ public:
|
||||
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,
|
||||
// "now" 14-16 px bold, Y labels + subtitle 14-16 px, threshold
|
||||
// label 14 px — all derived from widget height. Layout
|
||||
// standardised on the Python port: big current value top-right,
|
||||
// min/max/avg on a bottom line.
|
||||
int title_px = xframe::clamp_font_px(0.033 * h, 11, 16);
|
||||
int body_px = xframe::clamp_font_px(0.032 * h, 11, 16);
|
||||
XFontStruct* title_font = xframe::scaled_font(display_, title_px, true);
|
||||
if (!title_font) title_font = st.font_;
|
||||
XFontStruct* body_font = xframe::scaled_font(display_, body_px, false);
|
||||
if (!body_font) body_font = st.font_;
|
||||
XFontStruct* bold_font = xframe::scaled_font(display_, body_px, true);
|
||||
if (!bold_font) bold_font = st.font_;
|
||||
int title_asc = title_font ? title_font->ascent : 11;
|
||||
int body_asc = body_font ? body_font->ascent : 11;
|
||||
|
||||
const int title_h = title_px + 8;
|
||||
const int bottom_h = body_px + 8;
|
||||
double hi = y_max_ > 0.0 ? y_max_ : 100.0;
|
||||
|
||||
// Dynamic left margin: the widest "100.0C" label sets the plot's
|
||||
// left edge; labels are right-aligned, clear of the axis line.
|
||||
std::string ylabels[5];
|
||||
int axis_w = 8;
|
||||
for (int g = 0; g <= 4; ++g)
|
||||
{
|
||||
ylabels[g] = fmt1(hi * (4 - g) / 4.0) + "C";
|
||||
axis_w = std::max(axis_w, xframe::font_text_width(body_font, ylabels[g]) + 8);
|
||||
}
|
||||
const int title_h = 20;
|
||||
const int legend_h = 18;
|
||||
const int axis_w = 44;
|
||||
const int axis_h = 16;
|
||||
int px = x1_ + axis_w;
|
||||
int py = y1_ + title_h + 4;
|
||||
int py = y1_ + title_h + legend_h + 4;
|
||||
int pw = std::max(10, x2_ - 8 - px);
|
||||
int ph = std::max(10, y2_ - bottom_h - 6 - py);
|
||||
int ph = std::max(10, y2_ - axis_h - py);
|
||||
double hi = y_max_ > 0.0 ? y_max_ : 100.0;
|
||||
|
||||
auto y_at = [&](double v) {
|
||||
return py + ph - (int)(std::max(0.0, std::min(hi, v)) / hi * ph);
|
||||
};
|
||||
|
||||
// Title left, big current value right (bold, red when over).
|
||||
xframe::draw_text_font(display_, window_, gc_, title_font, x1_ + 8,
|
||||
y1_ + 4 + title_asc, text_, col(Srgb{20, 20, 20}));
|
||||
std::string cur = "now " + fmt1(current()) + "C";
|
||||
xframe::draw_text_font(display_, window_, gc_, bold_font,
|
||||
x2_ - 8 - xframe::font_text_width(bold_font, cur),
|
||||
y1_ + 4 + title_asc, cur,
|
||||
col(over_temp() ? hot_color_ : Srgb{20, 20, 20}));
|
||||
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
|
||||
|
||||
// Grid + y labels (right-aligned, clear of the plot).
|
||||
// Grid + y labels.
|
||||
XSetForeground(display_, gc_, col(grid_color_));
|
||||
for (int g = 0; g <= 4; ++g)
|
||||
{
|
||||
@@ -195,16 +172,11 @@ public:
|
||||
}
|
||||
for (int g = 0; g <= 4; ++g)
|
||||
{
|
||||
int gy = py + ph * g / 4;
|
||||
int lw = xframe::font_text_width(body_font, ylabels[g]);
|
||||
int ybase = (g == 0) ? gy + body_asc
|
||||
: (g == 4) ? gy - 2 : gy + body_asc / 2;
|
||||
xframe::draw_text_font(display_, window_, gc_, body_font, px - 4 - lw,
|
||||
ybase, ylabels[g], col(Srgb{100, 100, 100}));
|
||||
double v = hi * (4 - g) / 4.0;
|
||||
draw_text_color(px - 40, py + ph * g / 4 + 4, fmt1(v) + "C", Srgb{100, 100, 100});
|
||||
}
|
||||
|
||||
// Threshold: dashed red line + label ABOVE the line with
|
||||
// clearance (never on the line).
|
||||
// Threshold: dashed red line + label.
|
||||
{
|
||||
XGCValues gv;
|
||||
gv.line_style = LineOnOffDash;
|
||||
@@ -215,9 +187,7 @@ public:
|
||||
gv.line_style = LineSolid;
|
||||
XChangeGC(display_, gc_, GCLineStyle, &gv);
|
||||
std::string lim = fmt1(threshold_) + "C limit";
|
||||
xframe::draw_text_font(display_, window_, gc_, body_font,
|
||||
px + pw - xframe::font_text_width(body_font, lim) - 4,
|
||||
ty - 4, lim, col(hot_color_));
|
||||
draw_text_color(px + pw - xf::text_width(st, lim) - 4, ty - 4, lim, hot_color_);
|
||||
}
|
||||
|
||||
// Temperature line: green below the threshold, red above it.
|
||||
@@ -236,22 +206,30 @@ public:
|
||||
xf::set_line_width(display_, gc_, 1);
|
||||
}
|
||||
|
||||
// Bottom line: min / max / avg.
|
||||
double mn = 0.0, mx = 0.0, sum = 0.0;
|
||||
if (!temps_.empty())
|
||||
// Legend: current (big, coloured by state) + min / max.
|
||||
// Empty (no samples pushed, e.g. no thermal zones found):
|
||||
// say so honestly instead of a 0.0 C reading that never
|
||||
// happened. The threshold line and scale still draw.
|
||||
if (temps_.empty())
|
||||
{
|
||||
mn = mx = temps_[0];
|
||||
std::string nd = "no data";
|
||||
draw_text_color(x2_ - 8 - xf::text_width(st, nd), y1_ + title_h + 14, nd, Srgb{100, 100, 100});
|
||||
std::string mm = "threshold " + fmt1(threshold_) + "C";
|
||||
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + title_h + 14, mm);
|
||||
}
|
||||
else
|
||||
{
|
||||
double mn = temps_[0], mx = temps_[0];
|
||||
for (double v : temps_)
|
||||
{
|
||||
mn = std::min(mn, v);
|
||||
mx = std::max(mx, v);
|
||||
sum += v;
|
||||
}
|
||||
sum /= (double)temps_.size();
|
||||
std::string cur = "now " + fmt1(current()) + "C";
|
||||
draw_text_color(x2_ - 8 - xf::text_width(st, cur), y1_ + title_h + 14, cur, over_temp() ? hot_color_ : ok_color_);
|
||||
std::string mm = "min " + fmt1(mn) + "C max " + fmt1(mx) + "C threshold " + fmt1(threshold_) + "C";
|
||||
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + title_h + 14, mm);
|
||||
}
|
||||
std::string mm = "min " + fmt1(mn) + "C max " + fmt1(mx) + "C avg " + fmt1(sum) + "C";
|
||||
xframe::draw_text_font(display_, window_, gc_, body_font, x1_ + 8,
|
||||
py + ph + 4 + body_asc, mm, col(Srgb{100, 100, 100}));
|
||||
|
||||
XSetForeground(display_, gc_, col(axis_color_));
|
||||
XDrawRectangle(display_, window_, gc_, px, py, pw, ph);
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/xframe_core.h"
|
||||
#include "../InstData.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
@@ -111,33 +110,19 @@ public:
|
||||
XSetForeground(display_, gc_, col(white_color_));
|
||||
XFillRectangle(display_, window_, gc_, x1_, y1_, w, h);
|
||||
|
||||
// Bar geometry first: the label size derives from the bar.
|
||||
int bh = std::max(8, h - 16);
|
||||
int by = y1_ + (h - bh) / 2;
|
||||
// Label height = 0.45 x bar height (min 11 px). NOTE: at the
|
||||
// stretched 720x480 demo the bar is 464 px tall, so the literal
|
||||
// rule gives a 209 px label; capped at 40 px to match the
|
||||
// audit's demo-size expectation (~35-45 px). At real app sizes
|
||||
// (bar ~24 px) the rule gives ~11 px.
|
||||
int label_px = xframe::clamp_font_px(0.45 * bh, 11, 40);
|
||||
XFontStruct* label_font = xframe::scaled_font(display_, label_px, true);
|
||||
if (!label_font) label_font = st.font_;
|
||||
int label_asc = label_font ? label_font->ascent : 11;
|
||||
int label_desc = label_font ? label_font->descent : 2;
|
||||
// Labels outside the bar, vertically centred on it.
|
||||
int base = by + (bh + label_asc - label_desc) / 2;
|
||||
// Label left, % right.
|
||||
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + (h + 12) / 2 - 4, text_);
|
||||
char pct[16];
|
||||
std::snprintf(pct, sizeof(pct), "%.0f%%", fraction() * 100.0);
|
||||
int pct_w = xframe::font_text_width(label_font, pct);
|
||||
int label_w = xframe::font_text_width(label_font, text_) + 16;
|
||||
xframe::draw_text_font(display_, window_, gc_, label_font, x1_ + 8, base,
|
||||
text_, col(Srgb{20, 20, 20}));
|
||||
xframe::draw_text_font(display_, window_, gc_, label_font, x2_ - 8 - pct_w, base,
|
||||
pct, col(Srgb{20, 20, 20}));
|
||||
int pct_w = xf::text_width(st, pct);
|
||||
xf::draw_text_at(display_, window_, gc_, st, x2_ - 8 - pct_w, y1_ + (h + 12) / 2 - 4, pct);
|
||||
|
||||
// Bar track between label and %.
|
||||
int label_w = xf::text_width(st, text_) + 16;
|
||||
int bx = x1_ + label_w;
|
||||
int bw = std::max(10, x2_ - 16 - pct_w - bx);
|
||||
int bh = std::max(8, h - 16);
|
||||
int by = y1_ + (h - bh) / 2;
|
||||
XSetForeground(display_, gc_, col(track_color_));
|
||||
XFillRectangle(display_, window_, gc_, bx, by, bw, bh);
|
||||
// Gradient fill: 48 slices across the filled fraction.
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/xframe_core.h"
|
||||
#include "../InstData.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
@@ -121,38 +120,15 @@ public:
|
||||
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,
|
||||
// Y labels 14-16 px, "peak" 14 px bold, value 16 px bold inside
|
||||
// the bar (never on top of a tick label).
|
||||
int title_px = xframe::clamp_font_px(0.033 * h, 11, 16);
|
||||
int body_px = xframe::clamp_font_px(0.032 * h, 11, 16);
|
||||
int value_px = xframe::clamp_font_px(0.033 * h, 13, 16);
|
||||
XFontStruct* title_font = xframe::scaled_font(display_, title_px, true);
|
||||
if (!title_font) title_font = st.font_;
|
||||
XFontStruct* body_font = xframe::scaled_font(display_, body_px, false);
|
||||
if (!body_font) body_font = st.font_;
|
||||
XFontStruct* bold_font = xframe::scaled_font(display_, body_px, true);
|
||||
if (!bold_font) bold_font = st.font_;
|
||||
XFontStruct* value_font = xframe::scaled_font(display_, value_px, true);
|
||||
if (!value_font) value_font = st.font_;
|
||||
int title_asc = title_font ? title_font->ascent : 11;
|
||||
int body_asc = body_font ? body_font->ascent : 11;
|
||||
|
||||
const int title_h = title_px + 8;
|
||||
// Dynamic scale width: the widest Y label sets it.
|
||||
int scale_w = 8;
|
||||
for (int g = 0; g <= 4; ++g)
|
||||
{
|
||||
std::string lab = fmt0(max_ * (4 - g) / 4.0);
|
||||
scale_w = std::max(scale_w, xframe::font_text_width(body_font, lab) + 12);
|
||||
}
|
||||
const int title_h = 20;
|
||||
const int scale_w = 36;
|
||||
const int bottom_h = 18;
|
||||
int bx = x1_ + scale_w + 8;
|
||||
int by = y1_ + title_h + 8;
|
||||
int by = y1_ + title_h + 16;
|
||||
int bw = std::max(10, x2_ - 8 - bx);
|
||||
int bh = std::max(10, y2_ - 8 - by);
|
||||
int bh = std::max(10, y2_ - bottom_h - by);
|
||||
|
||||
xframe::draw_text_font(display_, window_, gc_, title_font, x1_ + 8,
|
||||
y1_ + 4 + title_asc, text_, col(Srgb{20, 20, 20}));
|
||||
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
|
||||
|
||||
// Scale ticks + labels (0 / 25 / 50 / 75 / 100 % of max).
|
||||
for (int g = 0; g <= 4; ++g)
|
||||
@@ -163,9 +139,7 @@ public:
|
||||
XSetForeground(display_, gc_, col(axis_color_));
|
||||
XDrawLine(display_, window_, gc_, bx - 4, gy, bx, gy);
|
||||
std::string lab = fmt0(max_ * (4 - g) / 4.0);
|
||||
int lw = xframe::font_text_width(body_font, lab);
|
||||
xframe::draw_text_font(display_, window_, gc_, body_font, bx - 8 - lw,
|
||||
gy + body_asc / 2, lab, col(Srgb{100, 100, 100}));
|
||||
xf::draw_text_at(display_, window_, gc_, st, bx - 8 - xf::text_width(st, lab), gy + 4, lab);
|
||||
}
|
||||
|
||||
// Fill from the bottom.
|
||||
@@ -175,8 +149,7 @@ public:
|
||||
XSetForeground(display_, gc_, col(axis_color_));
|
||||
XDrawRectangle(display_, window_, gc_, bx, by, bw, bh);
|
||||
|
||||
// Peak-hold marker: red line across the bar + value above it
|
||||
// with clearance, inset from the frame edge.
|
||||
// Peak-hold marker: red line across the bar + value.
|
||||
if (peak_hold_)
|
||||
{
|
||||
int py = by + bh - (int)(bh * peak_ / max_);
|
||||
@@ -185,40 +158,12 @@ public:
|
||||
XDrawLine(display_, window_, gc_, bx - 4, py, bx + bw + 4, py);
|
||||
xf::set_line_width(display_, gc_, 1);
|
||||
std::string pk = "peak " + fmt0(peak_);
|
||||
// Shrink to fit when the bar is narrow: the label must
|
||||
// stay inside the frame (right-aligned at the bar's right
|
||||
// edge minus 4px), never spill past the scale labels.
|
||||
int pk_px = 14;
|
||||
XFontStruct* pk_font = bold_font;
|
||||
int avail_w = (bx + bw - 4) - (x1_ + 8);
|
||||
for (int try_px = 14; try_px >= 11; --try_px)
|
||||
{
|
||||
XFontStruct* f = xframe::scaled_font(display_, try_px, true);
|
||||
if (xframe::font_text_width(f, pk) <= avail_w)
|
||||
{
|
||||
pk_px = try_px;
|
||||
pk_font = f;
|
||||
break;
|
||||
}
|
||||
pk_px = try_px;
|
||||
pk_font = f;
|
||||
}
|
||||
(void)pk_px;
|
||||
xframe::draw_text_font(display_, window_, gc_, pk_font,
|
||||
bx + bw - xframe::font_text_width(pk_font, pk) - 4,
|
||||
py - 6, pk, col(peak_color_));
|
||||
xf::draw_text_at(display_, window_, gc_, st, bx + bw - xf::text_width(st, pk) - 4, py - 4, pk);
|
||||
}
|
||||
|
||||
// Current value INSIDE the bar near the bottom, centred (white
|
||||
// on the fill; dark when the fill is too short to cover it).
|
||||
{
|
||||
char cur[16];
|
||||
std::snprintf(cur, sizeof(cur), "%.0f%%", value_);
|
||||
int cw = xframe::font_text_width(value_font, cur);
|
||||
Srgb vcolor = (filled >= value_px + 8) ? Srgb{255, 255, 255} : Srgb{20, 20, 20};
|
||||
xframe::draw_text_font(display_, window_, gc_, value_font,
|
||||
bx + (bw - cw) / 2, by + bh - 6, cur, col(vcolor));
|
||||
}
|
||||
// Current value under the bar.
|
||||
std::string cur = fmt0(value_) + " / " + fmt0(max_);
|
||||
xf::draw_text_at(display_, window_, gc_, st, bx, y2_ - 4, cur);
|
||||
|
||||
XSetForeground(display_, gc_, st.lineColor_);
|
||||
XDrawRectangle(display_, window_, gc_, x1_, y1_, w, h);
|
||||
|
||||