XFrame monitor widgets: XUsageBar, XStackBar, XVerticalBar, XTachometer, XArcGauge, XDonutGauge

Gradient usage bar (green->yellow->red, absolute scale), stacked
multi-segment bar with legend, vertical bar meter with scale +
peak-hold marker (click resets peak), round tachometer (needle,
redline zone, digital readout, peak hold), semicircular arc gauge
with coloured zones + big %, circular progress ring with centred %.
Header-only C++, demo mains, examples, Xvfb screenshots.
This commit is contained in:
Muse committed 2026-12-05 16:49:02 +00:00
1 parent f5b3b73e58
commit 4488d527a5
36 files changed
+8037

No files matched your search

+73
View File
@@ -0,0 +1,73 @@
// XArcGauge -- minimal XFrame example (C++).
// Build: g++ -std=c++17 -I/root/lg-review/lightgui/widgets/components -I/root/lg-review/lightgui/widgets XArcGauge.cpp -lX11 -o XArcGauge
// Run under Xvfb (or a real X server); close the window to quit.
// Setup mirrors the widget's own demo in widgets/components/XArcGauge/main_XArcGauge.cpp.
#include "XArcGauge/XArcGauge.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, "XArcGauge - 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");
XArcGauge 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;
}
+73
View File
@@ -0,0 +1,73 @@
// XDonutGauge -- minimal XFrame example (C++).
// Build: g++ -std=c++17 -I/root/lg-review/lightgui/widgets/components -I/root/lg-review/lightgui/widgets XDonutGauge.cpp -lX11 -o XDonutGauge
// Run under Xvfb (or a real X server); close the window to quit.
// Setup mirrors the widget's own demo in widgets/components/XDonutGauge/main_XDonutGauge.cpp.
#include "XDonutGauge/XDonutGauge.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, "XDonutGauge - 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");
XDonutGauge 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;
}
+73
View File
@@ -0,0 +1,73 @@
// XStackBar -- minimal XFrame example (C++).
// Build: g++ -std=c++17 -I/root/lg-review/lightgui/widgets/components -I/root/lg-review/lightgui/widgets XStackBar.cpp -lX11 -o XStackBar
// Run under Xvfb (or a real X server); close the window to quit.
// Setup mirrors the widget's own demo in widgets/components/XStackBar/main_XStackBar.cpp.
#include "XStackBar/XStackBar.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, "XStackBar - 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");
XStackBar 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;
}
+73
View File
@@ -0,0 +1,73 @@
// XTachometer -- minimal XFrame example (C++).
// Build: g++ -std=c++17 -I/root/lg-review/lightgui/widgets/components -I/root/lg-review/lightgui/widgets XTachometer.cpp -lX11 -o XTachometer
// Run under Xvfb (or a real X server); close the window to quit.
// Setup mirrors the widget's own demo in widgets/components/XTachometer/main_XTachometer.cpp.
#include "XTachometer/XTachometer.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, "XTachometer - 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");
XTachometer 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;
}
+73
View File
@@ -0,0 +1,73 @@
// XUsageBar -- minimal XFrame example (C++).
// Build: g++ -std=c++17 -I/root/lg-review/lightgui/widgets/components -I/root/lg-review/lightgui/widgets XUsageBar.cpp -lX11 -o XUsageBar
// Run under Xvfb (or a real X server); close the window to quit.
// Setup mirrors the widget's own demo in widgets/components/XUsageBar/main_XUsageBar.cpp.
#include "XUsageBar/XUsageBar.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, "XUsageBar - 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");
XUsageBar 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;
}
+73
View File
@@ -0,0 +1,73 @@
// XVerticalBar -- minimal XFrame example (C++).
// Build: g++ -std=c++17 -I/root/lg-review/lightgui/widgets/components -I/root/lg-review/lightgui/widgets XVerticalBar.cpp -lX11 -o XVerticalBar
// Run under Xvfb (or a real X server); close the window to quit.
// Setup mirrors the widget's own demo in widgets/components/XVerticalBar/main_XVerticalBar.cpp.
#include "XVerticalBar/XVerticalBar.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, "XVerticalBar - 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");
XVerticalBar 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(XArcGauge LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(X11 REQUIRED)
add_executable(XArcGauge main_XArcGauge.cpp)
target_link_libraries(XArcGauge PRIVATE X11::X11)
+188
View File
@@ -0,0 +1,188 @@
#pragma once
#include "core/xframe_core.h"
#include <algorithm>
#include <cstdio>
#include <string>
// region XArcGauge
//
// A semicircular arc gauge: a 180-degree arc with green / yellow / red
// zones and a big % in the middle. Raw X11, same as the rest of the
// XFrame set.
//
class XArcGauge
{
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 value_ = 0.0; // 0..100
double warn_ = 70.0; // yellow starts here
double crit_ = 90.0; // red starts here
Srgb white_color_{255, 255, 255};
Srgb axis_color_{150, 150, 150};
Srgb track_color_{230, 230, 230};
Srgb ok_color_{0, 170, 70};
Srgb warn_color_{220, 180, 0};
Srgb crit_color_{210, 40, 30};
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());
}
// Draw a thick arc slice of the upper semicircle: frac0..frac1 in 0..1.
void arc_slice(int cx, int cy, int r, double frac0, double frac1, Srgb color, int width)
{
// X angles: 0 deg = 3 o'clock, counter-clockwise. Upper semicircle
// runs 0..180 deg; value 0 is at 180 deg (9 o'clock), 1 at 0 deg.
int a1 = (int)((1.0 - frac1) * 180.0 * 64);
int a0 = (int)((1.0 - frac0) * 180.0 * 64);
XSetForeground(display_, gc_, col(color));
xf::set_line_width(display_, gc_, width);
XDrawArc(display_, window_, gc_, cx - r, cy - r, 2 * r, 2 * r, a1, a0 - a1);
xf::set_line_width(display_, gc_, 1);
}
public:
XArcGauge(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_ = "XArcGauge";
text_ = "CPU";
value_ = 63.0;
}
~XArcGauge() {}
// 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 Gauge API
void set_value(double v) { value_ = std::max(0.0, std::min(100.0, v)); }
double get_value() { return value_; }
void set_zones(double warn, double crit)
{
warn_ = std::max(0.0, std::min(100.0, warn));
crit_ = std::max(warn_, std::min(100.0, crit));
}
// 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;
int r = std::max(20, std::min(w / 2 - 20, h - title_h - 40));
int cx = x1_ + w / 2;
int cy = y1_ + title_h + r + 8;
double frac = value_ / 100.0;
double wfrac = warn_ / 100.0;
double cfrac = crit_ / 100.0;
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
// Track + coloured zones.
arc_slice(cx, cy, r, 0.0, 1.0, track_color_, 14);
arc_slice(cx, cy, r, 0.0, std::min(frac, wfrac), ok_color_, 14);
if (frac > wfrac) arc_slice(cx, cy, r, wfrac, std::min(frac, cfrac), warn_color_, 14);
if (frac > cfrac) arc_slice(cx, cy, r, cfrac, frac, crit_color_, 14);
// End labels 0 / 100.
draw_text_color(cx - r - 4, cy + 16, "0", Srgb{100, 100, 100});
draw_text_color(cx + r - 12, cy + 16, "100", Srgb{100, 100, 100});
// Big % in the middle.
char pct[16];
std::snprintf(pct, sizeof(pct), "%.0f%%", value_);
Srgb pct_color = value_ >= crit_ ? crit_color_ : (value_ >= warn_ ? warn_color_ : ok_color_);
int pw = xf::text_width(st, pct);
// Fake a larger readout by drawing the text twice offset by 1 px.
draw_text_color(cx - pw / 2, cy - 8, pct, pct_color);
draw_text_color(cx - pw / 2 + 1, cy - 8, pct, pct_color);
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 "XArcGauge.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, "XArcGauge - 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");
XArcGauge 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(XDonutGauge LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(X11 REQUIRED)
add_executable(XDonutGauge main_XDonutGauge.cpp)
target_link_libraries(XDonutGauge PRIVATE X11::X11)
@@ -0,0 +1,164 @@
#pragma once
#include "core/xframe_core.h"
#include <algorithm>
#include <cstdio>
#include <string>
// region XDonutGauge
//
// A circular progress ring: a thick arc proportional to the value with
// the % centred inside. Raw X11, same as the rest of the XFrame set.
//
class XDonutGauge
{
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 value_ = 0.0; // 0..100
Srgb ring_color_{0, 150, 200};
Srgb white_color_{255, 255, 255};
Srgb track_color_{230, 230, 230};
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());
}
public:
XDonutGauge(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_ = "XDonutGauge";
text_ = "Disk";
value_ = 47.0;
}
~XDonutGauge() {}
// 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 Gauge API
void set_value(double v) { value_ = std::max(0.0, std::min(100.0, v)); }
double get_value() { return value_; }
void set_ring_color(Srgb c) { ring_color_ = c; }
void set_ring_color_rgb(int r, int g, int b) { ring_color_ = Srgb{r, g, b}; }
// 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;
int cx = x1_ + w / 2;
int cy = y1_ + title_h + (h - title_h) / 2;
int r = std::max(16, std::min(w, h - title_h - 8) / 2 - 10);
const int thick = 16;
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
// Track ring (full circle) + value arc from the top, clockwise.
XSetForeground(display_, gc_, col(track_color_));
xf::set_line_width(display_, gc_, thick);
XDrawArc(display_, window_, gc_, cx - r, cy - r, 2 * r, 2 * r, 0, 360 * 64);
XSetForeground(display_, gc_, col(ring_color_));
int sweep = (int)(value_ / 100.0 * 360.0 * 64);
if (sweep > 0)
XDrawArc(display_, window_, gc_, cx - r, cy - r, 2 * r, 2 * r, 90 * 64, -sweep);
xf::set_line_width(display_, gc_, 1);
// % centred, label under it.
char pct[16];
std::snprintf(pct, sizeof(pct), "%.0f%%", value_);
int pw = xf::text_width(st, pct);
draw_text_color(cx - pw / 2, cy + 4, pct, ring_color_);
draw_text_color(cx - pw / 2 + 1, cy + 4, pct, ring_color_);
int tw = xf::text_width(st, text_);
xf::draw_text_at(display_, window_, gc_, st, cx - tw / 2, cy + 22, text_);
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 "XDonutGauge.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, "XDonutGauge - 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");
XDonutGauge 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(XStackBar LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(X11 REQUIRED)
add_executable(XStackBar main_XStackBar.cpp)
target_link_libraries(XStackBar PRIVATE X11::X11)
+209
View File
@@ -0,0 +1,209 @@
#pragma once
#include "core/xframe_core.h"
#include <algorithm>
#include <cstdio>
#include <string>
#include <vector>
// region XStackBar
//
// A stacked multi-segment horizontal bar (used / cached / buffers / free)
// with a legend underneath: each segment's label, value and share.
// Raw X11, same as the rest of the XFrame set.
//
class XStackBar
{
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 Segment
{
std::string name;
double value = 0.0;
Srgb color{0, 150, 60};
};
std::vector<Segment> segments_;
Srgb white_color_{255, 255, 255};
Srgb axis_color_{150, 150, 150};
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;
}
public:
XStackBar(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_ = "XStackBar";
text_ = "Memory";
// Preload a demo: a 30 GiB box split used / cached / buffers / free.
add_segment("Used", 11.7, Srgb{0, 150, 60});
add_segment("Cached", 15.5, Srgb{220, 180, 0});
add_segment("Buffers", 1.2, Srgb{0, 120, 200});
add_segment("Free", 1.9, Srgb{200, 200, 200});
}
~XStackBar() {}
// 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 Segment API
void add_segment(const std::string& name, double value, Srgb color)
{
Segment s;
s.name = name;
s.value = std::max(0.0, value);
s.color = color;
segments_.push_back(s);
}
void clear_segments() { segments_.clear(); }
void set_segment(int index, double value)
{
if (index >= 0 && index < (int)segments_.size())
segments_[index].value = std::max(0.0, value);
}
int segment_count() { return (int)segments_.size(); }
double total()
{
double t = 0.0;
for (auto& s : segments_) t += s.value;
return t;
}
// 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 bar_h = 26;
double tot = total();
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
if (tot > 0.0)
{
std::string tot_s = "total " + fmt1(tot) + " GiB";
xf::draw_text_at(display_, window_, gc_, st, x2_ - 8 - xf::text_width(st, tot_s), y1_ + 15, tot_s);
}
// Stacked bar.
int bx = x1_ + 8;
int by = y1_ + title_h + 4;
int bw = std::max(10, w - 16);
int x = bx;
for (auto& s : segments_)
{
int sw = tot > 0.0 ? (int)(bw * s.value / tot) : 0;
if (sw <= 0) continue;
XSetForeground(display_, gc_, col(s.color));
XFillRectangle(display_, window_, gc_, x, by, sw, bar_h);
x += sw;
}
XSetForeground(display_, gc_, col(axis_color_));
XDrawRectangle(display_, window_, gc_, bx, by, bw, bar_h);
// Legend: one row per segment — swatch, name, value, share.
int ly = by + bar_h + 18;
for (auto& s : segments_)
{
XSetForeground(display_, gc_, col(s.color));
XFillRectangle(display_, window_, gc_, x1_ + 8, ly - 10, 12, 12);
std::string line = s.name + " " + fmt1(s.value) + " GiB";
if (tot > 0.0)
{
char share[16];
std::snprintf(share, sizeof(share), " %.0f%%", s.value / tot * 100.0);
line += share;
}
xf::draw_text_at(display_, window_, gc_, st, x1_ + 28, ly, line);
ly += 18;
}
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 "XStackBar.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, "XStackBar - 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");
XStackBar 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(XTachometer LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(X11 REQUIRED)
add_executable(XTachometer main_XTachometer.cpp)
target_link_libraries(XTachometer PRIVATE X11::X11)
@@ -0,0 +1,244 @@
#pragma once
#include "core/xframe_core.h"
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <string>
// region XTachometer
//
// A round tachometer: needle over a 240-degree sweep, a redline zone at
// the top of the scale, a digital readout and a peak-hold tick.
// Raw X11, same as the rest of the XFrame set.
//
class XTachometer
{
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 value_ = 0.0;
double max_ = 8000.0;
double redline_ = 6500.0;
double peak_ = 0.0;
bool peak_hold_ = true;
Srgb white_color_{255, 255, 255};
Srgb axis_color_{150, 150, 150};
Srgb dial_color_{240, 240, 240};
Srgb redline_color_{210, 40, 30};
Srgb needle_color_{30, 30, 30};
unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); }
static std::string fmt0(double v)
{
char buf[32];
std::snprintf(buf, sizeof(buf), "%.0f", v);
return buf;
}
// Sweep: 240 degrees clockwise from 150 deg (7:30 position) to 30 deg.
static double angle_deg(double frac)
{
return 150.0 - frac * 240.0;
}
public:
XTachometer(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_ = "XTachometer";
text_ = "RPM";
value_ = 4200.0;
peak_ = 6100.0;
}
~XTachometer() {}
// 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 Gauge API
void set_value(double v)
{
value_ = std::max(0.0, std::min(max_, v));
if (peak_hold_) peak_ = std::max(peak_, value_);
}
double get_value() { return value_; }
void set_max(double m)
{
max_ = m > 0.0 ? m : 1.0;
value_ = std::min(value_, max_);
peak_ = std::min(peak_, max_);
redline_ = std::min(redline_, max_);
}
double get_max() { return max_; }
void set_redline(double r) { redline_ = std::max(0.0, std::min(max_, r)); }
double get_redline() { return redline_; }
double peak() { return peak_; }
void reset_peak() { peak_ = value_; }
// 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;
int cx = x1_ + w / 2;
int cy = y1_ + title_h + (h - title_h) / 2 + 8;
int r = std::max(20, std::min(w, h - title_h - 44) / 2 - 6);
double frac = max_ > 0.0 ? value_ / max_ : 0.0;
double red_frac = max_ > 0.0 ? redline_ / max_ : 1.0;
double peak_frac = max_ > 0.0 ? peak_ / max_ : 0.0;
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
// Dial face.
XSetForeground(display_, gc_, col(dial_color_));
XFillArc(display_, window_, gc_, cx - r, cy - r, 2 * r, 2 * r, 0, 360 * 64);
XSetForeground(display_, gc_, col(axis_color_));
XDrawArc(display_, window_, gc_, cx - r, cy - r, 2 * r, 2 * r, 0, 360 * 64);
// Redline zone: thick red arc from the redline to the end of scale.
{
int a0 = (int)(angle_deg(red_frac) * 64);
int sweep = (int)((1.0 - red_frac) * 240.0 * 64);
XSetForeground(display_, gc_, col(redline_color_));
xf::set_line_width(display_, gc_, 8);
// X angles run counter-clockwise; our sweep is clockwise.
XDrawArc(display_, window_, gc_, cx - r + 4, cy - r + 4, 2 * (r - 4), 2 * (r - 4),
a0 - sweep, sweep);
xf::set_line_width(display_, gc_, 1);
}
// Tick marks every 1/8 of the sweep, labels every 1/4.
for (int i = 0; i <= 8; ++i)
{
double f = (double)i / 8.0;
double a = angle_deg(f) * M_PI / 180.0;
int x0 = cx + (int)((r - 10) * std::cos(a));
int y0 = cy - (int)((r - 10) * std::sin(a));
int x1 = cx + (int)((r - 2) * std::cos(a));
int y1 = cy - (int)((r - 2) * std::sin(a));
XSetForeground(display_, gc_, col(axis_color_));
XDrawLine(display_, window_, gc_, x0, y0, x1, y1);
if (i % 2 == 0)
{
std::string lab = fmt0(max_ * f / 1000.0);
int lx = cx + (int)((r - 26) * std::cos(a));
int ly = cy - (int)((r - 26) * std::sin(a));
xf::draw_text_at(display_, window_, gc_, st, lx - xf::text_width(st, lab) / 2, ly + 4, lab);
}
}
// Peak-hold tick.
{
double a = angle_deg(peak_frac) * M_PI / 180.0;
int x0 = cx + (int)((r - 16) * std::cos(a));
int y0 = cy - (int)((r - 16) * std::sin(a));
int x1 = cx + (int)((r - 4) * std::cos(a));
int y1 = cy - (int)((r - 4) * std::sin(a));
XSetForeground(display_, gc_, col(redline_color_));
xf::set_line_width(display_, gc_, 3);
XDrawLine(display_, window_, gc_, x0, y0, x1, y1);
xf::set_line_width(display_, gc_, 1);
}
// Needle.
{
double a = angle_deg(frac) * M_PI / 180.0;
int nx = cx + (int)((r - 14) * std::cos(a));
int ny = cy - (int)((r - 14) * std::sin(a));
XSetForeground(display_, gc_, col(needle_color_));
xf::set_line_width(display_, gc_, 3);
XDrawLine(display_, window_, gc_, cx, cy, nx, ny);
xf::set_line_width(display_, gc_, 1);
XFillArc(display_, window_, gc_, cx - 6, cy - 6, 12, 12, 0, 360 * 64);
}
// Digital readout under the dial.
std::string readout = fmt0(value_) + " (peak " + fmt0(peak_) + ")";
int rw = xf::text_width(st, readout);
xf::draw_text_at(display_, window_, gc_, st, cx - rw / 2, y2_ - 10, readout);
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))
{
reset_peak();
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 "XTachometer.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, "XTachometer - 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");
XTachometer 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(XUsageBar LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(X11 REQUIRED)
add_executable(XUsageBar main_XUsageBar.cpp)
target_link_libraries(XUsageBar PRIVATE X11::X11)
+173
View File
@@ -0,0 +1,173 @@
#pragma once
#include "core/xframe_core.h"
#include <algorithm>
#include <cstdio>
#include <string>
// region XUsageBar
//
// A horizontal gradient usage bar (btop memory/disk style): label on the
// left, a green -> yellow -> red gradient fill, % text on the right.
// Raw X11, same as the rest of the XFrame set.
//
class XUsageBar
{
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 value_ = 0.0; // 0..100
double max_ = 100.0;
Srgb white_color_{255, 255, 255};
Srgb axis_color_{150, 150, 150};
Srgb track_color_{230, 230, 230};
unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); }
// Gradient stop: green (0%) -> yellow (60%) -> red (100%).
static Srgb gradient(double frac)
{
frac = std::max(0.0, std::min(1.0, frac));
if (frac < 0.6)
{
double t = frac / 0.6;
return Srgb{(int)(0 + t * 210), (int)(180 - t * 10), 40};
}
double t = (frac - 0.6) / 0.4;
return Srgb{(int)(210 + t * 10), (int)(170 - t * 130), (int)(40 - t * 10)};
}
public:
XUsageBar(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_ = "XUsageBar";
text_ = "CPU";
value_ = 63.0;
}
~XUsageBar() {}
// 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 Bar API
void set_value(double v) { value_ = std::max(0.0, std::min(max_, v)); }
double get_value() { return value_; }
void set_max(double m) { max_ = m > 0.0 ? m : 100.0; }
double get_max() { return max_; }
double fraction() { return max_ > 0.0 ? value_ / max_ : 0.0; }
// 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);
// Label left, % right.
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + (h + 12) / 2 - 4, text_);
char pct[16];
std::snprintf(pct, sizeof(pct), "%.0f%%", fraction() * 100.0);
int pct_w = xf::text_width(st, pct);
xf::draw_text_at(display_, window_, gc_, st, x2_ - 8 - pct_w, y1_ + (h + 12) / 2 - 4, pct);
// Bar track between label and %.
int label_w = xf::text_width(st, text_) + 16;
int bx = x1_ + label_w;
int bw = std::max(10, x2_ - 16 - pct_w - bx);
int bh = std::max(8, h - 16);
int by = y1_ + (h - bh) / 2;
XSetForeground(display_, gc_, col(track_color_));
XFillRectangle(display_, window_, gc_, bx, by, bw, bh);
// Gradient fill: 48 slices across the filled fraction.
int filled = (int)(bw * fraction());
const int slices = 48;
for (int i = 0; i < slices; ++i)
{
int sx = bx + filled * i / slices;
int sw = filled * (i + 1) / slices - filled * i / slices;
if (sw <= 0) continue;
XSetForeground(display_, gc_, col(gradient((double)i / slices * fraction())));
XFillRectangle(display_, window_, gc_, sx, by, sw, bh);
}
XSetForeground(display_, gc_, col(axis_color_));
XDrawRectangle(display_, window_, gc_, bx, by, bw, bh);
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 "XUsageBar.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, "XUsageBar - 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");
XUsageBar 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(XVerticalBar LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(X11 REQUIRED)
add_executable(XVerticalBar main_XVerticalBar.cpp)
target_link_libraries(XVerticalBar PRIVATE X11::X11)
@@ -0,0 +1,201 @@
#pragma once
#include "core/xframe_core.h"
#include <algorithm>
#include <cstdio>
#include <string>
// region XVerticalBar
//
// A vertical bar meter with a scale on the left and a peak-hold marker:
// the thin line (and peak value) stays at the highest recent value.
// Raw X11, same as the rest of the XFrame set.
//
class XVerticalBar
{
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 value_ = 0.0;
double max_ = 100.0;
double peak_ = 0.0;
bool peak_hold_ = true;
Srgb white_color_{255, 255, 255};
Srgb grid_color_{225, 225, 225};
Srgb axis_color_{150, 150, 150};
Srgb fill_color_{0, 170, 70};
Srgb peak_color_{210, 40, 30};
unsigned long col(Srgb c) { return styles_->get_color_Srgb(c); }
static std::string fmt0(double v)
{
char buf[32];
std::snprintf(buf, sizeof(buf), "%.0f", v);
return buf;
}
public:
XVerticalBar(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_ = "XVerticalBar";
text_ = "Load";
value_ = 72.0;
peak_ = 91.0;
}
~XVerticalBar() {}
// 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 Bar API
void set_value(double v)
{
value_ = std::max(0.0, std::min(max_, v));
if (peak_hold_) peak_ = std::max(peak_, value_);
}
double get_value() { return value_; }
void set_max(double m)
{
max_ = m > 0.0 ? m : 100.0;
value_ = std::min(value_, max_);
peak_ = std::min(peak_, max_);
}
double get_max() { return max_; }
double peak() { return peak_; }
void reset_peak() { peak_ = value_; }
void set_peak_hold(bool on) { peak_hold_ = on; }
// 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 scale_w = 36;
const int bottom_h = 18;
int bx = x1_ + scale_w + 8;
int by = y1_ + title_h + 16;
int bw = std::max(10, x2_ - 8 - bx);
int bh = std::max(10, y2_ - bottom_h - by);
xf::draw_text_at(display_, window_, gc_, st, x1_ + 8, y1_ + 15, text_);
// Scale ticks + labels (0 / 25 / 50 / 75 / 100 % of max).
for (int g = 0; g <= 4; ++g)
{
int gy = by + bh * g / 4;
XSetForeground(display_, gc_, col(grid_color_));
XDrawLine(display_, window_, gc_, bx, gy, bx + bw, gy);
XSetForeground(display_, gc_, col(axis_color_));
XDrawLine(display_, window_, gc_, bx - 4, gy, bx, gy);
std::string lab = fmt0(max_ * (4 - g) / 4.0);
xf::draw_text_at(display_, window_, gc_, st, bx - 8 - xf::text_width(st, lab), gy + 4, lab);
}
// Fill from the bottom.
int filled = (int)(bh * value_ / max_);
XSetForeground(display_, gc_, col(fill_color_));
XFillRectangle(display_, window_, gc_, bx, by + bh - filled, bw, filled);
XSetForeground(display_, gc_, col(axis_color_));
XDrawRectangle(display_, window_, gc_, bx, by, bw, bh);
// Peak-hold marker: red line across the bar + value.
if (peak_hold_)
{
int py = by + bh - (int)(bh * peak_ / max_);
XSetForeground(display_, gc_, col(peak_color_));
xf::set_line_width(display_, gc_, 2);
XDrawLine(display_, window_, gc_, bx - 4, py, bx + bw + 4, py);
xf::set_line_width(display_, gc_, 1);
std::string pk = "peak " + fmt0(peak_);
xf::draw_text_at(display_, window_, gc_, st, bx + bw - xf::text_width(st, pk) - 4, py - 4, pk);
}
// Current value under the bar.
std::string cur = fmt0(value_) + " / " + fmt0(max_);
xf::draw_text_at(display_, window_, gc_, st, bx, y2_ - 4, cur);
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))
{
reset_peak();
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 "XVerticalBar.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, "XVerticalBar - 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");
XVerticalBar 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: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.3 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB