Files
libgemui/README.md
T

9.6 KiB
Raw Blame History

libgemui

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).

It builds cleanly into both a shared library (libgemui.so) and a static archive (libgemui.a) with no heavyweight framework dependencies.


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:

#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:

cc -std=c11 -O2 main.c $(pkg-config --cflags --libs gemui) -o hello-gemui
./hello-gemui

Dependencies

  • 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)

Building & Installing

Build both libraries and the pkg-config metadata, then install (default prefix /usr/local):

make
sudo make install

Make Targets

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

Build Options

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}):

cc -std=c11 -O2 app.c $(pkg-config --cflags --libs gemui) -o app

(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:

make -C path/to/libgemui static

Compile with -Ipath/to/libgemui (#include <gemui.h>) and link path/to/libgemui/build/libgemui.a followed by its system dependencies:

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 for the full list of .Xresources / .xth theme keys and precedence rules.


Repository Layout

File Purpose
gemui.h Public C11 API header (types, macros, widgets, event loop, utilities, file dialog)
gemui.c Core X11/Cairo/Pango engine, theming, event loop, primitives, widgets, and process/IPC helpers
gemui_filedialog.c Built-in modal Open / Save file browser widget and standalone runner
gemui_scale.h Internal header helpers for X11 .Xresources loading and fractional DPI scaling
gemui_internal.h Internal bridge declarations between gemui.c and gemui_filedialog.c
gemui.pc.in pkg-config template substituted during build
Makefile Strict POSIX Makefile for building, installing, and uninstalling libgemui
docs/API.md Comprehensive C API reference and real-world application guide

Documentation

Read 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

Created by Luxferre in 2026, released into the public domain with no warranties.