Repository navigation
Add embedded-hardware draft extension - #526
defiantnerd wants to merge 3 commits into
Conversation
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
|
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 { |
There was a problem hiding this comment.
In clap_embedded_hardware_device_info it isn't clear to me which fields are optional?
There was a problem hiding this comment.
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 { |
There was a problem hiding this comment.
Should it be called clap_embedded_hardware_display_pixel_format?
There was a problem hiding this comment.
it is about the color capabilities. I don't think "pixel" is necessary.
There was a problem hiding this comment.
clap_embedded_hardware_display_color_format?
| uint32_t touched; | ||
|
|
||
| // SWITCH: the position, 0..steps-1 | ||
| uint32_t position; |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
sitch_index or something sure
| uint32_t reserved[4]; | ||
| } clap_embedded_hardware_element_info_t; | ||
|
|
||
| typedef struct clap_embedded_hardware_input_state { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
-
Replace
get_input_statewithbool 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 -
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
}
- Perhaps add a
size_t count_input_devices(uint32_t panelId)anduint32_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, |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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,
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
I would host the plugin on my computer and connect multiple usb controllers, ableton push, gamepads, ...
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
the "damage" list is a good idea, but then we probably need a new pixel protocol? I have to think about it.
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
I'm not sure about that one, isn't it simpler to just say "here's your display, deal with it"?
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
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.
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:
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.pixels/1first).Without the extension the plugin is a plain CLAP; a plugin built for hardware must still work in an ordinary host.
Conventions
CLAP_EMBEDDED_HARDWARE_KIND_*, element flagsCLAP_EMBEDDED_HARDWARE_FLAG_*, panel flags..._PANEL_*, event types..._EVENT_*, input roles..._INPUT_*and output roles..._OUTPUT_*. No two groups share a name, soKIND_RUMBLE,OUTPUT_RUMBLEandEVENT_RUMBLEcan't be confused.rolefield is never ambiguous.padding; every info and config struct ends in areservedblock so it can grow without a new id.CLAP_PLUGIN_FEATURE_EMBEDDED_HARDWARE("embedded-hardware") lets a launcher on a device list hardware plugins first.Notes
-Wall -Wextra -pedantic -Werror -Wpadded.Open points, to settle on this PR
set_rumble(), likeset_pixels().find_element(panel, role)helper on the host side, instead of every plugin looping over all elements.