docs: improve README readability and add comprehensive C API reference

This commit is contained in:
Luxferre committed 2026-11-30 17:51:29 +02:00
1 parent fab6a60459
commit de3445c06f
2 files changed
+1759 -22

No files matched your search

+205 -22
View File
@@ -1,48 +1,231 @@
# libgemui
`libgemui` is a lightweight C11 X11, Cairo, and Pango/HarfBuzz UI toolkit designed for the ANODE desktop environment and standalone X11 utilities. It can be built as either a shared library (`libgemui.so`) or a static archive (`libgemui.a`) with live `.Xresources` / `.xth` theming and fractional DPI scaling.
**GemUI** (`libgemui`) is a lightweight, zero-bloat **C11 immediate-mode UI toolkit** built directly on **X11**, **Cairo**, and **Pango/HarfBuzz**. Designed for the **ANODE** desktop environment and standalone X11 utilities, it delivers crisp fractional-DPI rendering, live `.Xresources` theming, and a compact API through a single public header (`gemui.h`).
## Features
It builds cleanly into both a shared library (`libgemui.so`) and a static archive (`libgemui.a`) with no heavyweight framework dependencies.
- **Double-buffered X11 + Cairo + Pango rendering**: Smooth redraws with XRandR/Xinerama monitor awareness, Pango/HarfBuzz font fallback (`theme.fontFace` + `theme.iconFontFace`), and automatic luminance-aware dark/light control contrast.
- **Fractional DPI scaling & touch sizing**: Reads `Xft.dpi`, `theme.scale` / `anode.scale`, `theme.touchMode` / `anode.touchMode`, `~/.Xresources`, root `XA_RESOURCE_MANAGER`, `GEMUI_SCALE` / `ANODE_SCALE`, and primary monitor EDID DPI (`0.50x` to `4.00x`), with live theme reload via `SIGUSR1` or property updates.
- **Standalone theming fallback**: Ships built-in `Anodized` theme defaults so applications work cleanly on any X11 desktop even when no ANODE theme files or `.Xresources` entries are present.
- **Immediate-mode helpers & hit tracking**: Rect/rounded primitives, text/icon drawing, headers, buttons, toggles, sliders, text inputs, toasts, clipboard retrieval (`gemui_clipboard_get`), non-blocking `poll()` loop for X11 + custom fds + timers, and optional `WM_DELETE_WINDOW` interception (`gemui_set_close_handler`).
- **Standardized File Open / Save dialog**: Built-in modal directory browser (`gemui_open_file_dialog`, `gemui_save_file_dialog`, `gemui_file_dialog_run`) with extension filters, dotfile toggle, UTF-8 filename input, overwrite confirmation, and automatic compact scaling down to tiny screens (`<= 320x360`).
---
## Key Features
- **Double-Buffered Cairo + Pango Rendering** — Flicker-free offscreen rendering with automatic font fallback (`theme.fontFace` + `theme.iconFontFace` via HarfBuzz/Pango) and luminance-aware dark/light text contrast on buttons.
- **Fractional DPI Scaling & Touch Mode** — Automatically scales UI geometry and typography between `0.50x` and `4.00x` using `Xft.dpi`, theme scale settings, environment overrides, or monitor EDID DPI, plus optional `1.18x` touch-friendly row sizing.
- **Live Theming with Standalone Defaults** — Ships built-in **Anodized** dark theme defaults so apps look good on any X11 desktop without configuration, while supporting live reloads from `.Xresources`, `.xth` files, `xrdb` updates, or `SIGUSR1`.
- **Immediate-Mode Widgets & Hit-Testing** — Includes drawing primitives, text/icon helpers, headers, buttons, toggle switches, sliders, text/password inputs, toast notifications, clipboard retrieval, and window-close interception.
- **Non-Blocking `poll()` Event Loop** — Integrates X11 input, up to 16 custom file descriptors (sockets, pipes, IPC), a repeating millisecond timer, and signal-driven theme reloads in one simple loop.
- **Built-In Modal Open / Save File Dialog** — Embeddable overlay (or standalone one-call runner) with directory navigation, extension filters, dotfile toggle, UTF-8 filename input, overwrite confirmation, and automatic compact layout on small screens (`<= 320x360`).
- **Multi-Monitor & Fullscreen Overlays** — Supports monitor-under-pointer placement, `_NET_WORKAREA` awareness, XRandR primary-monitor geometry, and `override_redirect` fullscreen overlays with keyboard/pointer grabbing for lockscreens or launchers.
---
## Quick Start
Save the following minimal application as `main.c`:
```c
#include <gemui.h>
#include <stdio.h>
enum {
HIT_CLICK = 1,
HIT_QUIT = 2,
};
typedef struct {
int clicks;
} app_state_t;
static void on_draw(gemui_app_t *app, cairo_t *cr, int w, int h, void *ud) {
app_state_t *st = (app_state_t *)ud;
const gemui_theme_t *t = gemui_theme(app);
gemui_draw_header(app, cr, NULL, "Hello GemUI", "Quick Start");
int pad = gemui_scale_px(app, 16);
int hdr_h = gemui_scale_px(app, 42);
int btn_h = gemui_touch_row_px(app, 36);
char msg[64];
snprintf(msg, sizeof(msg), "Button clicked %d time(s).", st->clicks);
gemui_draw_text(cr, t,
(gemui_rect_t){pad, hdr_h + pad, w - pad * 2, btn_h},
msg, 0, false, GEMUI_ALIGN_LEFT, t->fg_main);
int bw = (w - pad * 3) / 2;
int by = h - pad - btn_h;
gemui_draw_button(app, cr, (gemui_rect_t){pad, by, bw, btn_h},
"Click Me", HIT_CLICK, 0, true, false, false);
gemui_draw_button(app, cr, (gemui_rect_t){pad * 2 + bw, by, bw, btn_h},
"Quit", HIT_QUIT, 0, false, false, false);
}
static void on_ptr(gemui_app_t *app, int type, int x, int y, unsigned int btn, void *ud) {
app_state_t *st = (app_state_t *)ud;
if (type != GEMUI_PTR_DOWN || btn != Button1) return;
const gemui_hit_t *hit = gemui_hit_test(app, x, y);
if (!hit) return;
if (hit->id == HIT_CLICK) {
st->clicks++;
gemui_show_toast(app, "Clicked!", 1200);
gemui_request_redraw(app);
} else if (hit->id == HIT_QUIT) {
gemui_quit(app, 0);
}
}
static void on_key(gemui_app_t *app, KeySym ks, unsigned int state, const char *utf8, void *ud) {
(void)state;
(void)utf8;
(void)ud;
if (ks == XK_Escape || ks == XK_q) {
gemui_quit(app, 0);
}
}
int main(void) {
gemui_app_t *app = gemui_create("hello-gemui", "Hello GemUI", 420, 220, false);
if (!app) return 1;
app_state_t st = {0};
gemui_handlers_t h = {
.on_draw = on_draw,
.on_key = on_key,
.on_ptr = on_ptr,
};
gemui_set_handlers(app, &h, &st);
int rc = gemui_run(app);
gemui_destroy(app);
return rc;
}
```
Compile and run against an installed `libgemui`:
```sh
cc -std=c11 -O2 main.c $(pkg-config --cflags --libs gemui) -o hello-gemui
./hello-gemui
```
---
## Dependencies
- POSIX `make`, C11 compiler (`cc`), `ar`, and `pkg-config`
- X11, Cairo, Pango, HarfBuzz, Fontconfig, and FreeType development libraries:
`x11`, `xext`, `xft`, `xinerama`, `xrandr`, `cairo`, `pangocairo`, `pango`, `harfbuzz`, `fontconfig`, `freetype2`
- **Build tools**: POSIX `make`, C11 compiler (`cc`), `ar`, and `pkg-config`
- **Development packages** (`pkg-config` modules):
`x11`, `xext`, `xft`, `xinerama`, `xrandr`, `cairo`, `pangocairo`, `pango`, `harfbuzz`, `fontconfig`, `freetype2` (plus standard math library `-lm`)
## Build & Install
---
## Building & Installing
Build both libraries and the `pkg-config` metadata, then install (default prefix `/usr/local`):
```sh
make # Build build/libgemui.so, build/libgemui.a, and build/gemui.pc
sudo make install # Install libraries, gemui.h, and gemui.pc (default PREFIX=/usr/local)
make
sudo make install
```
Targets and options:
### Make Targets
- `make shared` / `make static`: Build only `build/libgemui.so` or `build/libgemui.a`.
- `make rebuild` / `make clean` / `make help`: Clean rebuild or inspect build variables (`PREFIX`, `ANODE_PREFIX`, `DESTDIR`, `CC`, `AR`, `CFLAGS`, `LDFLAGS`).
| Target | Description |
| :--- | :--- |
| `make` / `make all` | Build `build/libgemui.so`, `build/libgemui.a`, and `build/gemui.pc` |
| `make shared` | Build only the shared library (`build/libgemui.so`) |
| `make static` | Build only the static archive (`build/libgemui.a`) |
| `make rebuild` | Clean and rebuild all targets |
| `make clean` | Remove the `build/` directory |
| `make install` | Install `gemui.h`, `libgemui.so`, `libgemui.a`, and `gemui.pc` |
| `make uninstall` | Remove installed `libgemui` files |
| `make help` | Print available targets and build options |
### Using as a Git Submodule / Static Library
### Build Options
When embedding `libgemui` in another repository:
| Variable | Default | Purpose |
| :--- | :--- | :--- |
| `PREFIX` | `/usr/local` | Installation prefix |
| `ANODE_PREFIX` | `$(PREFIX)` | Prefix searched for fallback `share/anode/themes/Anodized.xth` |
| `DESTDIR` | *(empty)* | Staging directory prepended during `install` / `uninstall` |
| `CC` | `cc` | C11 compiler executable |
| `AR` | `ar` | Archive tool executable |
| `PKG_CONFIG` | `pkg-config` | `pkg-config` executable |
| `CFLAGS` | `-O2` | Additional compiler flags |
| `LDFLAGS` | *(empty)* | Additional linker flags |
| `VERSION` | `1.0.0` | Version written into `gemui.pc` |
### Integrating Into Your Project
#### Option A: System-Installed Library (`pkg-config`)
After `make install`, `gemui.pc` provides all required compiler and linker flags (including `-Wl,-rpath,${libdir}`):
```sh
make static
cc -std=c11 -O2 app.c $(pkg-config --cflags --libs gemui) -o app
```
Compile with `-I<path-to-libgemui>` (`#include <gemui.h>`) and link `<path-to-libgemui>/build/libgemui.a` followed by:
*(If installed to a custom `PREFIX`, export `PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig"`.)*
#### Option B: Git Submodule / Static Archive
To vendor `libgemui` inside another repository without system installation:
```sh
pkg-config --libs x11 xext xft xinerama xrandr cairo pangocairo pango harfbuzz fontconfig freetype2
make -C path/to/libgemui static
```
and `-lm`.
Compile with `-Ipath/to/libgemui` (`#include <gemui.h>`) and link `path/to/libgemui/build/libgemui.a` followed by its system dependencies:
```sh
cc -std=c11 -O2 -Ipath/to/libgemui app.c path/to/libgemui/build/libgemui.a \
$(pkg-config --libs x11 xext xft xinerama xrandr cairo pangocairo pango harfbuzz fontconfig freetype2) \
-lm -o app
```
---
## Theming & DPI Scaling Overview
`libgemui` loads colors, fonts, scaling, and touch sizing automatically on startup and whenever reloaded:
1. **Override file**: If `GEMUI_XRESOURCES` or `ANODE_XRESOURCES` is set to a readable file, only that file is loaded.
2. **User & live X11 resources**: Otherwise `~/.Xresources` is merged with the live X11 root resource manager (`xrdb`).
3. **ANODE fallback / built-in defaults**: If no resource database is present, `<ANODE_PREFIX>/share/anode/themes/Anodized.xth` is tried before falling back to the built-in **Anodized** dark palette.
4. **Fractional DPI scaling**: Scale (`0.50`–`4.00`) is resolved from `anode.scale` / `theme.scale`, `Xft.dpi`, `GEMUI_SCALE` / `ANODE_SCALE`, or primary screen EDID DPI. `anode.touchMode` / `theme.touchMode` enables touch-sized rows (`gemui_touch_row_px`).
5. **Live reload**: Running `xrdb -merge` or sending `SIGUSR1` (`kill -USR1 <pid>`) reloads the theme in place, triggers `handlers.on_theme`, and redraws the window.
See [docs/API.md](docs/API.md) for the full list of `.Xresources` / `.xth` theme keys and precedence rules.
---
## Repository Layout
| File | Purpose |
| :--- | :--- |
| [gemui.h](gemui.h) | Public C11 API header (types, macros, widgets, event loop, utilities, file dialog) |
| [gemui.c](gemui.c) | Core X11/Cairo/Pango engine, theming, event loop, primitives, widgets, and process/IPC helpers |
| [gemui_filedialog.c](gemui_filedialog.c) | Built-in modal Open / Save file browser widget and standalone runner |
| [gemui_scale.h](gemui_scale.h) | Internal header helpers for X11 `.Xresources` loading and fractional DPI scaling |
| [gemui_internal.h](gemui_internal.h) | Internal bridge declarations between `gemui.c` and `gemui_filedialog.c` |
| [gemui.pc.in](gemui.pc.in) | `pkg-config` template substituted during build |
| [Makefile](Makefile) | Strict POSIX Makefile for building, installing, and uninstalling `libgemui` |
| [docs/API.md](docs/API.md) | Comprehensive C API reference and real-world application guide |
---
## Documentation
Read **[docs/API.md](docs/API.md)** for complete documentation covering:
- The immediate-mode rendering & hit-testing architecture
- Every macro, type, struct, callback, and function in `gemui.h` with exact semantics
- Event handling nuances (UTF-8 key lookup, drag-only `GEMUI_PTR_MOVE`, modal dialog interception)
- Complete `.Xresources` / `.xth` theme key reference
- Integrated and standalone file Open/Save dialog usage
- Real-world, compilable C11 application recipes (interactive forms, async `poll()` pipes & timers, fullscreen multi-monitor overlays)
---
## Credits
+1554
View File
File diff suppressed because it is too large. Load diff