Skip to content

Add embedded-hardware draft extension - #526

Open
defiantnerd wants to merge 3 commits into
free-audio:nextfrom
defiantnerd:embedded-hardware
Open

defiantnerd wants to merge 3 commits into
free-audio:nextfrom
defiantnerd:embedded-hardware

Conversation

@defiantnerd

@defiantnerd defiantnerd commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Summary

New draft extension clap.embedded-hardware/1 (include/clap/ext/draft/embedded-hardware.h) for plugins that run inside a piece of hardware: a pedal, a desktop box, a mixing desk insert, a rack unit, a keyboard workstation or a handheld game device where the plugin is the whole app.

It lets a plugin:

  • Know the device: vendor, model, chip and serial; the panels, which can be attached and removed at runtime; and the elements on each panel. Each panel lies on one face of the case (front, back, top, bottom, left or right) or is detached, like a game controller, so controls on the edges and the back, such as shoulder buttons, can be placed. For each element: its kind, role, position and size in millimetres, the printed label and its colours, and links between parts (an encoder with its push switch and LED ring).
  • Receive input as events in a space from the event registry: buttons, switches, pots, faders, encoders, touch strips, pedals, sticks, touch surfaces and motion sensors. Delivery goes to process() or to the main thread, chosen per element and per event type, with RESET and state replay so held state stays consistent, also when the host takes a panel's inputs for its own menu.
  • Drive outputs: LEDs, LED rings and strips, rumble motors, and text on displays. The plugin says which outputs it takes, and from which thread. It can also claim a graphic display and draw it through a versioned display API (pixels/1 first).
  • Handle focus (the host's overlays), suspend/resume, and asking the host to quit.

Without the extension the plugin is a plain CLAP; a plugin built for hardware must still work in an ordinary host.

Conventions

  • Names: element kinds are CLAP_EMBEDDED_HARDWARE_KIND_*, element flags CLAP_EMBEDDED_HARDWARE_FLAG_*, panel flags ..._PANEL_*, event types ..._EVENT_*, input roles ..._INPUT_* and output roles ..._OUTPUT_*. No two groups share a name, so KIND_RUMBLE, OUTPUT_RUMBLE and EVENT_RUMBLE can't be confused.
  • Roles: input roles are below 1024 and output roles start at 1024, so one role field is never ambiguous.
  • Struct layout: events and info structs have no padding and the same layout on 32 and 64-bit ABIs. Fields that only align are named padding; every info and config struct ends in a reserved block so it can grow without a new id.
  • Time: all timestamps use one monotonic clock that does not advance while the device is suspended.
  • Feature: CLAP_PLUGIN_FEATURE_EMBEDDED_HARDWARE ("embedded-hardware") lets a launcher on a device list hardware plugins first.

Notes

  • The header was adapted from a clap-wrapper prototype into CLAP naming.
  • Compiles cleanly in the CLAP compile tests (C11, C17, C++11/14/17/20) and as standalone C99 with -Wall -Wextra -pedantic -Werror -Wpadded.

Open points, to settle on this PR

  • A blink primitive for LEDs, so blinking survives the host's rate limiting and suspend, and can use LED hardware that blinks by itself.
  • A direct main-thread set_rumble(), like set_pixels().
  • Whether a plugin without the plugin side takes every output as soon as it opts in, or only an output it first writes to.
  • A mapping blob the host stores per device for a plugin, so a user's control assignments carry over to new instances independently of the plugin's sound state.
  • Whether display calls may run on a render thread the host chooses, instead of only on the main thread. This can't be added later without a new id.
  • A find_element(panel, role) helper on the host side, instead of every plugin looping over all elements.

Lets a plugin running inside hardware (pedals, desktop boxes, mixing
desk inserts, handheld game devices) discover the device, its panels
and physical controls, receive their input events and drive leds,
rumble motors and displays.
- panels declare the face of the case they lie on (front, back, top,
  bottom, left, right or detached), so controls on the edges and the
  back, such as shoulder buttons, can be placed
- output roles start at 1024, so input and output roles never overlap
- add OUTPUT_LED_LOGO
- listed POWER and VOLUME elements are always HOST_SHARED
- rumble is never restored or replayed
- the host waits for init() to return before routing and delivery
- rename CENTER_DETENT to CENTER_NOTCH; the plugin handles any dead zone
- comments in proper English; keep clang-format off the top comment
- element kinds get a KIND_ infix and element flags a FLAG_ infix, so
  kinds, roles, flags and event types no longer share names
- a host taking a panel's inputs for its menu sends a RESET then too,
  not only when it gives them back
- fields that only align are named padding; every info and config
  struct ends in a reserved block, including device_info, panel_info
  and input_state
- get_time_ns does not advance while the device is suspended
- rename request_frame to display_request_frame
@baconpaul

Copy link
Copy Markdown
Collaborator

That build failure is an endemic fail across gh actions today. A rerun should fix it once they are bak.

@abique I developed this with @defiantnerd and have reviewed it several times before the submission here.


/* -- The device -- */

typedef struct clap_embedded_hardware_device_info {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In clap_embedded_hardware_device_info it isn't clear to me which fields are optional?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are all free form text so any can be "" would be my answer.


// What a display can show natively. The plugin draws XRGB8888 whatever this says, and
// the host converts.
enum clap_embedded_hardware_display_format {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be called clap_embedded_hardware_display_pixel_format?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is about the color capabilities. I don't think "pixel" is necessary.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clap_embedded_hardware_display_color_format?

uint32_t touched;

// SWITCH: the position, 0..steps-1
uint32_t position;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a bit confused, so when I've read position at first I though it was the touch position. But actually it isn't. Should it be named selected_index?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sitch_index or something sure

uint32_t reserved[4];
} clap_embedded_hardware_element_info_t;

typedef struct clap_embedded_hardware_input_state {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have the feeling that this structure owns a lot of data that isn't always necessary.
Should it be a union or a something similar to clap events where the header defines the type and then we perform a cast?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i have devices with pressable knobs and touchable sliders. i dont know if there is a pressable touchable discrete multi switch on any device i have but i dont see why there couldn't be. so i can imagine hardware cases which use multiple of these in events and state. No sure a strict union would work as a result.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I understands correctly, this struct holds the data for a single physical control.
If so, I'd still think that you could have a type and then either you cast the struct or use a union because then it is immediately clear which attributes are used, they can have specific documentation for their usage, which with the current approach an attribute might be used for many different input states with a possibly different interpretation?

The cost of such a change is a large expansion of the data structures, but at the same time you get clarity and it is more future proof, because you can add any control input state in the future without having to hack existing attributes description.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right so there's 5 fields if we use a union and cast you have the 5! Structures or some such.

If this was c++ I would use a std optional

Perhaps the solution is an enum for has touch has value has axes has switch etc which are bit masks and then a features uint32 since the problem is a real one - what does this gesture support - but the answer of combinatoric value combo structures probably isn't

Wdyt?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the number of structures will grow, but what you're doing is kind of a generic driver framework. It isn't shocking to me to have many structures, and a foundation that can grow later on.

I feel that it is one of those place where the "shortcut" is actually a the difficult path in the long run.

Maybe I'll connect a ring tomorrow that has an accelerometer, a heartbeat tracker and thermometer 🤷, ... how does that fit here?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I don’t disagree but the union as combination is the wrong pattern too since it requires either a variable list or a combinatoric number of structures

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So here's the problem

If we have a structure per gesture we need a list of them. That list is dynamic based on the item.

If we have a combination structure per combination gesture we need factorial of gestures
So this is a data structure problem.

For the audio thread we already do use events-by-type to do the event delivery (there's a button input and rumble output event). So this api (which lets you main thread query as opposed to wait for event delivery) is indeed at odds with that event structure.

but the problem perhaps is not so much this data structure but the api which queries it being single valued.

You could imagine get_input_stazte taking an allocated vector of states or have a get_input_state_count / etc... and since this is main thread it doesn't have to be realtime safe. Or I suppose we could replace get_input_state(...) with a function which took an output event queue and the job of the host is to full that queue with simultaneous events of some such.

But I agree some consideration on this query-api-point is tricky and needed. And single valued with a big fat struct seems a bit at odds with other query type apis.

@baconpaul baconpaul Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK so I've been thinking about this a bit more. The problem amounts to get_input_state being a single call without enough arguments. It is useful though.

I think the right thing to do is something like this (all very pseudo code)

  1. Replace get_input_state with bool probe_input_state(uint32_t panelId, /*enum*/ uint32_t InputDevice, input_state_t &) which returns false if the inputDevice isn't supported (like you ask for a joystick on an item without a joystick) and true if it is

  2. input_state_t looks something like

struct input_state
{
   uint32_t panelId
   uint32_t inputDevice
   union {
      double value
      int switchPosition
      bool toggle
   }
   uint8_t value_type
   bool isTouched
}
  1. Perhaps add a size_t count_input_devices(uint32_t panelId) and uint32_t getDeviceIdForInputDeviceIndex(uint32_t panelId, size_t index)

So basically move to "you ask for a state of a single input" and "you can probe a single input"

@defiantnerd what do you think? The rest of the conversation here is pretty clear and I think Alex is right on where we landed with his review, but this one is the one where I think we want to consider adjusting the design.

I was also considering just "not having a main thread probe" but I think it will be useful.

typedef struct clap_host_embedded_hardware {
// Returns false if the device does not describe itself.
// [main-thread]
bool(CLAP_ABI *get_device_info)(const clap_host_t *host,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if there are multiple devices?

I run Bitwig, I've got a gamepad, an Ableton Push and a NI Maschine. That's 3 hardware.

So I wonder if we should have a device_index as an argument and then get_panel() would also take a device_id?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is an interesting idea. i've been thinking about this as a clap running in a single device so these devices are like the computer. a clap doesn't need to have index for which computer it is running on.

but audio ports and midi ports are part of the spec. having the device have a clap_id for the device id and using that for get panel and get info and having a get device count is a future proofing idea for lots of smart display bound devices coordinated by a single daw. Makes the embedded pedal-style case a but clunkier but only in a few spots. I'd be supportive,

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't you instantiate a new plugin instance in this case and pass the state?
or do you think about assigning different hardware environments whilst running (like: "move that plugin to external unit 3")?
This would make the transition a bit heavier and also the plugin needs to remap everything.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would host the plugin on my computer and connect multiple usb controllers, ableton push, gamepads, ...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So actually changing the "hardware layout" depending on which "remote ui" you change the ühysical interaction. Imagining: bitwig hosts plugin, the host bar has a "edit on..." button that lets you select a controller hardware and the plugin rematches. The handover would be tricky. It would involve an "defocus" event to suppress any mismatching event output from the plugin and give the UI thread a chance to map the new controls, before resuming with the "focus" event?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would you need handover? I think Alex's point is a plugin running in bitwig could control the display on 3 attached devices

It's a very different model than the one we've worked towards - we are thinking the device is the computer - but it's not nuts

I think it means input events would need a device id also

And we could use -1 as a wildcard like with everything else

uint32_t(CLAP_ABI *count_elements)(const clap_host_t *host);

// [main-thread]
bool(CLAP_ABI *get_element)(const clap_host_t *host,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If an element must have a parent panel, maybe get_element() should take the panel_id and index goes from 0 to the number of elements in that panel -1?

Same would apply to count_elements().

// refresh, for an API which renders on request, as pixels/1 does; calls before that
// coalesce. Wait-free, so a meter can ask from the audio thread.
// [thread-safe]
void(CLAP_ABI *display_request_frame)(const clap_host_t *host, clap_id display_id);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have a display_present()?
This would enter the Vulkan territory with various presentation modes.
Some screens might be 120Hz in the future, or such screens would be rendered differently via other APIs?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my thinking was partly that the pixel api is a simple api, but this extension has an api which would allow you (if we can figure out how) to do things like use a gl context the host allows you to support. Hence having the pixel/1 api - wonder if your comment could be solved by imagining a future vulkan/1 api? Not an expert on this particular thought

@abique abique Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Frame presentation is a tough topic. And the protocol is missing a damaged presentation option: present a list of rectangles that changed.

So the current approach could be OK: host asks to present, but then the plugin calls back on the host to say: "here's my rectangles", no rectangles means same image.

That's good to have a simple pixel API, but damage support is a small addition with very nice results if the app knows what changed, it can drastically reduce the I/O, the HW can reduce energy usage and optimize the screen lifetime maybe?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the "damage" list is a good idea, but then we probably need a new pixel protocol? I have to think about it.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right for a 70x50 scribble strip you don't need damage but for a 1024x768 led you certainly might

// 320x240 for a pixel art UI; false to draw at the display's size. The host need not
// honour it; the config has the final size.
// [main-thread]
bool(CLAP_ABI *display_get_preferred_size)(const clap_plugin_t *plugin,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about that one, isn't it simpler to just say "here's your display, deal with it"?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah i agree. everyone can cpu or gpu scale themselves if they want.

// frame->surface.pixels->data. Only called while visible, and only for an API which
// renders on request. Returns true to present it, false to keep the previous frame.
// [main-thread]
bool(CLAP_ABI *display_render)(const clap_plugin_t *plugin,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this method return an enum: Rendered, Identical, Error? Identical would mean that the image didn't change and doesn't need to be uploaded again.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants