10 changed files with 314 additions and 232 deletions

No files matched your search

@@ -0,0 +1 @@
process-manager
+25
View File
@@ -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/).
+207
View File
@@ -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,28 +0,0 @@
# 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
@@ -1,59 +0,0 @@
# 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)
-145
View File
@@ -1,145 +0,0 @@
// 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;
}
Binary file not shown.

Before

Width:  |  Height:  |  Size: 3.1 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)