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,13 +0,0 @@
|
||||
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
|
||||
@@ -1,34 +0,0 @@
|
||||
# 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.
|
||||
@@ -1,420 +0,0 @@
|
||||
// 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;
|
||||
}
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 13 KiB |
Binary file not shown.
|
Before Width: | Height: | Size: 11 KiB |
@@ -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