fltk.callback
FLTK Callback Bridge
This module provides infrastructure for safely bridging D delegates to C function pointers required by FLTK. It handles GC pinning to prevent delegate contexts from being collected while callbacks are active.
The main components are:
- CallbackContext: Holds a delegate and manages its GC pinning
- CallbackManager: Global registry for active callbacks
- Trampoline functions: extern(C) functions that invoke D delegates
Threading:
- Callbacks must be registered and invoked from the main thread
- The callback manager is not thread-safe by design (FLTK is single-threaded)
Example:
// Register a callback
auto ctx = CallbackManager.register(() {
writeln("Callback invoked!");
});
// Later, unregister when done
CallbackManager.unregister(ctx);
License
Copyright
Types 5
Simple callback delegate with no parameters.
Widget callback delegate receiving the widget that triggered it.
Callback delegate with user data parameter.
Holds a callback delegate and manages its GC pinning.
When a D delegate is passed to C code, its context (closure) may be collected by the GC if there are no D references to it. This struct pins the delegate's context to prevent collection.
SimpleCallback simpleCallbackThe stored simple callbackWidgetCallback widgetCallbackThe stored widget callbackbool pinnedWhether this context is currently pinnedvoid * userDataUser data pointerCallbackContext * create(SimpleCallback callback)Creates a context for a simple callback.CallbackContext * create(WidgetCallback callback)Creates a context for a widget callback.void invoke()Invokes the simple callback if set.void invokeWidget(WidgetPtr widget)Invokes the widget callback if set.Global registry for managing active callbacks.
This manager tracks all registered callbacks and provides methods to register, unregister, and look up callbacks by their context pointer.
private CallbackContext *[] _contextsCallbackContext * register(SimpleCallback callback) nothrowRegisters a simple callback and returns its context.CallbackContext * register(WidgetCallback callback) nothrowRegisters a widget callback and returns its context.Functions 5
void widgetCallbackTrampoline(WidgetPtr widget, void * userdata) nothrowExtern(C) trampoline for widget callbacks.void timeoutCallbackTrampoline(void * userdata) nothrowExtern(C) trampoline for timeout/idle callbacks.CallbackContext * pinCallback(SimpleCallback callback) nothrowCreates a pinned callback context from a delegate.