Files
bananarchy/prebuilt/sxmo-x11/usr/include/neoui.h
T

575 lines
20 KiB
C++
Raw Normal View History

// SPDX-License-Identifier: GPL-2.0-only
// NeoUI - Bananarchy 240x320 X11 + Pango/Xft + .Xresources/.xth UI SDK
// Provides both a C-compatible FFI (libneoui.so) and C++20 SDK classes.
// Note: Every extern "C" function returns int, uint32_t, or const char* (never
// void) so that FFI callers such as Yabasic frnfn_call / frnfn_call$ work
// directly without void-return limitations.
#ifndef BANANARCHY_NEOUI_H
#define BANANARCHY_NEOUI_H
#include <stdint.h>
#ifdef __cplusplus
#include <X11/Xatom.h>
#include <X11/Xft/Xft.h>
#include <X11/Xlib.h>
#include <X11/Xresource.h>
#include <X11/Xutil.h>
#include <pango/pango.h>
#include <pango/pangoxft.h>
#include <string>
#include <unordered_map>
#include <vector>
extern "C" {
#endif
// Standard Bananarchy 240x320 QVGA geometry constants
enum NeoGeometry {
NEO_SCREEN_W = 240,
NEO_SCREEN_H = 320,
NEO_TOPBAR_H = 24,
NEO_WIN_X = 0,
NEO_WIN_Y = 24,
NEO_WIN_W = 240,
NEO_WIN_H = 296,
NEO_HEADER_H = 34,
NEO_SOFTBAR_H = 26
};
// Theme color token IDs for neoui_color(color_id)
enum NeoColorId {
NEO_COLOR_BG_MAIN = 0,
NEO_COLOR_BG_BAR = 1,
NEO_COLOR_BG_HEADER = 2,
NEO_COLOR_BG_FOOTER = 3,
NEO_COLOR_BG_OVERLAY = 4,
NEO_COLOR_BG_ALT_EVEN = 5,
NEO_COLOR_BG_ALT_ODD = 6,
NEO_COLOR_BG_INPUT = 7,
NEO_COLOR_BG_INPUT_FOCUS = 8,
NEO_COLOR_BG_BUTTON = 9,
NEO_COLOR_BG_SEARCH = 10,
NEO_COLOR_BG_BUBBLE_IN = 11,
NEO_COLOR_BG_BUBBLE_OUT = 12,
NEO_COLOR_BG_TOAST = 13,
NEO_COLOR_FG_MAIN = 14,
NEO_COLOR_FG_BRIGHT = 15,
NEO_COLOR_FG_MUTED = 16,
NEO_COLOR_SEL_BG = 17,
NEO_COLOR_SEL_FG = 18,
NEO_COLOR_ACCENT = 19,
NEO_COLOR_ACCENT_ALT = 20,
NEO_COLOR_HIGHLIGHT = 21,
NEO_COLOR_BORDER = 22,
NEO_COLOR_DIVIDER = 23,
NEO_COLOR_SUCCESS = 24,
NEO_COLOR_INFO = 25,
NEO_COLOR_WARNING = 26,
NEO_COLOR_DANGER = 27,
NEO_COLOR_DANGER_BG = 28
};
// Proportional Pango font roles
enum NeoFontRole {
NEO_FONT_SMALL = 0,
NEO_FONT_MAIN = 1,
NEO_FONT_ICON = 2,
NEO_FONT_LARGE = 3
};
// Semantic Xft text color roles
enum NeoTextColor {
NEO_TEXT_WHITE = 0,
NEO_TEXT_LIGHT = 1,
NEO_TEXT_MUTED = 2,
NEO_TEXT_YELLOW = 3,
NEO_TEXT_GREEN = 4,
NEO_TEXT_BLUE = 5,
NEO_TEXT_ORANGE = 6,
NEO_TEXT_RED = 7
};
// Event types returned by neoui_poll_event(timeout_ms)
enum NeoEventType {
NEO_EV_NONE = 0,
NEO_EV_EXPOSE = 1,
NEO_EV_KEY_PRESS = 2,
NEO_EV_KEY_RELEASE = 3,
NEO_EV_SOFTKEY_LEFT = 4,
NEO_EV_SOFTKEY_RIGHT = 5,
NEO_EV_SOFTKEY_CHORD = 6,
NEO_EV_FIFO_CMD = 7,
NEO_EV_THEME_RELOAD = 8,
NEO_EV_CLOSE_REQ = 9
};
// Logical keypad key codes returned by neoui_event_key()
enum NeoKey {
NEO_KEY_NONE = 0,
NEO_KEY_UP = 1,
NEO_KEY_DOWN = 2,
NEO_KEY_LEFT = 3,
NEO_KEY_RIGHT = 4,
NEO_KEY_OK = 5,
NEO_KEY_BACK = 6,
NEO_KEY_CALL = 7,
NEO_KEY_END = 8,
NEO_KEY_SOFT_LEFT = 9,
NEO_KEY_SOFT_RIGHT = 10,
NEO_KEY_SPACE = 32,
NEO_KEY_HASH = 35,
NEO_KEY_STAR = 42,
NEO_KEY_PLUS = 43,
NEO_KEY_0 = 48,
NEO_KEY_1 = 49,
NEO_KEY_2 = 50,
NEO_KEY_3 = 51,
NEO_KEY_4 = 52,
NEO_KEY_5 = 53,
NEO_KEY_6 = 54,
NEO_KEY_7 = 55,
NEO_KEY_8 = 56,
NEO_KEY_9 = 57
};
// Multi-tap input modes
enum NeoInputMode {
NEO_INPUT_ABC_TITLE = 0,
NEO_INPUT_ABC_LOWER = 1,
NEO_INPUT_ABC_UPPER = 2,
NEO_INPUT_123 = 3,
NEO_INPUT_SYM = 4
};
// Image scaling modes for neoui_draw_image_file
enum NeoScaleMode {
NEO_SCALE_FIT = 0,
NEO_SCALE_FILL = 1,
NEO_SCALE_STRETCH = 2
};
// Opaque C context handle
typedef struct NeoContext NeoContext;
// ---------------------------------------------------------------------------
// 1. Window & Daemon Lifecycle
// ---------------------------------------------------------------------------
int neoui_init(const char* app_name, int preload_mode);
int neoui_init_ex(const char* app_name, const char* res_name,
const char* res_class, const char* win_title,
int x, int y, int w, int h,
int override_redirect, int preload_mode);
int neoui_shutdown(void);
int neoui_show(void);
int neoui_hide(void);
int neoui_is_visible(void);
int neoui_raise_and_grab(void);
int neoui_ungrab(void);
int neoui_width(void);
int neoui_height(void);
int neoui_begin_frame(void);
int neoui_end_frame(void);
int neoui_flush(void);
// ---------------------------------------------------------------------------
// 2. Unified .Xresources / .xth Theme & Icon Resolution
// ---------------------------------------------------------------------------
int neoui_reload_theme(void);
int neoui_check_reload_theme(void);
int neoui_apply_theme_file(const char* xth_path);
int neoui_broadcast_theme_reload(const char* skip_plugin);
const char* neoui_theme_name(void);
const char* neoui_font_face(void);
const char* neoui_icon_font_face(void);
int neoui_base_font_size(void);
int neoui_font_size(int font_role);
uint32_t neoui_color(int color_id);
uint32_t neoui_adjust_rgb(uint32_t rgb, int delta);
uint32_t neoui_blend_rgb(uint32_t c1, uint32_t c2, int weight2_pct);
uint32_t neoui_parse_hex_rgb(const char* hex_str, uint32_t fallback_rgb);
const char* neoui_format_hex_rgb(uint32_t rgb);
const char* neoui_icon(const char* icon_name);
// ---------------------------------------------------------------------------
// 3. Drawing Primitives & Text Rendering
// ---------------------------------------------------------------------------
int neoui_clear(uint32_t rgb);
int neoui_fill_rect(int x, int y, int w, int h, uint32_t rgb);
int neoui_draw_rect(int x, int y, int w, int h, uint32_t rgb);
int neoui_draw_line(int x1, int y1, int x2, int y2, uint32_t rgb);
int neoui_set_clip(int x, int y, int w, int h);
int neoui_reset_clip(void);
int neoui_draw_text(int font_role, int color_role, int x, int y, int h,
const char* text, int max_w);
int neoui_draw_text_centered(int font_role, int color_role, int x, int y,
int w, int h, const char* text);
int neoui_draw_text_right(int font_role, int color_role, int right_x, int y,
int h, const char* text, int max_w);
int neoui_draw_text_wrapped(int font_role, int color_role, int x, int y,
int w, int h, const char* text, int scroll_line);
int neoui_text_width(int font_role, const char* text);
const char* neoui_tail_fit_text(int font_role, const char* text, int max_w);
int neoui_draw_image_file(const char* path, int x, int y, int w, int h,
int scale_mode);
// ---------------------------------------------------------------------------
// 4. Standardized OS Shell UI Components (Widgets)
// ---------------------------------------------------------------------------
int neoui_draw_header(const char* icon_str, int icon_color_role,
const char* title, const char* right_badge);
int neoui_draw_header_ex(int h, const char* icon_str, int icon_color_role,
const char* title, int title_color_role,
const char* right_badge, int right_color_role);
int neoui_draw_tabs(const char* pipe_labels, int active_idx, int y, int h);
int neoui_draw_softkeys(const char* left_label, const char* center_label,
const char* right_label);
int neoui_draw_softkeys_ex(const char* left_label, const char* center_label,
const char* right_label, int center_color_role);
int neoui_draw_list_row(int row_index, int y, int h, int is_selected,
const char* icon_str, int icon_color_role,
const char* title, const char* subtitle,
const char* right_text);
int neoui_draw_slider_row(int row_index, int y, int h, int is_selected,
const char* icon_str, int icon_color_role,
const char* label, int pct, const char* value_text);
int neoui_draw_toggle_row(int row_index, int y, int h, int is_selected,
const char* icon_str, int icon_color_role,
const char* label, int is_on,
const char* state_text);
int neoui_draw_button(int x, int y, int w, int h, int is_selected,
int is_danger, const char* icon_str, const char* label);
int neoui_draw_input_box(int x, int y, int w, int h, int is_focused,
const char* label, const char* value,
const char* placeholder, int password_mask);
int neoui_draw_progress_bar(int x, int y, int w, int h, int pct,
uint32_t fill_rgb);
int neoui_draw_scrollbar(int x, int y, int h, int total_items,
int visible_items, int scroll_offset);
int neoui_draw_modal_box(int x, int y, int w, int h, const char* icon_str,
int icon_color_role, const char* title);
int neoui_draw_confirm_dialog(const char* title, const char* line1,
const char* line2, const char* no_label,
const char* yes_label, int selected_yes);
int neoui_draw_menu_popup(const char* pipe_items, const char* pipe_icons,
int selected_idx, int max_visible_rows);
int neoui_draw_toast(const char* message);
int neoui_show_toast(const char* message, int duration_ms);
int neoui_clear_toast(void);
// ---------------------------------------------------------------------------
// 5. Event Loop, Keypad & Multi-Tap Input Management
// ---------------------------------------------------------------------------
int neoui_poll_event(int timeout_ms);
int neoui_event_key(void);
int neoui_event_keycode(void);
int neoui_event_keysym(void);
const char* neoui_event_command(void);
int neoui_set_chord_xdrawer(int enabled);
int neoui_input_reset(const char* initial_text);
int neoui_input_set_text(const char* text);
const char* neoui_input_get_text(void);
int neoui_input_set_mode(int mode);
int neoui_input_get_mode(void);
int neoui_input_cycle_mode(void);
const char* neoui_input_mode_badge(void);
int neoui_input_feed_key(int neo_key);
int neoui_input_commit_tap(void);
int neoui_input_backspace(void);
int neoui_input_is_tap_active(void);
const char* neoui_input_candidate_bar(const char* idle_hint);
int neoui_draw_multitap_bar(int x, int y, int w, int h, const char* idle_hint);
// ---------------------------------------------------------------------------
// 6. System & IPC Utilities
// ---------------------------------------------------------------------------
int neoui_send_plugin_cmd(const char* plugin_name, const char* cmd);
int neoui_vibrate(int duration_ms);
int neoui_now_ms(void);
#ifdef __cplusplus
} // extern "C"
namespace bananarchy {
std::string themeTrim(const std::string& s);
int themeHexNibble(char c);
bool parseHexRgb(const std::string& raw, uint32_t* outRgb);
std::string formatHexRgb(uint32_t rgb);
uint32_t adjustRgb(uint32_t rgb, int delta);
uint32_t blendRgb(uint32_t c1, uint32_t c2, int weight2Pct);
std::string getHomeDirPath();
std::string getXresourcesPath();
std::string getThemesDirPath();
struct ThemeSpec {
std::string name = "Default";
std::string fontFace = "Fira Code";
std::string iconFontFace = "DejaVuSansM Nerd Font";
int baseFontSize = 15;
uint32_t bgMain = 0x222222;
uint32_t bgBar = 0x222222;
uint32_t bgHeader = 0x1c1c1c;
uint32_t bgFooter = 0x181818;
uint32_t bgOverlay = 0x161616;
uint32_t bgAltEven = 0x282828;
uint32_t bgAltOdd = 0x2e2e2e;
uint32_t bgInput = 0x1a1a1a;
uint32_t bgInputFocus = 0x1b3b4b;
uint32_t bgButton = 0x2a2a2a;
uint32_t bgSearch = 0x383838;
uint32_t bgBubbleIn = 0x262d32;
uint32_t bgBubbleOut = 0x1f2d38;
uint32_t bgToast = 0x102535;
uint32_t fgMain = 0xdddddd;
uint32_t fgBright = 0xffffff;
uint32_t fgMuted = 0x999999;
uint32_t selBg = 0x005577;
uint32_t selFg = 0xffffff;
uint32_t accent = 0xf4d03f;
uint32_t accentAlt = 0x0088bb;
uint32_t highlight = 0xffc978;
uint32_t border = 0x444444;
uint32_t divider = 0x666666;
uint32_t success = 0x2ecc71;
uint32_t info = 0x5dade2;
uint32_t warning = 0xe67e22;
uint32_t danger = 0xe74c3c;
uint32_t dangerBg = 0x922b21;
int sizeSmall() const;
int sizeMain() const;
int sizeIcon() const;
int sizeLarge() const;
std::string pangoMainFontStr() const;
std::string pangoSmallFontStr() const;
std::string pangoIconFontStr() const;
std::string pangoLargeBoldFontStr() const;
std::string xftFontStr(int pxSize) const;
std::string xftIconFontStr(int pxSize) const;
void recomputeDerivedFromCore();
void parseFromMap(const std::unordered_map<std::string, std::string>& kv);
static void parseLinesIntoMap(
const std::string& content,
std::unordered_map<std::string, std::string>& kv);
bool loadFromFile(const std::string& path);
void loadActive(Display* dpy, int screen);
std::string serializeXresources() const;
bool saveToFile(const std::string& path) const;
uint32_t colorById(int colorId) const;
};
ThemeSpec loadThemeSpec(Display* dpy = nullptr, int screen = 0);
struct PluginTheme {
ThemeSpec spec;
time_t lastXresMtime = -1;
off_t lastXresSize = -1;
bool colorsAllocated = false;
Display* dpy_ = nullptr;
int screen_ = 0;
Visual* visual_ = nullptr;
Colormap cmap_ = 0;
Window win_ = None;
PangoContext* pangoCtx = nullptr;
PangoLayout* layoutMain = nullptr;
PangoLayout* layoutSmall = nullptr;
PangoLayout* layoutIcon = nullptr;
PangoLayout* layoutNum = nullptr;
XftColor colWhite{};
XftColor colLight{};
XftColor colMuted{};
XftColor colYellow{};
XftColor colGreen{};
XftColor colBlue{};
XftColor colOrange{};
XftColor colRed{};
void allocXft(Display* dpy, Visual* visual, Colormap cmap, uint32_t rgb,
XftColor* dst);
void init(Display* dpy, int screen, Visual* visual, Colormap cmap,
Window win = None);
void reload();
void load(Display* dpy, int screen, Visual* visual, Colormap cmap,
PangoLayout* lMain = nullptr, PangoLayout* lSmall = nullptr,
PangoLayout* lIcon = nullptr, PangoLayout* lNum = nullptr,
Window win = None);
bool checkAndReload();
bool checkAndReload(Display* dpy, int screen, Visual* visual, Colormap cmap,
PangoLayout* lMain = nullptr,
PangoLayout* lSmall = nullptr,
PangoLayout* lIcon = nullptr,
PangoLayout* lNum = nullptr,
Window win = None);
XftColor* colorByRole(int role);
PangoLayout* layoutByRole(int role);
};
// Shared Pango rendering & layout helpers in libneoui.so
int neoDrawPango(XftDraw* xftDraw, PangoLayout* layout, XftColor* color,
int x, int y, int h, const std::string& text,
int maxW = -1, int align = 0);
int neoPangoWidth(PangoLayout* layout, const std::string& text);
void neoDrawSoftbar(Display* dpy, Pixmap pix, GC gc, XftDraw* xftDraw,
PluginTheme& theme, int winW, int winH, int softbarH,
const std::string& leftLabel,
const std::string& centerLabel,
const std::string& rightLabel,
XftColor* centerCol = nullptr);
void neoDrawScrollbar(Display* dpy, Pixmap pix, GC gc, const PluginTheme& theme,
int x, int y, int h, int totalItems, int visibleItems,
int scrollOffset);
void neoDrawToast(Display* dpy, Pixmap pix, GC gc, XftDraw* xftDraw,
PluginTheme& theme, int winW, int winH, int softbarH,
const std::string& msg);
std::string formatMultiTapBar(const std::string& prefix, const char* table,
int activeIdx, bool makeUpper, int maxItems = 7,
size_t maxChars = 21);
std::string tailFitPangoText(PangoLayout* layout, const std::string& text,
int maxWidthPx);
// Shared image & media helpers in libneoui.so
bool saveRgb24AsJpeg(const std::string& path, const uint8_t* rgb, int width,
int height, int quality = 92);
bool loadJpegRgb24(const std::string& path, std::vector<uint8_t>* outRgb,
int* outW, int* outH);
bool loadPngRgb24(const std::string& path, std::vector<uint8_t>* outRgb,
int* outW, int* outH);
bool loadFfmpegImageRgb24(const std::string& path, std::vector<uint8_t>* outRgb,
int* outW, int* outH);
bool loadImageRgb24(const std::string& path, std::vector<uint8_t>* outRgb,
int* outW, int* outH);
void scaleRgb24ToBgrxCanvas(const uint8_t* srcRgb, int srcW, int srcH,
uint32_t* dstBgrx, int canvasW, int canvasH,
uint32_t bgPixel = 0x111111,
int scaleMode = NEO_SCALE_FIT);
void downscaleViewfinder800x600To240x180(const uint8_t* srcRgb,
uint32_t* dstBgrx,
int zoomFactor = 1);
std::string formatFileSize(off_t bytes);
// High-level C++ RAII Window + Theme + Offscreen Pixmap + Pango Context
class NeoWindow {
public:
Display* dpy = nullptr;
int screen = 0;
Window root = None;
Visual* visual = nullptr;
Colormap cmap = 0;
Window win = None;
Pixmap pix = None;
GC gc = nullptr;
XftDraw* xftDraw = nullptr;
PangoContext* pangoCtx = nullptr;
PangoLayout* layoutMain = nullptr;
PangoLayout* layoutSmall = nullptr;
PangoLayout* layoutIcon = nullptr;
PangoLayout* layoutNum = nullptr;
PluginTheme theme;
int x = NEO_WIN_X;
int y = NEO_WIN_Y;
int width = NEO_WIN_W;
int height = NEO_WIN_H;
NeoWindow() = default;
~NeoWindow();
NeoWindow(const NeoWindow&) = delete;
NeoWindow& operator=(const NeoWindow&) = delete;
bool create(const char* resName, const char* resClass, const char* title,
int winX = NEO_WIN_X, int winY = NEO_WIN_Y,
int winW = NEO_WIN_W, int winH = NEO_WIN_H,
bool overrideRedirect = true,
long eventMask = ExposureMask | KeyPressMask | KeyReleaseMask |
ButtonPressMask | VisibilityChangeMask |
StructureNotifyMask);
void destroy();
void bindAsActiveContext();
int drawPango(PangoLayout* layout, XftColor* color, int px, int py, int ph,
const std::string& text, int maxW = -1, int align = 0) {
return neoDrawPango(xftDraw, layout, color, px, py, ph, text, maxW, align);
}
int pangoWidth(PangoLayout* layout, const std::string& text) {
return neoPangoWidth(layout, text);
}
void drawSoftbar(const std::string& leftLabel, const std::string& centerLabel,
const std::string& rightLabel, XftColor* centerCol = nullptr,
int softbarH = NEO_SOFTBAR_H) {
neoDrawSoftbar(dpy, pix, gc, xftDraw, theme, width, height, softbarH,
leftLabel, centerLabel, rightLabel, centerCol);
}
void drawScrollbar(int sx, int sy, int sh, int totalItems, int visibleItems,
int scrollOffset) {
neoDrawScrollbar(dpy, pix, gc, theme, sx, sy, sh, totalItems, visibleItems,
scrollOffset);
}
void drawToast(const std::string& msg, int softbarH = NEO_SOFTBAR_H) {
neoDrawToast(dpy, pix, gc, xftDraw, theme, width, height, softbarH, msg);
}
void clearBg() {
if (dpy && pix != None && gc) {
XSetForeground(dpy, gc, theme.spec.bgMain);
XFillRectangle(dpy, pix, gc, 0, 0, width, height);
}
}
void present() {
if (dpy && pix != None && win != None && gc) {
XCopyArea(dpy, pix, win, gc, 0, 0, width, height, 0, 0);
XFlush(dpy);
}
}
bool checkReloadTheme() {
return theme.checkAndReload(dpy, screen, visual, cmap, layoutMain,
layoutSmall, layoutIcon, layoutNum, win);
}
void reloadTheme() {
theme.load(dpy, screen, visual, cmap, layoutMain, layoutSmall, layoutIcon,
layoutNum, win);
}
};
} // namespace bananarchy
#endif // __cplusplus
#endif // BANANARCHY_NEOUI_H