Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
82def11311 | ||
|
|
6339e64501 |
No files matched your search
@@ -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;
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 12 KiB |
@@ -1,23 +0,0 @@
|
||||
# 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
|
||||
@@ -1,37 +0,0 @@
|
||||
# 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/
|
||||
@@ -1,302 +0,0 @@
|
||||
// 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;
|
||||
}
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 8.7 KiB |
Binary file not shown.
@@ -3,6 +3,7 @@
|
||||
#include "core/xframe_core.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
@@ -50,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};
|
||||
@@ -231,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)
|
||||
@@ -247,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()
|
||||
@@ -302,6 +323,11 @@ public:
|
||||
XSetForeground(display_, gc_, col(Srgb{248, 248, 248}));
|
||||
XFillRectangle(display_, window_, gc_, ix, ry, iw, row_h);
|
||||
}
|
||||
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);
|
||||
xf::draw_text_at(display_, window_, gc_, st, xs[1] + 2, ry + 12, prog);
|
||||
@@ -346,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)
|
||||
|
||||
Reference in new issue
Block a user