bennetthardwick / rust-obs-plugins

A safe wrapper around the OBS API, useful for creating OBS sources, filters and effects.
GNU General Public License v2.0
186 stars 34 forks source link

[Discussion] Design of hotkey API? #7

Closed kmod-midori closed 3 years ago

kmod-midori commented 3 years ago

I was about to implement obs_hotkey_register_source, which looks like this:

pub fn obs_hotkey_register_source(
        source: *mut obs_source_t,
        name: *const ::std::os::raw::c_char,
        description: *const ::std::os::raw::c_char,
        func: obs_hotkey_func,
        data: *mut ::std::os::raw::c_void,
    ) -> obs_hotkey_id;

This API seems to be undocumented, but used in almost every plugin. By convention, this is called in create, in which we don't really have our Box<DataWrapper<D>> ready. However, without data this thing is useless. Any idea on how I can approach this?

In C, we control the malloc process, so we can throw the pointer into this function, on Rust, this seems impossible.

bennetthardwick commented 3 years ago

My idea would be to create a CreateContext that gets passed to the create method which creates a hashmap of obs_hotkey_id to FnMut(&mut D). This hashmap would then be added to DataWrapper so that the correct function can be called by obs_hotkey_register_source.

I'll quickly make a draft PR to see how it looks.

bennetthardwick commented 3 years ago

Had a go in #8. For some reason, obs_hotkey_regsiter_source doesn't seem to work for me. Though obs_hotkey_register_frontend works fine. I can't seem to find any documentation on why.