feat(runtime): native platform backends (AppKit, UIKit, Android, GTK4, SDL2, LVGL, Win32)

Add seven platform bridge implementations and the shared native target header:
el_native_target.h, el_appkit.m, el_uikit.m, el_android.c, el_gtk4.c,
el_sdl2.c, el_lvgl.c, el_win32.c, el_runtime_win32.c. Each bridge implements
the 33 __widget_* C builtins declared in el_native_target.h for its platform
toolkit. el_runtime_win32.c provides a POSIX-free runtime stub for cross-compiled
Win32 targets.
This commit is contained in:
2026-06-29 12:40:14 -05:00
parent 3da9181deb
commit 6271cb42b2
9 changed files with 9699 additions and 0 deletions
+949
View File
@@ -0,0 +1,949 @@
/*
* el_android.c — Android JNI backend for the el native widget system.
*
* This file implements the Android widget layer that el_seed.c calls through
* to when EL_TARGET_ANDROID is defined. It is the exact Android counterpart
* to el_appkit.m and presents the same C API surface.
*
* Architecture:
* el program (el code)
* → __widget_* C builtins in el_seed.c
* → el_android_* C-callable functions declared here
* → ElBridge static methods in Java via JNI
* → android.view.View subclasses on the UI thread
*
* Widget handles: every widget (window root, view, control) is assigned an
* int64_t slot index into view_slots[]. The el program holds these as opaque
* Int values. Slot 0 is never valid (null handle = -1 convention).
*
* Threading: Android requires all UI operations to run on the main (UI) thread.
* Every JNI call that mutates a View is dispatched through
* Activity.runOnUiThread(Runnable) if the current thread is not the UI thread.
* el_android_run_loop is a no-op — Android lifecycle is driven by the Activity.
*
* Callback mechanism: when a widget fires an event Java calls
* nativeOnClick / nativeOnChange / nativeOnSubmit
* The C side looks up the registered El function name for that slot, then:
* dlsym(RTLD_DEFAULT, fn_name)(widget_handle, event_data_string)
* This matches the __thread_create pattern in el_seed.c exactly.
*
* Compile / link (as part of libelruntime.so):
* Compiled by the Android Gradle NDK build system with -DEL_TARGET_ANDROID.
* Link flags: -landroid -llog -ldl
*
* Java companion: ElBridge.java in the same directory must be compiled into
* the Android application's APK (package com.neuron.el).
*/
#ifdef EL_TARGET_ANDROID
#include <jni.h>
#include <android/log.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include "el_runtime.h"
/* ── Logging ─────────────────────────────────────────────────────────────── */
#define EL_TAG "ElAndroid"
#define EL_LOGI(...) __android_log_print(ANDROID_LOG_INFO, EL_TAG, __VA_ARGS__)
#define EL_LOGW(...) __android_log_print(ANDROID_LOG_WARN, EL_TAG, __VA_ARGS__)
#define EL_LOGE(...) __android_log_print(ANDROID_LOG_ERROR, EL_TAG, __VA_ARGS__)
/* ── JNI global state ────────────────────────────────────────────────────── */
static JavaVM *g_jvm = NULL;
static jobject g_activity = NULL; /* global ref to Activity */
static jclass g_bridge_class = NULL; /* global ref to ElBridge class */
/* Cached method IDs on ElBridge — filled in el_android_init(). */
static jmethodID g_mid_createLinearLayout = NULL;
static jmethodID g_mid_createFrameLayout = NULL;
static jmethodID g_mid_createScrollView = NULL;
static jmethodID g_mid_createTextView = NULL;
static jmethodID g_mid_createButton = NULL;
static jmethodID g_mid_createEditText = NULL;
static jmethodID g_mid_createImageView = NULL;
static jmethodID g_mid_setContentView = NULL;
static jmethodID g_mid_setTitle = NULL;
static jmethodID g_mid_addChild = NULL;
static jmethodID g_mid_removeChild = NULL;
static jmethodID g_mid_destroyView = NULL;
static jmethodID g_mid_setText = NULL;
static jmethodID g_mid_getText = NULL;
static jmethodID g_mid_setTextColor = NULL;
static jmethodID g_mid_setBackgroundColor = NULL;
static jmethodID g_mid_setFont = NULL;
static jmethodID g_mid_setPadding = NULL;
static jmethodID g_mid_setWidth = NULL;
static jmethodID g_mid_setHeight = NULL;
static jmethodID g_mid_setFlex = NULL;
static jmethodID g_mid_setCornerRadius = NULL;
static jmethodID g_mid_setEnabled = NULL;
static jmethodID g_mid_setVisibility = NULL;
static jmethodID g_mid_setOnClickListener = NULL;
static jmethodID g_mid_setOnChangeListener = NULL;
static jmethodID g_mid_setOnSubmitListener = NULL;
static jmethodID g_mid_runOnUiThread = NULL; /* Activity.runOnUiThread */
/* ── Widget table ─────────────────────────────────────────────────────────── */
#define EL_ANDROID_MAX_WIDGETS 4096
typedef enum {
EL_WIDGET_FREE = 0,
EL_WIDGET_WINDOW = 1,
EL_WIDGET_VSTACK = 2,
EL_WIDGET_HSTACK = 3,
EL_WIDGET_ZSTACK = 4,
EL_WIDGET_SCROLL = 5,
EL_WIDGET_LABEL = 6,
EL_WIDGET_BUTTON = 7,
EL_WIDGET_TEXTFIELD = 8,
EL_WIDGET_TEXTAREA = 9,
EL_WIDGET_IMAGE = 10,
} ElWidgetKind;
typedef struct {
ElWidgetKind kind;
jint slot; /* Java-side slot index (matches C index) */
char *cb_click; /* El function name for click / submit events */
char *cb_change; /* El function name for value-change events */
} ElWidget;
static ElWidget _el_widgets[EL_ANDROID_MAX_WIDGETS];
static int64_t el_widget_alloc(ElWidgetKind kind, jint slot) {
for (int i = 1; i < EL_ANDROID_MAX_WIDGETS; i++) {
if (_el_widgets[i].kind == EL_WIDGET_FREE) {
_el_widgets[i].kind = kind;
_el_widgets[i].slot = slot;
_el_widgets[i].cb_click = NULL;
_el_widgets[i].cb_change = NULL;
return (int64_t)i;
}
}
EL_LOGE("el_widget_alloc: slot table full");
return -1;
}
static ElWidget *el_widget_get(int64_t handle) {
if (handle <= 0 || handle >= EL_ANDROID_MAX_WIDGETS) return NULL;
if (_el_widgets[handle].kind == EL_WIDGET_FREE) return NULL;
return &_el_widgets[handle];
}
static void el_widget_free(int64_t handle) {
ElWidget *w = el_widget_get(handle);
if (!w) return;
w->kind = EL_WIDGET_FREE;
w->slot = -1;
free(w->cb_click); w->cb_click = NULL;
free(w->cb_change); w->cb_change = NULL;
}
/* ── JNI environment helpers ─────────────────────────────────────────────── */
/*
* Obtain a JNIEnv for the calling thread. Attaches the thread to the JVM if
* needed (detaches in el_jni_detach_if_attached — call in pairs).
*/
static int g_was_attached = 0; /* thread-local would be cleaner but this is
safe for single-threaded el programs */
static JNIEnv *el_jni_env(void) {
if (!g_jvm) return NULL;
JNIEnv *env = NULL;
jint rc = (*g_jvm)->GetEnv(g_jvm, (void **)&env, JNI_VERSION_1_6);
if (rc == JNI_OK) { g_was_attached = 0; return env; }
if (rc == JNI_EDETACHED) {
if ((*g_jvm)->AttachCurrentThread(g_jvm, &env, NULL) == JNI_OK) {
g_was_attached = 1;
return env;
}
}
EL_LOGE("el_jni_env: failed to obtain JNIEnv");
return NULL;
}
static void el_jni_detach_if_attached(void) {
if (g_was_attached && g_jvm) {
(*g_jvm)->DetachCurrentThread(g_jvm);
g_was_attached = 0;
}
}
/* ── UI-thread dispatch ──────────────────────────────────────────────────── */
/*
* Most ElBridge static methods already dispatch to the UI thread internally
* (they call Activity.runOnUiThread). The helper below is available for
* cases where the caller needs to be sure the call has completed before
* returning (ElBridge methods marked "sync" use a CountDownLatch internally).
*
* For the current implementation we call ElBridge methods directly; ElBridge
* itself marshals to the UI thread via Activity.runOnUiThread + latch.
* This keeps the C side simple and mirrors the AppKit dispatch_sync pattern.
*/
/* ── JNI_OnLoad ──────────────────────────────────────────────────────────── */
JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM *vm, void *reserved) {
(void)reserved;
g_jvm = vm;
EL_LOGI("JNI_OnLoad: el Android bridge loaded");
return JNI_VERSION_1_6;
}
/* ── el_android_init ─────────────────────────────────────────────────────── */
/*
* Called from __native_init(). The Activity must have already called
* ElBridge.registerActivity(activity) from Java before this runs, which sets
* g_activity via the nativeRegisterActivity JNI method below.
*
* Caches all method IDs used later so individual widget calls avoid repeated
* FindClass / GetStaticMethodID lookups.
*/
void el_android_init(void) {
static int done = 0;
if (done) return;
done = 1;
JNIEnv *env = el_jni_env();
if (!env) { EL_LOGE("el_android_init: no JNIEnv"); return; }
jclass cls = (*env)->FindClass(env, "com/neuron/el/ElBridge");
if (!cls) { EL_LOGE("el_android_init: ElBridge class not found"); return; }
g_bridge_class = (*env)->NewGlobalRef(env, cls);
(*env)->DeleteLocalRef(env, cls);
#define CACHE_STATIC(var, name, sig) \
var = (*env)->GetStaticMethodID(env, g_bridge_class, name, sig); \
if (!var) EL_LOGW("el_android_init: method not found: %s %s", name, sig)
CACHE_STATIC(g_mid_createLinearLayout, "createLinearLayout", "(II)I");
CACHE_STATIC(g_mid_createFrameLayout, "createFrameLayout", "()I");
CACHE_STATIC(g_mid_createScrollView, "createScrollView", "()I");
CACHE_STATIC(g_mid_createTextView, "createTextView", "(Ljava/lang/String;)I");
CACHE_STATIC(g_mid_createButton, "createButton", "(Ljava/lang/String;)I");
CACHE_STATIC(g_mid_createEditText, "createEditText", "(Ljava/lang/String;Z)I");
CACHE_STATIC(g_mid_createImageView, "createImageView", "(Ljava/lang/String;)I");
CACHE_STATIC(g_mid_setContentView, "setContentView", "(I)V");
CACHE_STATIC(g_mid_setTitle, "setTitle", "(Ljava/lang/String;)V");
CACHE_STATIC(g_mid_addChild, "addChild", "(II)V");
CACHE_STATIC(g_mid_removeChild, "removeChild", "(II)V");
CACHE_STATIC(g_mid_destroyView, "destroyView", "(I)V");
CACHE_STATIC(g_mid_setText, "setText", "(ILjava/lang/String;)V");
CACHE_STATIC(g_mid_getText, "getText", "(I)Ljava/lang/String;");
CACHE_STATIC(g_mid_setTextColor, "setTextColor", "(IFFFF)V");
CACHE_STATIC(g_mid_setBackgroundColor, "setBackgroundColor", "(IFFFF)V");
CACHE_STATIC(g_mid_setFont, "setFont", "(ILjava/lang/String;IZ)V");
CACHE_STATIC(g_mid_setPadding, "setPadding", "(IIIII)V");
CACHE_STATIC(g_mid_setWidth, "setWidth", "(II)V");
CACHE_STATIC(g_mid_setHeight, "setHeight", "(II)V");
CACHE_STATIC(g_mid_setFlex, "setFlex", "(II)V");
CACHE_STATIC(g_mid_setCornerRadius, "setCornerRadius", "(IF)V");
CACHE_STATIC(g_mid_setEnabled, "setEnabled", "(IZ)V");
CACHE_STATIC(g_mid_setVisibility, "setVisibility", "(IZ)V");
CACHE_STATIC(g_mid_setOnClickListener, "setOnClickListener", "(I)V");
CACHE_STATIC(g_mid_setOnChangeListener, "setOnChangeListener", "(I)V");
CACHE_STATIC(g_mid_setOnSubmitListener, "setOnSubmitListener", "(I)V");
#undef CACHE_STATIC
el_jni_detach_if_attached();
EL_LOGI("el_android_init: complete");
}
/* ── JNI: Activity registration ─────────────────────────────────────────── */
/*
* Called from Java: ElBridge.registerActivity(activity) calls back here.
* Stores a global reference to the Activity so C code can dispatch to it.
*/
JNIEXPORT void JNICALL
Java_com_neuron_el_ElBridge_nativeRegisterActivity(JNIEnv *env, jclass cls,
jobject activity) {
(void)cls;
if (g_activity) {
(*env)->DeleteGlobalRef(env, g_activity);
g_activity = NULL;
}
if (activity) {
g_activity = (*env)->NewGlobalRef(env, activity);
EL_LOGI("nativeRegisterActivity: activity registered");
}
}
/* ── El callback invocation ──────────────────────────────────────────────── */
/*
* Invoke an El callback by symbol name.
* Signature matches AppKit: fn(handle: Int, data: String) -> Void
* compiled to: void fn(el_val_t handle, el_val_t data)
*/
typedef void (*ElCb2)(int64_t handle, int64_t data);
static void el_android_invoke_cb(const char *fn_name, int64_t handle,
const char *data) {
if (!fn_name || !*fn_name) return;
void *sym = dlsym(RTLD_DEFAULT, fn_name);
if (!sym) { EL_LOGW("invoke_cb: symbol not found: %s", fn_name); return; }
ElCb2 fn = (ElCb2)sym;
fn(handle, (int64_t)(uintptr_t)(data ? data : ""));
}
/* ── JNI: callbacks from Java → C ───────────────────────────────────────── */
JNIEXPORT void JNICALL
Java_com_neuron_el_ElBridge_nativeOnClick(JNIEnv *env, jclass cls, jint slot) {
(void)env; (void)cls;
int64_t handle = (int64_t)slot;
ElWidget *w = el_widget_get(handle);
if (w && w->cb_click) {
el_android_invoke_cb(w->cb_click, handle, "");
}
}
JNIEXPORT void JNICALL
Java_com_neuron_el_ElBridge_nativeOnChange(JNIEnv *env, jclass cls,
jint slot, jstring text) {
(void)cls;
int64_t handle = (int64_t)slot;
ElWidget *w = el_widget_get(handle);
if (w && w->cb_change) {
const char *ctext = text ? (*env)->GetStringUTFChars(env, text, NULL) : "";
el_android_invoke_cb(w->cb_change, handle, ctext);
if (text) (*env)->ReleaseStringUTFChars(env, text, ctext);
}
}
JNIEXPORT void JNICALL
Java_com_neuron_el_ElBridge_nativeOnSubmit(JNIEnv *env, jclass cls,
jint slot, jstring text) {
(void)cls;
int64_t handle = (int64_t)slot;
ElWidget *w = el_widget_get(handle);
if (w && w->cb_click) { /* submit stored in cb_click, same as AppKit */
const char *ctext = text ? (*env)->GetStringUTFChars(env, text, NULL) : "";
el_android_invoke_cb(w->cb_click, handle, ctext);
if (text) (*env)->ReleaseStringUTFChars(env, text, ctext);
}
}
/* ── Helper: jstring from C string ──────────────────────────────────────── */
static jstring el_jstr(JNIEnv *env, const char *s) {
return (*env)->NewStringUTF(env, s ? s : "");
}
/* ── Window ──────────────────────────────────────────────────────────────── */
/*
* el_android_window_create — on Android a "window" is the root LinearLayout
* set as the Activity's content view. We create a vertical LinearLayout and
* store it. el_android_window_show calls setContentView on the Activity.
*/
int64_t el_android_window_create(const char *title, int width, int height,
int min_width, int min_height) {
(void)width; (void)height; (void)min_width; (void)min_height;
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return -1;
/* VERTICAL LinearLayout with no spacing (spacing added via margins in Java) */
jint slot = (*env)->CallStaticIntMethod(env, g_bridge_class,
g_mid_createLinearLayout,
(jint)1 /* VERTICAL */, (jint)0);
if ((*env)->ExceptionCheck(env)) {
(*env)->ExceptionClear(env); el_jni_detach_if_attached(); return -1;
}
/* Set activity title */
if (g_mid_setTitle && title) {
jstring jtitle = el_jstr(env, title);
(*env)->CallStaticVoidMethod(env, g_bridge_class, g_mid_setTitle, jtitle);
(*env)->DeleteLocalRef(env, jtitle);
}
int64_t handle = el_widget_alloc(EL_WIDGET_WINDOW, (int)slot);
el_jni_detach_if_attached();
return handle;
}
void el_android_window_show(int64_t handle) {
ElWidget *w = el_widget_get(handle);
if (!w || w->kind != EL_WIDGET_WINDOW) return;
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return;
(*env)->CallStaticVoidMethod(env, g_bridge_class, g_mid_setContentView,
(jint)w->slot);
if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env);
el_jni_detach_if_attached();
}
void el_android_window_set_title(int64_t handle, const char *title) {
(void)handle;
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return;
jstring jtitle = el_jstr(env, title);
(*env)->CallStaticVoidMethod(env, g_bridge_class, g_mid_setTitle, jtitle);
(*env)->DeleteLocalRef(env, jtitle);
if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env);
el_jni_detach_if_attached();
}
/* ── Layout containers ───────────────────────────────────────────────────── */
int64_t el_android_vstack_create(int spacing) {
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return -1;
jint slot = (*env)->CallStaticIntMethod(env, g_bridge_class,
g_mid_createLinearLayout,
(jint)1 /* VERTICAL */, (jint)spacing);
if ((*env)->ExceptionCheck(env)) { (*env)->ExceptionClear(env); el_jni_detach_if_attached(); return -1; }
int64_t h = el_widget_alloc(EL_WIDGET_VSTACK, (int)slot);
el_jni_detach_if_attached();
return h;
}
int64_t el_android_hstack_create(int spacing) {
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return -1;
jint slot = (*env)->CallStaticIntMethod(env, g_bridge_class,
g_mid_createLinearLayout,
(jint)0 /* HORIZONTAL */, (jint)spacing);
if ((*env)->ExceptionCheck(env)) { (*env)->ExceptionClear(env); el_jni_detach_if_attached(); return -1; }
int64_t h = el_widget_alloc(EL_WIDGET_HSTACK, (int)slot);
el_jni_detach_if_attached();
return h;
}
int64_t el_android_zstack_create(void) {
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return -1;
jint slot = (*env)->CallStaticIntMethod(env, g_bridge_class,
g_mid_createFrameLayout);
if ((*env)->ExceptionCheck(env)) { (*env)->ExceptionClear(env); el_jni_detach_if_attached(); return -1; }
int64_t h = el_widget_alloc(EL_WIDGET_ZSTACK, (int)slot);
el_jni_detach_if_attached();
return h;
}
int64_t el_android_scroll_create(void) {
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return -1;
jint slot = (*env)->CallStaticIntMethod(env, g_bridge_class,
g_mid_createScrollView);
if ((*env)->ExceptionCheck(env)) { (*env)->ExceptionClear(env); el_jni_detach_if_attached(); return -1; }
int64_t h = el_widget_alloc(EL_WIDGET_SCROLL, (int)slot);
el_jni_detach_if_attached();
return h;
}
/* ── Widget factories ─────────────────────────────────────────────────────── */
int64_t el_android_label_create(const char *text) {
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return -1;
jstring jt = el_jstr(env, text);
jint slot = (*env)->CallStaticIntMethod(env, g_bridge_class,
g_mid_createTextView, jt);
(*env)->DeleteLocalRef(env, jt);
if ((*env)->ExceptionCheck(env)) { (*env)->ExceptionClear(env); el_jni_detach_if_attached(); return -1; }
int64_t h = el_widget_alloc(EL_WIDGET_LABEL, (int)slot);
el_jni_detach_if_attached();
return h;
}
int64_t el_android_button_create(const char *label) {
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return -1;
jstring jl = el_jstr(env, label);
jint slot = (*env)->CallStaticIntMethod(env, g_bridge_class,
g_mid_createButton, jl);
(*env)->DeleteLocalRef(env, jl);
if ((*env)->ExceptionCheck(env)) { (*env)->ExceptionClear(env); el_jni_detach_if_attached(); return -1; }
int64_t h = el_widget_alloc(EL_WIDGET_BUTTON, (int)slot);
el_jni_detach_if_attached();
return h;
}
int64_t el_android_text_field_create(const char *placeholder) {
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return -1;
jstring jp = el_jstr(env, placeholder);
/* singleLine = true */
jint slot = (*env)->CallStaticIntMethod(env, g_bridge_class,
g_mid_createEditText, jp, (jboolean)JNI_TRUE);
(*env)->DeleteLocalRef(env, jp);
if ((*env)->ExceptionCheck(env)) { (*env)->ExceptionClear(env); el_jni_detach_if_attached(); return -1; }
int64_t h = el_widget_alloc(EL_WIDGET_TEXTFIELD, (int)slot);
el_jni_detach_if_attached();
return h;
}
int64_t el_android_text_area_create(const char *placeholder) {
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return -1;
jstring jp = el_jstr(env, placeholder);
/* singleLine = false → multiline EditText */
jint slot = (*env)->CallStaticIntMethod(env, g_bridge_class,
g_mid_createEditText, jp, (jboolean)JNI_FALSE);
(*env)->DeleteLocalRef(env, jp);
if ((*env)->ExceptionCheck(env)) { (*env)->ExceptionClear(env); el_jni_detach_if_attached(); return -1; }
int64_t h = el_widget_alloc(EL_WIDGET_TEXTAREA, (int)slot);
el_jni_detach_if_attached();
return h;
}
int64_t el_android_image_create(const char *path) {
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return -1;
jstring jp = el_jstr(env, path);
jint slot = (*env)->CallStaticIntMethod(env, g_bridge_class,
g_mid_createImageView, jp);
(*env)->DeleteLocalRef(env, jp);
if ((*env)->ExceptionCheck(env)) { (*env)->ExceptionClear(env); el_jni_detach_if_attached(); return -1; }
int64_t h = el_widget_alloc(EL_WIDGET_IMAGE, (int)slot);
el_jni_detach_if_attached();
return h;
}
/* ── Widget property setters ─────────────────────────────────────────────── */
void el_android_widget_set_text(int64_t handle, const char *text) {
ElWidget *w = el_widget_get(handle);
if (!w) return;
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return;
jstring jt = el_jstr(env, text);
(*env)->CallStaticVoidMethod(env, g_bridge_class, g_mid_setText,
(jint)w->slot, jt);
(*env)->DeleteLocalRef(env, jt);
if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env);
el_jni_detach_if_attached();
}
const char *el_android_widget_get_text(int64_t handle) {
ElWidget *w = el_widget_get(handle);
if (!w) return "";
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return "";
jstring js = (jstring)(*env)->CallStaticObjectMethod(env, g_bridge_class,
g_mid_getText,
(jint)w->slot);
if ((*env)->ExceptionCheck(env)) { (*env)->ExceptionClear(env); el_jni_detach_if_attached(); return ""; }
const char *result = "";
if (js) {
const char *cstr = (*env)->GetStringUTFChars(env, js, NULL);
result = cstr ? strdup(cstr) : "";
if (cstr) (*env)->ReleaseStringUTFChars(env, js, cstr);
(*env)->DeleteLocalRef(env, js);
}
el_jni_detach_if_attached();
return result;
}
void el_android_widget_set_color(int64_t handle, float r, float g, float b, float a) {
ElWidget *w = el_widget_get(handle);
if (!w) return;
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return;
(*env)->CallStaticVoidMethod(env, g_bridge_class, g_mid_setTextColor,
(jint)w->slot, (jfloat)r, (jfloat)g,
(jfloat)b, (jfloat)a);
if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env);
el_jni_detach_if_attached();
}
void el_android_widget_set_bg_color(int64_t handle, float r, float g, float b, float a) {
ElWidget *w = el_widget_get(handle);
if (!w) return;
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return;
(*env)->CallStaticVoidMethod(env, g_bridge_class, g_mid_setBackgroundColor,
(jint)w->slot, (jfloat)r, (jfloat)g,
(jfloat)b, (jfloat)a);
if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env);
el_jni_detach_if_attached();
}
void el_android_widget_set_font(int64_t handle, const char *family, int size, int bold) {
ElWidget *w = el_widget_get(handle);
if (!w) return;
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return;
jstring jfam = el_jstr(env, family);
(*env)->CallStaticVoidMethod(env, g_bridge_class, g_mid_setFont,
(jint)w->slot, jfam, (jint)size,
(jboolean)(bold ? JNI_TRUE : JNI_FALSE));
(*env)->DeleteLocalRef(env, jfam);
if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env);
el_jni_detach_if_attached();
}
void el_android_widget_set_padding(int64_t handle, int top, int right, int bottom, int left) {
ElWidget *w = el_widget_get(handle);
if (!w) return;
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return;
(*env)->CallStaticVoidMethod(env, g_bridge_class, g_mid_setPadding,
(jint)w->slot, (jint)top, (jint)right,
(jint)bottom, (jint)left);
if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env);
el_jni_detach_if_attached();
}
void el_android_widget_set_width(int64_t handle, int width) {
ElWidget *w = el_widget_get(handle);
if (!w) return;
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return;
(*env)->CallStaticVoidMethod(env, g_bridge_class, g_mid_setWidth,
(jint)w->slot, (jint)width);
if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env);
el_jni_detach_if_attached();
}
void el_android_widget_set_height(int64_t handle, int height) {
ElWidget *w = el_widget_get(handle);
if (!w) return;
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return;
(*env)->CallStaticVoidMethod(env, g_bridge_class, g_mid_setHeight,
(jint)w->slot, (jint)height);
if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env);
el_jni_detach_if_attached();
}
void el_android_widget_set_flex(int64_t handle, int flex) {
ElWidget *w = el_widget_get(handle);
if (!w) return;
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return;
(*env)->CallStaticVoidMethod(env, g_bridge_class, g_mid_setFlex,
(jint)w->slot, (jint)flex);
if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env);
el_jni_detach_if_attached();
}
void el_android_widget_set_corner_radius(int64_t handle, int radius) {
ElWidget *w = el_widget_get(handle);
if (!w) return;
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return;
(*env)->CallStaticVoidMethod(env, g_bridge_class, g_mid_setCornerRadius,
(jint)w->slot, (jfloat)radius);
if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env);
el_jni_detach_if_attached();
}
void el_android_widget_set_disabled(int64_t handle, int disabled) {
ElWidget *w = el_widget_get(handle);
if (!w) return;
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return;
(*env)->CallStaticVoidMethod(env, g_bridge_class, g_mid_setEnabled,
(jint)w->slot,
(jboolean)(disabled ? JNI_FALSE : JNI_TRUE));
if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env);
el_jni_detach_if_attached();
}
void el_android_widget_set_hidden(int64_t handle, int hidden) {
ElWidget *w = el_widget_get(handle);
if (!w) return;
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return;
/* visible=true means NOT hidden */
(*env)->CallStaticVoidMethod(env, g_bridge_class, g_mid_setVisibility,
(jint)w->slot,
(jboolean)(hidden ? JNI_FALSE : JNI_TRUE));
if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env);
el_jni_detach_if_attached();
}
/* ── Child management ─────────────────────────────────────────────────────── */
void el_android_widget_add_child(int64_t parent, int64_t child) {
ElWidget *pw = el_widget_get(parent);
ElWidget *cw = el_widget_get(child);
if (!pw || !cw) return;
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return;
(*env)->CallStaticVoidMethod(env, g_bridge_class, g_mid_addChild,
(jint)pw->slot, (jint)cw->slot);
if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env);
el_jni_detach_if_attached();
}
void el_android_widget_remove_child(int64_t parent, int64_t child) {
ElWidget *pw = el_widget_get(parent);
ElWidget *cw = el_widget_get(child);
if (!pw || !cw) return;
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return;
(*env)->CallStaticVoidMethod(env, g_bridge_class, g_mid_removeChild,
(jint)pw->slot, (jint)cw->slot);
if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env);
el_jni_detach_if_attached();
}
/* ── Event registration ───────────────────────────────────────────────────── */
void el_android_widget_on_click(int64_t handle, const char *fn_name) {
ElWidget *w = el_widget_get(handle);
if (!w) return;
free(w->cb_click);
w->cb_click = (fn_name && *fn_name) ? strdup(fn_name) : NULL;
if (!w->cb_click) return;
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return;
(*env)->CallStaticVoidMethod(env, g_bridge_class, g_mid_setOnClickListener,
(jint)w->slot);
if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env);
el_jni_detach_if_attached();
}
void el_android_widget_on_change(int64_t handle, const char *fn_name) {
ElWidget *w = el_widget_get(handle);
if (!w) return;
free(w->cb_change);
w->cb_change = (fn_name && *fn_name) ? strdup(fn_name) : NULL;
if (!w->cb_change) return;
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return;
(*env)->CallStaticVoidMethod(env, g_bridge_class, g_mid_setOnChangeListener,
(jint)w->slot);
if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env);
el_jni_detach_if_attached();
}
void el_android_widget_on_submit(int64_t handle, const char *fn_name) {
/* Submit stored in cb_click, same as AppKit. */
ElWidget *w = el_widget_get(handle);
if (!w) return;
free(w->cb_click);
w->cb_click = (fn_name && *fn_name) ? strdup(fn_name) : NULL;
if (!w->cb_click) return;
JNIEnv *env = el_jni_env();
if (!env || !g_bridge_class) return;
(*env)->CallStaticVoidMethod(env, g_bridge_class, g_mid_setOnSubmitListener,
(jint)w->slot);
if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env);
el_jni_detach_if_attached();
}
/* ── Widget destroy ───────────────────────────────────────────────────────── */
void el_android_widget_destroy(int64_t handle) {
ElWidget *w = el_widget_get(handle);
if (!w) return;
JNIEnv *env = el_jni_env();
if (env && g_bridge_class) {
(*env)->CallStaticVoidMethod(env, g_bridge_class, g_mid_destroyView,
(jint)w->slot);
if ((*env)->ExceptionCheck(env)) (*env)->ExceptionClear(env);
}
el_widget_free(handle);
el_jni_detach_if_attached();
}
/* ── Manifest reader ─────────────────────────────────────────────────────── */
/*
* __manifest_read: parse the app{} block from a manifest file.
* Returns the raw file contents as an el_val_t (const char* cast).
* The caller (el program) parses the returned string.
* Reads from the filesystem; for APK assets use the AssetManager path instead.
*/
static char *el_read_file(const char *path) {
if (!path || !*path) return NULL;
FILE *f = fopen(path, "rb");
if (!f) return NULL;
fseek(f, 0, SEEK_END);
long len = ftell(f);
fseek(f, 0, SEEK_SET);
if (len <= 0) { fclose(f); return NULL; }
char *buf = (char *)malloc((size_t)len + 1);
if (!buf) { fclose(f); return NULL; }
fread(buf, 1, (size_t)len, f);
buf[len] = '\0';
fclose(f);
return buf;
}
el_val_t el_android_manifest_read(const char *path) {
char *contents = el_read_file(path);
if (!contents) return (el_val_t)(uintptr_t)"";
return (el_val_t)(uintptr_t)contents; /* caller owns allocation */
}
/* ── __widget_* C API (called from el_seed.c) ────────────────────────────── */
/*
* These are the functions declared in el_native_target.h under EL_TARGET_ANDROID.
* They forward to the el_android_* internal functions above.
*
* The el_val_t / int64_t ABI matches the AppKit functions exactly:
* - Integer params passed as int64_t, extracted with (int)
* - String params passed as int64_t, extracted with (const char*)(uintptr_t)
* - Float params (r,g,b,a) passed as int64_t bit-cast from double; extracted
* with el_to_float / bit-cast union
*/
static inline float el_val_to_float(el_val_t v) {
union { double d; int64_t i; } u;
u.i = v;
return (float)u.d;
}
void __native_init(void) {
el_android_init();
}
void __native_run_loop(void) {
/* No-op on Android — lifecycle is driven by the Activity. */
}
el_val_t __window_create(el_val_t title, el_val_t width, el_val_t height,
el_val_t min_width, el_val_t min_height) {
return (el_val_t)el_android_window_create(
(const char *)(uintptr_t)title,
(int)width, (int)height, (int)min_width, (int)min_height);
}
void __window_show(el_val_t handle) {
el_android_window_show((int64_t)handle);
}
void __window_set_title(el_val_t handle, el_val_t title) {
el_android_window_set_title((int64_t)handle,
(const char *)(uintptr_t)title);
}
el_val_t __vstack_create(el_val_t spacing) {
return (el_val_t)el_android_vstack_create((int)spacing);
}
el_val_t __hstack_create(el_val_t spacing) {
return (el_val_t)el_android_hstack_create((int)spacing);
}
el_val_t __zstack_create(void) {
return (el_val_t)el_android_zstack_create();
}
el_val_t __scroll_create(void) {
return (el_val_t)el_android_scroll_create();
}
el_val_t __label_create(el_val_t text) {
return (el_val_t)el_android_label_create((const char *)(uintptr_t)text);
}
el_val_t __button_create(el_val_t label) {
return (el_val_t)el_android_button_create((const char *)(uintptr_t)label);
}
el_val_t __text_field_create(el_val_t placeholder) {
return (el_val_t)el_android_text_field_create((const char *)(uintptr_t)placeholder);
}
el_val_t __text_area_create(el_val_t placeholder) {
return (el_val_t)el_android_text_area_create((const char *)(uintptr_t)placeholder);
}
el_val_t __image_create(el_val_t path_or_name) {
return (el_val_t)el_android_image_create((const char *)(uintptr_t)path_or_name);
}
void __widget_set_text(el_val_t handle, el_val_t text) {
el_android_widget_set_text((int64_t)handle,
(const char *)(uintptr_t)text);
}
el_val_t __widget_get_text(el_val_t handle) {
return (el_val_t)(uintptr_t)el_android_widget_get_text((int64_t)handle);
}
void __widget_set_color(el_val_t handle, el_val_t r, el_val_t g,
el_val_t b, el_val_t a) {
el_android_widget_set_color((int64_t)handle,
el_val_to_float(r), el_val_to_float(g),
el_val_to_float(b), el_val_to_float(a));
}
void __widget_set_bg_color(el_val_t handle, el_val_t r, el_val_t g,
el_val_t b, el_val_t a) {
el_android_widget_set_bg_color((int64_t)handle,
el_val_to_float(r), el_val_to_float(g),
el_val_to_float(b), el_val_to_float(a));
}
void __widget_set_font(el_val_t handle, el_val_t family,
el_val_t size, el_val_t bold) {
el_android_widget_set_font((int64_t)handle,
(const char *)(uintptr_t)family,
(int)size, (int)bold);
}
void __widget_set_padding(el_val_t handle, el_val_t top, el_val_t right,
el_val_t bottom, el_val_t left) {
el_android_widget_set_padding((int64_t)handle,
(int)top, (int)right, (int)bottom, (int)left);
}
void __widget_set_width(el_val_t handle, el_val_t width) {
el_android_widget_set_width((int64_t)handle, (int)width);
}
void __widget_set_height(el_val_t handle, el_val_t height) {
el_android_widget_set_height((int64_t)handle, (int)height);
}
void __widget_set_flex(el_val_t handle, el_val_t flex) {
el_android_widget_set_flex((int64_t)handle, (int)flex);
}
void __widget_set_corner_radius(el_val_t handle, el_val_t radius) {
el_android_widget_set_corner_radius((int64_t)handle, (int)radius);
}
void __widget_set_disabled(el_val_t handle, el_val_t disabled) {
el_android_widget_set_disabled((int64_t)handle, (int)disabled);
}
void __widget_set_hidden(el_val_t handle, el_val_t hidden) {
el_android_widget_set_hidden((int64_t)handle, (int)hidden);
}
void __widget_add_child(el_val_t parent, el_val_t child) {
el_android_widget_add_child((int64_t)parent, (int64_t)child);
}
void __widget_remove_child(el_val_t parent, el_val_t child) {
el_android_widget_remove_child((int64_t)parent, (int64_t)child);
}
void __widget_destroy(el_val_t handle) {
el_android_widget_destroy((int64_t)handle);
}
void __widget_on_click(el_val_t handle, el_val_t fn_name) {
el_android_widget_on_click((int64_t)handle,
(const char *)(uintptr_t)fn_name);
}
void __widget_on_change(el_val_t handle, el_val_t fn_name) {
el_android_widget_on_change((int64_t)handle,
(const char *)(uintptr_t)fn_name);
}
void __widget_on_submit(el_val_t handle, el_val_t fn_name) {
el_android_widget_on_submit((int64_t)handle,
(const char *)(uintptr_t)fn_name);
}
el_val_t __manifest_read(el_val_t path) {
return el_android_manifest_read((const char *)(uintptr_t)path);
}
#endif /* EL_TARGET_ANDROID */
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
+844
View File
@@ -0,0 +1,844 @@
/*
* el_lvgl.c — LVGL v9 backend for the el native widget system.
*
* This file implements the microcontroller/embedded widget layer that el_seed.c
* calls through to when EL_TARGET_LVGL is defined.
*
* Architecture:
* el program (el code)
* → __widget_* C builtins in el_seed.c
* → el_lvgl_* C functions defined here
* → lv_obj_t widgets via LVGL v9
*
* Target platforms: ESP32, STM32, industrial panels, any system with 256KB+
* RAM and an LVGL-compatible display driver. No OS required.
*
* Widget handles: every widget is assigned an int64_t slot index into
* g_widgets[]. The el program holds these as opaque Int values.
* Slot 0 is reserved; -1 = invalid handle.
*
* Window model: on embedded there is one screen. __window_create configures
* lv_scr_act() as the root container. __window_show is a no-op (the screen
* is always visible). __native_run_loop calls lv_task_handler() in a tight
* loop — on RTOS this runs inside a dedicated task; on bare metal it IS the
* main loop. The host application is responsible for initialising the display
* driver and calling lv_tick_inc() before calling __native_run_loop.
*
* Callback dispatch:
* When EL_LVGL_NO_DLSYM is NOT defined (hosted Linux, testing):
* dlsym(RTLD_DEFAULT, fn_name) resolves the El function symbol at runtime.
* When EL_LVGL_NO_DLSYM IS defined (bare-metal ESP32/STM32):
* The caller must provide:
* el_val_t el_lvgl_dispatch(const char *fn, el_val_t a, el_val_t b);
* which maps function names to function pointers via a compile-time table.
*
* Font mapping: LVGL v9 ships Montserrat in discrete sizes. __widget_set_font
* maps the requested point size to the nearest available Montserrat variant.
* Bold is approximated by stepping up two sizes (no separate bold face in the
* default LVGL font set). Define EL_LVGL_CUSTOM_FONT to override font_select().
*
* Compile (hosted test build):
* gcc -DEL_TARGET_LVGL -I./lvgl el_lvgl.c -c -o el_lvgl.o
* # Then link with lvgl.a / lvgl source tree.
*
* Compile (bare-metal, no dynamic linker):
* arm-none-eabi-gcc -DEL_TARGET_LVGL -DEL_LVGL_NO_DLSYM \
* -I./lvgl el_lvgl.c -c -o el_lvgl.o
*/
#ifdef EL_TARGET_LVGL
#include "lvgl/lvgl.h"
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#ifndef EL_LVGL_NO_DLSYM
#include <dlfcn.h>
#endif
#include "el_runtime.h"
/* ── Callback dispatch macro ─────────────────────────────────────────────── */
#ifdef EL_LVGL_NO_DLSYM
/*
* Bare-metal path. The application provides this function:
* el_val_t el_lvgl_dispatch(const char *fn, el_val_t a, el_val_t b);
* It maps string names → function pointers, typically via a switch on a hash
* or a sorted table of {name, fn_ptr} pairs generated by elc.
*/
extern el_val_t el_lvgl_dispatch(const char *fn, el_val_t a, el_val_t b);
#define EL_LVGL_CALL(fn_name, a, b) el_lvgl_dispatch((fn_name), (a), (b))
#else
/*
* Hosted path. dlsym resolves the El symbol at call time.
* We use a compound-statement expression (GCC/Clang extension) to avoid
* executing dlsym more than once per call.
*/
#define EL_LVGL_CALL(fn_name, a, b) \
({ \
typedef el_val_t (*_el_fn_t)(el_val_t, el_val_t); \
_el_fn_t _fn = (_el_fn_t)(uintptr_t)dlsym(RTLD_DEFAULT, (fn_name)); \
_fn ? _fn((a), (b)) : (el_val_t)0; \
})
#endif
/* ── Widget table ─────────────────────────────────────────────────────────── */
#define EL_LVGL_MAX_WIDGETS 4096
/*
* Widget kinds — mirrors AppKit/GTK4 backends so future tooling can stay
* consistent across all targets.
*/
typedef enum {
EL_LVGL_FREE = 0,
EL_LVGL_WINDOW = 1,
EL_LVGL_VSTACK = 2,
EL_LVGL_HSTACK = 3,
EL_LVGL_ZSTACK = 4,
EL_LVGL_SCROLL = 5,
EL_LVGL_LABEL = 6,
EL_LVGL_BUTTON = 7, /* lv_btn_create; inner label at slot_btn_label */
EL_LVGL_TEXTFIELD = 8, /* lv_textarea, one-line */
EL_LVGL_TEXTAREA = 9, /* lv_textarea, multiline */
EL_LVGL_IMAGE = 10,
EL_LVGL_DIVIDER = 11,
EL_LVGL_SPACER = 12,
} ElLvglKind;
/*
* Per-slot state. Callback names are stored inline (256 bytes each) to avoid
* heap allocation on targets with no malloc or fragmented heaps.
*/
typedef struct {
ElLvglKind kind;
lv_obj_t *obj; /* primary LVGL object */
lv_obj_t *btn_label; /* for EL_LVGL_BUTTON: inner lv_label child */
char cb_click[256];
char cb_change[256];
char cb_submit[256];
} ElLvglWidget;
static ElLvglWidget g_widgets[EL_LVGL_MAX_WIDGETS];
/* ── Slot helpers ─────────────────────────────────────────────────────────── */
static int64_t lvgl_slot_alloc(ElLvglKind kind, lv_obj_t *obj) {
for (int i = 1; i < EL_LVGL_MAX_WIDGETS; i++) {
if (g_widgets[i].kind == EL_LVGL_FREE) {
g_widgets[i].kind = kind;
g_widgets[i].obj = obj;
g_widgets[i].btn_label = NULL;
g_widgets[i].cb_click[0] = '\0';
g_widgets[i].cb_change[0] = '\0';
g_widgets[i].cb_submit[0] = '\0';
return (int64_t)i;
}
}
return -1; /* table full */
}
static ElLvglWidget *lvgl_slot_get(int64_t handle) {
if (handle <= 0 || handle >= EL_LVGL_MAX_WIDGETS) return NULL;
if (g_widgets[handle].kind == EL_LVGL_FREE) return NULL;
return &g_widgets[handle];
}
static void lvgl_slot_free(int64_t handle) {
if (handle <= 0 || handle >= EL_LVGL_MAX_WIDGETS) return;
ElLvglWidget *w = &g_widgets[handle];
w->kind = EL_LVGL_FREE;
w->obj = NULL;
w->btn_label = NULL;
w->cb_click[0] = '\0';
w->cb_change[0] = '\0';
w->cb_submit[0] = '\0';
}
/* ── Font selection ───────────────────────────────────────────────────────── */
/*
* LVGL ships Montserrat in the following sizes (subset enabled by lv_conf.h):
* 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48
*
* We map the requested size to the nearest available size. Bold is
* approximated by stepping up two sizes (no separate bold face in the default
* font set). Define EL_LVGL_CUSTOM_FONT to replace this function entirely.
*/
#ifndef EL_LVGL_CUSTOM_FONT
static const lv_font_t *font_select(int size, int bold) {
/* Step up two sizes for bold approximation. */
if (bold) size += 4;
/* Clamp to available range. */
if (size < 8) size = 8;
if (size > 48) size = 48;
/* Round to nearest even size >= 8. */
if (size % 2 != 0) size++;
switch (size) {
#if LV_FONT_MONTSERRAT_8
case 8: return &lv_font_montserrat_8;
#endif
#if LV_FONT_MONTSERRAT_10
case 10: return &lv_font_montserrat_10;
#endif
#if LV_FONT_MONTSERRAT_12
case 12: return &lv_font_montserrat_12;
#endif
#if LV_FONT_MONTSERRAT_14
case 14: return &lv_font_montserrat_14;
#endif
#if LV_FONT_MONTSERRAT_16
case 16: return &lv_font_montserrat_16;
#endif
#if LV_FONT_MONTSERRAT_18
case 18: return &lv_font_montserrat_18;
#endif
#if LV_FONT_MONTSERRAT_20
case 20: return &lv_font_montserrat_20;
#endif
#if LV_FONT_MONTSERRAT_22
case 22: return &lv_font_montserrat_22;
#endif
#if LV_FONT_MONTSERRAT_24
case 24: return &lv_font_montserrat_24;
#endif
#if LV_FONT_MONTSERRAT_26
case 26: return &lv_font_montserrat_26;
#endif
#if LV_FONT_MONTSERRAT_28
case 28: return &lv_font_montserrat_28;
#endif
#if LV_FONT_MONTSERRAT_30
case 30: return &lv_font_montserrat_30;
#endif
#if LV_FONT_MONTSERRAT_32
case 32: return &lv_font_montserrat_32;
#endif
#if LV_FONT_MONTSERRAT_34
case 34: return &lv_font_montserrat_34;
#endif
#if LV_FONT_MONTSERRAT_36
case 36: return &lv_font_montserrat_36;
#endif
#if LV_FONT_MONTSERRAT_38
case 38: return &lv_font_montserrat_38;
#endif
#if LV_FONT_MONTSERRAT_40
case 40: return &lv_font_montserrat_40;
#endif
#if LV_FONT_MONTSERRAT_42
case 42: return &lv_font_montserrat_42;
#endif
#if LV_FONT_MONTSERRAT_44
case 44: return &lv_font_montserrat_44;
#endif
#if LV_FONT_MONTSERRAT_46
case 46: return &lv_font_montserrat_46;
#endif
#if LV_FONT_MONTSERRAT_48
case 48: return &lv_font_montserrat_48;
#endif
default:
/*
* Requested size is not compiled in. Fall back to the default
* theme font, which is guaranteed to be present.
*/
return LV_FONT_DEFAULT;
}
}
#endif /* EL_LVGL_CUSTOM_FONT */
/* ── Event callback ───────────────────────────────────────────────────────── */
/*
* Single LVGL event callback used for all widget events. The user_data is
* the slot index cast to (void*) via intptr_t — avoids heap allocation.
*
* Three event codes are handled:
* LV_EVENT_CLICKED → cb_click (buttons, any tappable widget)
* LV_EVENT_VALUE_CHANGED → cb_change (textarea, checkbox, etc.)
* LV_EVENT_READY → cb_submit (Enter pressed in textarea one-line mode)
*/
static void el_lvgl_event_cb(lv_event_t *e) {
lv_event_code_t code = lv_event_get_code(e);
intptr_t slot = (intptr_t)lv_event_get_user_data(e);
ElLvglWidget *w = lvgl_slot_get((int64_t)slot);
if (!w) return;
if (code == LV_EVENT_CLICKED && w->cb_click[0]) {
EL_LVGL_CALL(w->cb_click, (el_val_t)slot, (el_val_t)0);
}
if (code == LV_EVENT_VALUE_CHANGED && w->cb_change[0]) {
/*
* Retrieve current text for textarea/textfield widgets so the handler
* receives the updated value as its second argument.
*/
const char *txt = "";
lv_obj_t *target = lv_event_get_target(e);
if (w->kind == EL_LVGL_TEXTFIELD || w->kind == EL_LVGL_TEXTAREA) {
txt = lv_textarea_get_text(target);
if (!txt) txt = "";
}
EL_LVGL_CALL(w->cb_change, (el_val_t)slot,
(el_val_t)(uintptr_t)txt);
}
if (code == LV_EVENT_READY && w->cb_submit[0]) {
/* LV_EVENT_READY fires when Enter is pressed in a one-line textarea. */
EL_LVGL_CALL(w->cb_submit, (el_val_t)slot, (el_val_t)0);
}
}
/* ── Initialisation ───────────────────────────────────────────────────────── */
/*
* el_lvgl_init — call lv_init(). The host must have already initialised the
* display driver and input driver before this, or immediately after. Idempotent.
*/
void el_lvgl_init(void) {
static int done = 0;
if (done) return;
done = 1;
lv_init();
}
/*
* el_lvgl_run_loop — drive lv_task_handler() indefinitely.
*
* On RTOS: this function should run inside a dedicated FreeRTOS/Zephyr task.
* On bare metal: call this as the last statement of main().
*
* The 5 ms delay between handler calls matches the LVGL documentation
* recommendation for a ~200 Hz refresh budget.
*
* On hosted Linux (EL_LVGL_SDL or similar), usleep(5000) is used. On RTOS
* targets define EL_LVGL_RTOS_DELAY(ms) to map to vTaskDelay/k_sleep/etc.
*/
void el_lvgl_run_loop(void) {
for (;;) {
lv_task_handler();
#if defined(EL_LVGL_RTOS_DELAY)
EL_LVGL_RTOS_DELAY(5);
#elif defined(__linux__) || defined(__APPLE__)
{
/* Hosted test build — usleep available. */
#include <unistd.h>
usleep(5000);
}
#endif
/* Bare-metal without a delay macro: the HAL tick increment loop
* is the caller's responsibility. No sleep needed if lv_tick_inc()
* is driven from a hardware timer ISR. */
}
}
/* ── Window ───────────────────────────────────────────────────────────────── */
/*
* el_lvgl_window_create — configure lv_scr_act() as a vertical flex container
* and return a slot handle wrapping it. The title is stored for informational
* purposes (e.g., a status bar widget the host might create). Width/height
* are ignored on embedded targets because the screen size is fixed by the
* display driver; they are accepted for API compatibility with other backends.
*/
int64_t el_lvgl_window_create(const char *title, int width, int height,
int min_width, int min_height) {
(void)width; (void)height; (void)min_width; (void)min_height;
lv_obj_t *scr = lv_scr_act();
/* Configure the active screen as a vertical flex container so that
* widgets added via __widget_add_child stack naturally. */
lv_obj_set_flex_flow(scr, LV_FLEX_FLOW_COLUMN);
lv_obj_set_size(scr, LV_PCT(100), LV_PCT(100));
/* Store the window title in a user-data string on the screen object
* so host code can retrieve it if it wants to render a title bar. */
if (title && *title) {
/* lv_obj_set_user_data stores a void* — we cast the string pointer.
* The string must outlive the screen object; for literals this is
* always true. For dynamic titles use el_lvgl_window_set_title. */
lv_obj_set_user_data(scr, (void *)(uintptr_t)title);
}
/* Allocate a slot for the screen object. */
int64_t h = lvgl_slot_alloc(EL_LVGL_WINDOW, scr);
return h;
}
/*
* el_lvgl_window_show — no-op on embedded. The screen is always visible.
*/
void el_lvgl_window_show(int64_t handle) {
(void)handle;
/* On multi-screen setups, load the screen: */
/* ElLvglWidget *w = lvgl_slot_get(handle);
* if (w) lv_scr_load(w->obj); */
}
/*
* el_lvgl_window_set_title — update the user_data pointer on the screen object.
* On embedded, "title" is typically only used by a custom host title bar.
*/
void el_lvgl_window_set_title(int64_t handle, const char *title) {
ElLvglWidget *w = lvgl_slot_get(handle);
if (!w || w->kind != EL_LVGL_WINDOW) return;
lv_obj_set_user_data(w->obj, (void *)(uintptr_t)(title ? title : ""));
}
/* ── Layout containers ────────────────────────────────────────────────────── */
/*
* el_lvgl_vstack_create — vertical flex column with inter-item gap = spacing.
*/
int64_t el_lvgl_vstack_create(int spacing) {
lv_obj_t *obj = lv_obj_create(lv_scr_act());
lv_obj_set_flex_flow(obj, LV_FLEX_FLOW_COLUMN);
lv_obj_set_style_pad_row(obj, (lv_coord_t)spacing, 0);
lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
/* Remove default LVGL border and background so containers are transparent
* by default, matching the AppKit/GTK4 backends. */
lv_obj_set_style_border_width(obj, 0, 0);
lv_obj_set_style_bg_opa(obj, LV_OPA_TRANSP, 0);
lv_obj_set_style_pad_all(obj, 0, 0);
return lvgl_slot_alloc(EL_LVGL_VSTACK, obj);
}
/*
* el_lvgl_hstack_create — horizontal flex row with inter-item gap = spacing.
*/
int64_t el_lvgl_hstack_create(int spacing) {
lv_obj_t *obj = lv_obj_create(lv_scr_act());
lv_obj_set_flex_flow(obj, LV_FLEX_FLOW_ROW);
lv_obj_set_style_pad_column(obj, (lv_coord_t)spacing, 0);
lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
lv_obj_set_style_border_width(obj, 0, 0);
lv_obj_set_style_bg_opa(obj, LV_OPA_TRANSP, 0);
lv_obj_set_style_pad_all(obj, 0, 0);
return lvgl_slot_alloc(EL_LVGL_HSTACK, obj);
}
/*
* el_lvgl_zstack_create — plain container, children positioned absolutely.
* No flex flow is set; callers use lv_obj_set_pos() on children directly,
* or rely on their natural 0,0 origin.
*/
int64_t el_lvgl_zstack_create(void) {
lv_obj_t *obj = lv_obj_create(lv_scr_act());
lv_obj_set_size(obj, LV_SIZE_CONTENT, LV_SIZE_CONTENT);
lv_obj_set_style_border_width(obj, 0, 0);
lv_obj_set_style_bg_opa(obj, LV_OPA_TRANSP, 0);
lv_obj_set_style_pad_all(obj, 0, 0);
return lvgl_slot_alloc(EL_LVGL_ZSTACK, obj);
}
/*
* el_lvgl_scroll_create — vertically scrollable container.
*/
int64_t el_lvgl_scroll_create(void) {
lv_obj_t *obj = lv_obj_create(lv_scr_act());
lv_obj_set_scroll_dir(obj, LV_DIR_VER);
lv_obj_set_flex_flow(obj, LV_FLEX_FLOW_COLUMN);
lv_obj_set_size(obj, LV_PCT(100), LV_SIZE_CONTENT);
lv_obj_set_style_border_width(obj, 0, 0);
lv_obj_set_style_pad_all(obj, 0, 0);
return lvgl_slot_alloc(EL_LVGL_SCROLL, obj);
}
/* ── Widget factories ─────────────────────────────────────────────────────── */
/*
* el_lvgl_label_create — static text label.
*/
int64_t el_lvgl_label_create(const char *text) {
lv_obj_t *obj = lv_label_create(lv_scr_act());
lv_label_set_text(obj, text ? text : "");
return lvgl_slot_alloc(EL_LVGL_LABEL, obj);
}
/*
* el_lvgl_button_create — pressable button with a child label.
*
* LVGL buttons are containers; text is placed in an inner lv_label child.
* We store the child label pointer in btn_label so set_text / get_text can
* reach it without searching the object tree at runtime.
*/
int64_t el_lvgl_button_create(const char *label) {
lv_obj_t *btn = lv_btn_create(lv_scr_act());
lv_obj_t *lbl = lv_label_create(btn);
lv_label_set_text(lbl, label ? label : "");
lv_obj_center(lbl);
int64_t h = lvgl_slot_alloc(EL_LVGL_BUTTON, btn);
if (h >= 0) {
g_widgets[h].btn_label = lbl;
/* Register click callback immediately so button responds when a
* callback name is registered later via __widget_on_click. */
lv_obj_add_event_cb(btn, el_lvgl_event_cb, LV_EVENT_CLICKED,
(void *)(intptr_t)h);
}
return h;
}
/*
* el_lvgl_text_field_create — single-line text input.
*/
int64_t el_lvgl_text_field_create(const char *placeholder) {
lv_obj_t *obj = lv_textarea_create(lv_scr_act());
lv_textarea_set_one_line(obj, true);
if (placeholder && *placeholder) {
lv_textarea_set_placeholder_text(obj, placeholder);
}
int64_t h = lvgl_slot_alloc(EL_LVGL_TEXTFIELD, obj);
if (h >= 0) {
lv_obj_add_event_cb(obj, el_lvgl_event_cb, LV_EVENT_VALUE_CHANGED,
(void *)(intptr_t)h);
lv_obj_add_event_cb(obj, el_lvgl_event_cb, LV_EVENT_READY,
(void *)(intptr_t)h);
}
return h;
}
/*
* el_lvgl_text_area_create — multi-line text input.
*/
int64_t el_lvgl_text_area_create(const char *placeholder) {
lv_obj_t *obj = lv_textarea_create(lv_scr_act());
lv_textarea_set_one_line(obj, false);
if (placeholder && *placeholder) {
lv_textarea_set_placeholder_text(obj, placeholder);
}
int64_t h = lvgl_slot_alloc(EL_LVGL_TEXTAREA, obj);
if (h >= 0) {
lv_obj_add_event_cb(obj, el_lvgl_event_cb, LV_EVENT_VALUE_CHANGED,
(void *)(intptr_t)h);
}
return h;
}
/*
* el_lvgl_image_create — image widget.
*
* On hosted Linux (SDL backend), path is a filesystem path.
* On embedded with SPIFFS/LittleFS, path is a SPIFFS URI: "S:/image.bin".
* LVGL image decoders are registered separately by the host application.
*/
int64_t el_lvgl_image_create(const char *path_or_name) {
lv_obj_t *obj = lv_img_create(lv_scr_act());
if (path_or_name && *path_or_name) {
lv_img_set_src(obj, path_or_name);
}
return lvgl_slot_alloc(EL_LVGL_IMAGE, obj);
}
/* ── Widget property setters ─────────────────────────────────────────────── */
/*
* el_lvgl_widget_set_text — update visible text.
*
* Dispatch per kind:
* LABEL → lv_label_set_text
* BUTTON → lv_label_set_text on inner btn_label child
* TEXTFIELD / TEXTAREA → lv_textarea_set_text
* WINDOW → lv_obj_set_user_data (stores title string)
*/
void el_lvgl_widget_set_text(int64_t handle, const char *text) {
ElLvglWidget *w = lvgl_slot_get(handle);
if (!w) return;
const char *t = text ? text : "";
switch (w->kind) {
case EL_LVGL_LABEL:
lv_label_set_text(w->obj, t);
break;
case EL_LVGL_BUTTON:
if (w->btn_label) lv_label_set_text(w->btn_label, t);
break;
case EL_LVGL_TEXTFIELD:
case EL_LVGL_TEXTAREA:
lv_textarea_set_text(w->obj, t);
break;
case EL_LVGL_WINDOW:
lv_obj_set_user_data(w->obj, (void *)(uintptr_t)t);
break;
default:
break;
}
}
/*
* el_lvgl_widget_get_text — retrieve visible text.
*
* Returns a pointer into LVGL's internal storage — valid until the next LVGL
* operation that modifies the widget. Callers that need to hold the value
* across LVGL calls must strdup() it.
*/
const char *el_lvgl_widget_get_text(int64_t handle) {
ElLvglWidget *w = lvgl_slot_get(handle);
if (!w) return "";
switch (w->kind) {
case EL_LVGL_LABEL:
return lv_label_get_text(w->obj);
case EL_LVGL_BUTTON:
return w->btn_label ? lv_label_get_text(w->btn_label) : "";
case EL_LVGL_TEXTFIELD:
case EL_LVGL_TEXTAREA:
return lv_textarea_get_text(w->obj);
default:
return "";
}
}
/*
* el_lvgl_widget_set_color — foreground (text) colour.
*
* r/g/b are 0.01.0 floats bit-cast as el_val_t (see el_runtime.h).
* LVGL lv_color_make takes uint8_t 0255 components.
*/
void el_lvgl_widget_set_color(int64_t handle,
float r, float g, float b, float a) {
(void)a; /* LVGL text colour has no per-glyph alpha channel */
ElLvglWidget *w = lvgl_slot_get(handle);
if (!w) return;
lv_color_t c = lv_color_make(
(uint8_t)(r * 255.0f + 0.5f),
(uint8_t)(g * 255.0f + 0.5f),
(uint8_t)(b * 255.0f + 0.5f));
lv_obj_set_style_text_color(w->obj, c, 0);
if (w->kind == EL_LVGL_BUTTON && w->btn_label) {
lv_obj_set_style_text_color(w->btn_label, c, 0);
}
}
/*
* el_lvgl_widget_set_bg_color — background fill colour + opacity.
*/
void el_lvgl_widget_set_bg_color(int64_t handle,
float r, float g, float b, float a) {
ElLvglWidget *w = lvgl_slot_get(handle);
if (!w) return;
lv_color_t c = lv_color_make(
(uint8_t)(r * 255.0f + 0.5f),
(uint8_t)(g * 255.0f + 0.5f),
(uint8_t)(b * 255.0f + 0.5f));
lv_opa_t opa = (lv_opa_t)(a * 255.0f + 0.5f);
lv_obj_set_style_bg_color(w->obj, c, 0);
lv_obj_set_style_bg_opa(w->obj, opa, 0);
}
/*
* el_lvgl_widget_set_font — apply font to text-bearing widget.
*
* The `family` parameter is accepted for API compatibility but LVGL uses
* compiled-in fonts only. Only the size and bold flag have effect unless
* EL_LVGL_CUSTOM_FONT is defined by the host.
*/
void el_lvgl_widget_set_font(int64_t handle,
const char *family, int size, int bold) {
(void)family; /* ignored; LVGL uses compiled-in Montserrat fonts */
ElLvglWidget *w = lvgl_slot_get(handle);
if (!w) return;
const lv_font_t *font = font_select(size, bold);
if (!font) return;
lv_obj_set_style_text_font(w->obj, font, 0);
if (w->kind == EL_LVGL_BUTTON && w->btn_label) {
lv_obj_set_style_text_font(w->btn_label, font, 0);
}
}
/*
* el_lvgl_widget_set_padding — set per-side padding (top/right/bottom/left).
*/
void el_lvgl_widget_set_padding(int64_t handle,
int top, int right, int bottom, int left) {
ElLvglWidget *w = lvgl_slot_get(handle);
if (!w) return;
lv_obj_set_style_pad_top(w->obj, (lv_coord_t)top, 0);
lv_obj_set_style_pad_right(w->obj, (lv_coord_t)right, 0);
lv_obj_set_style_pad_bottom(w->obj, (lv_coord_t)bottom, 0);
lv_obj_set_style_pad_left(w->obj, (lv_coord_t)left, 0);
}
/*
* el_lvgl_widget_set_width — set explicit pixel width.
*/
void el_lvgl_widget_set_width(int64_t handle, int width) {
ElLvglWidget *w = lvgl_slot_get(handle);
if (!w) return;
lv_obj_set_width(w->obj, (lv_coord_t)width);
}
/*
* el_lvgl_widget_set_height — set explicit pixel height.
*/
void el_lvgl_widget_set_height(int64_t handle, int height) {
ElLvglWidget *w = lvgl_slot_get(handle);
if (!w) return;
lv_obj_set_height(w->obj, (lv_coord_t)height);
}
/*
* el_lvgl_widget_set_flex — set flex grow factor.
*
* flex > 0 → lv_obj_set_flex_grow(obj, flex): object expands to fill
* remaining space proportional to its grow factor.
* flex == 0 → lv_obj_set_flex_grow(obj, 0): object uses natural size.
*/
void el_lvgl_widget_set_flex(int64_t handle, int flex) {
ElLvglWidget *w = lvgl_slot_get(handle);
if (!w) return;
lv_obj_set_flex_grow(w->obj, (uint8_t)(flex > 0 ? flex : 0));
}
/*
* el_lvgl_widget_set_corner_radius — set border radius.
*/
void el_lvgl_widget_set_corner_radius(int64_t handle, int radius) {
ElLvglWidget *w = lvgl_slot_get(handle);
if (!w) return;
lv_obj_set_style_radius(w->obj, (lv_coord_t)radius, 0);
}
/*
* el_lvgl_widget_set_disabled — enable/disable interactive state.
*
* LV_STATE_DISABLED greys out the widget and prevents input events.
*/
void el_lvgl_widget_set_disabled(int64_t handle, int disabled) {
ElLvglWidget *w = lvgl_slot_get(handle);
if (!w) return;
if (disabled) {
lv_obj_add_state(w->obj, LV_STATE_DISABLED);
} else {
lv_obj_clear_state(w->obj, LV_STATE_DISABLED);
}
}
/*
* el_lvgl_widget_set_hidden — show/hide widget.
*
* LV_OBJ_FLAG_HIDDEN hides the widget and removes it from layout flow.
*/
void el_lvgl_widget_set_hidden(int64_t handle, int hidden) {
ElLvglWidget *w = lvgl_slot_get(handle);
if (!w) return;
if (hidden) {
lv_obj_add_flag(w->obj, LV_OBJ_FLAG_HIDDEN);
} else {
lv_obj_clear_flag(w->obj, LV_OBJ_FLAG_HIDDEN);
}
}
/* ── Tree operations ──────────────────────────────────────────────────────── */
/*
* el_lvgl_widget_add_child — attach child widget to parent.
*
* lv_obj_set_parent() reparents the child object inside the LVGL tree.
* For WINDOW parents we use the screen object itself as the parent, since
* lv_scr_act() IS the root container.
*/
void el_lvgl_widget_add_child(int64_t parent, int64_t child) {
ElLvglWidget *pw = lvgl_slot_get(parent);
ElLvglWidget *cw = lvgl_slot_get(child);
if (!pw || !cw) return;
lv_obj_set_parent(cw->obj, pw->obj);
}
/*
* el_lvgl_widget_remove_child — detach child from its current parent.
*
* LVGL has no explicit "remove from parent without deleting" operation.
* We reparent the child back to the active screen (making it a root-level
* floating widget) and then hide it. The widget still occupies a slot and
* can be re-attached or destroyed later.
*/
void el_lvgl_widget_remove_child(int64_t parent, int64_t child) {
(void)parent;
ElLvglWidget *cw = lvgl_slot_get(child);
if (!cw) return;
/* Move to screen root and hide. */
lv_obj_set_parent(cw->obj, lv_scr_act());
lv_obj_add_flag(cw->obj, LV_OBJ_FLAG_HIDDEN);
}
/*
* el_lvgl_widget_destroy — delete widget and its children from the LVGL tree,
* then free the slot.
*
* lv_obj_del() recursively deletes the object and all children. After this
* call the handle is invalid and must not be used.
*/
void el_lvgl_widget_destroy(int64_t handle) {
ElLvglWidget *w = lvgl_slot_get(handle);
if (!w) return;
lv_obj_del(w->obj);
lvgl_slot_free(handle);
}
/* ── Event registration ───────────────────────────────────────────────────── */
/*
* Event registration stores the El function name in the widget slot. The
* actual lv_obj_add_event_cb() call is made here (or was made in the factory
* for buttons/textfields where we know the relevant event codes upfront).
*
* For widgets that did not register their event callback in the factory (e.g.
* labels receiving a click handler), we add the LVGL event binding now.
*/
void el_lvgl_widget_on_click(int64_t handle, const char *fn_name) {
ElLvglWidget *w = lvgl_slot_get(handle);
if (!w) return;
strncpy(w->cb_click, fn_name ? fn_name : "", 255);
w->cb_click[255] = '\0';
/*
* Buttons already have LV_EVENT_CLICKED registered in the factory.
* For other widget kinds (labels, containers used as tap targets), add
* the click flag and register the callback.
*/
if (w->kind != EL_LVGL_BUTTON) {
lv_obj_add_flag(w->obj, LV_OBJ_FLAG_CLICKABLE);
lv_obj_add_event_cb(w->obj, el_lvgl_event_cb, LV_EVENT_CLICKED,
(void *)(intptr_t)handle);
}
}
void el_lvgl_widget_on_change(int64_t handle, const char *fn_name) {
ElLvglWidget *w = lvgl_slot_get(handle);
if (!w) return;
strncpy(w->cb_change, fn_name ? fn_name : "", 255);
w->cb_change[255] = '\0';
/*
* Textfield/textarea factories already register VALUE_CHANGED.
* For other kinds (e.g. a custom toggle), add the binding now.
*/
if (w->kind != EL_LVGL_TEXTFIELD && w->kind != EL_LVGL_TEXTAREA) {
lv_obj_add_event_cb(w->obj, el_lvgl_event_cb, LV_EVENT_VALUE_CHANGED,
(void *)(intptr_t)handle);
}
}
void el_lvgl_widget_on_submit(int64_t handle, const char *fn_name) {
ElLvglWidget *w = lvgl_slot_get(handle);
if (!w) return;
strncpy(w->cb_submit, fn_name ? fn_name : "", 255);
w->cb_submit[255] = '\0';
/*
* LV_EVENT_READY fires when Enter is pressed in a one-line textarea.
* Textfield factories already register READY. For other kinds, add it.
*/
if (w->kind != EL_LVGL_TEXTFIELD) {
lv_obj_add_event_cb(w->obj, el_lvgl_event_cb, LV_EVENT_READY,
(void *)(intptr_t)handle);
}
}
#endif /* EL_TARGET_LVGL */
+574
View File
@@ -0,0 +1,574 @@
/*
* el_native_target.h — Native widget declarations for el programs targeting
* native desktop UI (AppKit / GTK4 / Win32).
*
* This header is designed to be included AFTER el_runtime.h without conflict:
* - It does NOT redefine el_to_float, el_from_float, or any el_runtime.h
* static inlines.
* - It does NOT redeclare __println, __print, or other functions whose
* return types differ between el_seed.h and el_runtime.h.
* - It adds: native widget builtins + float arithmetic helpers that the
* current el_runtime.h omits but elc still emits calls to.
*
* Usage:
* Inject via -include at compile time, OR #include it after el_runtime.h.
*
* clang -DEL_TARGET_MACOS -include el_native_target.h -c my_app.c ...
*/
#pragma once
#include <stdint.h>
#include <stdlib.h>
/* el_val_t must already be defined by el_runtime.h or el_seed.h. */
#ifndef EL_VAL_T_DEFINED
typedef int64_t el_val_t;
#endif
/* ── Float arithmetic helpers ───────────────────────────────────────────────
* elc emits calls to float_div / float_mul etc. for Float-typed expressions.
* These were in el_runtime.c through v1.0.0-20260501 but are missing from the
* current el_runtime.h. Redeclared here as static inline to avoid link deps.
* Only defined if not already declared (old runtimes that still have them). */
#ifndef EL_FLOAT_OPS_DEFINED
#define EL_FLOAT_OPS_DEFINED
/* el_to_float / el_from_float — bit-cast between el_val_t and double.
* Defined as static inline in both el_runtime.h and el_seed.h; we do NOT
* redefine them here. We rely on one of those headers being included first. */
static inline el_val_t float_div(el_val_t a, el_val_t b) {
union { double d; int64_t i; } ua, ub, ur;
ua.i = a; ub.i = b;
ur.d = (ub.d != 0.0) ? (ua.d / ub.d) : 0.0;
return ur.i;
}
static inline el_val_t float_mul(el_val_t a, el_val_t b) {
union { double d; int64_t i; } ua, ub, ur;
ua.i = a; ub.i = b; ur.d = ua.d * ub.d;
return ur.i;
}
static inline el_val_t float_add(el_val_t a, el_val_t b) {
union { double d; int64_t i; } ua, ub, ur;
ua.i = a; ub.i = b; ur.d = ua.d + ub.d;
return ur.i;
}
static inline el_val_t float_sub(el_val_t a, el_val_t b) {
union { double d; int64_t i; } ua, ub, ur;
ua.i = a; ub.i = b; ur.d = ua.d - ub.d;
return ur.i;
}
static inline el_val_t float_lt(el_val_t a, el_val_t b) {
union { double d; int64_t i; } ua, ub;
ua.i = a; ub.i = b;
return (el_val_t)(ua.d < ub.d);
}
static inline el_val_t float_gt(el_val_t a, el_val_t b) {
union { double d; int64_t i; } ua, ub;
ua.i = a; ub.i = b;
return (el_val_t)(ua.d > ub.d);
}
static inline el_val_t float_lte(el_val_t a, el_val_t b) {
union { double d; int64_t i; } ua, ub;
ua.i = a; ub.i = b;
return (el_val_t)(ua.d <= ub.d);
}
static inline el_val_t float_gte(el_val_t a, el_val_t b) {
union { double d; int64_t i; } ua, ub;
ua.i = a; ub.i = b;
return (el_val_t)(ua.d >= ub.d);
}
static inline el_val_t float_eq(el_val_t a, el_val_t b) {
union { double d; int64_t i; } ua, ub;
ua.i = a; ub.i = b;
return (el_val_t)(ua.d == ub.d);
}
#endif /* EL_FLOAT_OPS_DEFINED */
/* ── Native widget system (macOS AppKit) ────────────────────────────────────
* Available when compiled with -DEL_TARGET_MACOS and linked with el_appkit.m.
* Widget handles are opaque int64_t slot indices; -1 = invalid. */
#ifdef EL_TARGET_MACOS
/* Initialisation */
void __native_init(void);
void __native_run_loop(void);
/* Window */
el_val_t __window_create(el_val_t title, el_val_t width, el_val_t height,
el_val_t min_width, el_val_t min_height);
void __window_show(el_val_t handle);
void __window_set_title(el_val_t handle, el_val_t title);
/* Layout containers */
el_val_t __vstack_create(el_val_t spacing);
el_val_t __hstack_create(el_val_t spacing);
el_val_t __zstack_create(void);
el_val_t __scroll_create(void);
/* Widgets */
el_val_t __label_create(el_val_t text);
el_val_t __button_create(el_val_t label);
el_val_t __text_field_create(el_val_t placeholder);
el_val_t __text_area_create(el_val_t placeholder);
el_val_t __image_create(el_val_t path_or_name);
/* Widget properties */
void __widget_set_text(el_val_t handle, el_val_t text);
el_val_t __widget_get_text(el_val_t handle);
void __widget_set_color(el_val_t handle, el_val_t r, el_val_t g,
el_val_t b, el_val_t a);
void __widget_set_bg_color(el_val_t handle, el_val_t r, el_val_t g,
el_val_t b, el_val_t a);
void __widget_set_font(el_val_t handle, el_val_t family,
el_val_t size, el_val_t bold);
void __widget_set_padding(el_val_t handle, el_val_t top, el_val_t right,
el_val_t bottom, el_val_t left);
void __widget_set_width(el_val_t handle, el_val_t width);
void __widget_set_height(el_val_t handle, el_val_t height);
void __widget_set_flex(el_val_t handle, el_val_t flex);
void __widget_set_corner_radius(el_val_t handle, el_val_t radius);
void __widget_set_disabled(el_val_t handle, el_val_t disabled);
void __widget_set_hidden(el_val_t handle, el_val_t hidden);
/* Layout / tree */
void __widget_add_child(el_val_t parent, el_val_t child);
void __widget_remove_child(el_val_t parent, el_val_t child);
void __widget_destroy(el_val_t handle);
/* Events */
void __widget_on_click(el_val_t handle, el_val_t fn_name);
void __widget_on_change(el_val_t handle, el_val_t fn_name);
void __widget_on_submit(el_val_t handle, el_val_t fn_name);
void __widget_set_data(el_val_t handle, el_val_t data_str);
/* Manifest reader */
el_val_t __manifest_read(el_val_t path);
#endif /* EL_TARGET_MACOS */
/* ── Native widget system (Linux GTK4) ──────────────────────────────────────
* Available when compiled with -DEL_TARGET_LINUX and linked with el_gtk4.c.
* Widget handles are opaque int64_t slot indices; -1 = invalid.
* All functions have the same signatures as EL_TARGET_MACOS above. */
#ifdef EL_TARGET_LINUX
/* Initialisation */
void __native_init(void);
void __native_run_loop(void);
/* Window */
el_val_t __window_create(el_val_t title, el_val_t width, el_val_t height,
el_val_t min_width, el_val_t min_height);
void __window_show(el_val_t handle);
void __window_set_title(el_val_t handle, el_val_t title);
/* Layout containers */
el_val_t __vstack_create(el_val_t spacing);
el_val_t __hstack_create(el_val_t spacing);
el_val_t __zstack_create(void);
el_val_t __scroll_create(void);
/* Widgets */
el_val_t __label_create(el_val_t text);
el_val_t __button_create(el_val_t label);
el_val_t __text_field_create(el_val_t placeholder);
el_val_t __text_area_create(el_val_t placeholder);
el_val_t __image_create(el_val_t path_or_name);
/* Widget properties */
void __widget_set_text(el_val_t handle, el_val_t text);
el_val_t __widget_get_text(el_val_t handle);
void __widget_set_color(el_val_t handle, el_val_t r, el_val_t g,
el_val_t b, el_val_t a);
void __widget_set_bg_color(el_val_t handle, el_val_t r, el_val_t g,
el_val_t b, el_val_t a);
void __widget_set_font(el_val_t handle, el_val_t family,
el_val_t size, el_val_t bold);
void __widget_set_padding(el_val_t handle, el_val_t top, el_val_t right,
el_val_t bottom, el_val_t left);
void __widget_set_width(el_val_t handle, el_val_t width);
void __widget_set_height(el_val_t handle, el_val_t height);
void __widget_set_flex(el_val_t handle, el_val_t flex);
void __widget_set_corner_radius(el_val_t handle, el_val_t radius);
void __widget_set_disabled(el_val_t handle, el_val_t disabled);
void __widget_set_hidden(el_val_t handle, el_val_t hidden);
/* Layout / tree */
void __widget_add_child(el_val_t parent, el_val_t child);
void __widget_remove_child(el_val_t parent, el_val_t child);
void __widget_destroy(el_val_t handle);
/* Events */
void __widget_on_click(el_val_t handle, el_val_t fn_name);
void __widget_on_change(el_val_t handle, el_val_t fn_name);
void __widget_on_submit(el_val_t handle, el_val_t fn_name);
/* Manifest reader — same JSON output as EL_TARGET_MACOS */
el_val_t __manifest_read(el_val_t path);
#endif /* EL_TARGET_LINUX */
/* ── Native widget system (Windows Win32) ───────────────────────────────────
* Available when compiled with -DEL_TARGET_WIN32 and linked with el_win32.c.
* Widget handles are opaque int64_t slot indices; -1 = invalid.
* Link: el_win32.obj comctl32.lib user32.lib gdi32.lib */
#ifdef EL_TARGET_WIN32
/* Initialisation */
void __native_init(void);
void __native_run_loop(void);
/* Window */
el_val_t __window_create(el_val_t title, el_val_t width, el_val_t height,
el_val_t min_width, el_val_t min_height);
void __window_show(el_val_t handle);
void __window_set_title(el_val_t handle, el_val_t title);
/* Layout containers */
el_val_t __vstack_create(el_val_t spacing);
el_val_t __hstack_create(el_val_t spacing);
el_val_t __zstack_create(void);
el_val_t __scroll_create(void);
/* Widgets */
el_val_t __label_create(el_val_t text);
el_val_t __button_create(el_val_t label);
el_val_t __text_field_create(el_val_t placeholder);
el_val_t __text_area_create(el_val_t placeholder);
el_val_t __image_create(el_val_t path_or_name);
/* Widget properties */
void __widget_set_text(el_val_t handle, el_val_t text);
el_val_t __widget_get_text(el_val_t handle);
void __widget_set_color(el_val_t handle, el_val_t r, el_val_t g,
el_val_t b, el_val_t a);
void __widget_set_bg_color(el_val_t handle, el_val_t r, el_val_t g,
el_val_t b, el_val_t a);
void __widget_set_font(el_val_t handle, el_val_t family,
el_val_t size, el_val_t bold);
void __widget_set_padding(el_val_t handle, el_val_t top, el_val_t right,
el_val_t bottom, el_val_t left);
void __widget_set_width(el_val_t handle, el_val_t width);
void __widget_set_height(el_val_t handle, el_val_t height);
void __widget_set_flex(el_val_t handle, el_val_t flex);
void __widget_set_corner_radius(el_val_t handle, el_val_t radius);
void __widget_set_disabled(el_val_t handle, el_val_t disabled);
void __widget_set_hidden(el_val_t handle, el_val_t hidden);
/* Layout / tree */
void __widget_add_child(el_val_t parent, el_val_t child);
void __widget_remove_child(el_val_t parent, el_val_t child);
void __widget_destroy(el_val_t handle);
/* Events */
void __widget_on_click(el_val_t handle, el_val_t fn_name);
void __widget_on_change(el_val_t handle, el_val_t fn_name);
void __widget_on_submit(el_val_t handle, el_val_t fn_name);
/* Manifest reader */
el_val_t __manifest_read(el_val_t path);
#endif /* EL_TARGET_WIN32 */
/* ── Native widget system (iOS UIKit) ───────────────────────────────────────
* Available when compiled with -DEL_TARGET_IOS and linked with el_uikit.m.
* Widget handles are opaque int64_t slot indices; -1 = invalid.
*
* iOS lifecycle note: UIApplicationMain never returns. The el program must
* store its UI-build logic in a void(*)(void) function pointer, assign it to
* el_main_entry_fn, then call __native_run_loop. ElAppDelegate invokes
* el_main_entry_fn inside didFinishLaunchingWithOptions.
* Call el_uikit_set_args(argc, argv) from main() before __native_run_loop. */
#ifdef EL_TARGET_IOS
/* Lifecycle entry-function hook — set before calling __native_run_loop. */
extern void (*el_main_entry_fn)(void);
/* Forward argc/argv from main() to UIApplicationMain. */
void el_uikit_set_args(int argc, char** argv);
/* Initialisation */
void __native_init(void);
void __native_run_loop(void);
/* Window */
el_val_t __window_create(el_val_t title, el_val_t width, el_val_t height,
el_val_t min_width, el_val_t min_height);
void __window_show(el_val_t handle);
void __window_set_title(el_val_t handle, el_val_t title);
/* Layout containers */
el_val_t __vstack_create(el_val_t spacing);
el_val_t __hstack_create(el_val_t spacing);
el_val_t __zstack_create(void);
el_val_t __scroll_create(void);
/* Widgets */
el_val_t __label_create(el_val_t text);
el_val_t __button_create(el_val_t label);
el_val_t __text_field_create(el_val_t placeholder);
el_val_t __text_area_create(el_val_t placeholder);
el_val_t __image_create(el_val_t path_or_name);
/* Widget properties */
void __widget_set_text(el_val_t handle, el_val_t text);
el_val_t __widget_get_text(el_val_t handle);
void __widget_set_color(el_val_t handle, el_val_t r, el_val_t g,
el_val_t b, el_val_t a);
void __widget_set_bg_color(el_val_t handle, el_val_t r, el_val_t g,
el_val_t b, el_val_t a);
void __widget_set_font(el_val_t handle, el_val_t family,
el_val_t size, el_val_t bold);
void __widget_set_padding(el_val_t handle, el_val_t top, el_val_t right,
el_val_t bottom, el_val_t left);
void __widget_set_width(el_val_t handle, el_val_t width);
void __widget_set_height(el_val_t handle, el_val_t height);
void __widget_set_flex(el_val_t handle, el_val_t flex);
void __widget_set_corner_radius(el_val_t handle, el_val_t radius);
void __widget_set_disabled(el_val_t handle, el_val_t disabled);
void __widget_set_hidden(el_val_t handle, el_val_t hidden);
/* Layout / tree */
void __widget_add_child(el_val_t parent, el_val_t child);
void __widget_remove_child(el_val_t parent, el_val_t child);
void __widget_destroy(el_val_t handle);
/* Events */
void __widget_on_click(el_val_t handle, el_val_t fn_name);
void __widget_on_change(el_val_t handle, el_val_t fn_name);
void __widget_on_submit(el_val_t handle, el_val_t fn_name);
/* Manifest reader */
el_val_t __manifest_read(el_val_t path);
#endif /* EL_TARGET_IOS */
/* ── Native widget system (Android JNI) ─────────────────────────────────────
* Available when compiled with -DEL_TARGET_ANDROID and linked with
* libelruntime.so (which includes el_android.c compiled by the NDK build).
* Widget handles are opaque int64_t slot indices; -1 = invalid.
*
* Java companion: ElBridge.java (package com.neuron.el) must be compiled into
* the APK. The Activity must call ElBridge.init(this) before any widget ops.
*
* Link flags (in Android.mk or CMakeLists.txt):
* -landroid -llog -ldl */
#ifdef EL_TARGET_ANDROID
/* Initialisation */
void __native_init(void);
void __native_run_loop(void); /* no-op on Android */
/* Window */
el_val_t __window_create(el_val_t title, el_val_t width, el_val_t height,
el_val_t min_width, el_val_t min_height);
void __window_show(el_val_t handle);
void __window_set_title(el_val_t handle, el_val_t title);
/* Layout containers */
el_val_t __vstack_create(el_val_t spacing);
el_val_t __hstack_create(el_val_t spacing);
el_val_t __zstack_create(void);
el_val_t __scroll_create(void);
/* Widgets */
el_val_t __label_create(el_val_t text);
el_val_t __button_create(el_val_t label);
el_val_t __text_field_create(el_val_t placeholder);
el_val_t __text_area_create(el_val_t placeholder);
el_val_t __image_create(el_val_t path_or_name);
/* Widget properties */
void __widget_set_text(el_val_t handle, el_val_t text);
el_val_t __widget_get_text(el_val_t handle);
void __widget_set_color(el_val_t handle, el_val_t r, el_val_t g,
el_val_t b, el_val_t a);
void __widget_set_bg_color(el_val_t handle, el_val_t r, el_val_t g,
el_val_t b, el_val_t a);
void __widget_set_font(el_val_t handle, el_val_t family,
el_val_t size, el_val_t bold);
void __widget_set_padding(el_val_t handle, el_val_t top, el_val_t right,
el_val_t bottom, el_val_t left);
void __widget_set_width(el_val_t handle, el_val_t width);
void __widget_set_height(el_val_t handle, el_val_t height);
void __widget_set_flex(el_val_t handle, el_val_t flex);
void __widget_set_corner_radius(el_val_t handle, el_val_t radius);
void __widget_set_disabled(el_val_t handle, el_val_t disabled);
void __widget_set_hidden(el_val_t handle, el_val_t hidden);
/* Layout / tree */
void __widget_add_child(el_val_t parent, el_val_t child);
void __widget_remove_child(el_val_t parent, el_val_t child);
void __widget_destroy(el_val_t handle);
/* Events */
void __widget_on_click(el_val_t handle, el_val_t fn_name);
void __widget_on_change(el_val_t handle, el_val_t fn_name);
void __widget_on_submit(el_val_t handle, el_val_t fn_name);
/* Manifest reader */
el_val_t __manifest_read(el_val_t path);
#endif /* EL_TARGET_ANDROID */
/* ── Native widget system (LVGL v9 — embedded / microcontroller) ─────────────
* Available when compiled with -DEL_TARGET_LVGL and linked with el_lvgl.c
* and the LVGL library (lvgl.a or lvgl source tree).
*
* Target platforms: ESP32, STM32, industrial panels. Any system with 256KB+
* RAM and an LVGL-compatible display driver. No OS required.
*
* Widget handles are opaque int64_t slot indices; -1 = invalid.
*
* Bare-metal / no dynamic linker:
* Compile with -DEL_LVGL_NO_DLSYM and provide:
* el_val_t el_lvgl_dispatch(const char *fn, el_val_t a, el_val_t b);
*
* Compile:
* gcc -DEL_TARGET_LVGL -I./lvgl el_lvgl.c -c -o el_lvgl.o
* # Then link with lvgl.a. */
#ifdef EL_TARGET_LVGL
/* Initialisation */
void __native_init(void);
void __native_run_loop(void);
/* Window */
el_val_t __window_create(el_val_t title, el_val_t width, el_val_t height,
el_val_t min_width, el_val_t min_height);
void __window_show(el_val_t handle);
void __window_set_title(el_val_t handle, el_val_t title);
/* Layout containers */
el_val_t __vstack_create(el_val_t spacing);
el_val_t __hstack_create(el_val_t spacing);
el_val_t __zstack_create(void);
el_val_t __scroll_create(void);
/* Widgets */
el_val_t __label_create(el_val_t text);
el_val_t __button_create(el_val_t label);
el_val_t __text_field_create(el_val_t placeholder);
el_val_t __text_area_create(el_val_t placeholder);
el_val_t __image_create(el_val_t path_or_name);
/* Widget properties */
void __widget_set_text(el_val_t handle, el_val_t text);
el_val_t __widget_get_text(el_val_t handle);
void __widget_set_color(el_val_t handle, el_val_t r, el_val_t g,
el_val_t b, el_val_t a);
void __widget_set_bg_color(el_val_t handle, el_val_t r, el_val_t g,
el_val_t b, el_val_t a);
void __widget_set_font(el_val_t handle, el_val_t family,
el_val_t size, el_val_t bold);
void __widget_set_padding(el_val_t handle, el_val_t top, el_val_t right,
el_val_t bottom, el_val_t left);
void __widget_set_width(el_val_t handle, el_val_t width);
void __widget_set_height(el_val_t handle, el_val_t height);
void __widget_set_flex(el_val_t handle, el_val_t flex);
void __widget_set_corner_radius(el_val_t handle, el_val_t radius);
void __widget_set_disabled(el_val_t handle, el_val_t disabled);
void __widget_set_hidden(el_val_t handle, el_val_t hidden);
/* Layout / tree */
void __widget_add_child(el_val_t parent, el_val_t child);
void __widget_remove_child(el_val_t parent, el_val_t child);
void __widget_destroy(el_val_t handle);
/* Events */
void __widget_on_click(el_val_t handle, el_val_t fn_name);
void __widget_on_change(el_val_t handle, el_val_t fn_name);
void __widget_on_submit(el_val_t handle, el_val_t fn_name);
/* Manifest reader — same JSON output as all other native targets */
el_val_t __manifest_read(el_val_t path);
#endif /* EL_TARGET_LVGL */
/* ── Native widget system (SDL2 — embedded / Pi) ────────────────────────────
* Available when compiled with -DEL_TARGET_SDL2 and linked with el_sdl2.c.
* Widget handles are opaque int64_t slot indices; -1 = invalid.
*
* Target: Raspberry Pi Zero, embedded Linux, any system with a framebuffer
* and SDL2 available. No GTK, no desktop environment required.
*
* Compile:
* gcc -DEL_TARGET_SDL2 $(sdl2-config --cflags) -c el_sdl2.c -o el_sdl2.o
* Link:
* $(sdl2-config --libs) -lSDL2_ttf -lSDL2_image -ldl */
#ifdef EL_TARGET_SDL2
/* Initialisation */
void __native_init(void);
void __native_run_loop(void);
/* Window */
el_val_t __window_create(el_val_t title, el_val_t width, el_val_t height,
el_val_t min_width, el_val_t min_height);
void __window_show(el_val_t handle);
void __window_set_title(el_val_t handle, el_val_t title);
/* Layout containers */
el_val_t __vstack_create(el_val_t spacing);
el_val_t __hstack_create(el_val_t spacing);
el_val_t __zstack_create(void);
el_val_t __scroll_create(void);
/* Widgets */
el_val_t __label_create(el_val_t text);
el_val_t __button_create(el_val_t label);
el_val_t __text_field_create(el_val_t placeholder);
el_val_t __text_area_create(el_val_t placeholder);
el_val_t __image_create(el_val_t path_or_name);
/* Widget properties */
void __widget_set_text(el_val_t handle, el_val_t text);
el_val_t __widget_get_text(el_val_t handle);
void __widget_set_color(el_val_t handle, el_val_t r, el_val_t g,
el_val_t b, el_val_t a);
void __widget_set_bg_color(el_val_t handle, el_val_t r, el_val_t g,
el_val_t b, el_val_t a);
void __widget_set_font(el_val_t handle, el_val_t family,
el_val_t size, el_val_t bold);
void __widget_set_padding(el_val_t handle, el_val_t top, el_val_t right,
el_val_t bottom, el_val_t left);
void __widget_set_width(el_val_t handle, el_val_t width);
void __widget_set_height(el_val_t handle, el_val_t height);
void __widget_set_flex(el_val_t handle, el_val_t flex);
void __widget_set_corner_radius(el_val_t handle, el_val_t radius);
void __widget_set_disabled(el_val_t handle, el_val_t disabled);
void __widget_set_hidden(el_val_t handle, el_val_t hidden);
/* Layout / tree */
void __widget_add_child(el_val_t parent, el_val_t child);
void __widget_remove_child(el_val_t parent, el_val_t child);
void __widget_destroy(el_val_t handle);
/* Events */
void __widget_on_click(el_val_t handle, el_val_t fn_name);
void __widget_on_change(el_val_t handle, el_val_t fn_name);
void __widget_on_submit(el_val_t handle, el_val_t fn_name);
/* Manifest reader */
el_val_t __manifest_read(el_val_t path);
#endif /* EL_TARGET_SDL2 */
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff
File diff suppressed because it is too large Load Diff