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.
This commit is contained in:
1 parent
4488d527a5
commit
8c295dbbdf
30 files changed
+7161
No files matched your search
@@ -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 <cstdio>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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 <cstdio>
|
||||
|
||||
#include <ctime>
|
||||
#include <unistd.h>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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 <cstdio>
|
||||
|
||||
#include <ctime>
|
||||
#include <unistd.h>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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 <cstdio>
|
||||
|
||||
#include <ctime>
|
||||
#include <unistd.h>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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 <cstdio>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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)
|
||||
@@ -0,0 +1,219 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/xframe_core.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <fstream>
|
||||
#include <string>
|
||||
#include <unistd.h>
|
||||
|
||||
// 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;
|
||||
}
|
||||
};
|
||||
@@ -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 <X11/Xlib.h>
|
||||
|
||||
#include <X11/Xutil.h>
|
||||
|
||||
#include <X11/keysym.h>
|
||||
|
||||
#include <X11/fonts/font.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <vector>
|
||||
|
||||
//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<SStyle> 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<long>(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<int>(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<uint32_t> 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<size_t>(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<char*>(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<size_t>(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<size_t>(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<uint8_t>(255.0 * x / std::max(1, width - 1));
|
||||
|
||||
uint8_t green = static_cast<uint8_t>(255.0 * y / std::max(1, height - 1));
|
||||
|
||||
uint8_t blue = 100;
|
||||
|
||||
uint32_t color = (static_cast<uint32_t>(red) << 16) | (static_cast<uint32_t>(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
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "XLoadBadge.h"
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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)
|
||||
@@ -0,0 +1,313 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/xframe_core.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <dirent.h>
|
||||
#include <string>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/types.h>
|
||||
#include <netinet/in.h>
|
||||
#include <arpa/inet.h>
|
||||
#include <unistd.h>
|
||||
|
||||
// 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;
|
||||
}
|
||||
};
|
||||
@@ -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 <X11/Xlib.h>
|
||||
|
||||
#include <X11/Xutil.h>
|
||||
|
||||
#include <X11/keysym.h>
|
||||
|
||||
#include <X11/fonts/font.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <vector>
|
||||
|
||||
//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<SStyle> 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<long>(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<int>(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<uint32_t> 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<size_t>(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<char*>(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<size_t>(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<size_t>(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<uint8_t>(255.0 * x / std::max(1, width - 1));
|
||||
|
||||
uint8_t green = static_cast<uint8_t>(255.0 * y / std::max(1, height - 1));
|
||||
|
||||
uint8_t blue = 100;
|
||||
|
||||
uint32_t color = (static_cast<uint32_t>(red) << 16) | (static_cast<uint32_t>(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
|
||||
@@ -0,0 +1,88 @@
|
||||
#include "XMonitorButton.h"
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
#include <ctime>
|
||||
#include <unistd.h>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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)
|
||||
@@ -0,0 +1,193 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/xframe_core.h"
|
||||
#include "../XMonitorButton/XMonitorButton.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// 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<XMonitorButton> 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;
|
||||
}
|
||||
};
|
||||
@@ -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 <X11/Xlib.h>
|
||||
|
||||
#include <X11/Xutil.h>
|
||||
|
||||
#include <X11/keysym.h>
|
||||
|
||||
#include <X11/fonts/font.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <vector>
|
||||
|
||||
//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<SStyle> 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<long>(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<int>(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<uint32_t> 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<size_t>(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<char*>(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<size_t>(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<size_t>(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<uint8_t>(255.0 * x / std::max(1, width - 1));
|
||||
|
||||
uint8_t green = static_cast<uint8_t>(255.0 * y / std::max(1, height - 1));
|
||||
|
||||
uint8_t blue = 100;
|
||||
|
||||
uint32_t color = (static_cast<uint32_t>(red) << 16) | (static_cast<uint32_t>(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
|
||||
@@ -0,0 +1,88 @@
|
||||
#include "XMonitorGrid.h"
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
#include <ctime>
|
||||
#include <unistd.h>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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)
|
||||
@@ -0,0 +1,370 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/xframe_core.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <dirent.h>
|
||||
#include <fstream>
|
||||
#include <pwd.h>
|
||||
#include <string>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <vector>
|
||||
|
||||
// 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<Row> 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/<pid>/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;
|
||||
}
|
||||
};
|
||||
@@ -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 <X11/Xlib.h>
|
||||
|
||||
#include <X11/Xutil.h>
|
||||
|
||||
#include <X11/keysym.h>
|
||||
|
||||
#include <X11/fonts/font.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <vector>
|
||||
|
||||
//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<SStyle> 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<long>(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<int>(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<uint32_t> 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<size_t>(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<char*>(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<size_t>(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<size_t>(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<uint8_t>(255.0 * x / std::max(1, width - 1));
|
||||
|
||||
uint8_t green = static_cast<uint8_t>(255.0 * y / std::max(1, height - 1));
|
||||
|
||||
uint8_t blue = 100;
|
||||
|
||||
uint32_t color = (static_cast<uint32_t>(red) << 16) | (static_cast<uint32_t>(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
|
||||
@@ -0,0 +1,88 @@
|
||||
#include "XProcessList.h"
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
#include <ctime>
|
||||
#include <unistd.h>
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -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)
|
||||
@@ -0,0 +1,237 @@
|
||||
#pragma once
|
||||
|
||||
#include "core/xframe_core.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
// 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<double> 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<XPoint> 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;
|
||||
}
|
||||
};
|
||||
@@ -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 <X11/Xlib.h>
|
||||
|
||||
#include <X11/Xutil.h>
|
||||
|
||||
#include <X11/keysym.h>
|
||||
|
||||
#include <X11/fonts/font.h>
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <cstdlib>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
#include <functional>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <string>
|
||||
|
||||
#include <vector>
|
||||
|
||||
//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<SStyle> 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<long>(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<int>(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<uint32_t> 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<size_t>(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<char*>(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<size_t>(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<size_t>(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<uint8_t>(255.0 * x / std::max(1, width - 1));
|
||||
|
||||
uint8_t green = static_cast<uint8_t>(255.0 * y / std::max(1, height - 1));
|
||||
|
||||
uint8_t blue = 100;
|
||||
|
||||
uint32_t color = (static_cast<uint32_t>(red) << 16) | (static_cast<uint32_t>(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
|
||||
@@ -0,0 +1,69 @@
|
||||
#include "XSysBox.h"
|
||||
|
||||
#include <cstdio>
|
||||
|
||||
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;
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 1.1 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 741 B |
Binary file not shown.
|
After Width: | Height: | Size: 1.9 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 7.4 KiB |
Binary file not shown.
|
After Width: | Height: | Size: 3.5 KiB |
Reference in new issue
Block a user