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

Authors

Dejan Lekić

License

BSD-3-Clause

Types 5

aliasSimpleCallback = void delegate()

Simple callback delegate with no parameters.

aliasWidgetCallback = void delegate(WidgetPtr widget)

Widget callback delegate receiving the widget that triggered it.

aliasDataCallback = void delegate(void * data)

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.

Fields
SimpleCallback simpleCallbackThe stored simple callback
WidgetCallback widgetCallbackThe stored widget callback
bool pinnedWhether this context is currently pinned
void * userDataUser data pointer
Methods
CallbackContext * create(SimpleCallback callback)Creates a context for a simple callback.
CallbackContext * create(WidgetCallback callback)Creates a context for a widget callback.
void pin() nothrow @trustedPins this context to prevent GC collection.
void unpin() nothrow @trustedUnpins this context, allowing GC collection.
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.

Fields
private CallbackContext *[] _contexts
Methods
CallbackContext * register(SimpleCallback callback) nothrowRegisters a simple callback and returns its context.
CallbackContext * register(WidgetCallback callback) nothrowRegisters a widget callback and returns its context.
void unregister(CallbackContext * ctx) nothrowUnregisters a callback context.
size_t count() nothrow @trustedReturns the number of registered callbacks.
void clear() nothrowClears all registered callbacks.

Functions 5

fnvoid simpleCallbackTrampoline(void * userdata) nothrowExtern(C) trampoline for simple callbacks.
fnvoid widgetCallbackTrampoline(WidgetPtr widget, void * userdata) nothrowExtern(C) trampoline for widget callbacks.
fnvoid timeoutCallbackTrampoline(void * userdata) nothrowExtern(C) trampoline for timeout/idle callbacks.
fnCallbackContext * pinCallback(SimpleCallback callback) nothrowCreates a pinned callback context from a delegate.
fnvoid unpinCallback(CallbackContext * ctx) nothrowUnpins and releases a callback context.