From 8c295dbbdff0a4f25a791010c87e455f169bb16d Mon Sep 17 00:00:00 2001 From: Muse Date: Sat, 5 Dec 2026 16:49:29 +0000 Subject: [PATCH] XFrame monitor widgets: XProcessList, XLoadBadge, XMonitorButton, XMonitorGrid, XSysBox Live process table from /proc (sortable columns, in-cell CPU% bars, colour-coded rows, refresh()), load-average badge (1/5/15 min, threshold colours, uptime from /proc), monitor button bound to a process (/proc scan), systemd unit (systemctl is-active) or TCP port (connect to localhost) with poll()/update() and click-for-details, grid panel of monitor buttons with one shared poll_all(), composite resource box (title + big number + history + usage bar). Header-only C++, demo mains (polling loop for the live three), examples, Xvfb screenshots. --- examples/XLoadBadge.cpp | 73 ++ examples/XMonitorButton.cpp | 92 ++ examples/XMonitorGrid.cpp | 92 ++ examples/XProcessList.cpp | 92 ++ examples/XSysBox.cpp | 73 ++ widgets/components/XLoadBadge/CMakeLists.txt | 13 + widgets/components/XLoadBadge/XLoadBadge.h | 219 ++++ .../components/XLoadBadge/core/xframe_core.h | 988 ++++++++++++++++++ .../components/XLoadBadge/main_XLoadBadge.cpp | 69 ++ .../components/XMonitorButton/CMakeLists.txt | 13 + .../XMonitorButton/XMonitorButton.h | 313 ++++++ .../XMonitorButton/core/xframe_core.h | 988 ++++++++++++++++++ .../XMonitorButton/main_XMonitorButton.cpp | 88 ++ .../components/XMonitorGrid/CMakeLists.txt | 13 + .../components/XMonitorGrid/XMonitorGrid.h | 193 ++++ .../XMonitorGrid/core/xframe_core.h | 988 ++++++++++++++++++ .../XMonitorGrid/main_XMonitorGrid.cpp | 88 ++ .../components/XProcessList/CMakeLists.txt | 13 + .../components/XProcessList/XProcessList.h | 370 +++++++ .../XProcessList/core/xframe_core.h | 988 ++++++++++++++++++ .../XProcessList/main_XProcessList.cpp | 88 ++ widgets/components/XSysBox/CMakeLists.txt | 13 + widgets/components/XSysBox/XSysBox.h | 237 +++++ widgets/components/XSysBox/core/xframe_core.h | 988 ++++++++++++++++++ widgets/components/XSysBox/main_XSysBox.cpp | 69 ++ widgets/screenshots/XLoadBadge.png | Bin 0 -> 1138 bytes widgets/screenshots/XMonitorButton.png | Bin 0 -> 741 bytes widgets/screenshots/XMonitorGrid.png | Bin 0 -> 1941 bytes widgets/screenshots/XProcessList.png | Bin 0 -> 7539 bytes widgets/screenshots/XSysBox.png | Bin 0 -> 3606 bytes 30 files changed, 7161 insertions(+) create mode 100644 examples/XLoadBadge.cpp create mode 100644 examples/XMonitorButton.cpp create mode 100644 examples/XMonitorGrid.cpp create mode 100644 examples/XProcessList.cpp create mode 100644 examples/XSysBox.cpp create mode 100644 widgets/components/XLoadBadge/CMakeLists.txt create mode 100644 widgets/components/XLoadBadge/XLoadBadge.h create mode 100644 widgets/components/XLoadBadge/core/xframe_core.h create mode 100644 widgets/components/XLoadBadge/main_XLoadBadge.cpp create mode 100644 widgets/components/XMonitorButton/CMakeLists.txt create mode 100644 widgets/components/XMonitorButton/XMonitorButton.h create mode 100644 widgets/components/XMonitorButton/core/xframe_core.h create mode 100644 widgets/components/XMonitorButton/main_XMonitorButton.cpp create mode 100644 widgets/components/XMonitorGrid/CMakeLists.txt create mode 100644 widgets/components/XMonitorGrid/XMonitorGrid.h create mode 100644 widgets/components/XMonitorGrid/core/xframe_core.h create mode 100644 widgets/components/XMonitorGrid/main_XMonitorGrid.cpp create mode 100644 widgets/components/XProcessList/CMakeLists.txt create mode 100644 widgets/components/XProcessList/XProcessList.h create mode 100644 widgets/components/XProcessList/core/xframe_core.h create mode 100644 widgets/components/XProcessList/main_XProcessList.cpp create mode 100644 widgets/components/XSysBox/CMakeLists.txt create mode 100644 widgets/components/XSysBox/XSysBox.h create mode 100644 widgets/components/XSysBox/core/xframe_core.h create mode 100644 widgets/components/XSysBox/main_XSysBox.cpp create mode 100644 widgets/screenshots/XLoadBadge.png create mode 100644 widgets/screenshots/XMonitorButton.png create mode 100644 widgets/screenshots/XMonitorGrid.png create mode 100644 widgets/screenshots/XProcessList.png create mode 100644 widgets/screenshots/XSysBox.png diff --git a/examples/XLoadBadge.cpp b/examples/XLoadBadge.cpp new file mode 100644 index 0000000..2be7845 --- /dev/null +++ b/examples/XLoadBadge.cpp @@ -0,0 +1,73 @@ +// XLoadBadge -- minimal XFrame example (C++). +// Build: g++ -std=c++17 -I/root/lg-review/lightgui/widgets/components -I/root/lg-review/lightgui/widgets XLoadBadge.cpp -lX11 -o XLoadBadge +// Run under Xvfb (or a real X server); close the window to quit. +// Setup mirrors the widget's own demo in widgets/components/XLoadBadge/main_XLoadBadge.cpp. +#include "XLoadBadge/XLoadBadge.h" + +#include + +int main() +{ + Display* display = XOpenDisplay(nullptr); + if (!display) { return 1; } + int screen = DefaultScreen(display); + Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 800, 600, 1, BlackPixel(display, screen), WhitePixel(display, screen)); + XStoreName(display, window, "XLoadBadge - XFrame demo"); + 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"); + XLoadBadge widget(display, screen, window, gc, &styles, 20, 20, 720, 480); + widget.draw(); + bool running = true; + while (running) + { + XEvent event; + XNextEvent(display, &event); + bool handled = false; + switch (event.type) + { + case Expose: + handled = true; + break; + case ButtonPress: + handled = widget.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case ButtonRelease: + handled = widget.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case MotionNotify: + handled = widget.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 = widget.on_key(key, buffer); + break; + } + case ClientMessage: + if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) + { + running = false; + } + break; + } + if (handled) + { + XClearWindow(display, window); + widget.draw(); + XFlush(display); + } + } + + XCloseDisplay(display); + return 0; +} diff --git a/examples/XMonitorButton.cpp b/examples/XMonitorButton.cpp new file mode 100644 index 0000000..116b1e0 --- /dev/null +++ b/examples/XMonitorButton.cpp @@ -0,0 +1,92 @@ +// XMonitorButton -- minimal XFrame example (C++). +// Build: g++ -std=c++17 -I/root/lg-review/lightgui/widgets/components -I/root/lg-review/lightgui/widgets XMonitorButton.cpp -lX11 -o XMonitorButton +// Run under Xvfb (or a real X server); close the window to quit. +// Setup mirrors the widget's own demo in widgets/components/XMonitorButton/main_XMonitorButton.cpp. +#include "XMonitorButton/XMonitorButton.h" + +#include + +#include +#include + +int main() +{ + Display* display = XOpenDisplay(nullptr); + if (!display) { return 1; } + int screen = DefaultScreen(display); + Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 800, 600, 1, BlackPixel(display, screen), WhitePixel(display, screen)); + XStoreName(display, window, "XMonitorButton - XFrame demo"); + 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"); + XMonitorButton widget(display, screen, window, gc, &styles, 20, 20, 720, 480); + widget.draw(); + bool running = true; + long last_poll_ms = 0; + while (running) + { + while (XPending(display) > 0) + { + XEvent event; + XNextEvent(display, &event); + bool handled = false; + switch (event.type) + { + case Expose: + handled = true; + break; + case ButtonPress: + handled = widget.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case ButtonRelease: + handled = widget.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case MotionNotify: + handled = widget.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 = widget.on_key(key, buffer); + break; + } + case ClientMessage: + if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) + { + running = false; + } + break; + } + if (handled) + { + XClearWindow(display, window); + widget.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; + widget.poll(); + XClearWindow(display, window); + widget.draw(); + XFlush(display); + } + usleep(50000); + } + + XCloseDisplay(display); + return 0; +} diff --git a/examples/XMonitorGrid.cpp b/examples/XMonitorGrid.cpp new file mode 100644 index 0000000..c79b62b --- /dev/null +++ b/examples/XMonitorGrid.cpp @@ -0,0 +1,92 @@ +// XMonitorGrid -- minimal XFrame example (C++). +// Build: g++ -std=c++17 -I/root/lg-review/lightgui/widgets/components -I/root/lg-review/lightgui/widgets XMonitorGrid.cpp -lX11 -o XMonitorGrid +// Run under Xvfb (or a real X server); close the window to quit. +// Setup mirrors the widget's own demo in widgets/components/XMonitorGrid/main_XMonitorGrid.cpp. +#include "XMonitorGrid/XMonitorGrid.h" + +#include + +#include +#include + +int main() +{ + Display* display = XOpenDisplay(nullptr); + if (!display) { return 1; } + int screen = DefaultScreen(display); + Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 800, 600, 1, BlackPixel(display, screen), WhitePixel(display, screen)); + XStoreName(display, window, "XMonitorGrid - XFrame demo"); + 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 widget(display, screen, window, gc, &styles, 20, 20, 720, 480); + widget.draw(); + bool running = true; + long last_poll_ms = 0; + while (running) + { + while (XPending(display) > 0) + { + XEvent event; + XNextEvent(display, &event); + bool handled = false; + switch (event.type) + { + case Expose: + handled = true; + break; + case ButtonPress: + handled = widget.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case ButtonRelease: + handled = widget.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case MotionNotify: + handled = widget.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 = widget.on_key(key, buffer); + break; + } + case ClientMessage: + if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) + { + running = false; + } + break; + } + if (handled) + { + XClearWindow(display, window); + widget.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; + widget.poll_all(); + XClearWindow(display, window); + widget.draw(); + XFlush(display); + } + usleep(50000); + } + + XCloseDisplay(display); + return 0; +} diff --git a/examples/XProcessList.cpp b/examples/XProcessList.cpp new file mode 100644 index 0000000..a85557e --- /dev/null +++ b/examples/XProcessList.cpp @@ -0,0 +1,92 @@ +// XProcessList -- minimal XFrame example (C++). +// Build: g++ -std=c++17 -I/root/lg-review/lightgui/widgets/components -I/root/lg-review/lightgui/widgets XProcessList.cpp -lX11 -o XProcessList +// Run under Xvfb (or a real X server); close the window to quit. +// Setup mirrors the widget's own demo in widgets/components/XProcessList/main_XProcessList.cpp. +#include "XProcessList/XProcessList.h" + +#include + +#include +#include + +int main() +{ + Display* display = XOpenDisplay(nullptr); + if (!display) { return 1; } + int screen = DefaultScreen(display); + Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 800, 600, 1, BlackPixel(display, screen), WhitePixel(display, screen)); + XStoreName(display, window, "XProcessList - XFrame demo"); + 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"); + XProcessList widget(display, screen, window, gc, &styles, 20, 20, 720, 480); + widget.draw(); + bool running = true; + long last_poll_ms = 0; + while (running) + { + while (XPending(display) > 0) + { + XEvent event; + XNextEvent(display, &event); + bool handled = false; + switch (event.type) + { + case Expose: + handled = true; + break; + case ButtonPress: + handled = widget.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case ButtonRelease: + handled = widget.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case MotionNotify: + handled = widget.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 = widget.on_key(key, buffer); + break; + } + case ClientMessage: + if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) + { + running = false; + } + break; + } + if (handled) + { + XClearWindow(display, window); + widget.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; + widget.refresh(); + XClearWindow(display, window); + widget.draw(); + XFlush(display); + } + usleep(50000); + } + + XCloseDisplay(display); + return 0; +} diff --git a/examples/XSysBox.cpp b/examples/XSysBox.cpp new file mode 100644 index 0000000..a7a0997 --- /dev/null +++ b/examples/XSysBox.cpp @@ -0,0 +1,73 @@ +// XSysBox -- minimal XFrame example (C++). +// Build: g++ -std=c++17 -I/root/lg-review/lightgui/widgets/components -I/root/lg-review/lightgui/widgets XSysBox.cpp -lX11 -o XSysBox +// Run under Xvfb (or a real X server); close the window to quit. +// Setup mirrors the widget's own demo in widgets/components/XSysBox/main_XSysBox.cpp. +#include "XSysBox/XSysBox.h" + +#include + +int main() +{ + Display* display = XOpenDisplay(nullptr); + if (!display) { return 1; } + int screen = DefaultScreen(display); + Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 800, 600, 1, BlackPixel(display, screen), WhitePixel(display, screen)); + XStoreName(display, window, "XSysBox - XFrame demo"); + 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 widget(display, screen, window, gc, &styles, 20, 20, 720, 480); + widget.draw(); + bool running = true; + while (running) + { + XEvent event; + XNextEvent(display, &event); + bool handled = false; + switch (event.type) + { + case Expose: + handled = true; + break; + case ButtonPress: + handled = widget.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case ButtonRelease: + handled = widget.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case MotionNotify: + handled = widget.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 = widget.on_key(key, buffer); + break; + } + case ClientMessage: + if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) + { + running = false; + } + break; + } + if (handled) + { + XClearWindow(display, window); + widget.draw(); + XFlush(display); + } + } + + XCloseDisplay(display); + return 0; +} diff --git a/widgets/components/XLoadBadge/CMakeLists.txt b/widgets/components/XLoadBadge/CMakeLists.txt new file mode 100644 index 0000000..56e855e --- /dev/null +++ b/widgets/components/XLoadBadge/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.20) + +project(XLoadBadge LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 17) + +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +find_package(X11 REQUIRED) + +add_executable(XLoadBadge main_XLoadBadge.cpp) + +target_link_libraries(XLoadBadge PRIVATE X11::X11) diff --git a/widgets/components/XLoadBadge/XLoadBadge.h b/widgets/components/XLoadBadge/XLoadBadge.h new file mode 100644 index 0000000..3180fb2 --- /dev/null +++ b/widgets/components/XLoadBadge/XLoadBadge.h @@ -0,0 +1,219 @@ +#pragma once + +#include "core/xframe_core.h" + +#include +#include +#include +#include +#include + +// region XLoadBadge +// +// A load-average badge (1 / 5 / 15 min) with colour thresholds and +// uptime, in the btop header style. Reads /proc/loadavg + /proc/uptime +// on refresh(). Raw X11, same as the rest of the XFrame set. +// +class XLoadBadge +{ +private: + Display* display_; + int screen_; + Window window_; + GC gc_; + SStyle_Manager* styles_; + int x1_, y1_, x2_, y2_; + std::string name_; + std::string text_; + std::string style_name_; + bool visible_ = true; + bool enabled_ = true; + bool focused_ = false; + + double load1_ = 0.0; + double load5_ = 0.0; + double load15_ = 0.0; + long uptime_secs_ = 0; + int ncpu_ = 1; + + Srgb white_color_{255, 255, 255}; + Srgb axis_color_{150, 150, 150}; + Srgb ok_color_{0, 170, 70}; + Srgb warn_color_{200, 170, 0}; + Srgb crit_color_{210, 40, 30}; + + unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); } + + static std::string fmt1(double v) + { + char buf[32]; + std::snprintf(buf, sizeof(buf), "%.2f", v); + return buf; + } + + void draw_text_color(int x, int baseline_y, const std::string& s, Srgb color) + { + SStyle st = style(); + if (!st.font_ || s.empty()) return; + XSetFont(display_, gc_, st.font_->fid); + XSetForeground(display_, gc_, col(color)); + XDrawString(display_, window_, gc_, x, baseline_y, s.c_str(), (int)s.size()); + } + + Srgb state_color() const + { + double per_core = ncpu_ > 0 ? load1_ / ncpu_ : load1_; + if (per_core < 0.7) return ok_color_; + if (per_core < 1.5) return warn_color_; + return crit_color_; + } + +public: + XLoadBadge(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles, int x, int y, int width, int height) + { + display_ = display; + screen_ = screen; + window_ = window; + gc_ = gc; + styles_ = styles; + x1_ = x; + y1_ = y; + x2_ = x + width; + y2_ = y + height; + name_ = "XLoadBadge"; + text_ = "Load avg"; + long n = sysconf(_SC_NPROCESSORS_ONLN); + ncpu_ = n > 0 ? (int)n : 1; + refresh(); + if (load1_ == 0.0 && load5_ == 0.0 && load15_ == 0.0 && uptime_secs_ == 0) + { + // No /proc (non-Linux): demo values so the widget still shows. + load1_ = 2.09; + load5_ = 1.85; + load15_ = 1.60; + uptime_secs_ = 2 * 86400 + 14 * 3600 + 21 * 60; + } + } + + ~XLoadBadge() {} + + // region Standard plumbing + void set_bounds(int x, int y, int width, int height) + { + x1_ = x; + y1_ = y; + x2_ = x + width; + y2_ = y + height; + } + int width() { return x2_ - x1_; } + int height() { return y2_ - y1_; } + bool contains(int x, int y) + { + return visible_ && x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_; + } + void set_visible(bool v) { visible_ = v; } + bool get_visible() { return visible_; } + void set_enabled(bool e) { enabled_ = e; } + bool get_enabled() { return enabled_; } + void set_style_name(std::string s) { style_name_ = s; } + void set_text(std::string t) { text_ = t; } + std::string get_text() { return text_; } + SStyle style() { return styles_->resolve(style_name_); } + // endregion + + // region Load API + void refresh() + { + std::ifstream in("/proc/loadavg"); + if (in) in >> load1_ >> load5_ >> load15_; + std::ifstream up("/proc/uptime"); + double secs = 0.0; + if (up && (up >> secs)) uptime_secs_ = (long)secs; + } + void set_loads(double l1, double l5, double l15) + { + load1_ = l1; + load5_ = l5; + load15_ = l15; + } + void set_uptime_secs(long s) { uptime_secs_ = s; } + std::string uptime_string() + { + long s = uptime_secs_; + long d = s / 86400; + long h = (s % 86400) / 3600; + long m = (s % 3600) / 60; + char buf[48]; + if (d > 0) std::snprintf(buf, sizeof(buf), "up %ldd %02ld:%02ld", d, h, m); + else std::snprintf(buf, sizeof(buf), "up %02ld:%02ld", h, m); + return buf; + } + // endregion + + void draw() + { + if (!visible_) return; + SStyle st = style(); + int w = width(); + int h = height(); + XSetForeground(display_, gc_, col(white_color_)); + XFillRectangle(display_, window_, gc_, x1_, y1_, w, h); + + Srgb state = state_color(); + // Left status stripe. + XSetForeground(display_, gc_, col(state)); + XFillRectangle(display_, window_, gc_, x1_, y1_, 8, h); + + xf::draw_text_at(display_, window_, gc_, st, x1_ + 16, y1_ + 15, text_); + std::string cpus = std::to_string(ncpu_) + " cores"; + xf::draw_text_at(display_, window_, gc_, st, x2_ - 8 - xf::text_width(st, cpus), y1_ + 15, cpus); + + // Big load numbers: 1 / 5 / 15 min. + std::string loads = fmt1(load1_) + " " + fmt1(load5_) + " " + fmt1(load15_); + int lw = xf::text_width(st, loads); + draw_text_color(x1_ + (w - lw) / 2, y1_ + 42, loads, state); + draw_text_color(x1_ + (w - lw) / 2 + 1, y1_ + 42, loads, state); + + std::string labels = "1 min 5 min 15 min"; + int tw = xf::text_width(st, labels); + xf::draw_text_at(display_, window_, gc_, st, x1_ + (w - tw) / 2, y1_ + 58, labels); + + // Uptime line. + std::string up = uptime_string(); + int uw = xf::text_width(st, up); + xf::draw_text_at(display_, window_, gc_, st, x1_ + (w - uw) / 2, y2_ - 8, up); + + XSetForeground(display_, gc_, st.lineColor_); + XDrawRectangle(display_, window_, gc_, x1_, y1_, w, h); + XFlush(display_); + } + + bool on_button_press(int x, int y, unsigned int button) + { + if (button == 1 && contains(x, y)) + { + refresh(); + return true; + } + return false; + } + bool on_button_release(int x, int y, unsigned int button) + { + (void)x; + (void)y; + (void)button; + return false; + } + bool on_motion(int x, int y) + { + (void)x; + (void)y; + return false; + } + bool on_key(KeySym key, const char* str) + { + (void)key; + (void)str; + return false; + } +}; diff --git a/widgets/components/XLoadBadge/core/xframe_core.h b/widgets/components/XLoadBadge/core/xframe_core.h new file mode 100644 index 0000000..098187d --- /dev/null +++ b/widgets/components/XLoadBadge/core/xframe_core.h @@ -0,0 +1,988 @@ +#pragma once +#ifndef XFRAME_CORE_H +#define XFRAME_CORE_H + +// +// xframe_core.h - shared core for the XFrame widget library. +// +// Everything in here follows the format of the "Writing Your Own +// X-11 Framework Basics" articles (hotconfig.com): +// +// Part 1 - Display / Screen / Window boilerplate, the GC as a +// "pen", the drawing primitives, and the Canvas class. +// Part 2 - widgets are standalone classes that record display_, +// screen_, window_ and gc_ at construction time. +// Part 3 - SStyle_Manager tracks named styles in a vector. +// Part 4 - a polished widget is built from primitives: two fill +// rectangles + four fill arcs make a rounded body, four +// lines + four draw arcs make its outline, and text is +// centred with XTextWidth + font ascent / descent. +// +// Two small completions versus the articles, needed by Part 4's own +// draw code: SStyle gains lineColor_ and text_color_, and the style +// manager gains the delete_style() function Part 3 left as a reader +// challenge, plus bounds-safe getters. +// + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +//region SStyle + +struct Srgb + +{ + + int r; + + int g; + + int b; + +}; + +struct SStyle + +{ + + unsigned long fillColor_ = 0; + + unsigned long outlineColor_ = 0; + + unsigned long lineColor_ = 0; + + unsigned long text_color_ = 0; + + unsigned long radius_ = 0; + + unsigned long lineWidth_ = 1; + + XFontStruct *font_ = nullptr; + + std::string style_name_; + +}; + +//endregion + +//region SStyle_Manager + +class SStyle_Manager + +{ + +private: + + std::vector styles_; + + Display *display_; + + Window window_; + + int screen_; + + GC gc_; + + int active_style = 0; // just set the default to first passed. + +public: + + SStyle_Manager(Display *display, Window window, int screen, GC gc) + + { + + display_ = display; + + window_ = window; + + screen_ = screen; + + gc_ = gc; + + } + + // NOTE (test agent, Nov 26 2026): do NOT XFreeFont here. Every demo + // main calls XCloseDisplay() before this destructor runs (stack + // unwinding after main returns), so XFreeFont would operate on a + // closed Display and corrupt the heap (SIGSEGV/SIGABRT inside + // XFreeFont on every clean exit). The fonts are process-lifetime + // resources; the X server reclaims them when the client disconnects. + // Use delete_style() to free a single style while the display is + // still open. + ~SStyle_Manager() + + { + + styles_.clear(); + + } + + int add_style(std::string styleName, Srgb fillColor, Srgb outlineColor, int radius, int linewidth, std::string fontName) + + { + + return add_style_ex(styleName, fillColor, outlineColor, outlineColor, Srgb{0, 0, 0}, radius, linewidth, fontName); + + } + + int add_style_ex(std::string styleName, Srgb fillColor, Srgb outlineColor, Srgb lineColor, Srgb textColor, int radius, int linewidth, std::string fontName) + + { + + SStyle nstyle; + + nstyle.style_name_ = std::move(styleName); + + nstyle.fillColor_ = get_color_Srgb(fillColor); + + nstyle.outlineColor_ = get_color_Srgb(outlineColor); + + nstyle.lineColor_ = get_color_Srgb(lineColor); + + nstyle.text_color_ = get_color_Srgb(textColor); + + nstyle.radius_ = radius; + + nstyle.lineWidth_ = linewidth; + + // Define fallback fonts to try if the requested font fails + + const char* fallbackFonts[] = { "fixed", "helvetica" }; + + size_t numFallbacks = sizeof(fallbackFonts) / sizeof(*fallbackFonts); + + nstyle.font_ = nullptr; + + // Attempt to load the primary font (if provided and not empty) + + if (!fontName.empty()) { + + nstyle.font_ = XLoadQueryFont(display_, fontName.c_str()); + + } + + // If the primary failed, iterate through fallback fonts until one succeeds or all are exhausted + + if (!nstyle.font_) { + + for (size_t i = 0; i < numFallbacks && !nstyle.font_; ++i) { + + nstyle.font_ = XLoadQueryFont(display_, fallbackFonts[i]); + + } + + } + + // Final validation: if no font loaded, report which fonts were attempted and exit + + if (!nstyle.font_) + + { + + printf("Failed to load any font (tried: "); + + if (!fontName.empty()) printf("%s", fontName.c_str()); + + for (size_t i = 0; i < numFallbacks; ++i) { + + if (!fontName.empty() || i > 0) printf(", "); + + printf("%s", fallbackFonts[i]); + + } + + printf(")\n"); + + return -1; + + } + + styles_.push_back(nstyle); + + return 0; + + } + + // The delete function Part 3 left as a challenge: remove a style + // by name, freeing its font, and keep active_style in range. + + int delete_style(std::string styleName) + + { + + for (size_t i = 0; i < styles_.size(); ++i) + + { + + if (styles_[i].style_name_ == styleName) + + { + + if (styles_[i].font_) + + { + + XFreeFont(display_, styles_[i].font_); + + } + + styles_.erase(styles_.begin() + static_cast(i)); + + if (active_style >= (int)styles_.size()) + + { + + active_style = styles_.empty() ? 0 : (int)styles_.size() - 1; + + } + + return 0; + + } + + } + + return -1; // Not found + + } + + int style_count() + + { + + return (int)styles_.size(); + + } + + unsigned long get_color_Srgb(Srgb color) + + { + + XColor xcolor; + + xcolor.red = color.r << 8; + + xcolor.green = color.g << 8; + + xcolor.blue = color.b << 8; + + if (XAllocColor(display_, DefaultColormap(display_, screen_), &xcolor)) + + { + + return xcolor.pixel; + + } else { + + printf("Failed to allocate color! RGB(%d,%d,%d)\n", color.r, color.g, color.b); + + return BlackPixel(display_, screen_); // Fallback + + } + + } + + unsigned long get_color_rgb(unsigned char r, unsigned char g, unsigned char b) + + { + + XColor xcolor; + + xcolor.red = r << 8; + + xcolor.green = g << 8; + + xcolor.blue = b << 8; + + if (XAllocColor(display_, DefaultColormap(display_, screen_), &xcolor)) + + { + + return xcolor.pixel; + + } else { + + printf("Failed to allocate color! RGB(%d,%d,%d)\n", r, g, b); + + return BlackPixel(display_, screen_); // Fallback + + } + + } + + int set_active_style_byName(std::string styleName) + + { + + auto it = std::find_if(styles_.begin(), styles_.end(), + + [&](const SStyle& s) { return s.style_name_ == styleName; }); + + if (it != styles_.end()) + + { + + active_style = static_cast(std::distance(styles_.begin(), it)); + + return active_style; + + } + + return -1; // Not found + + } + + SStyle get_active_style_byName(std::string styleName) + + { + + auto it = std::find_if(styles_.begin(), styles_.end(), + + [&](const SStyle& s) { return s.style_name_ == styleName; }); + + if (it != styles_.end()) + + { + + return *it; + + } + + return get_active_style(); + + } + + SStyle get_active_style() + + { + + if (styles_.empty()) + + { + + return SStyle{}; + + } + + if (active_style < 0 || active_style >= (int)styles_.size()) + + { + + active_style = 0; + + } + + return styles_[active_style]; + + } + + SStyle get_index_style(int index) + + { + + if (index >= 0 && index < (int)styles_.size()) + + { + + return styles_[index]; + + } + + return get_active_style(); + + } + + // Resolve the style a widget should draw with: its own named + // style when set, otherwise the manager's active style. + + SStyle resolve(const std::string& styleName) + + { + + if (!styleName.empty()) + + { + + return get_active_style_byName(styleName); + + } + + return get_active_style(); + + } + +}; + +//endregion + +//region Drawing helpers (Part 4 construction, shared by all widgets) + +namespace xf + +{ + + inline void set_line_width(Display* display, GC gc, int lineWidth) + + { + + unsigned long valuemask = GCLineWidth; + + XGCValues gc_values; + + gc_values.line_width = lineWidth; + + XChangeGC(display, gc, valuemask, &gc_values); + + } + + // The Part 4 rounded body: Rectangle A + Rectangle B + 4 fill arcs. + + inline void fill_rounded_rect(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + int r = (int)style.radius_; + + int w = x2 - x1; + + int h = y2 - y1; + + if (r <= 0 || w <= 2 * r || h <= 2 * r) + + { + + XFillRectangle(display, window, gc, x1, y1, w, h); + + return; + + } + + XSetForeground(display, gc, style.fillColor_); + + //Rectangle A + + XFillRectangle(display, window, gc, x1 + r, y1, w - 2 * r, h); + + //Rectangle B + + XFillRectangle(display, window, gc, x1, y1 + r, w, h - 2 * r); + + //region Arc C Top Left + + XFillArc(display, window, gc, x1, y1, 2 * r, 2 * r, 90 * 64, 90 * 64); + + //region Arc D Top Right + + XFillArc(display, window, gc, x2 - 2 * r, y1, 2 * r, 2 * r, 0, 90 * 64); + + //region Arc E Bottom Right + + XFillArc(display, window, gc, x2 - 2 * r, y2 - 2 * r, 2 * r, 2 * r, 270 * 64, 90 * 64); + + //region Arc F Bottom Left + + XFillArc(display, window, gc, x1, y2 - 2 * r, 2 * r, 2 * r, 180 * 64, 90 * 64); + + //endregion + + } + + // The Part 4 outline: 4 line bars + 4 draw arcs, in lineColor_. + + inline void draw_rounded_outline(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + int r = (int)style.radius_; + + XSetForeground(display, gc, style.lineColor_); + + set_line_width(display, gc, (int)style.lineWidth_); + + if (r <= 0) + + { + + XDrawRectangle(display, window, gc, x1, y1, x2 - x1, y2 - y1); + + return; + + } + + // 4 Line Bars + + XDrawLine(display, window, gc, x1 + r, y1, x2 - r, y1); + + XDrawLine(display, window, gc, x2, y1 + r, x2, y2 - r); + + XDrawLine(display, window, gc, x1, y1 + r, x1, y2 - r); + + XDrawLine(display, window, gc, x1 + r, y2, x2 - r, y2); + + // 4 Arcs + + XDrawArc(display, window, gc, x1, y1, 2 * r, 2 * r, 90 * 64, 90 * 64); + + //region Arc D Top Right + + XDrawArc(display, window, gc, x2 - 2 * r, y1, 2 * r, 2 * r, 0, 90 * 64); + + //region Arc E Bottom Right + + XDrawArc(display, window, gc, x2 - 2 * r, y2 - 2 * r, 2 * r, 2 * r, 270 * 64, 90 * 64); + + //region Arc F Bottom Left + + XDrawArc(display, window, gc, x1, y2 - 2 * r, 2 * r, 2 * r, 180 * 64, 90 * 64); + + //endregion + + } + + // Convenience: body + outline in one call, the polished look. + + inline void draw_polished(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + fill_rounded_rect(display, window, gc, style, x1, y1, x2, y2); + + draw_rounded_outline(display, window, gc, style, x1, y1, x2, y2); + + } + + inline int text_width(const SStyle& style, const std::string& text) + + { + + if (!style.font_) + + { + + return (int)text.size() * 8; + + } + + return XTextWidth(style.font_, text.c_str(), (int)text.length()); + + } + + // Part 4 centred text: XTextWidth + ascent / descent. + + inline void draw_text_centered(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + int tw = XTextWidth(style.font_, text.c_str(), (int)text.length()); + + int text_x = x1 + ((x2 - x1) - tw) / 2; + + int text_y = y1 + ((y2 - y1) + style.font_->ascent - style.font_->descent) / 2; + + XDrawString(display, window, gc, text_x, text_y, text.c_str(), (int)text.length()); + + } + + // Left-aligned text, vertically centred in the given band. + + inline void draw_text_left(Display* display, Window window, GC gc, const SStyle& style, int x, int y1, int y2, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + int text_y = y1 + ((y2 - y1) + style.font_->ascent - style.font_->descent) / 2; + + XDrawString(display, window, gc, x, text_y, text.c_str(), (int)text.length()); + + } + + // Baseline text at an explicit position (labels, grid cells...). + + inline void draw_text_at(Display* display, Window window, GC gc, const SStyle& style, int x, int baseline_y, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + XDrawString(display, window, gc, x, baseline_y, text.c_str(), (int)text.length()); + + } + + inline void draw_checkmark(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + set_line_width(display, gc, 2); + + int s = size / 2; + + XDrawLine(display, window, gc, cx - s, cy, cx - s / 3, cy + s); + + XDrawLine(display, window, gc, cx - s / 3, cy + s, cx + s, cy - s); + + set_line_width(display, gc, 1); + + } + + inline void draw_triangle_down(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx - (size - i), cy + i, cx + (size - i), cy + i); + + } + + } + + inline void draw_triangle_up(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx - (size - i), cy - i, cx + (size - i), cy - i); + + } + + } + + inline void draw_triangle_right(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx + i, cy - (size - i), cx + i, cy + (size - i)); + + } + + } + +} + +//endregion + +//region Region Canvas (from Part 1) + +class Canvas + +{ + +private: + + Display* display = nullptr; + + Window window = 0; + + GC gc = nullptr; + + XImage* image = nullptr; + + int width = 0; + + int height = 0; + + std::vector pixels; + +public: + + Canvas(Display* display, Window window, GC graphicsContext, int width, int height) : display(display), window(window), gc(graphicsContext) + + { + + resize(width, height); + + } + + ~Canvas() { destroyImage(); } + + void resize(int newWidth, int newHeight) + + { + + width = std::max(1, newWidth); + + height = std::max(1, newHeight); + + pixels.resize(static_cast(width) * height); + + clear(0x202020); + + destroyImage(); + + Visual* visual = DefaultVisual(display, DefaultScreen(display)); + + int depth = DefaultDepth(display, DefaultScreen(display)); + + image = XCreateImage(display, visual, depth, ZPixmap, 0, nullptr, width, height, 32, 0); + + if (!image) + + { + + std::exit(1); + + } + + image->data = static_cast(std::malloc(image->bytes_per_line * height)); + + if (!image->data) + + { + + std::exit(1); + + } + + } + + int getWidth() const + + { + + return width; + + } + + int getHeight() const + + { + + return height; + + } + + // Direct access to the raw framebuffer. + + uint32_t* data() + + { + + return pixels.data(); + + } + + const uint32_t* data() const + + { + + return pixels.data(); + + } + + void clear(uint32_t rgb) + + { + + std::fill(pixels.begin(), pixels.end(), rgb); + + } + + void setPixel(int x, int y, uint32_t rgb) + + { + + if (x < 0 || x >= width || y < 0 || y >= height) + + { + + return; + + } + + pixels[static_cast(y) * width + x] = rgb; + + } + + uint32_t getPixel(int x, int y) const + + { + + if (x < 0 || x >= width || y < 0 || y >= height) + + { + + return 0; + + } + + return pixels[static_cast(y) * width + x]; + + } + + void fillRect(int x, int y, int w, int h, uint32_t rgb) + + { + + int x0 = std::max(0, x); + + int y0 = std::max(0, y); + + int x1 = std::min(width, x + w); + + int y1 = std::min(height, y + h); + + for (int py = y0; py < y1; ++py) + + { + + for (int px = x0; px < x1; ++px) + + { + + setPixel(px, py, rgb); + + } + + } + + } + + void drawGradient() + + { + + for (int y = 0; y < height; ++y) + + { + + for (int x = 0; x < width; ++x) + + { + + uint8_t red = static_cast(255.0 * x / std::max(1, width - 1)); + + uint8_t green = static_cast(255.0 * y / std::max(1, height - 1)); + + uint8_t blue = 100; + + uint32_t color = (static_cast(red) << 16) | (static_cast(green) << 8) | blue; + + setPixel(x, y, color); + + } + + } + + } + + void present() + + { + + for (int y = 0; y < height; ++y) + + { + + for (int x = 0; x < width; ++x) + + { + + uint32_t rgb = getPixel(x, y); + + unsigned long x11Pixel = ((rgb >> 16) & 0xff) << 16 | ((rgb >> 8) & 0xff) << 8 | (rgb & 0xff); + + XPutPixel(image, x, y, x11Pixel); + + } + + } + + XPutImage(display, window, gc, image, 0, 0, 0, 0, width, height); + + XFlush(display); + + } + +private: + + void destroyImage() + + { + + if (image) + + { + + XDestroyImage(image); + + image = nullptr; + + } + + } + +}; + +//endregion +#endif // XFRAME_CORE_H diff --git a/widgets/components/XLoadBadge/main_XLoadBadge.cpp b/widgets/components/XLoadBadge/main_XLoadBadge.cpp new file mode 100644 index 0000000..9307efa --- /dev/null +++ b/widgets/components/XLoadBadge/main_XLoadBadge.cpp @@ -0,0 +1,69 @@ +#include "XLoadBadge.h" + +#include + +int main() +{ + Display* display = XOpenDisplay(nullptr); + if (!display) { return 1; } + int screen = DefaultScreen(display); + Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 800, 600, 1, BlackPixel(display, screen), WhitePixel(display, screen)); + XStoreName(display, window, "XLoadBadge - XFrame demo"); + 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"); + XLoadBadge widget(display, screen, window, gc, &styles, 20, 20, 720, 480); + widget.draw(); + bool running = true; + while (running) + { + XEvent event; + XNextEvent(display, &event); + bool handled = false; + switch (event.type) + { + case Expose: + handled = true; + break; + case ButtonPress: + handled = widget.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case ButtonRelease: + handled = widget.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case MotionNotify: + handled = widget.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 = widget.on_key(key, buffer); + break; + } + case ClientMessage: + if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) + { + running = false; + } + break; + } + if (handled) + { + XClearWindow(display, window); + widget.draw(); + XFlush(display); + } + } + + XCloseDisplay(display); + return 0; +} diff --git a/widgets/components/XMonitorButton/CMakeLists.txt b/widgets/components/XMonitorButton/CMakeLists.txt new file mode 100644 index 0000000..b18c704 --- /dev/null +++ b/widgets/components/XMonitorButton/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.20) + +project(XMonitorButton LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 17) + +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +find_package(X11 REQUIRED) + +add_executable(XMonitorButton main_XMonitorButton.cpp) + +target_link_libraries(XMonitorButton PRIVATE X11::X11) diff --git a/widgets/components/XMonitorButton/XMonitorButton.h b/widgets/components/XMonitorButton/XMonitorButton.h new file mode 100644 index 0000000..4d58106 --- /dev/null +++ b/widgets/components/XMonitorButton/XMonitorButton.h @@ -0,0 +1,313 @@ +#pragma once + +#include "core/xframe_core.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// region XMonitorButton +// +// A button that IS a live monitor: bound to a process running (by name), +// a systemd unit active (systemctl is-active), or a TCP port open. +// Glows green when up, red when down, dim when unknown; label + status +// text; polls on a timer via poll(); click polls now and shows details. +// Checks use stdlib / syscalls only. Raw X11, same as the XFrame set. +// +class XMonitorButton +{ +public: + enum Kind + { + Process = 0, // target = process name (comm) + Unit = 1, // target = systemd unit, e.g. "sshd.service" + Port = 2 // target = TCP port number, e.g. "22" + }; + enum MonitorStatus + { + MonUnknown = 0, + MonUp = 1, + MonDown = 2 + }; + +private: + Display* display_; + int screen_; + Window window_; + GC gc_; + SStyle_Manager* styles_; + int x1_, y1_, x2_, y2_; + std::string name_; + std::string text_; // label + std::string style_name_; + bool visible_ = true; + bool enabled_ = true; + bool focused_ = false; + + Kind kind_ = Port; + std::string target_ = "22"; + MonitorStatus status_ = MonUnknown; + std::string details_; + bool show_details_ = false; + + Srgb white_color_{255, 255, 255}; + Srgb up_color_{0, 170, 70}; + Srgb down_color_{210, 40, 30}; + Srgb unknown_color_{150, 150, 150}; + + unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); } + + void draw_text_color(int x, int baseline_y, const std::string& s, Srgb color) + { + SStyle st = style(); + if (!st.font_ || s.empty()) return; + XSetFont(display_, gc_, st.font_->fid); + XSetForeground(display_, gc_, col(color)); + XDrawString(display_, window_, gc_, x, baseline_y, s.c_str(), (int)s.size()); + } + + bool check_process(const std::string& name, int& found_pid) + { + DIR* dir = opendir("/proc"); + if (!dir) return false; + bool found = false; + struct dirent* ent; + while ((ent = readdir(dir)) != nullptr) + { + if (ent->d_name[0] < '0' || ent->d_name[0] > '9') continue; + std::string comm_path = std::string("/proc/") + ent->d_name + "/comm"; + FILE* f = std::fopen(comm_path.c_str(), "r"); + if (!f) continue; + char buf[256]; + if (std::fgets(buf, sizeof(buf), f)) + { + buf[std::strcspn(buf, "\n")] = 0; + if (name == buf) + { + found = true; + found_pid = std::atoi(ent->d_name); + std::fclose(f); + break; + } + } + std::fclose(f); + } + closedir(dir); + return found; + } + + bool check_unit(const std::string& unit, std::string& out) + { + // Only safe unit-name characters reach the shell. + for (char c : unit) + { + bool ok = (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || + (c >= '0' && c <= '9') || c == '@' || c == '.' || c == '_' || c == '-'; + if (!ok) return false; + } + std::string cmd = "systemctl is-active " + unit + " 2>/dev/null"; + FILE* p = popen(cmd.c_str(), "r"); + if (!p) return false; + char buf[128]; + bool active = false; + if (std::fgets(buf, sizeof(buf), p)) + { + buf[std::strcspn(buf, "\n")] = 0; + out = buf; + active = (out == "active"); + } + pclose(p); + return active; + } + + bool check_port(int port) + { + int fd = socket(AF_INET, SOCK_STREAM, 0); + if (fd < 0) return false; + struct timeval tv; + tv.tv_sec = 1; + tv.tv_usec = 0; + setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)); + struct sockaddr_in addr; + std::memset(&addr, 0, sizeof(addr)); + addr.sin_family = AF_INET; + addr.sin_port = htons((uint16_t)port); + addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); + bool open = connect(fd, (struct sockaddr*)&addr, sizeof(addr)) == 0; + close(fd); + return open; + } + +public: + XMonitorButton(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles, int x, int y, int width, int height) + { + display_ = display; + screen_ = screen; + window_ = window; + gc_ = gc; + styles_ = styles; + x1_ = x; + y1_ = y; + x2_ = x + width; + y2_ = y + height; + name_ = "XMonitorButton"; + text_ = "sshd :22"; + poll(); + } + + ~XMonitorButton() {} + + // region Standard plumbing + void set_bounds(int x, int y, int width, int height) + { + x1_ = x; + y1_ = y; + x2_ = x + width; + y2_ = y + height; + } + int width() { return x2_ - x1_; } + int height() { return y2_ - y1_; } + bool contains(int x, int y) + { + return visible_ && x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_; + } + void set_visible(bool v) { visible_ = v; } + bool get_visible() { return visible_; } + void set_enabled(bool e) { enabled_ = e; } + bool get_enabled() { return enabled_; } + void set_style_name(std::string s) { style_name_ = s; } + void set_text(std::string t) { text_ = t; } + std::string get_text() { return text_; } + SStyle style() { return styles_->resolve(style_name_); } + // endregion + + // region Monitor API + void set_kind(Kind k) { kind_ = k; } + Kind get_kind() { return kind_; } + void set_target(const std::string& t) { target_ = t; } + std::string get_target() { return target_; } + MonitorStatus status() { return status_; } + std::string status_text() + { + switch (status_) + { + case MonUp: return "UP"; + case MonDown: return "DOWN"; + default: return "UNKNOWN"; + } + } + std::string details() { return details_; } + + // Run one check now; call on a timer for live monitoring. + void poll() + { + bool ok = false; + if (kind_ == Process) + { + int pid = 0; + ok = check_process(target_, pid); + details_ = ok ? ("pid " + std::to_string(pid)) : "not running"; + } + else if (kind_ == Unit) + { + std::string out; + ok = check_unit(target_, out); + details_ = out.empty() ? "no result" : out; + } + else + { + int port = std::atoi(target_.c_str()); + if (port <= 0 || port > 65535) + { + status_ = MonUnknown; + details_ = "bad port"; + return; + } + ok = check_port(port); + details_ = ok ? "open" : "closed"; + } + status_ = ok ? MonUp : MonDown; + } + // Backwards-compatible name: update() polls once. + void update() { poll(); } + // endregion + + void draw() + { + if (!visible_) return; + SStyle st = style(); + int w = width(); + int h = height(); + Srgb state = status_ == MonUp ? up_color_ : (status_ == MonDown ? down_color_ : unknown_color_); + + // Body: white, thick status-coloured border = the glow. + XSetForeground(display_, gc_, col(white_color_)); + XFillRectangle(display_, window_, gc_, x1_, y1_, w, h); + XSetForeground(display_, gc_, col(state)); + xf::set_line_width(display_, gc_, 3); + XDrawRectangle(display_, window_, gc_, x1_ + 1, y1_ + 1, w - 2, h - 2); + xf::set_line_width(display_, gc_, 1); + + // 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. + xf::draw_text_at(display_, window_, gc_, st, x1_ + 30, dot_cy + 4, text_); + std::string stxt = status_text(); + 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); + + if (show_details_) + { + 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)); + } + + XFlush(display_); + } + + bool on_button_press(int x, int y, unsigned int button) + { + if (button == 1 && contains(x, y)) + { + show_details_ = !show_details_; + poll(); + return true; + } + return false; + } + bool on_button_release(int x, int y, unsigned int button) + { + (void)x; + (void)y; + (void)button; + return false; + } + bool on_motion(int x, int y) + { + (void)x; + (void)y; + return false; + } + bool on_key(KeySym key, const char* str) + { + (void)key; + (void)str; + return false; + } +}; diff --git a/widgets/components/XMonitorButton/core/xframe_core.h b/widgets/components/XMonitorButton/core/xframe_core.h new file mode 100644 index 0000000..098187d --- /dev/null +++ b/widgets/components/XMonitorButton/core/xframe_core.h @@ -0,0 +1,988 @@ +#pragma once +#ifndef XFRAME_CORE_H +#define XFRAME_CORE_H + +// +// xframe_core.h - shared core for the XFrame widget library. +// +// Everything in here follows the format of the "Writing Your Own +// X-11 Framework Basics" articles (hotconfig.com): +// +// Part 1 - Display / Screen / Window boilerplate, the GC as a +// "pen", the drawing primitives, and the Canvas class. +// Part 2 - widgets are standalone classes that record display_, +// screen_, window_ and gc_ at construction time. +// Part 3 - SStyle_Manager tracks named styles in a vector. +// Part 4 - a polished widget is built from primitives: two fill +// rectangles + four fill arcs make a rounded body, four +// lines + four draw arcs make its outline, and text is +// centred with XTextWidth + font ascent / descent. +// +// Two small completions versus the articles, needed by Part 4's own +// draw code: SStyle gains lineColor_ and text_color_, and the style +// manager gains the delete_style() function Part 3 left as a reader +// challenge, plus bounds-safe getters. +// + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +//region SStyle + +struct Srgb + +{ + + int r; + + int g; + + int b; + +}; + +struct SStyle + +{ + + unsigned long fillColor_ = 0; + + unsigned long outlineColor_ = 0; + + unsigned long lineColor_ = 0; + + unsigned long text_color_ = 0; + + unsigned long radius_ = 0; + + unsigned long lineWidth_ = 1; + + XFontStruct *font_ = nullptr; + + std::string style_name_; + +}; + +//endregion + +//region SStyle_Manager + +class SStyle_Manager + +{ + +private: + + std::vector styles_; + + Display *display_; + + Window window_; + + int screen_; + + GC gc_; + + int active_style = 0; // just set the default to first passed. + +public: + + SStyle_Manager(Display *display, Window window, int screen, GC gc) + + { + + display_ = display; + + window_ = window; + + screen_ = screen; + + gc_ = gc; + + } + + // NOTE (test agent, Nov 26 2026): do NOT XFreeFont here. Every demo + // main calls XCloseDisplay() before this destructor runs (stack + // unwinding after main returns), so XFreeFont would operate on a + // closed Display and corrupt the heap (SIGSEGV/SIGABRT inside + // XFreeFont on every clean exit). The fonts are process-lifetime + // resources; the X server reclaims them when the client disconnects. + // Use delete_style() to free a single style while the display is + // still open. + ~SStyle_Manager() + + { + + styles_.clear(); + + } + + int add_style(std::string styleName, Srgb fillColor, Srgb outlineColor, int radius, int linewidth, std::string fontName) + + { + + return add_style_ex(styleName, fillColor, outlineColor, outlineColor, Srgb{0, 0, 0}, radius, linewidth, fontName); + + } + + int add_style_ex(std::string styleName, Srgb fillColor, Srgb outlineColor, Srgb lineColor, Srgb textColor, int radius, int linewidth, std::string fontName) + + { + + SStyle nstyle; + + nstyle.style_name_ = std::move(styleName); + + nstyle.fillColor_ = get_color_Srgb(fillColor); + + nstyle.outlineColor_ = get_color_Srgb(outlineColor); + + nstyle.lineColor_ = get_color_Srgb(lineColor); + + nstyle.text_color_ = get_color_Srgb(textColor); + + nstyle.radius_ = radius; + + nstyle.lineWidth_ = linewidth; + + // Define fallback fonts to try if the requested font fails + + const char* fallbackFonts[] = { "fixed", "helvetica" }; + + size_t numFallbacks = sizeof(fallbackFonts) / sizeof(*fallbackFonts); + + nstyle.font_ = nullptr; + + // Attempt to load the primary font (if provided and not empty) + + if (!fontName.empty()) { + + nstyle.font_ = XLoadQueryFont(display_, fontName.c_str()); + + } + + // If the primary failed, iterate through fallback fonts until one succeeds or all are exhausted + + if (!nstyle.font_) { + + for (size_t i = 0; i < numFallbacks && !nstyle.font_; ++i) { + + nstyle.font_ = XLoadQueryFont(display_, fallbackFonts[i]); + + } + + } + + // Final validation: if no font loaded, report which fonts were attempted and exit + + if (!nstyle.font_) + + { + + printf("Failed to load any font (tried: "); + + if (!fontName.empty()) printf("%s", fontName.c_str()); + + for (size_t i = 0; i < numFallbacks; ++i) { + + if (!fontName.empty() || i > 0) printf(", "); + + printf("%s", fallbackFonts[i]); + + } + + printf(")\n"); + + return -1; + + } + + styles_.push_back(nstyle); + + return 0; + + } + + // The delete function Part 3 left as a challenge: remove a style + // by name, freeing its font, and keep active_style in range. + + int delete_style(std::string styleName) + + { + + for (size_t i = 0; i < styles_.size(); ++i) + + { + + if (styles_[i].style_name_ == styleName) + + { + + if (styles_[i].font_) + + { + + XFreeFont(display_, styles_[i].font_); + + } + + styles_.erase(styles_.begin() + static_cast(i)); + + if (active_style >= (int)styles_.size()) + + { + + active_style = styles_.empty() ? 0 : (int)styles_.size() - 1; + + } + + return 0; + + } + + } + + return -1; // Not found + + } + + int style_count() + + { + + return (int)styles_.size(); + + } + + unsigned long get_color_Srgb(Srgb color) + + { + + XColor xcolor; + + xcolor.red = color.r << 8; + + xcolor.green = color.g << 8; + + xcolor.blue = color.b << 8; + + if (XAllocColor(display_, DefaultColormap(display_, screen_), &xcolor)) + + { + + return xcolor.pixel; + + } else { + + printf("Failed to allocate color! RGB(%d,%d,%d)\n", color.r, color.g, color.b); + + return BlackPixel(display_, screen_); // Fallback + + } + + } + + unsigned long get_color_rgb(unsigned char r, unsigned char g, unsigned char b) + + { + + XColor xcolor; + + xcolor.red = r << 8; + + xcolor.green = g << 8; + + xcolor.blue = b << 8; + + if (XAllocColor(display_, DefaultColormap(display_, screen_), &xcolor)) + + { + + return xcolor.pixel; + + } else { + + printf("Failed to allocate color! RGB(%d,%d,%d)\n", r, g, b); + + return BlackPixel(display_, screen_); // Fallback + + } + + } + + int set_active_style_byName(std::string styleName) + + { + + auto it = std::find_if(styles_.begin(), styles_.end(), + + [&](const SStyle& s) { return s.style_name_ == styleName; }); + + if (it != styles_.end()) + + { + + active_style = static_cast(std::distance(styles_.begin(), it)); + + return active_style; + + } + + return -1; // Not found + + } + + SStyle get_active_style_byName(std::string styleName) + + { + + auto it = std::find_if(styles_.begin(), styles_.end(), + + [&](const SStyle& s) { return s.style_name_ == styleName; }); + + if (it != styles_.end()) + + { + + return *it; + + } + + return get_active_style(); + + } + + SStyle get_active_style() + + { + + if (styles_.empty()) + + { + + return SStyle{}; + + } + + if (active_style < 0 || active_style >= (int)styles_.size()) + + { + + active_style = 0; + + } + + return styles_[active_style]; + + } + + SStyle get_index_style(int index) + + { + + if (index >= 0 && index < (int)styles_.size()) + + { + + return styles_[index]; + + } + + return get_active_style(); + + } + + // Resolve the style a widget should draw with: its own named + // style when set, otherwise the manager's active style. + + SStyle resolve(const std::string& styleName) + + { + + if (!styleName.empty()) + + { + + return get_active_style_byName(styleName); + + } + + return get_active_style(); + + } + +}; + +//endregion + +//region Drawing helpers (Part 4 construction, shared by all widgets) + +namespace xf + +{ + + inline void set_line_width(Display* display, GC gc, int lineWidth) + + { + + unsigned long valuemask = GCLineWidth; + + XGCValues gc_values; + + gc_values.line_width = lineWidth; + + XChangeGC(display, gc, valuemask, &gc_values); + + } + + // The Part 4 rounded body: Rectangle A + Rectangle B + 4 fill arcs. + + inline void fill_rounded_rect(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + int r = (int)style.radius_; + + int w = x2 - x1; + + int h = y2 - y1; + + if (r <= 0 || w <= 2 * r || h <= 2 * r) + + { + + XFillRectangle(display, window, gc, x1, y1, w, h); + + return; + + } + + XSetForeground(display, gc, style.fillColor_); + + //Rectangle A + + XFillRectangle(display, window, gc, x1 + r, y1, w - 2 * r, h); + + //Rectangle B + + XFillRectangle(display, window, gc, x1, y1 + r, w, h - 2 * r); + + //region Arc C Top Left + + XFillArc(display, window, gc, x1, y1, 2 * r, 2 * r, 90 * 64, 90 * 64); + + //region Arc D Top Right + + XFillArc(display, window, gc, x2 - 2 * r, y1, 2 * r, 2 * r, 0, 90 * 64); + + //region Arc E Bottom Right + + XFillArc(display, window, gc, x2 - 2 * r, y2 - 2 * r, 2 * r, 2 * r, 270 * 64, 90 * 64); + + //region Arc F Bottom Left + + XFillArc(display, window, gc, x1, y2 - 2 * r, 2 * r, 2 * r, 180 * 64, 90 * 64); + + //endregion + + } + + // The Part 4 outline: 4 line bars + 4 draw arcs, in lineColor_. + + inline void draw_rounded_outline(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + int r = (int)style.radius_; + + XSetForeground(display, gc, style.lineColor_); + + set_line_width(display, gc, (int)style.lineWidth_); + + if (r <= 0) + + { + + XDrawRectangle(display, window, gc, x1, y1, x2 - x1, y2 - y1); + + return; + + } + + // 4 Line Bars + + XDrawLine(display, window, gc, x1 + r, y1, x2 - r, y1); + + XDrawLine(display, window, gc, x2, y1 + r, x2, y2 - r); + + XDrawLine(display, window, gc, x1, y1 + r, x1, y2 - r); + + XDrawLine(display, window, gc, x1 + r, y2, x2 - r, y2); + + // 4 Arcs + + XDrawArc(display, window, gc, x1, y1, 2 * r, 2 * r, 90 * 64, 90 * 64); + + //region Arc D Top Right + + XDrawArc(display, window, gc, x2 - 2 * r, y1, 2 * r, 2 * r, 0, 90 * 64); + + //region Arc E Bottom Right + + XDrawArc(display, window, gc, x2 - 2 * r, y2 - 2 * r, 2 * r, 2 * r, 270 * 64, 90 * 64); + + //region Arc F Bottom Left + + XDrawArc(display, window, gc, x1, y2 - 2 * r, 2 * r, 2 * r, 180 * 64, 90 * 64); + + //endregion + + } + + // Convenience: body + outline in one call, the polished look. + + inline void draw_polished(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + fill_rounded_rect(display, window, gc, style, x1, y1, x2, y2); + + draw_rounded_outline(display, window, gc, style, x1, y1, x2, y2); + + } + + inline int text_width(const SStyle& style, const std::string& text) + + { + + if (!style.font_) + + { + + return (int)text.size() * 8; + + } + + return XTextWidth(style.font_, text.c_str(), (int)text.length()); + + } + + // Part 4 centred text: XTextWidth + ascent / descent. + + inline void draw_text_centered(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + int tw = XTextWidth(style.font_, text.c_str(), (int)text.length()); + + int text_x = x1 + ((x2 - x1) - tw) / 2; + + int text_y = y1 + ((y2 - y1) + style.font_->ascent - style.font_->descent) / 2; + + XDrawString(display, window, gc, text_x, text_y, text.c_str(), (int)text.length()); + + } + + // Left-aligned text, vertically centred in the given band. + + inline void draw_text_left(Display* display, Window window, GC gc, const SStyle& style, int x, int y1, int y2, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + int text_y = y1 + ((y2 - y1) + style.font_->ascent - style.font_->descent) / 2; + + XDrawString(display, window, gc, x, text_y, text.c_str(), (int)text.length()); + + } + + // Baseline text at an explicit position (labels, grid cells...). + + inline void draw_text_at(Display* display, Window window, GC gc, const SStyle& style, int x, int baseline_y, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + XDrawString(display, window, gc, x, baseline_y, text.c_str(), (int)text.length()); + + } + + inline void draw_checkmark(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + set_line_width(display, gc, 2); + + int s = size / 2; + + XDrawLine(display, window, gc, cx - s, cy, cx - s / 3, cy + s); + + XDrawLine(display, window, gc, cx - s / 3, cy + s, cx + s, cy - s); + + set_line_width(display, gc, 1); + + } + + inline void draw_triangle_down(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx - (size - i), cy + i, cx + (size - i), cy + i); + + } + + } + + inline void draw_triangle_up(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx - (size - i), cy - i, cx + (size - i), cy - i); + + } + + } + + inline void draw_triangle_right(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx + i, cy - (size - i), cx + i, cy + (size - i)); + + } + + } + +} + +//endregion + +//region Region Canvas (from Part 1) + +class Canvas + +{ + +private: + + Display* display = nullptr; + + Window window = 0; + + GC gc = nullptr; + + XImage* image = nullptr; + + int width = 0; + + int height = 0; + + std::vector pixels; + +public: + + Canvas(Display* display, Window window, GC graphicsContext, int width, int height) : display(display), window(window), gc(graphicsContext) + + { + + resize(width, height); + + } + + ~Canvas() { destroyImage(); } + + void resize(int newWidth, int newHeight) + + { + + width = std::max(1, newWidth); + + height = std::max(1, newHeight); + + pixels.resize(static_cast(width) * height); + + clear(0x202020); + + destroyImage(); + + Visual* visual = DefaultVisual(display, DefaultScreen(display)); + + int depth = DefaultDepth(display, DefaultScreen(display)); + + image = XCreateImage(display, visual, depth, ZPixmap, 0, nullptr, width, height, 32, 0); + + if (!image) + + { + + std::exit(1); + + } + + image->data = static_cast(std::malloc(image->bytes_per_line * height)); + + if (!image->data) + + { + + std::exit(1); + + } + + } + + int getWidth() const + + { + + return width; + + } + + int getHeight() const + + { + + return height; + + } + + // Direct access to the raw framebuffer. + + uint32_t* data() + + { + + return pixels.data(); + + } + + const uint32_t* data() const + + { + + return pixels.data(); + + } + + void clear(uint32_t rgb) + + { + + std::fill(pixels.begin(), pixels.end(), rgb); + + } + + void setPixel(int x, int y, uint32_t rgb) + + { + + if (x < 0 || x >= width || y < 0 || y >= height) + + { + + return; + + } + + pixels[static_cast(y) * width + x] = rgb; + + } + + uint32_t getPixel(int x, int y) const + + { + + if (x < 0 || x >= width || y < 0 || y >= height) + + { + + return 0; + + } + + return pixels[static_cast(y) * width + x]; + + } + + void fillRect(int x, int y, int w, int h, uint32_t rgb) + + { + + int x0 = std::max(0, x); + + int y0 = std::max(0, y); + + int x1 = std::min(width, x + w); + + int y1 = std::min(height, y + h); + + for (int py = y0; py < y1; ++py) + + { + + for (int px = x0; px < x1; ++px) + + { + + setPixel(px, py, rgb); + + } + + } + + } + + void drawGradient() + + { + + for (int y = 0; y < height; ++y) + + { + + for (int x = 0; x < width; ++x) + + { + + uint8_t red = static_cast(255.0 * x / std::max(1, width - 1)); + + uint8_t green = static_cast(255.0 * y / std::max(1, height - 1)); + + uint8_t blue = 100; + + uint32_t color = (static_cast(red) << 16) | (static_cast(green) << 8) | blue; + + setPixel(x, y, color); + + } + + } + + } + + void present() + + { + + for (int y = 0; y < height; ++y) + + { + + for (int x = 0; x < width; ++x) + + { + + uint32_t rgb = getPixel(x, y); + + unsigned long x11Pixel = ((rgb >> 16) & 0xff) << 16 | ((rgb >> 8) & 0xff) << 8 | (rgb & 0xff); + + XPutPixel(image, x, y, x11Pixel); + + } + + } + + XPutImage(display, window, gc, image, 0, 0, 0, 0, width, height); + + XFlush(display); + + } + +private: + + void destroyImage() + + { + + if (image) + + { + + XDestroyImage(image); + + image = nullptr; + + } + + } + +}; + +//endregion +#endif // XFRAME_CORE_H diff --git a/widgets/components/XMonitorButton/main_XMonitorButton.cpp b/widgets/components/XMonitorButton/main_XMonitorButton.cpp new file mode 100644 index 0000000..197fcec --- /dev/null +++ b/widgets/components/XMonitorButton/main_XMonitorButton.cpp @@ -0,0 +1,88 @@ +#include "XMonitorButton.h" + +#include + +#include +#include + +int main() +{ + Display* display = XOpenDisplay(nullptr); + if (!display) { return 1; } + int screen = DefaultScreen(display); + Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 800, 600, 1, BlackPixel(display, screen), WhitePixel(display, screen)); + XStoreName(display, window, "XMonitorButton - XFrame demo"); + 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"); + XMonitorButton widget(display, screen, window, gc, &styles, 20, 20, 720, 480); + widget.draw(); + bool running = true; + long last_poll_ms = 0; + while (running) + { + while (XPending(display) > 0) + { + XEvent event; + XNextEvent(display, &event); + bool handled = false; + switch (event.type) + { + case Expose: + handled = true; + break; + case ButtonPress: + handled = widget.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case ButtonRelease: + handled = widget.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case MotionNotify: + handled = widget.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 = widget.on_key(key, buffer); + break; + } + case ClientMessage: + if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) + { + running = false; + } + break; + } + if (handled) + { + XClearWindow(display, window); + widget.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; + widget.poll(); + XClearWindow(display, window); + widget.draw(); + XFlush(display); + } + usleep(50000); + } + + XCloseDisplay(display); + return 0; +} diff --git a/widgets/components/XMonitorGrid/CMakeLists.txt b/widgets/components/XMonitorGrid/CMakeLists.txt new file mode 100644 index 0000000..003af45 --- /dev/null +++ b/widgets/components/XMonitorGrid/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.20) + +project(XMonitorGrid LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 17) + +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +find_package(X11 REQUIRED) + +add_executable(XMonitorGrid main_XMonitorGrid.cpp) + +target_link_libraries(XMonitorGrid PRIVATE X11::X11) diff --git a/widgets/components/XMonitorGrid/XMonitorGrid.h b/widgets/components/XMonitorGrid/XMonitorGrid.h new file mode 100644 index 0000000..9475880 --- /dev/null +++ b/widgets/components/XMonitorGrid/XMonitorGrid.h @@ -0,0 +1,193 @@ +#pragma once + +#include "core/xframe_core.h" +#include "../XMonitorButton/XMonitorButton.h" + +#include +#include +#include + +// region XMonitorGrid +// +// A panel of XMonitorButtons in a labeled grid, sharing one poll timer: +// poll_all() checks every monitor at once. Each cell is a real +// XMonitorButton (process / systemd unit / TCP port). Raw X11, same as +// the rest of the XFrame set. +// +class XMonitorGrid +{ +private: + Display* display_; + int screen_; + Window window_; + GC gc_; + SStyle_Manager* styles_; + int x1_, y1_, x2_, y2_; + std::string name_; + std::string text_; + std::string style_name_; + bool visible_ = true; + bool enabled_ = true; + bool focused_ = false; + + std::vector monitors_; + int columns_ = 2; + + Srgb white_color_{255, 255, 255}; + + unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); } + + void layout() + { + if (monitors_.empty()) return; + const int title_h = 20; + const int gap = 6; + 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; + for (size_t i = 0; i < monitors_.size(); ++i) + { + int c = (int)i % cols; + int r = (int)i / cols; + monitors_[i].set_bounds(x1_ + 4 + c * (cw + gap), + y1_ + title_h + 4 + r * (ch + gap), cw, ch); + } + } + +public: + XMonitorGrid(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles, int x, int y, int width, int height) + { + display_ = display; + screen_ = screen; + window_ = window; + gc_ = gc; + styles_ = styles; + x1_ = x; + y1_ = y; + x2_ = x + width; + y2_ = y + height; + name_ = "XMonitorGrid"; + text_ = "Services"; + + // Preload a demo: sshd port, the X server process, ssh unit. + add_monitor("sshd :22", XMonitorButton::Port, "22"); + add_monitor("Xvfb", XMonitorButton::Process, "Xvfb"); + add_monitor("ssh service", XMonitorButton::Unit, "ssh.service"); + add_monitor("docker :2375", XMonitorButton::Port, "2375"); + poll_all(); + } + + ~XMonitorGrid() {} + + // region Standard plumbing + void set_bounds(int x, int y, int width, int height) + { + x1_ = x; + y1_ = y; + x2_ = x + width; + y2_ = y + height; + layout(); + } + int width() { return x2_ - x1_; } + int height() { return y2_ - y1_; } + bool contains(int x, int y) + { + return visible_ && x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_; + } + void set_visible(bool v) { visible_ = v; } + bool get_visible() { return visible_; } + void set_enabled(bool e) { enabled_ = e; } + bool get_enabled() { return enabled_; } + void set_style_name(std::string s) { style_name_ = s; } + void set_text(std::string t) { text_ = t; } + std::string get_text() { return text_; } + SStyle style() { return styles_->resolve(style_name_); } + // endregion + + // region Grid API + void add_monitor(const std::string& label, XMonitorButton::Kind kind, const std::string& target) + { + monitors_.emplace_back(display_, screen_, window_, gc_, styles_, 0, 0, 10, 10); + XMonitorButton& m = monitors_.back(); + m.set_text(label); + m.set_kind(kind); + m.set_target(target); + m.poll(); + layout(); + } + void clear_monitors() { monitors_.clear(); } + int monitor_count() { return (int)monitors_.size(); } + void set_columns(int c) + { + columns_ = c > 0 ? c : 1; + layout(); + } + // One shared poll: check every monitor (call on a timer). + void poll_all() + { + for (auto& m : monitors_) m.poll(); + } + void update() { poll_all(); } + int up_count() + { + int n = 0; + for (auto& m : monitors_) + if (m.status() == XMonitorButton::MonUp) ++n; + return n; + } + // endregion + + void draw() + { + if (!visible_) return; + SStyle st = style(); + int w = width(); + int h = height(); + XSetForeground(display_, gc_, col(white_color_)); + XFillRectangle(display_, window_, gc_, x1_, y1_, w, h); + + 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"; + 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(); + + XSetForeground(display_, gc_, st.lineColor_); + XDrawRectangle(display_, window_, gc_, x1_, y1_, w, h); + XFlush(display_); + } + + bool on_button_press(int x, int y, unsigned int button) + { + if (!contains(x, y)) return false; + for (auto& m : monitors_) + { + if (m.contains(x, y)) return m.on_button_press(x, y, button); + } + return false; + } + bool on_button_release(int x, int y, unsigned int button) + { + for (auto& m : monitors_) + { + if (m.contains(x, y)) return m.on_button_release(x, y, button); + } + return false; + } + bool on_motion(int x, int y) + { + for (auto& m : monitors_) + { + if (m.contains(x, y)) return m.on_motion(x, y); + } + return false; + } + bool on_key(KeySym key, const char* str) + { + (void)key; + (void)str; + return false; + } +}; diff --git a/widgets/components/XMonitorGrid/core/xframe_core.h b/widgets/components/XMonitorGrid/core/xframe_core.h new file mode 100644 index 0000000..098187d --- /dev/null +++ b/widgets/components/XMonitorGrid/core/xframe_core.h @@ -0,0 +1,988 @@ +#pragma once +#ifndef XFRAME_CORE_H +#define XFRAME_CORE_H + +// +// xframe_core.h - shared core for the XFrame widget library. +// +// Everything in here follows the format of the "Writing Your Own +// X-11 Framework Basics" articles (hotconfig.com): +// +// Part 1 - Display / Screen / Window boilerplate, the GC as a +// "pen", the drawing primitives, and the Canvas class. +// Part 2 - widgets are standalone classes that record display_, +// screen_, window_ and gc_ at construction time. +// Part 3 - SStyle_Manager tracks named styles in a vector. +// Part 4 - a polished widget is built from primitives: two fill +// rectangles + four fill arcs make a rounded body, four +// lines + four draw arcs make its outline, and text is +// centred with XTextWidth + font ascent / descent. +// +// Two small completions versus the articles, needed by Part 4's own +// draw code: SStyle gains lineColor_ and text_color_, and the style +// manager gains the delete_style() function Part 3 left as a reader +// challenge, plus bounds-safe getters. +// + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +//region SStyle + +struct Srgb + +{ + + int r; + + int g; + + int b; + +}; + +struct SStyle + +{ + + unsigned long fillColor_ = 0; + + unsigned long outlineColor_ = 0; + + unsigned long lineColor_ = 0; + + unsigned long text_color_ = 0; + + unsigned long radius_ = 0; + + unsigned long lineWidth_ = 1; + + XFontStruct *font_ = nullptr; + + std::string style_name_; + +}; + +//endregion + +//region SStyle_Manager + +class SStyle_Manager + +{ + +private: + + std::vector styles_; + + Display *display_; + + Window window_; + + int screen_; + + GC gc_; + + int active_style = 0; // just set the default to first passed. + +public: + + SStyle_Manager(Display *display, Window window, int screen, GC gc) + + { + + display_ = display; + + window_ = window; + + screen_ = screen; + + gc_ = gc; + + } + + // NOTE (test agent, Nov 26 2026): do NOT XFreeFont here. Every demo + // main calls XCloseDisplay() before this destructor runs (stack + // unwinding after main returns), so XFreeFont would operate on a + // closed Display and corrupt the heap (SIGSEGV/SIGABRT inside + // XFreeFont on every clean exit). The fonts are process-lifetime + // resources; the X server reclaims them when the client disconnects. + // Use delete_style() to free a single style while the display is + // still open. + ~SStyle_Manager() + + { + + styles_.clear(); + + } + + int add_style(std::string styleName, Srgb fillColor, Srgb outlineColor, int radius, int linewidth, std::string fontName) + + { + + return add_style_ex(styleName, fillColor, outlineColor, outlineColor, Srgb{0, 0, 0}, radius, linewidth, fontName); + + } + + int add_style_ex(std::string styleName, Srgb fillColor, Srgb outlineColor, Srgb lineColor, Srgb textColor, int radius, int linewidth, std::string fontName) + + { + + SStyle nstyle; + + nstyle.style_name_ = std::move(styleName); + + nstyle.fillColor_ = get_color_Srgb(fillColor); + + nstyle.outlineColor_ = get_color_Srgb(outlineColor); + + nstyle.lineColor_ = get_color_Srgb(lineColor); + + nstyle.text_color_ = get_color_Srgb(textColor); + + nstyle.radius_ = radius; + + nstyle.lineWidth_ = linewidth; + + // Define fallback fonts to try if the requested font fails + + const char* fallbackFonts[] = { "fixed", "helvetica" }; + + size_t numFallbacks = sizeof(fallbackFonts) / sizeof(*fallbackFonts); + + nstyle.font_ = nullptr; + + // Attempt to load the primary font (if provided and not empty) + + if (!fontName.empty()) { + + nstyle.font_ = XLoadQueryFont(display_, fontName.c_str()); + + } + + // If the primary failed, iterate through fallback fonts until one succeeds or all are exhausted + + if (!nstyle.font_) { + + for (size_t i = 0; i < numFallbacks && !nstyle.font_; ++i) { + + nstyle.font_ = XLoadQueryFont(display_, fallbackFonts[i]); + + } + + } + + // Final validation: if no font loaded, report which fonts were attempted and exit + + if (!nstyle.font_) + + { + + printf("Failed to load any font (tried: "); + + if (!fontName.empty()) printf("%s", fontName.c_str()); + + for (size_t i = 0; i < numFallbacks; ++i) { + + if (!fontName.empty() || i > 0) printf(", "); + + printf("%s", fallbackFonts[i]); + + } + + printf(")\n"); + + return -1; + + } + + styles_.push_back(nstyle); + + return 0; + + } + + // The delete function Part 3 left as a challenge: remove a style + // by name, freeing its font, and keep active_style in range. + + int delete_style(std::string styleName) + + { + + for (size_t i = 0; i < styles_.size(); ++i) + + { + + if (styles_[i].style_name_ == styleName) + + { + + if (styles_[i].font_) + + { + + XFreeFont(display_, styles_[i].font_); + + } + + styles_.erase(styles_.begin() + static_cast(i)); + + if (active_style >= (int)styles_.size()) + + { + + active_style = styles_.empty() ? 0 : (int)styles_.size() - 1; + + } + + return 0; + + } + + } + + return -1; // Not found + + } + + int style_count() + + { + + return (int)styles_.size(); + + } + + unsigned long get_color_Srgb(Srgb color) + + { + + XColor xcolor; + + xcolor.red = color.r << 8; + + xcolor.green = color.g << 8; + + xcolor.blue = color.b << 8; + + if (XAllocColor(display_, DefaultColormap(display_, screen_), &xcolor)) + + { + + return xcolor.pixel; + + } else { + + printf("Failed to allocate color! RGB(%d,%d,%d)\n", color.r, color.g, color.b); + + return BlackPixel(display_, screen_); // Fallback + + } + + } + + unsigned long get_color_rgb(unsigned char r, unsigned char g, unsigned char b) + + { + + XColor xcolor; + + xcolor.red = r << 8; + + xcolor.green = g << 8; + + xcolor.blue = b << 8; + + if (XAllocColor(display_, DefaultColormap(display_, screen_), &xcolor)) + + { + + return xcolor.pixel; + + } else { + + printf("Failed to allocate color! RGB(%d,%d,%d)\n", r, g, b); + + return BlackPixel(display_, screen_); // Fallback + + } + + } + + int set_active_style_byName(std::string styleName) + + { + + auto it = std::find_if(styles_.begin(), styles_.end(), + + [&](const SStyle& s) { return s.style_name_ == styleName; }); + + if (it != styles_.end()) + + { + + active_style = static_cast(std::distance(styles_.begin(), it)); + + return active_style; + + } + + return -1; // Not found + + } + + SStyle get_active_style_byName(std::string styleName) + + { + + auto it = std::find_if(styles_.begin(), styles_.end(), + + [&](const SStyle& s) { return s.style_name_ == styleName; }); + + if (it != styles_.end()) + + { + + return *it; + + } + + return get_active_style(); + + } + + SStyle get_active_style() + + { + + if (styles_.empty()) + + { + + return SStyle{}; + + } + + if (active_style < 0 || active_style >= (int)styles_.size()) + + { + + active_style = 0; + + } + + return styles_[active_style]; + + } + + SStyle get_index_style(int index) + + { + + if (index >= 0 && index < (int)styles_.size()) + + { + + return styles_[index]; + + } + + return get_active_style(); + + } + + // Resolve the style a widget should draw with: its own named + // style when set, otherwise the manager's active style. + + SStyle resolve(const std::string& styleName) + + { + + if (!styleName.empty()) + + { + + return get_active_style_byName(styleName); + + } + + return get_active_style(); + + } + +}; + +//endregion + +//region Drawing helpers (Part 4 construction, shared by all widgets) + +namespace xf + +{ + + inline void set_line_width(Display* display, GC gc, int lineWidth) + + { + + unsigned long valuemask = GCLineWidth; + + XGCValues gc_values; + + gc_values.line_width = lineWidth; + + XChangeGC(display, gc, valuemask, &gc_values); + + } + + // The Part 4 rounded body: Rectangle A + Rectangle B + 4 fill arcs. + + inline void fill_rounded_rect(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + int r = (int)style.radius_; + + int w = x2 - x1; + + int h = y2 - y1; + + if (r <= 0 || w <= 2 * r || h <= 2 * r) + + { + + XFillRectangle(display, window, gc, x1, y1, w, h); + + return; + + } + + XSetForeground(display, gc, style.fillColor_); + + //Rectangle A + + XFillRectangle(display, window, gc, x1 + r, y1, w - 2 * r, h); + + //Rectangle B + + XFillRectangle(display, window, gc, x1, y1 + r, w, h - 2 * r); + + //region Arc C Top Left + + XFillArc(display, window, gc, x1, y1, 2 * r, 2 * r, 90 * 64, 90 * 64); + + //region Arc D Top Right + + XFillArc(display, window, gc, x2 - 2 * r, y1, 2 * r, 2 * r, 0, 90 * 64); + + //region Arc E Bottom Right + + XFillArc(display, window, gc, x2 - 2 * r, y2 - 2 * r, 2 * r, 2 * r, 270 * 64, 90 * 64); + + //region Arc F Bottom Left + + XFillArc(display, window, gc, x1, y2 - 2 * r, 2 * r, 2 * r, 180 * 64, 90 * 64); + + //endregion + + } + + // The Part 4 outline: 4 line bars + 4 draw arcs, in lineColor_. + + inline void draw_rounded_outline(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + int r = (int)style.radius_; + + XSetForeground(display, gc, style.lineColor_); + + set_line_width(display, gc, (int)style.lineWidth_); + + if (r <= 0) + + { + + XDrawRectangle(display, window, gc, x1, y1, x2 - x1, y2 - y1); + + return; + + } + + // 4 Line Bars + + XDrawLine(display, window, gc, x1 + r, y1, x2 - r, y1); + + XDrawLine(display, window, gc, x2, y1 + r, x2, y2 - r); + + XDrawLine(display, window, gc, x1, y1 + r, x1, y2 - r); + + XDrawLine(display, window, gc, x1 + r, y2, x2 - r, y2); + + // 4 Arcs + + XDrawArc(display, window, gc, x1, y1, 2 * r, 2 * r, 90 * 64, 90 * 64); + + //region Arc D Top Right + + XDrawArc(display, window, gc, x2 - 2 * r, y1, 2 * r, 2 * r, 0, 90 * 64); + + //region Arc E Bottom Right + + XDrawArc(display, window, gc, x2 - 2 * r, y2 - 2 * r, 2 * r, 2 * r, 270 * 64, 90 * 64); + + //region Arc F Bottom Left + + XDrawArc(display, window, gc, x1, y2 - 2 * r, 2 * r, 2 * r, 180 * 64, 90 * 64); + + //endregion + + } + + // Convenience: body + outline in one call, the polished look. + + inline void draw_polished(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + fill_rounded_rect(display, window, gc, style, x1, y1, x2, y2); + + draw_rounded_outline(display, window, gc, style, x1, y1, x2, y2); + + } + + inline int text_width(const SStyle& style, const std::string& text) + + { + + if (!style.font_) + + { + + return (int)text.size() * 8; + + } + + return XTextWidth(style.font_, text.c_str(), (int)text.length()); + + } + + // Part 4 centred text: XTextWidth + ascent / descent. + + inline void draw_text_centered(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + int tw = XTextWidth(style.font_, text.c_str(), (int)text.length()); + + int text_x = x1 + ((x2 - x1) - tw) / 2; + + int text_y = y1 + ((y2 - y1) + style.font_->ascent - style.font_->descent) / 2; + + XDrawString(display, window, gc, text_x, text_y, text.c_str(), (int)text.length()); + + } + + // Left-aligned text, vertically centred in the given band. + + inline void draw_text_left(Display* display, Window window, GC gc, const SStyle& style, int x, int y1, int y2, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + int text_y = y1 + ((y2 - y1) + style.font_->ascent - style.font_->descent) / 2; + + XDrawString(display, window, gc, x, text_y, text.c_str(), (int)text.length()); + + } + + // Baseline text at an explicit position (labels, grid cells...). + + inline void draw_text_at(Display* display, Window window, GC gc, const SStyle& style, int x, int baseline_y, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + XDrawString(display, window, gc, x, baseline_y, text.c_str(), (int)text.length()); + + } + + inline void draw_checkmark(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + set_line_width(display, gc, 2); + + int s = size / 2; + + XDrawLine(display, window, gc, cx - s, cy, cx - s / 3, cy + s); + + XDrawLine(display, window, gc, cx - s / 3, cy + s, cx + s, cy - s); + + set_line_width(display, gc, 1); + + } + + inline void draw_triangle_down(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx - (size - i), cy + i, cx + (size - i), cy + i); + + } + + } + + inline void draw_triangle_up(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx - (size - i), cy - i, cx + (size - i), cy - i); + + } + + } + + inline void draw_triangle_right(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx + i, cy - (size - i), cx + i, cy + (size - i)); + + } + + } + +} + +//endregion + +//region Region Canvas (from Part 1) + +class Canvas + +{ + +private: + + Display* display = nullptr; + + Window window = 0; + + GC gc = nullptr; + + XImage* image = nullptr; + + int width = 0; + + int height = 0; + + std::vector pixels; + +public: + + Canvas(Display* display, Window window, GC graphicsContext, int width, int height) : display(display), window(window), gc(graphicsContext) + + { + + resize(width, height); + + } + + ~Canvas() { destroyImage(); } + + void resize(int newWidth, int newHeight) + + { + + width = std::max(1, newWidth); + + height = std::max(1, newHeight); + + pixels.resize(static_cast(width) * height); + + clear(0x202020); + + destroyImage(); + + Visual* visual = DefaultVisual(display, DefaultScreen(display)); + + int depth = DefaultDepth(display, DefaultScreen(display)); + + image = XCreateImage(display, visual, depth, ZPixmap, 0, nullptr, width, height, 32, 0); + + if (!image) + + { + + std::exit(1); + + } + + image->data = static_cast(std::malloc(image->bytes_per_line * height)); + + if (!image->data) + + { + + std::exit(1); + + } + + } + + int getWidth() const + + { + + return width; + + } + + int getHeight() const + + { + + return height; + + } + + // Direct access to the raw framebuffer. + + uint32_t* data() + + { + + return pixels.data(); + + } + + const uint32_t* data() const + + { + + return pixels.data(); + + } + + void clear(uint32_t rgb) + + { + + std::fill(pixels.begin(), pixels.end(), rgb); + + } + + void setPixel(int x, int y, uint32_t rgb) + + { + + if (x < 0 || x >= width || y < 0 || y >= height) + + { + + return; + + } + + pixels[static_cast(y) * width + x] = rgb; + + } + + uint32_t getPixel(int x, int y) const + + { + + if (x < 0 || x >= width || y < 0 || y >= height) + + { + + return 0; + + } + + return pixels[static_cast(y) * width + x]; + + } + + void fillRect(int x, int y, int w, int h, uint32_t rgb) + + { + + int x0 = std::max(0, x); + + int y0 = std::max(0, y); + + int x1 = std::min(width, x + w); + + int y1 = std::min(height, y + h); + + for (int py = y0; py < y1; ++py) + + { + + for (int px = x0; px < x1; ++px) + + { + + setPixel(px, py, rgb); + + } + + } + + } + + void drawGradient() + + { + + for (int y = 0; y < height; ++y) + + { + + for (int x = 0; x < width; ++x) + + { + + uint8_t red = static_cast(255.0 * x / std::max(1, width - 1)); + + uint8_t green = static_cast(255.0 * y / std::max(1, height - 1)); + + uint8_t blue = 100; + + uint32_t color = (static_cast(red) << 16) | (static_cast(green) << 8) | blue; + + setPixel(x, y, color); + + } + + } + + } + + void present() + + { + + for (int y = 0; y < height; ++y) + + { + + for (int x = 0; x < width; ++x) + + { + + uint32_t rgb = getPixel(x, y); + + unsigned long x11Pixel = ((rgb >> 16) & 0xff) << 16 | ((rgb >> 8) & 0xff) << 8 | (rgb & 0xff); + + XPutPixel(image, x, y, x11Pixel); + + } + + } + + XPutImage(display, window, gc, image, 0, 0, 0, 0, width, height); + + XFlush(display); + + } + +private: + + void destroyImage() + + { + + if (image) + + { + + XDestroyImage(image); + + image = nullptr; + + } + + } + +}; + +//endregion +#endif // XFRAME_CORE_H diff --git a/widgets/components/XMonitorGrid/main_XMonitorGrid.cpp b/widgets/components/XMonitorGrid/main_XMonitorGrid.cpp new file mode 100644 index 0000000..b8892fc --- /dev/null +++ b/widgets/components/XMonitorGrid/main_XMonitorGrid.cpp @@ -0,0 +1,88 @@ +#include "XMonitorGrid.h" + +#include + +#include +#include + +int main() +{ + Display* display = XOpenDisplay(nullptr); + if (!display) { return 1; } + int screen = DefaultScreen(display); + Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 800, 600, 1, BlackPixel(display, screen), WhitePixel(display, screen)); + XStoreName(display, window, "XMonitorGrid - XFrame demo"); + 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 widget(display, screen, window, gc, &styles, 20, 20, 720, 480); + widget.draw(); + bool running = true; + long last_poll_ms = 0; + while (running) + { + while (XPending(display) > 0) + { + XEvent event; + XNextEvent(display, &event); + bool handled = false; + switch (event.type) + { + case Expose: + handled = true; + break; + case ButtonPress: + handled = widget.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case ButtonRelease: + handled = widget.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case MotionNotify: + handled = widget.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 = widget.on_key(key, buffer); + break; + } + case ClientMessage: + if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) + { + running = false; + } + break; + } + if (handled) + { + XClearWindow(display, window); + widget.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; + widget.poll_all(); + XClearWindow(display, window); + widget.draw(); + XFlush(display); + } + usleep(50000); + } + + XCloseDisplay(display); + return 0; +} diff --git a/widgets/components/XProcessList/CMakeLists.txt b/widgets/components/XProcessList/CMakeLists.txt new file mode 100644 index 0000000..f6ed8ef --- /dev/null +++ b/widgets/components/XProcessList/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.20) + +project(XProcessList LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 17) + +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +find_package(X11 REQUIRED) + +add_executable(XProcessList main_XProcessList.cpp) + +target_link_libraries(XProcessList PRIVATE X11::X11) diff --git a/widgets/components/XProcessList/XProcessList.h b/widgets/components/XProcessList/XProcessList.h new file mode 100644 index 0000000..412693b --- /dev/null +++ b/widgets/components/XProcessList/XProcessList.h @@ -0,0 +1,370 @@ +#pragma once + +#include "core/xframe_core.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// region XProcessList +// +// A live process table (btop proc box): sortable columns, an in-cell +// CPU% bar, colour-coded rows, auto-refresh from /proc. Click a column +// header to sort by it; click again to reverse. Raw X11, same as the +// rest of the XFrame set. +// +class XProcessList +{ +private: + Display* display_; + int screen_; + Window window_; + GC gc_; + SStyle_Manager* styles_; + int x1_, y1_, x2_, y2_; + std::string name_; + std::string text_; + std::string style_name_; + bool visible_ = true; + bool enabled_ = true; + bool focused_ = false; + + struct Row + { + int pid = 0; + std::string name; + std::string user; + double mem_mb = 0.0; + double cpu = 0.0; // lifetime average % of one core + }; + + std::vector rows_; + int sort_col_ = 4; // 0 pid, 1 name, 2 user, 3 mem, 4 cpu + bool sort_desc_ = true; + int max_rows_ = 40; + + Srgb white_color_{255, 255, 255}; + Srgb header_color_{235, 235, 235}; + Srgb axis_color_{150, 150, 150}; + Srgb bar_color_{0, 150, 60}; + Srgb hot_row_{255, 235, 235}; + + unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); } + + static std::string fmt1(double v) + { + char buf[32]; + std::snprintf(buf, sizeof(buf), "%.1f", v); + return buf; + } + + static std::string fmt_mem(double mb) + { + char buf[32]; + if (mb >= 1024.0) std::snprintf(buf, sizeof(buf), "%.1fG", mb / 1024.0); + else if (mb >= 1.0) std::snprintf(buf, sizeof(buf), "%.0fM", mb); + else std::snprintf(buf, sizeof(buf), "%.0fK", mb * 1024.0); + return buf; + } + + void draw_text_color(int x, int baseline_y, const std::string& s, Srgb color) + { + SStyle st = style(); + if (!st.font_ || s.empty()) return; + XSetFont(display_, gc_, st.font_->fid); + XSetForeground(display_, gc_, col(color)); + XDrawString(display_, window_, gc_, x, baseline_y, s.c_str(), (int)s.size()); + } + + void sort_rows() + { + std::sort(rows_.begin(), rows_.end(), [&](const Row& a, const Row& b) { + int cmp = 0; + switch (sort_col_) + { + case 0: cmp = a.pid < b.pid ? -1 : (a.pid > b.pid ? 1 : 0); break; + case 1: cmp = a.name.compare(b.name); break; + case 2: cmp = a.user.compare(b.user); break; + case 3: cmp = a.mem_mb < b.mem_mb ? -1 : (a.mem_mb > b.mem_mb ? 1 : 0); break; + default: cmp = a.cpu < b.cpu ? -1 : (a.cpu > b.cpu ? 1 : 0); break; + } + return sort_desc_ ? cmp > 0 : cmp < 0; + }); + } + + // Column x-offsets (fractions of the inner width) for Pid/Program/User/MemB/Cpu%. + void col_xs(int inner_x, int inner_w, int xs[6]) const + { + xs[0] = inner_x; + xs[1] = inner_x + inner_w * 14 / 100; + xs[2] = inner_x + inner_w * 52 / 100; + xs[3] = inner_x + inner_w * 68 / 100; + xs[4] = inner_x + inner_w * 82 / 100; + xs[5] = inner_x + inner_w; + } + +public: + XProcessList(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles, int x, int y, int width, int height) + { + display_ = display; + screen_ = screen; + window_ = window; + gc_ = gc; + styles_ = styles; + x1_ = x; + y1_ = y; + x2_ = x + width; + y2_ = y + height; + name_ = "XProcessList"; + text_ = "Processes"; + refresh(); + } + + ~XProcessList() {} + + // region Standard plumbing + void set_bounds(int x, int y, int width, int height) + { + x1_ = x; + y1_ = y; + x2_ = x + width; + y2_ = y + height; + } + int width() { return x2_ - x1_; } + int height() { return y2_ - y1_; } + bool contains(int x, int y) + { + return visible_ && x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_; + } + void set_visible(bool v) { visible_ = v; } + bool get_visible() { return visible_; } + void set_enabled(bool e) { enabled_ = e; } + bool get_enabled() { return enabled_; } + void set_style_name(std::string s) { style_name_ = s; } + void set_text(std::string t) { text_ = t; } + std::string get_text() { return text_; } + SStyle style() { return styles_->resolve(style_name_); } + // endregion + + // region Process API + // Re-scan /proc and rebuild the table (call on a timer for live data). + void refresh() + { + rows_.clear(); + double uptime = 0.0; + { + std::ifstream up("/proc/uptime"); + if (up) up >> uptime; + } + long ticks = sysconf(_SC_CLK_TCK); + long page = sysconf(_SC_PAGESIZE); + if (ticks <= 0) ticks = 100; + if (page <= 0) page = 4096; + DIR* dir = opendir("/proc"); + if (!dir) return; + struct dirent* ent; + while ((ent = readdir(dir)) != nullptr) + { + if (ent->d_name[0] < '0' || ent->d_name[0] > '9') continue; + int pid = std::atoi(ent->d_name); + std::string stat_path = std::string("/proc/") + ent->d_name + "/stat"; + std::ifstream in(stat_path); + if (!in) continue; + std::string line; + std::getline(in, line); + size_t open = line.find('('); + size_t close = line.rfind(')'); + if (open == std::string::npos || close == std::string::npos || close <= open) continue; + Row r; + r.pid = pid; + r.name = line.substr(open + 1, close - open - 1); + // Fields after ")": state ppid pgrp session tty_nr tpgid flags + // minflt cminflt majflt cmajflt utime stime cutime cstime + // priority nice num_threads itrealvalue starttime vsize rss. + unsigned long utime = 0, stime = 0, starttime = 0; + long rss = 0; + int scanned = std::sscanf(line.c_str() + close + 1, + "%*s %*d %*d %*d %*d %*u %*u %*u %*u %*u %*u %lu %lu %*d %*d %*d %*d %*d %*d %lu %*u %ld", + &utime, &stime, &starttime, &rss); + if (scanned < 4) continue; + double age = uptime - (double)starttime / ticks; + if (age > 1.0) + r.cpu = ((double)(utime + stime) / ticks) / age * 100.0; + r.mem_mb = (double)rss * page / (1024.0 * 1024.0); + // User from /proc//status Uid line. + std::string status_path = std::string("/proc/") + ent->d_name + "/status"; + std::ifstream st(status_path); + std::string key; + long uid = -1; + if (st) + { + while (st >> key) + { + if (key == "Uid:") + { + st >> uid; + break; + } + std::string skip; + std::getline(st, skip); + } + } + if (uid >= 0) + { + struct passwd* pw = getpwuid((uid_t)uid); + r.user = pw ? pw->pw_name : std::to_string(uid); + } + else + { + r.user = "?"; + } + rows_.push_back(r); + if ((int)rows_.size() >= max_rows_ * 4) break; // bound the scan + } + closedir(dir); + sort_rows(); + if ((int)rows_.size() > max_rows_) rows_.resize(max_rows_); + } + void set_sort_column(int c) + { + if (c < 0 || c > 4) return; + if (c == sort_col_) sort_desc_ = !sort_desc_; + else + { + sort_col_ = c; + sort_desc_ = (c == 3 || c == 4); + } + sort_rows(); + } + 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; } + // endregion + + void draw() + { + if (!visible_) return; + SStyle st = style(); + int w = width(); + int h = height(); + XSetForeground(display_, gc_, col(white_color_)); + XFillRectangle(display_, window_, gc_, x1_, y1_, w, h); + + 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%"}; + + xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_); + std::string count = std::to_string(rows_.size()) + " procs"; + 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); + for (int c = 0; c < 5; ++c) + { + std::string head = heads[c]; + if (c == sort_col_) head += sort_desc_ ? " v" : " ^"; + 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); + + // Rows. + int y = hy + header_h; + int shown = 0; + int max_fit = (y2_ - 6 - y) / row_h; + for (auto& r : rows_) + { + if (shown >= max_fit) break; + int ry = y + shown * row_h; + if (r.cpu >= 50.0) + { + XSetForeground(display_, gc_, col(hot_row_)); + XFillRectangle(display_, window_, gc_, ix, ry, iw, row_h); + } + else if (shown % 2 == 1) + { + XSetForeground(display_, gc_, col(Srgb{248, 248, 248})); + 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); + 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; + int bar_w = xs[5] - bar_x - 4; + int fill = (int)(bar_w * std::min(100.0, r.cpu) / 100.0); + if (fill > 0) + { + 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; + } + + XSetForeground(display_, gc_, st.lineColor_); + XDrawRectangle(display_, window_, gc_, x1_, y1_, w, h); + XFlush(display_); + } + + bool on_button_press(int x, int y, unsigned int button) + { + if (button != 1 || !contains(x, y)) return false; + const int title_h = 20; + const int header_h = 18; + int hy = y1_ + title_h; + if (y >= hy && y < hy + header_h) + { + int ix = x1_ + 6; + int iw = width() - 12; + int xs[6]; + col_xs(ix, iw, xs); + for (int c = 0; c < 5; ++c) + { + if (x >= xs[c] && x < xs[c + 1]) + { + set_sort_column(c); + return true; + } + } + } + return false; + } + bool on_button_release(int x, int y, unsigned int button) + { + (void)x; + (void)y; + (void)button; + return false; + } + bool on_motion(int x, int y) + { + (void)x; + (void)y; + return false; + } + bool on_key(KeySym key, const char* str) + { + (void)key; + (void)str; + return false; + } +}; diff --git a/widgets/components/XProcessList/core/xframe_core.h b/widgets/components/XProcessList/core/xframe_core.h new file mode 100644 index 0000000..098187d --- /dev/null +++ b/widgets/components/XProcessList/core/xframe_core.h @@ -0,0 +1,988 @@ +#pragma once +#ifndef XFRAME_CORE_H +#define XFRAME_CORE_H + +// +// xframe_core.h - shared core for the XFrame widget library. +// +// Everything in here follows the format of the "Writing Your Own +// X-11 Framework Basics" articles (hotconfig.com): +// +// Part 1 - Display / Screen / Window boilerplate, the GC as a +// "pen", the drawing primitives, and the Canvas class. +// Part 2 - widgets are standalone classes that record display_, +// screen_, window_ and gc_ at construction time. +// Part 3 - SStyle_Manager tracks named styles in a vector. +// Part 4 - a polished widget is built from primitives: two fill +// rectangles + four fill arcs make a rounded body, four +// lines + four draw arcs make its outline, and text is +// centred with XTextWidth + font ascent / descent. +// +// Two small completions versus the articles, needed by Part 4's own +// draw code: SStyle gains lineColor_ and text_color_, and the style +// manager gains the delete_style() function Part 3 left as a reader +// challenge, plus bounds-safe getters. +// + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +//region SStyle + +struct Srgb + +{ + + int r; + + int g; + + int b; + +}; + +struct SStyle + +{ + + unsigned long fillColor_ = 0; + + unsigned long outlineColor_ = 0; + + unsigned long lineColor_ = 0; + + unsigned long text_color_ = 0; + + unsigned long radius_ = 0; + + unsigned long lineWidth_ = 1; + + XFontStruct *font_ = nullptr; + + std::string style_name_; + +}; + +//endregion + +//region SStyle_Manager + +class SStyle_Manager + +{ + +private: + + std::vector styles_; + + Display *display_; + + Window window_; + + int screen_; + + GC gc_; + + int active_style = 0; // just set the default to first passed. + +public: + + SStyle_Manager(Display *display, Window window, int screen, GC gc) + + { + + display_ = display; + + window_ = window; + + screen_ = screen; + + gc_ = gc; + + } + + // NOTE (test agent, Nov 26 2026): do NOT XFreeFont here. Every demo + // main calls XCloseDisplay() before this destructor runs (stack + // unwinding after main returns), so XFreeFont would operate on a + // closed Display and corrupt the heap (SIGSEGV/SIGABRT inside + // XFreeFont on every clean exit). The fonts are process-lifetime + // resources; the X server reclaims them when the client disconnects. + // Use delete_style() to free a single style while the display is + // still open. + ~SStyle_Manager() + + { + + styles_.clear(); + + } + + int add_style(std::string styleName, Srgb fillColor, Srgb outlineColor, int radius, int linewidth, std::string fontName) + + { + + return add_style_ex(styleName, fillColor, outlineColor, outlineColor, Srgb{0, 0, 0}, radius, linewidth, fontName); + + } + + int add_style_ex(std::string styleName, Srgb fillColor, Srgb outlineColor, Srgb lineColor, Srgb textColor, int radius, int linewidth, std::string fontName) + + { + + SStyle nstyle; + + nstyle.style_name_ = std::move(styleName); + + nstyle.fillColor_ = get_color_Srgb(fillColor); + + nstyle.outlineColor_ = get_color_Srgb(outlineColor); + + nstyle.lineColor_ = get_color_Srgb(lineColor); + + nstyle.text_color_ = get_color_Srgb(textColor); + + nstyle.radius_ = radius; + + nstyle.lineWidth_ = linewidth; + + // Define fallback fonts to try if the requested font fails + + const char* fallbackFonts[] = { "fixed", "helvetica" }; + + size_t numFallbacks = sizeof(fallbackFonts) / sizeof(*fallbackFonts); + + nstyle.font_ = nullptr; + + // Attempt to load the primary font (if provided and not empty) + + if (!fontName.empty()) { + + nstyle.font_ = XLoadQueryFont(display_, fontName.c_str()); + + } + + // If the primary failed, iterate through fallback fonts until one succeeds or all are exhausted + + if (!nstyle.font_) { + + for (size_t i = 0; i < numFallbacks && !nstyle.font_; ++i) { + + nstyle.font_ = XLoadQueryFont(display_, fallbackFonts[i]); + + } + + } + + // Final validation: if no font loaded, report which fonts were attempted and exit + + if (!nstyle.font_) + + { + + printf("Failed to load any font (tried: "); + + if (!fontName.empty()) printf("%s", fontName.c_str()); + + for (size_t i = 0; i < numFallbacks; ++i) { + + if (!fontName.empty() || i > 0) printf(", "); + + printf("%s", fallbackFonts[i]); + + } + + printf(")\n"); + + return -1; + + } + + styles_.push_back(nstyle); + + return 0; + + } + + // The delete function Part 3 left as a challenge: remove a style + // by name, freeing its font, and keep active_style in range. + + int delete_style(std::string styleName) + + { + + for (size_t i = 0; i < styles_.size(); ++i) + + { + + if (styles_[i].style_name_ == styleName) + + { + + if (styles_[i].font_) + + { + + XFreeFont(display_, styles_[i].font_); + + } + + styles_.erase(styles_.begin() + static_cast(i)); + + if (active_style >= (int)styles_.size()) + + { + + active_style = styles_.empty() ? 0 : (int)styles_.size() - 1; + + } + + return 0; + + } + + } + + return -1; // Not found + + } + + int style_count() + + { + + return (int)styles_.size(); + + } + + unsigned long get_color_Srgb(Srgb color) + + { + + XColor xcolor; + + xcolor.red = color.r << 8; + + xcolor.green = color.g << 8; + + xcolor.blue = color.b << 8; + + if (XAllocColor(display_, DefaultColormap(display_, screen_), &xcolor)) + + { + + return xcolor.pixel; + + } else { + + printf("Failed to allocate color! RGB(%d,%d,%d)\n", color.r, color.g, color.b); + + return BlackPixel(display_, screen_); // Fallback + + } + + } + + unsigned long get_color_rgb(unsigned char r, unsigned char g, unsigned char b) + + { + + XColor xcolor; + + xcolor.red = r << 8; + + xcolor.green = g << 8; + + xcolor.blue = b << 8; + + if (XAllocColor(display_, DefaultColormap(display_, screen_), &xcolor)) + + { + + return xcolor.pixel; + + } else { + + printf("Failed to allocate color! RGB(%d,%d,%d)\n", r, g, b); + + return BlackPixel(display_, screen_); // Fallback + + } + + } + + int set_active_style_byName(std::string styleName) + + { + + auto it = std::find_if(styles_.begin(), styles_.end(), + + [&](const SStyle& s) { return s.style_name_ == styleName; }); + + if (it != styles_.end()) + + { + + active_style = static_cast(std::distance(styles_.begin(), it)); + + return active_style; + + } + + return -1; // Not found + + } + + SStyle get_active_style_byName(std::string styleName) + + { + + auto it = std::find_if(styles_.begin(), styles_.end(), + + [&](const SStyle& s) { return s.style_name_ == styleName; }); + + if (it != styles_.end()) + + { + + return *it; + + } + + return get_active_style(); + + } + + SStyle get_active_style() + + { + + if (styles_.empty()) + + { + + return SStyle{}; + + } + + if (active_style < 0 || active_style >= (int)styles_.size()) + + { + + active_style = 0; + + } + + return styles_[active_style]; + + } + + SStyle get_index_style(int index) + + { + + if (index >= 0 && index < (int)styles_.size()) + + { + + return styles_[index]; + + } + + return get_active_style(); + + } + + // Resolve the style a widget should draw with: its own named + // style when set, otherwise the manager's active style. + + SStyle resolve(const std::string& styleName) + + { + + if (!styleName.empty()) + + { + + return get_active_style_byName(styleName); + + } + + return get_active_style(); + + } + +}; + +//endregion + +//region Drawing helpers (Part 4 construction, shared by all widgets) + +namespace xf + +{ + + inline void set_line_width(Display* display, GC gc, int lineWidth) + + { + + unsigned long valuemask = GCLineWidth; + + XGCValues gc_values; + + gc_values.line_width = lineWidth; + + XChangeGC(display, gc, valuemask, &gc_values); + + } + + // The Part 4 rounded body: Rectangle A + Rectangle B + 4 fill arcs. + + inline void fill_rounded_rect(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + int r = (int)style.radius_; + + int w = x2 - x1; + + int h = y2 - y1; + + if (r <= 0 || w <= 2 * r || h <= 2 * r) + + { + + XFillRectangle(display, window, gc, x1, y1, w, h); + + return; + + } + + XSetForeground(display, gc, style.fillColor_); + + //Rectangle A + + XFillRectangle(display, window, gc, x1 + r, y1, w - 2 * r, h); + + //Rectangle B + + XFillRectangle(display, window, gc, x1, y1 + r, w, h - 2 * r); + + //region Arc C Top Left + + XFillArc(display, window, gc, x1, y1, 2 * r, 2 * r, 90 * 64, 90 * 64); + + //region Arc D Top Right + + XFillArc(display, window, gc, x2 - 2 * r, y1, 2 * r, 2 * r, 0, 90 * 64); + + //region Arc E Bottom Right + + XFillArc(display, window, gc, x2 - 2 * r, y2 - 2 * r, 2 * r, 2 * r, 270 * 64, 90 * 64); + + //region Arc F Bottom Left + + XFillArc(display, window, gc, x1, y2 - 2 * r, 2 * r, 2 * r, 180 * 64, 90 * 64); + + //endregion + + } + + // The Part 4 outline: 4 line bars + 4 draw arcs, in lineColor_. + + inline void draw_rounded_outline(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + int r = (int)style.radius_; + + XSetForeground(display, gc, style.lineColor_); + + set_line_width(display, gc, (int)style.lineWidth_); + + if (r <= 0) + + { + + XDrawRectangle(display, window, gc, x1, y1, x2 - x1, y2 - y1); + + return; + + } + + // 4 Line Bars + + XDrawLine(display, window, gc, x1 + r, y1, x2 - r, y1); + + XDrawLine(display, window, gc, x2, y1 + r, x2, y2 - r); + + XDrawLine(display, window, gc, x1, y1 + r, x1, y2 - r); + + XDrawLine(display, window, gc, x1 + r, y2, x2 - r, y2); + + // 4 Arcs + + XDrawArc(display, window, gc, x1, y1, 2 * r, 2 * r, 90 * 64, 90 * 64); + + //region Arc D Top Right + + XDrawArc(display, window, gc, x2 - 2 * r, y1, 2 * r, 2 * r, 0, 90 * 64); + + //region Arc E Bottom Right + + XDrawArc(display, window, gc, x2 - 2 * r, y2 - 2 * r, 2 * r, 2 * r, 270 * 64, 90 * 64); + + //region Arc F Bottom Left + + XDrawArc(display, window, gc, x1, y2 - 2 * r, 2 * r, 2 * r, 180 * 64, 90 * 64); + + //endregion + + } + + // Convenience: body + outline in one call, the polished look. + + inline void draw_polished(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + fill_rounded_rect(display, window, gc, style, x1, y1, x2, y2); + + draw_rounded_outline(display, window, gc, style, x1, y1, x2, y2); + + } + + inline int text_width(const SStyle& style, const std::string& text) + + { + + if (!style.font_) + + { + + return (int)text.size() * 8; + + } + + return XTextWidth(style.font_, text.c_str(), (int)text.length()); + + } + + // Part 4 centred text: XTextWidth + ascent / descent. + + inline void draw_text_centered(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + int tw = XTextWidth(style.font_, text.c_str(), (int)text.length()); + + int text_x = x1 + ((x2 - x1) - tw) / 2; + + int text_y = y1 + ((y2 - y1) + style.font_->ascent - style.font_->descent) / 2; + + XDrawString(display, window, gc, text_x, text_y, text.c_str(), (int)text.length()); + + } + + // Left-aligned text, vertically centred in the given band. + + inline void draw_text_left(Display* display, Window window, GC gc, const SStyle& style, int x, int y1, int y2, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + int text_y = y1 + ((y2 - y1) + style.font_->ascent - style.font_->descent) / 2; + + XDrawString(display, window, gc, x, text_y, text.c_str(), (int)text.length()); + + } + + // Baseline text at an explicit position (labels, grid cells...). + + inline void draw_text_at(Display* display, Window window, GC gc, const SStyle& style, int x, int baseline_y, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + XDrawString(display, window, gc, x, baseline_y, text.c_str(), (int)text.length()); + + } + + inline void draw_checkmark(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + set_line_width(display, gc, 2); + + int s = size / 2; + + XDrawLine(display, window, gc, cx - s, cy, cx - s / 3, cy + s); + + XDrawLine(display, window, gc, cx - s / 3, cy + s, cx + s, cy - s); + + set_line_width(display, gc, 1); + + } + + inline void draw_triangle_down(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx - (size - i), cy + i, cx + (size - i), cy + i); + + } + + } + + inline void draw_triangle_up(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx - (size - i), cy - i, cx + (size - i), cy - i); + + } + + } + + inline void draw_triangle_right(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx + i, cy - (size - i), cx + i, cy + (size - i)); + + } + + } + +} + +//endregion + +//region Region Canvas (from Part 1) + +class Canvas + +{ + +private: + + Display* display = nullptr; + + Window window = 0; + + GC gc = nullptr; + + XImage* image = nullptr; + + int width = 0; + + int height = 0; + + std::vector pixels; + +public: + + Canvas(Display* display, Window window, GC graphicsContext, int width, int height) : display(display), window(window), gc(graphicsContext) + + { + + resize(width, height); + + } + + ~Canvas() { destroyImage(); } + + void resize(int newWidth, int newHeight) + + { + + width = std::max(1, newWidth); + + height = std::max(1, newHeight); + + pixels.resize(static_cast(width) * height); + + clear(0x202020); + + destroyImage(); + + Visual* visual = DefaultVisual(display, DefaultScreen(display)); + + int depth = DefaultDepth(display, DefaultScreen(display)); + + image = XCreateImage(display, visual, depth, ZPixmap, 0, nullptr, width, height, 32, 0); + + if (!image) + + { + + std::exit(1); + + } + + image->data = static_cast(std::malloc(image->bytes_per_line * height)); + + if (!image->data) + + { + + std::exit(1); + + } + + } + + int getWidth() const + + { + + return width; + + } + + int getHeight() const + + { + + return height; + + } + + // Direct access to the raw framebuffer. + + uint32_t* data() + + { + + return pixels.data(); + + } + + const uint32_t* data() const + + { + + return pixels.data(); + + } + + void clear(uint32_t rgb) + + { + + std::fill(pixels.begin(), pixels.end(), rgb); + + } + + void setPixel(int x, int y, uint32_t rgb) + + { + + if (x < 0 || x >= width || y < 0 || y >= height) + + { + + return; + + } + + pixels[static_cast(y) * width + x] = rgb; + + } + + uint32_t getPixel(int x, int y) const + + { + + if (x < 0 || x >= width || y < 0 || y >= height) + + { + + return 0; + + } + + return pixels[static_cast(y) * width + x]; + + } + + void fillRect(int x, int y, int w, int h, uint32_t rgb) + + { + + int x0 = std::max(0, x); + + int y0 = std::max(0, y); + + int x1 = std::min(width, x + w); + + int y1 = std::min(height, y + h); + + for (int py = y0; py < y1; ++py) + + { + + for (int px = x0; px < x1; ++px) + + { + + setPixel(px, py, rgb); + + } + + } + + } + + void drawGradient() + + { + + for (int y = 0; y < height; ++y) + + { + + for (int x = 0; x < width; ++x) + + { + + uint8_t red = static_cast(255.0 * x / std::max(1, width - 1)); + + uint8_t green = static_cast(255.0 * y / std::max(1, height - 1)); + + uint8_t blue = 100; + + uint32_t color = (static_cast(red) << 16) | (static_cast(green) << 8) | blue; + + setPixel(x, y, color); + + } + + } + + } + + void present() + + { + + for (int y = 0; y < height; ++y) + + { + + for (int x = 0; x < width; ++x) + + { + + uint32_t rgb = getPixel(x, y); + + unsigned long x11Pixel = ((rgb >> 16) & 0xff) << 16 | ((rgb >> 8) & 0xff) << 8 | (rgb & 0xff); + + XPutPixel(image, x, y, x11Pixel); + + } + + } + + XPutImage(display, window, gc, image, 0, 0, 0, 0, width, height); + + XFlush(display); + + } + +private: + + void destroyImage() + + { + + if (image) + + { + + XDestroyImage(image); + + image = nullptr; + + } + + } + +}; + +//endregion +#endif // XFRAME_CORE_H diff --git a/widgets/components/XProcessList/main_XProcessList.cpp b/widgets/components/XProcessList/main_XProcessList.cpp new file mode 100644 index 0000000..6af296b --- /dev/null +++ b/widgets/components/XProcessList/main_XProcessList.cpp @@ -0,0 +1,88 @@ +#include "XProcessList.h" + +#include + +#include +#include + +int main() +{ + Display* display = XOpenDisplay(nullptr); + if (!display) { return 1; } + int screen = DefaultScreen(display); + Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 800, 600, 1, BlackPixel(display, screen), WhitePixel(display, screen)); + XStoreName(display, window, "XProcessList - XFrame demo"); + 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"); + XProcessList widget(display, screen, window, gc, &styles, 20, 20, 720, 480); + widget.draw(); + bool running = true; + long last_poll_ms = 0; + while (running) + { + while (XPending(display) > 0) + { + XEvent event; + XNextEvent(display, &event); + bool handled = false; + switch (event.type) + { + case Expose: + handled = true; + break; + case ButtonPress: + handled = widget.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case ButtonRelease: + handled = widget.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case MotionNotify: + handled = widget.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 = widget.on_key(key, buffer); + break; + } + case ClientMessage: + if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) + { + running = false; + } + break; + } + if (handled) + { + XClearWindow(display, window); + widget.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; + widget.refresh(); + XClearWindow(display, window); + widget.draw(); + XFlush(display); + } + usleep(50000); + } + + XCloseDisplay(display); + return 0; +} diff --git a/widgets/components/XSysBox/CMakeLists.txt b/widgets/components/XSysBox/CMakeLists.txt new file mode 100644 index 0000000..9f5a1c7 --- /dev/null +++ b/widgets/components/XSysBox/CMakeLists.txt @@ -0,0 +1,13 @@ +cmake_minimum_required(VERSION 3.20) + +project(XSysBox LANGUAGES CXX) + +set(CMAKE_CXX_STANDARD 17) + +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +find_package(X11 REQUIRED) + +add_executable(XSysBox main_XSysBox.cpp) + +target_link_libraries(XSysBox PRIVATE X11::X11) diff --git a/widgets/components/XSysBox/XSysBox.h b/widgets/components/XSysBox/XSysBox.h new file mode 100644 index 0000000..6948ae6 --- /dev/null +++ b/widgets/components/XSysBox/XSysBox.h @@ -0,0 +1,237 @@ +#pragma once + +#include "core/xframe_core.h" + +#include +#include +#include +#include +#include + +// region XSysBox +// +// A composite resource box in the btop style: title + big number + +// history graph + usage bar (one box per resource: CPU / MEM / NET). +// Raw X11, same as the rest of the XFrame set. +// +class XSysBox +{ +private: + Display* display_; + int screen_; + Window window_; + GC gc_; + SStyle_Manager* styles_; + int x1_, y1_, x2_, y2_; + std::string name_; + std::string text_; // title + std::string style_name_; + bool visible_ = true; + bool enabled_ = true; + bool focused_ = false; + + double value_ = 0.0; + double max_ = 100.0; + std::string unit_ = "%"; + std::string big_suffix_; + int capacity_ = 60; + std::vector history_; + + Srgb white_color_{255, 255, 255}; + Srgb grid_color_{225, 225, 225}; + Srgb axis_color_{150, 150, 150}; + Srgb graph_color_{0, 170, 70}; + Srgb bar_color_{0, 150, 60}; + Srgb track_color_{230, 230, 230}; + + unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); } + + static std::string fmt1(double v) + { + char buf[32]; + std::snprintf(buf, sizeof(buf), "%.1f", v); + return buf; + } + + void draw_text_color(int x, int baseline_y, const std::string& s, Srgb color) + { + SStyle st = style(); + if (!st.font_ || s.empty()) return; + XSetFont(display_, gc_, st.font_->fid); + XSetForeground(display_, gc_, col(color)); + XDrawString(display_, window_, gc_, x, baseline_y, s.c_str(), (int)s.size()); + } + +public: + XSysBox(Display* display, int screen, Window window, GC gc, SStyle_Manager* styles, int x, int y, int width, int height) + { + display_ = display; + screen_ = screen; + window_ = window; + gc_ = gc; + styles_ = styles; + x1_ = x; + y1_ = y; + x2_ = x + width; + y2_ = y + height; + name_ = "XSysBox"; + text_ = "CPU"; + unit_ = "%"; + max_ = 100.0; + + // Preload a demo: CPU-ish sine + noise history. + unsigned int seed = 31337; + auto rnd = [&]() { + seed = seed * 1103515245 + 12345; + return ((seed >> 16) & 0x7fff) / 32767.0; + }; + for (int i = 0; i < capacity_; ++i) + { + double t = (double)i / capacity_; + double v = 55.0 + 28.0 * std::sin(t * 8.0) + (rnd() - 0.5) * 14.0; + history_.push_back(std::max(0.0, std::min(100.0, v))); + } + value_ = history_.empty() ? 0.0 : history_.back(); + } + + ~XSysBox() {} + + // region Standard plumbing + void set_bounds(int x, int y, int width, int height) + { + x1_ = x; + y1_ = y; + x2_ = x + width; + y2_ = y + height; + } + int width() { return x2_ - x1_; } + int height() { return y2_ - y1_; } + bool contains(int x, int y) + { + return visible_ && x >= x1_ && x <= x2_ && y >= y1_ && y <= y2_; + } + void set_visible(bool v) { visible_ = v; } + bool get_visible() { return visible_; } + void set_enabled(bool e) { enabled_ = e; } + bool get_enabled() { return enabled_; } + void set_style_name(std::string s) { style_name_ = s; } + void set_text(std::string t) { text_ = t; } + std::string get_text() { return text_; } + SStyle style() { return styles_->resolve(style_name_); } + // endregion + + // region Box API + // Sets the current value and appends it to the history graph. + void set_value(double v) + { + value_ = std::max(0.0, std::min(max_, v)); + history_.push_back(value_); + while ((int)history_.size() > capacity_) history_.erase(history_.begin()); + } + double get_value() { return value_; } + void set_max(double m) + { + max_ = m > 0.0 ? m : 100.0; + value_ = std::min(value_, max_); + } + double get_max() { return max_; } + void set_unit(const std::string& u) { unit_ = u; } + std::string get_unit() { return unit_; } + void set_graph_color(Srgb c) { graph_color_ = c; } + // endregion + + void draw() + { + if (!visible_) return; + SStyle st = style(); + int w = width(); + int h = height(); + XSetForeground(display_, gc_, col(white_color_)); + XFillRectangle(display_, window_, gc_, x1_, y1_, w, h); + + const int title_h = 20; + const int big_h = 30; + const int bar_h = 18; + int gx = x1_ + 8; + 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. + xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_); + std::string big = fmt1(value_) + unit_; + 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_; + 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_)); + XDrawLine(display_, window_, gc_, gx, gy + gh / 2, gx + gw, gy + gh / 2); + if (history_.size() >= 2) + { + std::vector pts; + int n = (int)history_.size(); + for (int i = 0; i < n; ++i) + { + int px = gx + (int)((double)i / (capacity_ - 1) * gw); + int py = gy + gh - (int)(history_[i] / max_ * gh); + pts.push_back(XPoint{(short)px, (short)py}); + } + XSetForeground(display_, gc_, col(graph_color_)); + xf::set_line_width(display_, gc_, 2); + XDrawLines(display_, window_, gc_, pts.data(), (int)pts.size(), CoordModeOrigin); + xf::set_line_width(display_, gc_, 1); + } + XSetForeground(display_, gc_, col(axis_color_)); + XDrawRectangle(display_, window_, gc_, gx, gy, gw, gh); + + // Usage bar at the bottom. + int bx = x1_ + 8; + int by = y2_ - bar_h - 6; + int bw = std::max(10, w - 16); + XSetForeground(display_, gc_, col(track_color_)); + XFillRectangle(display_, window_, gc_, bx, by, bw, bar_h); + int filled = (int)(bw * (max_ > 0.0 ? value_ / max_ : 0.0)); + XSetForeground(display_, gc_, col(bar_color_)); + XFillRectangle(display_, window_, gc_, bx, by, filled, bar_h); + XSetForeground(display_, gc_, col(axis_color_)); + 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 = 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); + XFlush(display_); + } + + bool on_button_press(int x, int y, unsigned int button) + { + (void)x; + (void)y; + (void)button; + return false; + } + bool on_button_release(int x, int y, unsigned int button) + { + (void)x; + (void)y; + (void)button; + return false; + } + bool on_motion(int x, int y) + { + (void)x; + (void)y; + return false; + } + bool on_key(KeySym key, const char* str) + { + (void)key; + (void)str; + return false; + } +}; diff --git a/widgets/components/XSysBox/core/xframe_core.h b/widgets/components/XSysBox/core/xframe_core.h new file mode 100644 index 0000000..098187d --- /dev/null +++ b/widgets/components/XSysBox/core/xframe_core.h @@ -0,0 +1,988 @@ +#pragma once +#ifndef XFRAME_CORE_H +#define XFRAME_CORE_H + +// +// xframe_core.h - shared core for the XFrame widget library. +// +// Everything in here follows the format of the "Writing Your Own +// X-11 Framework Basics" articles (hotconfig.com): +// +// Part 1 - Display / Screen / Window boilerplate, the GC as a +// "pen", the drawing primitives, and the Canvas class. +// Part 2 - widgets are standalone classes that record display_, +// screen_, window_ and gc_ at construction time. +// Part 3 - SStyle_Manager tracks named styles in a vector. +// Part 4 - a polished widget is built from primitives: two fill +// rectangles + four fill arcs make a rounded body, four +// lines + four draw arcs make its outline, and text is +// centred with XTextWidth + font ascent / descent. +// +// Two small completions versus the articles, needed by Part 4's own +// draw code: SStyle gains lineColor_ and text_color_, and the style +// manager gains the delete_style() function Part 3 left as a reader +// challenge, plus bounds-safe getters. +// + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +#include + +//region SStyle + +struct Srgb + +{ + + int r; + + int g; + + int b; + +}; + +struct SStyle + +{ + + unsigned long fillColor_ = 0; + + unsigned long outlineColor_ = 0; + + unsigned long lineColor_ = 0; + + unsigned long text_color_ = 0; + + unsigned long radius_ = 0; + + unsigned long lineWidth_ = 1; + + XFontStruct *font_ = nullptr; + + std::string style_name_; + +}; + +//endregion + +//region SStyle_Manager + +class SStyle_Manager + +{ + +private: + + std::vector styles_; + + Display *display_; + + Window window_; + + int screen_; + + GC gc_; + + int active_style = 0; // just set the default to first passed. + +public: + + SStyle_Manager(Display *display, Window window, int screen, GC gc) + + { + + display_ = display; + + window_ = window; + + screen_ = screen; + + gc_ = gc; + + } + + // NOTE (test agent, Nov 26 2026): do NOT XFreeFont here. Every demo + // main calls XCloseDisplay() before this destructor runs (stack + // unwinding after main returns), so XFreeFont would operate on a + // closed Display and corrupt the heap (SIGSEGV/SIGABRT inside + // XFreeFont on every clean exit). The fonts are process-lifetime + // resources; the X server reclaims them when the client disconnects. + // Use delete_style() to free a single style while the display is + // still open. + ~SStyle_Manager() + + { + + styles_.clear(); + + } + + int add_style(std::string styleName, Srgb fillColor, Srgb outlineColor, int radius, int linewidth, std::string fontName) + + { + + return add_style_ex(styleName, fillColor, outlineColor, outlineColor, Srgb{0, 0, 0}, radius, linewidth, fontName); + + } + + int add_style_ex(std::string styleName, Srgb fillColor, Srgb outlineColor, Srgb lineColor, Srgb textColor, int radius, int linewidth, std::string fontName) + + { + + SStyle nstyle; + + nstyle.style_name_ = std::move(styleName); + + nstyle.fillColor_ = get_color_Srgb(fillColor); + + nstyle.outlineColor_ = get_color_Srgb(outlineColor); + + nstyle.lineColor_ = get_color_Srgb(lineColor); + + nstyle.text_color_ = get_color_Srgb(textColor); + + nstyle.radius_ = radius; + + nstyle.lineWidth_ = linewidth; + + // Define fallback fonts to try if the requested font fails + + const char* fallbackFonts[] = { "fixed", "helvetica" }; + + size_t numFallbacks = sizeof(fallbackFonts) / sizeof(*fallbackFonts); + + nstyle.font_ = nullptr; + + // Attempt to load the primary font (if provided and not empty) + + if (!fontName.empty()) { + + nstyle.font_ = XLoadQueryFont(display_, fontName.c_str()); + + } + + // If the primary failed, iterate through fallback fonts until one succeeds or all are exhausted + + if (!nstyle.font_) { + + for (size_t i = 0; i < numFallbacks && !nstyle.font_; ++i) { + + nstyle.font_ = XLoadQueryFont(display_, fallbackFonts[i]); + + } + + } + + // Final validation: if no font loaded, report which fonts were attempted and exit + + if (!nstyle.font_) + + { + + printf("Failed to load any font (tried: "); + + if (!fontName.empty()) printf("%s", fontName.c_str()); + + for (size_t i = 0; i < numFallbacks; ++i) { + + if (!fontName.empty() || i > 0) printf(", "); + + printf("%s", fallbackFonts[i]); + + } + + printf(")\n"); + + return -1; + + } + + styles_.push_back(nstyle); + + return 0; + + } + + // The delete function Part 3 left as a challenge: remove a style + // by name, freeing its font, and keep active_style in range. + + int delete_style(std::string styleName) + + { + + for (size_t i = 0; i < styles_.size(); ++i) + + { + + if (styles_[i].style_name_ == styleName) + + { + + if (styles_[i].font_) + + { + + XFreeFont(display_, styles_[i].font_); + + } + + styles_.erase(styles_.begin() + static_cast(i)); + + if (active_style >= (int)styles_.size()) + + { + + active_style = styles_.empty() ? 0 : (int)styles_.size() - 1; + + } + + return 0; + + } + + } + + return -1; // Not found + + } + + int style_count() + + { + + return (int)styles_.size(); + + } + + unsigned long get_color_Srgb(Srgb color) + + { + + XColor xcolor; + + xcolor.red = color.r << 8; + + xcolor.green = color.g << 8; + + xcolor.blue = color.b << 8; + + if (XAllocColor(display_, DefaultColormap(display_, screen_), &xcolor)) + + { + + return xcolor.pixel; + + } else { + + printf("Failed to allocate color! RGB(%d,%d,%d)\n", color.r, color.g, color.b); + + return BlackPixel(display_, screen_); // Fallback + + } + + } + + unsigned long get_color_rgb(unsigned char r, unsigned char g, unsigned char b) + + { + + XColor xcolor; + + xcolor.red = r << 8; + + xcolor.green = g << 8; + + xcolor.blue = b << 8; + + if (XAllocColor(display_, DefaultColormap(display_, screen_), &xcolor)) + + { + + return xcolor.pixel; + + } else { + + printf("Failed to allocate color! RGB(%d,%d,%d)\n", r, g, b); + + return BlackPixel(display_, screen_); // Fallback + + } + + } + + int set_active_style_byName(std::string styleName) + + { + + auto it = std::find_if(styles_.begin(), styles_.end(), + + [&](const SStyle& s) { return s.style_name_ == styleName; }); + + if (it != styles_.end()) + + { + + active_style = static_cast(std::distance(styles_.begin(), it)); + + return active_style; + + } + + return -1; // Not found + + } + + SStyle get_active_style_byName(std::string styleName) + + { + + auto it = std::find_if(styles_.begin(), styles_.end(), + + [&](const SStyle& s) { return s.style_name_ == styleName; }); + + if (it != styles_.end()) + + { + + return *it; + + } + + return get_active_style(); + + } + + SStyle get_active_style() + + { + + if (styles_.empty()) + + { + + return SStyle{}; + + } + + if (active_style < 0 || active_style >= (int)styles_.size()) + + { + + active_style = 0; + + } + + return styles_[active_style]; + + } + + SStyle get_index_style(int index) + + { + + if (index >= 0 && index < (int)styles_.size()) + + { + + return styles_[index]; + + } + + return get_active_style(); + + } + + // Resolve the style a widget should draw with: its own named + // style when set, otherwise the manager's active style. + + SStyle resolve(const std::string& styleName) + + { + + if (!styleName.empty()) + + { + + return get_active_style_byName(styleName); + + } + + return get_active_style(); + + } + +}; + +//endregion + +//region Drawing helpers (Part 4 construction, shared by all widgets) + +namespace xf + +{ + + inline void set_line_width(Display* display, GC gc, int lineWidth) + + { + + unsigned long valuemask = GCLineWidth; + + XGCValues gc_values; + + gc_values.line_width = lineWidth; + + XChangeGC(display, gc, valuemask, &gc_values); + + } + + // The Part 4 rounded body: Rectangle A + Rectangle B + 4 fill arcs. + + inline void fill_rounded_rect(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + int r = (int)style.radius_; + + int w = x2 - x1; + + int h = y2 - y1; + + if (r <= 0 || w <= 2 * r || h <= 2 * r) + + { + + XFillRectangle(display, window, gc, x1, y1, w, h); + + return; + + } + + XSetForeground(display, gc, style.fillColor_); + + //Rectangle A + + XFillRectangle(display, window, gc, x1 + r, y1, w - 2 * r, h); + + //Rectangle B + + XFillRectangle(display, window, gc, x1, y1 + r, w, h - 2 * r); + + //region Arc C Top Left + + XFillArc(display, window, gc, x1, y1, 2 * r, 2 * r, 90 * 64, 90 * 64); + + //region Arc D Top Right + + XFillArc(display, window, gc, x2 - 2 * r, y1, 2 * r, 2 * r, 0, 90 * 64); + + //region Arc E Bottom Right + + XFillArc(display, window, gc, x2 - 2 * r, y2 - 2 * r, 2 * r, 2 * r, 270 * 64, 90 * 64); + + //region Arc F Bottom Left + + XFillArc(display, window, gc, x1, y2 - 2 * r, 2 * r, 2 * r, 180 * 64, 90 * 64); + + //endregion + + } + + // The Part 4 outline: 4 line bars + 4 draw arcs, in lineColor_. + + inline void draw_rounded_outline(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + int r = (int)style.radius_; + + XSetForeground(display, gc, style.lineColor_); + + set_line_width(display, gc, (int)style.lineWidth_); + + if (r <= 0) + + { + + XDrawRectangle(display, window, gc, x1, y1, x2 - x1, y2 - y1); + + return; + + } + + // 4 Line Bars + + XDrawLine(display, window, gc, x1 + r, y1, x2 - r, y1); + + XDrawLine(display, window, gc, x2, y1 + r, x2, y2 - r); + + XDrawLine(display, window, gc, x1, y1 + r, x1, y2 - r); + + XDrawLine(display, window, gc, x1 + r, y2, x2 - r, y2); + + // 4 Arcs + + XDrawArc(display, window, gc, x1, y1, 2 * r, 2 * r, 90 * 64, 90 * 64); + + //region Arc D Top Right + + XDrawArc(display, window, gc, x2 - 2 * r, y1, 2 * r, 2 * r, 0, 90 * 64); + + //region Arc E Bottom Right + + XDrawArc(display, window, gc, x2 - 2 * r, y2 - 2 * r, 2 * r, 2 * r, 270 * 64, 90 * 64); + + //region Arc F Bottom Left + + XDrawArc(display, window, gc, x1, y2 - 2 * r, 2 * r, 2 * r, 180 * 64, 90 * 64); + + //endregion + + } + + // Convenience: body + outline in one call, the polished look. + + inline void draw_polished(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2) + + { + + fill_rounded_rect(display, window, gc, style, x1, y1, x2, y2); + + draw_rounded_outline(display, window, gc, style, x1, y1, x2, y2); + + } + + inline int text_width(const SStyle& style, const std::string& text) + + { + + if (!style.font_) + + { + + return (int)text.size() * 8; + + } + + return XTextWidth(style.font_, text.c_str(), (int)text.length()); + + } + + // Part 4 centred text: XTextWidth + ascent / descent. + + inline void draw_text_centered(Display* display, Window window, GC gc, const SStyle& style, int x1, int y1, int x2, int y2, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + int tw = XTextWidth(style.font_, text.c_str(), (int)text.length()); + + int text_x = x1 + ((x2 - x1) - tw) / 2; + + int text_y = y1 + ((y2 - y1) + style.font_->ascent - style.font_->descent) / 2; + + XDrawString(display, window, gc, text_x, text_y, text.c_str(), (int)text.length()); + + } + + // Left-aligned text, vertically centred in the given band. + + inline void draw_text_left(Display* display, Window window, GC gc, const SStyle& style, int x, int y1, int y2, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + int text_y = y1 + ((y2 - y1) + style.font_->ascent - style.font_->descent) / 2; + + XDrawString(display, window, gc, x, text_y, text.c_str(), (int)text.length()); + + } + + // Baseline text at an explicit position (labels, grid cells...). + + inline void draw_text_at(Display* display, Window window, GC gc, const SStyle& style, int x, int baseline_y, const std::string& text) + + { + + if (!style.font_ || text.empty()) + + { + + return; + + } + + XSetFont(display, gc, style.font_->fid); + + XSetForeground(display, gc, style.text_color_); + + XDrawString(display, window, gc, x, baseline_y, text.c_str(), (int)text.length()); + + } + + inline void draw_checkmark(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + set_line_width(display, gc, 2); + + int s = size / 2; + + XDrawLine(display, window, gc, cx - s, cy, cx - s / 3, cy + s); + + XDrawLine(display, window, gc, cx - s / 3, cy + s, cx + s, cy - s); + + set_line_width(display, gc, 1); + + } + + inline void draw_triangle_down(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx - (size - i), cy + i, cx + (size - i), cy + i); + + } + + } + + inline void draw_triangle_up(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx - (size - i), cy - i, cx + (size - i), cy - i); + + } + + } + + inline void draw_triangle_right(Display* display, Window window, GC gc, unsigned long color, int cx, int cy, int size) + + { + + XSetForeground(display, gc, color); + + for (int i = 0; i < size; ++i) + + { + + XDrawLine(display, window, gc, cx + i, cy - (size - i), cx + i, cy + (size - i)); + + } + + } + +} + +//endregion + +//region Region Canvas (from Part 1) + +class Canvas + +{ + +private: + + Display* display = nullptr; + + Window window = 0; + + GC gc = nullptr; + + XImage* image = nullptr; + + int width = 0; + + int height = 0; + + std::vector pixels; + +public: + + Canvas(Display* display, Window window, GC graphicsContext, int width, int height) : display(display), window(window), gc(graphicsContext) + + { + + resize(width, height); + + } + + ~Canvas() { destroyImage(); } + + void resize(int newWidth, int newHeight) + + { + + width = std::max(1, newWidth); + + height = std::max(1, newHeight); + + pixels.resize(static_cast(width) * height); + + clear(0x202020); + + destroyImage(); + + Visual* visual = DefaultVisual(display, DefaultScreen(display)); + + int depth = DefaultDepth(display, DefaultScreen(display)); + + image = XCreateImage(display, visual, depth, ZPixmap, 0, nullptr, width, height, 32, 0); + + if (!image) + + { + + std::exit(1); + + } + + image->data = static_cast(std::malloc(image->bytes_per_line * height)); + + if (!image->data) + + { + + std::exit(1); + + } + + } + + int getWidth() const + + { + + return width; + + } + + int getHeight() const + + { + + return height; + + } + + // Direct access to the raw framebuffer. + + uint32_t* data() + + { + + return pixels.data(); + + } + + const uint32_t* data() const + + { + + return pixels.data(); + + } + + void clear(uint32_t rgb) + + { + + std::fill(pixels.begin(), pixels.end(), rgb); + + } + + void setPixel(int x, int y, uint32_t rgb) + + { + + if (x < 0 || x >= width || y < 0 || y >= height) + + { + + return; + + } + + pixels[static_cast(y) * width + x] = rgb; + + } + + uint32_t getPixel(int x, int y) const + + { + + if (x < 0 || x >= width || y < 0 || y >= height) + + { + + return 0; + + } + + return pixels[static_cast(y) * width + x]; + + } + + void fillRect(int x, int y, int w, int h, uint32_t rgb) + + { + + int x0 = std::max(0, x); + + int y0 = std::max(0, y); + + int x1 = std::min(width, x + w); + + int y1 = std::min(height, y + h); + + for (int py = y0; py < y1; ++py) + + { + + for (int px = x0; px < x1; ++px) + + { + + setPixel(px, py, rgb); + + } + + } + + } + + void drawGradient() + + { + + for (int y = 0; y < height; ++y) + + { + + for (int x = 0; x < width; ++x) + + { + + uint8_t red = static_cast(255.0 * x / std::max(1, width - 1)); + + uint8_t green = static_cast(255.0 * y / std::max(1, height - 1)); + + uint8_t blue = 100; + + uint32_t color = (static_cast(red) << 16) | (static_cast(green) << 8) | blue; + + setPixel(x, y, color); + + } + + } + + } + + void present() + + { + + for (int y = 0; y < height; ++y) + + { + + for (int x = 0; x < width; ++x) + + { + + uint32_t rgb = getPixel(x, y); + + unsigned long x11Pixel = ((rgb >> 16) & 0xff) << 16 | ((rgb >> 8) & 0xff) << 8 | (rgb & 0xff); + + XPutPixel(image, x, y, x11Pixel); + + } + + } + + XPutImage(display, window, gc, image, 0, 0, 0, 0, width, height); + + XFlush(display); + + } + +private: + + void destroyImage() + + { + + if (image) + + { + + XDestroyImage(image); + + image = nullptr; + + } + + } + +}; + +//endregion +#endif // XFRAME_CORE_H diff --git a/widgets/components/XSysBox/main_XSysBox.cpp b/widgets/components/XSysBox/main_XSysBox.cpp new file mode 100644 index 0000000..71821d8 --- /dev/null +++ b/widgets/components/XSysBox/main_XSysBox.cpp @@ -0,0 +1,69 @@ +#include "XSysBox.h" + +#include + +int main() +{ + Display* display = XOpenDisplay(nullptr); + if (!display) { return 1; } + int screen = DefaultScreen(display); + Window window = XCreateSimpleWindow(display, RootWindow(display, screen), 100, 100, 800, 600, 1, BlackPixel(display, screen), WhitePixel(display, screen)); + XStoreName(display, window, "XSysBox - XFrame demo"); + 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 widget(display, screen, window, gc, &styles, 20, 20, 720, 480); + widget.draw(); + bool running = true; + while (running) + { + XEvent event; + XNextEvent(display, &event); + bool handled = false; + switch (event.type) + { + case Expose: + handled = true; + break; + case ButtonPress: + handled = widget.on_button_press(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case ButtonRelease: + handled = widget.on_button_release(event.xbutton.x, event.xbutton.y, event.xbutton.button); + break; + case MotionNotify: + handled = widget.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 = widget.on_key(key, buffer); + break; + } + case ClientMessage: + if ((Atom)event.xclient.data.l[0] == deleteWindowMessage) + { + running = false; + } + break; + } + if (handled) + { + XClearWindow(display, window); + widget.draw(); + XFlush(display); + } + } + + XCloseDisplay(display); + return 0; +} diff --git a/widgets/screenshots/XLoadBadge.png b/widgets/screenshots/XLoadBadge.png new file mode 100644 index 0000000000000000000000000000000000000000..895c2b20ad8922e985f2ac2dde3ba03d2a79819f GIT binary patch literal 1138 zcmeAS@N?(olHy`uVBq!ia0y~yU{+vYV2WU328ukF$a)N<6p}rHd>I(3)EF2VS{N99 z0fib~Fff!FFfhDIU|_JC!N4G1FlSew4N!t7z$e7@|Ns9sHZ}~a+(bk~)Mgu;1&T8! zdAqwXbg;^L0Bv~T>EaktaqI0(|E$MW3Gke(ITWc;07ozTdAeO{y0^Un`z6Q~MM11Suv) z1lXComHXj_SIbZDF4`LZ{#>)~=Ji~c-kjPpd-uytK7088RvN5%KWmjjD(ftR^OdHK z%eKy7KfKWB#4E0u+ppR0{`}z8doIOx?Uem?bIyAN-vBHh zfBojR(5BvG%bF>)uPX3=W)>W_VxwCxRZn=3cH=oasG*Vf9U77XLJ_n~= zds&~@D!vt;5W|{M&0|=$e0t=uqEo?A?c7VOb3dQe?QYpqt}qJ74T1a7Hmzn;(kmtH zdw0fpNUI-SX5%z(eyMZYrxTV(thR?`%|FF;ByWaAu8rN(ZDA)2MV+;FugbkWp?_tR zhu(zBU&2dxmUM-yv`Jp-HFY+7+Os9`Uewcx#ivT0b}jVTBfb2v-Ls+_+jjI(3)EF2VS{N99 z0fib~Fff!FFfhDIU|_JC!N4G1FlSew4N!tJz$e7@|Ns9CtK39HM6Nzkk(p_zflvP@B&tr(TgM{w_>;<0 z?V4pfBM#sAyXUp-3HP4Ix+-r!tqnL~aIZBd$>xqj(A^2ouK3-5ImOAibzj8nVuL$WlFAXQh&{QBK2nQ{OPrJ z=Z?yGe*enw>AP&CR{6ShwtqKWzx3<>)+}z8AqSq=PKZcU2z3Ca0o;h+ih1wf&A~nM Sv@QaZ9fPNW-zd|dv5S+9!cBpCX&=zi4F`2Sp=UY9<T*zNolJmE&mg`6Kb+8#S)o3|WRk!lm0y~B8d&6}AD zxz*cU9z7}RSGX&)%HN_baf?33rT2atYdR161*AV*=XlDU(Q~(AB-fI7o|QG6+e?M4 zC+j-CJ2yelLoQt|XRcRt-zVi0Z;YRqL@EBfw48srTNc|~-f$0N#!UHj6KWe{e&x@% za<-bNd19ONwd4uAja80`v%XY)lydr3+2LPYDqr+Byz;5CobWK<{P{O!E0#}jy7g(# zndIM0OQO$PTV475!|$`B-5!3)?X#a-FlN4sU-^E!>b;fkx3Ry!5-)V*{iZUuq8Wc| zhe|XeQe33q6JT~~W@X`+aMWRon?nN!hrn8fq66WP&tD&`VrF7Fk)$x)IrrFduA()} zL(F^b43-_IC7%2_q_BKjZa-Vm-e>8*Rw>*}-c)|HRx{=m!*Ye^JVkq@-yKg}KIQ+? z2|;J#ukHL(8B)0_<}<_nS+9L2Y;sWZeq!ixLukpJKF6-(8(i#IIlrc--YHybSys1N z=zL>H|3{&l54mue->9iRlcgLuw?7beGHz4 ze9XDW7U#d3enWS{clmXkpT606$kojZU3q)nuj++<8*ZH6vE$dZuP=CZHa-bFTDM@M zbm9wliCyPI#66=jZ!mk@3|MdM=(kvO68rqN%+PmJKW~_yBiXsMYfi z`Z5iX8K6!LW3h~4eUiWkDyRk40!#0xCszNjY0dlruarn-{I9HMc;|Fc!uI-|a9{<= N;OXk;vd$@?2>{;3d3XQ- literal 0 HcmV?d00001 diff --git a/widgets/screenshots/XProcessList.png b/widgets/screenshots/XProcessList.png new file mode 100644 index 0000000000000000000000000000000000000000..37e2238b96cfc89c5c10ac8bba3bc6df49ca4bde GIT binary patch literal 7539 zcmch6cT`hfw`~ewXeKlPDM=_Q(gf+nL;?~(k=~^IXwpO!0Z}Q50tt!`kSZO6bP$j( zq6ip@Ad#X}2_Vu0X(2RUeBXWFz3;vI_Z?%8amLu^?0xoFd#yFsoGaGM)R3FwBnJQh z;5NeQTL1uH1ONcCV*xW~yzO!^%m>09YjFhtxPJ-&z(oN7d(0`^PXHkJ900KN4*;N* z4gd(=%4#;(VZIPByKH6f=g%J<9UXpt{^jN6)YMc!s?P52?wySWGV=qVn~9-5fXFI_ z1pq|98|h!P3LRU`o=Kc-Oo|eI^-u0G$Qd>^Z?Q(W*E<8lpTao!)^rqKDIjUdCc(_0 z24bGka{hUaPb}gBD9<5<;)+8+9{xAuXY>&`<7ci7;!hUcVc8e}LcnkUhzCfDB|-d} zJW8XDhqP~!j~ZPv{h$q4^u~FJMx00VN57x+s$5?I`U)X1tVu0Y)Kk#ttFbB~)DomWjNOp1C?Vou&;7C91RE6&fYCQ5Qo4D!QF0FnM zFn7NxnXLibO=#>(EAakdQGsMYD!fugNq*w3cNP9@MAEAi1!rCFlw4`-**gtxy7j2a z-w%7CjT7Dtq7;f>Jg2u&B@97ofv|65^e)tU_cWtaZ#`m_BPN&*QaC92~Yi+40YSH6jflFi*XLjPJOdYc!yyeZg7 z4z`}mucFpX**ealca73^=*Xacx_0`#{xK+J$!C`D^$!(!yvo!|03;%HI1#{t&f~@( zD88;eI+j~<_cY+17ov`q=r`rp8q!i6wL#+jXa3@%N4UbT06C|(lW%|hd7wq^2b=*} z_&yXHeZO-{Mons3UfE8(33X{%33?X3G6Y+S_b9zf-3ndrhD?0Mu zYJm*bsFk)DoxSkq_|M_rRP^G(dZ(1%>iIr8X`1JScsmuUGVM9Y+MVWgi(qR1LB%tr z>bcE%XHY8yGU+MsSR0I-uJ{>F^dfqU_CSwb8DzsqW!mcbDnC;m!gq~6q2KD|(UG-= z{#OxWi~?u>Y###KB-YRQK_udt$QB>2eDny705JFwlCNw3T38-WhJ92T#6V`#+wPa_ zuKr#S6qC)JKMWfe32V7s6m`&di0_!FC&2?nEWapefhDw*6Cv)8ON1$-t8HKI_~Rz* z`4Q$r;5Cl9h*DleAkd2#_hyWKSlT>ykhl=Yudp<;qq;;O?kf0^P36%A5J&>)_X_$b9TXczrg(j2 z@yMz9Ss!OeJWQ?w@OBRHgJb2xYgtKVzj}SRg|D5U%opT%yUL%hRVCFFPe&T%P&Uh1 zD1vZF;R@tuSwurvTH)I%0Qo!+fJoWSsUBA}MEK4s z9nf&Cq}n_U{1|uHApOhE(MHbQk02f=Ocrp)@xh6YtIIo)2bWhn&$7`ROU_hG#Rg+$ zZlI6O!NS=$Q8fYNcj8xHG%lX~p8cm*^O3Px30Iw+>h60Mim04a@2xzu0@2N(u*D2; z{>kNc=2l0lBnMd(>$5?jGMlGr30-W@A4Nw=MxDho-Gr|JWYNbv(dm!A?k?eFciTP` zF`ba2=~{12EZXrLG3F%Q+E-?wl{O3wn+gDB)SfI&XiJk&FVQ-=lS5rUWc<$D^SH@o z;V5{?a)g%Wyrm-m3kXg#aBu$v)UAR#q*^TP9YpRt`((piW)RLJCw2Zv{`oXyu1xg9 zk8sEqFGYUZlOa$AKxDHsv)&j7yOg>hU5+2#PDSz#4 z%?ksclG)WRT@S0KbgCCn#(G~FF$jaC{4nuV;>2Z;MBxAM)Z36SC zu7#LHx5O4cT_#J*$JOm+<2q7fNN|UjB3Y#fT*otJeD4kxT zv))^MV(tku0}2nL$&%!L>j0qC32jiMkEWgtAh8nJg-!xtRjUUd*ZdR->FhjAEIveT zw3MUj2v(=1-}W|NRZ#|qh?YrKJF0M&UGk6&;Fz7V*vHJRVNOL7OY>9LhA^Qu{lb@R z>zl9I&+hMHE&wRTGhX&gkAbi#|Anj9VlZ{e(s0`JKDqDPW(56kC;r*Z&T!fJETlJQ ztp~atAq%|Ii)x1Z2)s@HB4T`Ln$tv#Fz&yq&?$jvDyGU95+6`Sa{MCX3B;dvR>o(S zzKxfLPtQww#Ao^FzR;|Ep+PG}b6Tq ztK&pe1bBpwNq))mlX zKcW(B(ZA}NR`GQJKVoKAk>1uz-NW$TwU1<3R9wbu><}ml)%VQd-m1`HI^XWn`A|6X zACzeVeXfbJYOhFD2I%eW ze9dr^aq-o)EbTq%zlm8ApEa+)xUH!BXMME0fMAeja&A2+;cfJ(HggfuE4MD{96=7JGGU|xGP)ybm~}}c(nW`pRK;9e zZuodR|8|dqhghNKG{3BKaBBvDr!_+K1sArQua=#pJV-vL1}ZU;2@aoA;c>uyh1WO^ zEh>Jvup<^80cl%I(ib1RU5-O5;ty<5EHybE!jL~D{9)|aG5 z`{`@C@}xD}ski8thg+F=7Tex?V8M35vPAsGG8J^~UPxDR0fJ1mRSU4~v{0_+8B^8w1~^8_wY(E>JJ zajHJRLkxyu_&coCK(OhL95vc{7(TpKssUBhT>^?@jC8y}8&e=-7Ly=vG%K*~M*cd%5L8w3H^utA6PKx3iMXH=KA z*QC(tsoP^wF2g2|3hM7Bv^ve&sY_3b5zBwmvMY+*hWS-1MCKUC7RTaNP~zI*1@1FW z1}k~r;#>mB9}h^0#s-#85$td1+O1NfsWxA*x9?mIp2PTdX5-wVxHVtl-S z59@`_Xy)mY%&w-rVLgg5yCT`CA;>{0vC)oye1CCJ8AS%ghACJ0ASj{W$h*H%KO0?`1 zC?f}M@v_|NheXcAN8g0ZE0SALTAuPavbkWldm3EX$s&Y-bVJ;Uz&Z`}wtZ5(q?Tj$ zK->CscTgMeOGG+NHW94U19csB5&GoVc>mL*^p?~7UgOhKK5zXV7d8)mJoq`c0Kp$; zWm?n-k7oOqhtay1kmd6)rkgCf6^|6GWDVL{XWvlaZmIF_JX@0#Yo<*y!)^^$T_9VG#5M$iD$=jU|dBlcq6G zDBj+`W4TiDs2x)gI&SHlLyJKdC&|k{nH)>W0mMq!WS-#|@4E0oE>BodCJZCP(*k8IH zn)&EP}B&?L!!CiR3Xc-l4k zl^{tVgvDG-kB;tom&jii7JeW?YMh2tv$@Sfg-t@tm$U!L{$6_8|9MR+n#$GT7S-W| zx7){P2l(N$ZlX%)-YOh!ogB#Y*Dae@jx#N zrRwf;8PVzBY<5&H2DQ*VZ$ZP_Fo zU!2WQVsanz>q<}oRozw$$v=($5vQpUby;%Ctw)P#zXaDo zSjaF>lBB>1HZg_zkfX)MZ_Y*AgyaL*d*SGBJj!xnmcuHvr?{*kW;hsrgJei_h~uN~ z0;#pA4wC>&#z@a6v~jbEGe+Z5hd6QWR_i|I;Slf7#}~5&gO86uCTtvrzBN{B^Xy_y zYA=>M+=&Kay|lO>EQliTLnvuI7KsB;t^s8jNXN#n&W%|o+S*c#D;j^w%q#iEt5Y^H zJs~qS@-f;ViKWw1aW3`SHNg>((FKcGX`D`Y`0N2pIc7asK;*f@GNVX?0&?lKG2^pBvNmhsJn{70z;gLokRQci8e zFkJ6Xi#$i^M8n7axu&mI&AwV<%rBo6^ph#s+~BQoxqmcM#a&FP%wO4{WDYg7=)QEb zUTq@cb_1oZZZB&gjwQf^VJ;z;0*)yMKMvU;fq&OU46946^4rGD@>=a#A0Xe3>Br

ffu0x})u{Afkxn0|OT9qT`bxeP2&kgZq&le=67|V*KaLz4z zr4%OTm_NkmsXpCxZ7NO^B|B}%Gl5L27ZDC`SC>D5&iiq@@B}XC)W2x@E`U8(2n4~s z;Ugg?Im7I!TjV~?5#8_yDU={{(`S(lW8bv8Ji3SZda)2xFy8K58$-eM9g(7=@x>c7 zo-1zeY`*aXNp|>h8*T7d93ZFhQ5+tT;>)-*qcA>%`El~~=FTM(x!7~AJfd!#`qYSd zr__V%_Uuuk3ul&MHJH)v7GN|NdGax)Qt67 zx&K;TV81LiY|}wH8!=eCVBp$1{vf*z2hNPz?S0z2;xM2~Xc_lLDN%M_LJ5zgwA*nRl38m)V zngM9_CV_Z|YvBMUeo~V=&L)V1we}v|Ef>Baend(zDz-bn0wkZe2jKy86Xlt9N+{|}zPX7R8LDf#fTP6sWm4&xaGFgV zsey&oq|!8g0MiT^)0wyu537ka<1Uep7GNNSu?-9)?SsyZE>sI|7W{JEDOAV8s|@E& z48LSlrcfhDcg=q-s_n$B8HbSiNK)14+1I&%8Y( z%rBC-^)@Z%oVK3E`R*`tmozx-YAT$D8i;xIl}BumSmjreqTiv_ zXY1o9o+beg+TlqB2Gw;u+-2w>_x{Ca)Kk|mG_O5Ovh3`uw#7~NL*#XI zM;Xm<57QU6G#)~=FBISS{~Q%Y44a-Zx|*}#$9=8JVu4^c>}lSef1D^Aob~G%?2DP8 zJ&K8@ac2eZsJ=WWHZzlOQ*3YwuiXS`)b!d;d11$If9+^O6a$IW!;@chL&ej=jBe5m#nbO z1K4)^8BqI_1v|-Y@Bv853gP-^%w_)oc4)AZup-jR=VkUOwSFmcJ=x(s1Nq?}T|FtR z4RbRf&w@}t1KFv;(^&VqcpRwI53SxP379bQN^$w^2w1#K05Ux}=s#~j7O~Mf?e!jB zqf6@Ww*oTC4@lnDho-C<#)#ZxxEq9pfs{v=A0Qvh5rhDmOmxNi4PQGzvYj9CIYgRs zk>A6q+N7LABnLwA5P5=b$Hdw7Jy8SQ-^>yZ%yL9ZHPtE`8WmzD$pbmgzk1w%)P>-{ zww9?WI885C7qhxH0jCuXYcpNnzc)BZtm1AYMGG--i0s<7Z=YJ7fa?u%*H}=~j$oV! z=Kgd2Tht}G?Gfye>Kv1cE$|IY_ZPp1)95Y+rpwSou+KVyD;vD{4KC3~_1++)gA+xW zJ7Vo~J=4C*Mq*#tVZK9smdLffpGe0@{Xs&^n$7eW3mh@ShXDXdcR_m5SCp>l235uv zrp)A(@?~U7L=|rm&Rh*Jvr_mG#&W*~LOx~_#mlvH!Q_r3*+X%5EfAEKE{;74UyJAy(!FueIEtGo35N z6LX%Hj%It9EiV}m&CfOG`>g=(j)mi?DI+Nba+zaOygR>@(8V@SU@4nVvh&5uY*!%s(y!rS0fh!Y_+J?ia-E8L5BTt6eu$sq4C%PQX#IE1D zL=n$EH`Kh~xU_b81yZS&pL~B*;#S*kZ_xl}I5=akN{kZJ)qj=y?VavU3M=Iw@La^_ z36|36e)ngh<=c9UV2|Hzcey>xKr`s-J>Hy{SZNaVJtl*^6x=B$el&KV4xo-je@UJ{ zn2X`smi0qKGz&ez;Kt0whTyUGPe5Y6+m z!Zzt}e_7Y#PpaJ8ZVNE8rDP;TP^PN6;LCvJFZIu!ol|R?MbrZV(Gl1xNe^}pvhjj2MOof80P%x?3sHTmUj`(-!mVRKCOBY)dLmXUz@f_j%=st? z-nu{V9yt-JaNa5E@e1Z*2HYZ~>~5v5#QQP0UjO5KMtN#TR1M#oS*Kl1(~er}M~`cc ztr4=9*y|X`NwAEK;QJZ9+fldlF79|YHccG@iElnTf;{Y4_0N6z24)W{8a9op+&9zm z+y#1k9|gn2U;}g>-Vet=-MQBj_(W!T=7txd<%)gJ0c=Oi1jnQ@jhkb(UGE0+1X??Y zjm^b`bDmFLwcKbhaAL|gMPSB0ss9kE*!3*h^`nEIsdprzs>`y?<|*bi>Mz}6{V#M+ z$_Rs@e)v!x>ZtXw{LnX&Xzv&;ie2I$t~_Syd`XpRoM>k?7yI$DiE-z*F+{vn=?qyk zerfO(vksvX(t$k3JCaNii)dDN*9X+fy<3FaT2UPw#Mgp%d^T4wSSwF4{tR&0U-lJ; zle%|R?kO_MGI5FvJK%>+rt6n(b`HtR5M_GD;BG-_0%Yrz*S>u=BwkTBIER`8bIlY1 zU9p*J!Gyhk^+x`ci|fI(V!gIWI4$SM>47|BFwi*@?&fZoXW?Yzzjg<4gm#HvaagBW ze6%QE8mhCGKsXAoN?&`5VzgjPW}{lQLuZE1uNGIf{`8O}18%Q+bwn3=+TPy5eFYEh zVIag?m$#31KrLf8dt2A;J{y520#cX^6j0{ZmEA{?)_`d+JqPdX( literal 0 HcmV?d00001 diff --git a/widgets/screenshots/XSysBox.png b/widgets/screenshots/XSysBox.png new file mode 100644 index 0000000000000000000000000000000000000000..d7377306c239506933aa06853418facd2748dd59 GIT binary patch literal 3606 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 S{8quk46rb>p;sL^n($ADFyJ2m literal 0 HcmV?d00001