Skip to content

Commit

Permalink
Decouple from cxx crate (#97)
Browse files Browse the repository at this point in the history
- rename ffi::Message into FlMessage
- restructure lib.rs
  • Loading branch information
ales-tsurko authored May 22, 2020
1 parent 5563990 commit db5affb
Show file tree
Hide file tree
Showing 9 changed files with 574 additions and 603 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@ edition = "2018"

[dependencies]
bitflags = "1.2"
cxx = "0.2"
hresult = "0.0.1"
log = "0.4"

[build-dependencies]
cxx = "0.2"
cc = { version = "1.0", features = ["parallel"] }

[dev-dependencies]
bincode = "1.2"
Expand Down
5 changes: 2 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
fn main() {
cxx::Build::new()
.bridge("src/lib.rs")
cc::Build::new()
.include("src/cxx")
.file("src/cxx/fp_plugclass.cpp")
.file("src/cxx/wrapper.cpp")
.cpp(true)
.flag("-std=c++11")
.compile("fpsdk");

println!("cargo:rerun-if-changed=src/lib.rs");
println!("cargo:rerun-if-changed=src/cxx/fp_plugclass.h");
println!("cargo:rerun-if-changed=src/cxx/wrapper.h");
println!("cargo:rerun-if-changed=src/cxx/wrapper.cpp");
Expand Down
43 changes: 12 additions & 31 deletions src/cxx/wrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "wrapper.h"
#include "fp_plugclass.h"
#include "src/lib.rs.h"
#include <_types/_uint8_t.h>
#include <cstring>
#include <stdlib.h>

intptr_t init_p_notes_params(int target, int flags, int ch_num, int pat_num,
TNoteParams *notes, int len) {
Expand All @@ -18,13 +19,6 @@ intptr_t init_p_notes_params(int target, int flags, int ch_num, int pat_num,
return (intptr_t)params;
}

TimeSignature time_sig_from_raw(intptr_t raw_time_sig) {
PTimeSigInfo time_sig = (TTimeSigInfo *)raw_time_sig;

return {(uint32_t)time_sig->StepsPerBar, (uint32_t)time_sig->StepsPerBeat,
(uint32_t)time_sig->PPQ};
}

char *alloc_real_cstr(char *rust_cstr) {
char *result = (char *)malloc(strlen(rust_cstr) + 1);
strcpy(result, rust_cstr);
Expand Down Expand Up @@ -67,11 +61,6 @@ void *create_plug_instance_c(void *host, intptr_t tag, void *adapter) {

free_rbox_raw(info);

int ver = ((TFruityPlugHost *)host)->HostVersion;
std::string sver = std::to_string(ver);
fplog(rust::Str(sver.c_str()));
fplog(rust::Str("host version above"));

PluginWrapper *wrapper = new PluginWrapper(
(TFruityPlugHost *)host, tag, (PluginAdapter *)adapter, c_info);

Expand Down Expand Up @@ -109,14 +98,14 @@ intptr_t _stdcall PluginWrapper::Dispatcher(intptr_t id, intptr_t index,
// host->Dispatcher(HostTag, FHD_WantMIDIInput, 0, value);
// }

Message message = {id, index, value};
FlMessage message = {id, index, value};

return plugin_dispatcher(adapter, message);
}

void _stdcall PluginWrapper::GetName(int section, int index, int value,
char *name) {
Message message = {
FlMessage message = {
(intptr_t)section,
(intptr_t)index,
(intptr_t)value,
Expand All @@ -129,7 +118,7 @@ void _stdcall PluginWrapper::GetName(int section, int index, int value,

int _stdcall PluginWrapper::ProcessEvent(int event_id, int event_value,
int flags) {
Message message = {
FlMessage message = {
(intptr_t)event_id,
(intptr_t)event_value,
(intptr_t)flags,
Expand All @@ -141,7 +130,7 @@ int _stdcall PluginWrapper::ProcessEvent(int event_id, int event_value,
}

int _stdcall PluginWrapper::ProcessParam(int index, int value, int rec_flags) {
Message message = {
FlMessage message = {
(intptr_t)index,
(intptr_t)value,
(intptr_t)rec_flags,
Expand Down Expand Up @@ -194,7 +183,7 @@ void _stdcall PluginWrapper::Voice_Kill(TVoiceHandle handle) {
int _stdcall PluginWrapper::Voice_ProcessEvent(TVoiceHandle handle,
int event_id, int event_value,
int flags) {
Message message = {
FlMessage message = {
(intptr_t)event_id,
(intptr_t)event_value,
(intptr_t)flags,
Expand All @@ -213,15 +202,7 @@ void _stdcall PluginWrapper::NewTick() { plugin_tick(adapter); }

void _stdcall PluginWrapper::MIDITick() { plugin_midi_tick(adapter); }

void _stdcall PluginWrapper::MIDIIn(int &msg) {
MidiMessage message = {
(uint8_t)(msg & 0xff),
(uint8_t)((msg >> 8) & 0xff),
(uint8_t)((msg >> 16) & 0xff),
(int)((msg >> 24) & 0xff),
};
plugin_midi_in(adapter, message);
}
void _stdcall PluginWrapper::MIDIIn(int &msg) { plugin_midi_in(adapter, msg); }

void _stdcall PluginWrapper::MsgIn(intptr_t msg) {
plugin_loop_in(adapter, msg);
Expand All @@ -231,7 +212,7 @@ int _stdcall PluginWrapper::OutputVoice_ProcessEvent(TOutVoiceHandle handle,
int event_id,
int event_value,
int flags) {
Message message = {
FlMessage message = {
(intptr_t)event_id,
(intptr_t)event_value,
(intptr_t)flags,
Expand All @@ -245,7 +226,7 @@ void _stdcall PluginWrapper::OutputVoice_Kill(TVoiceHandle handle) {
}

// host
intptr_t host_on_message(void *host, TPluginTag tag, Message message) {
intptr_t host_on_message(void *host, TPluginTag tag, FlMessage message) {
return ((TFruityPlugHost *)host)
->Dispatcher(tag, message.id, message.index, message.value);
}
Expand Down Expand Up @@ -353,7 +334,7 @@ bool prompt_show(void *host, int x, int y, char *msg, char *result,

// Host voice-related

intptr_t host_on_voice_event(void *host, intptr_t tag, Message message) {
intptr_t host_on_voice_event(void *host, intptr_t tag, FlMessage message) {
return ((TFruityPlugHost *)host)
->Voice_ProcessEvent((TOutVoiceHandle)tag, message.id, message.index,
message.value);
Expand Down Expand Up @@ -381,7 +362,7 @@ void host_kill_out_voice(void *host, intptr_t tag) {
((TFruityPlugHost *)host)->OutputVoice_Kill((TOutVoiceHandle)tag);
}

intptr_t host_on_out_voice_event(void *host, intptr_t tag, Message message) {
intptr_t host_on_out_voice_event(void *host, intptr_t tag, FlMessage message) {
return ((TFruityPlugHost *)host)
->OutputVoice_ProcessEvent((TOutVoiceHandle)tag, message.id,
message.index, message.value);
Expand Down
37 changes: 21 additions & 16 deletions src/cxx/wrapper.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
#pragma once

#include "fp_plugclass.h"
#include "rust/cxx.h"

struct Message;
struct MidiMessage;
struct PluginAdapter;
struct TimeSignature;

struct FlMessage {
intptr_t id;
intptr_t index;
intptr_t value;
};

// from plugin.rs
struct Info {
Expand Down Expand Up @@ -74,18 +76,20 @@ class PluginWrapper : public TFruityPlug {
PluginAdapter *adapter;
};

TimeSignature time_sig_from_raw(intptr_t raw_time_sig);

// Unsafe Rust FFI
//
// PluginAdapter methods
extern "C" void *create_plug_instance_c(void *host, intptr_t tag,
void *adapter);
extern "C" Info *plugin_info(PluginAdapter *adapter);
extern "C" intptr_t plugin_dispatcher(PluginAdapter *adapter, Message message);
extern "C" intptr_t plugin_process_event(PluginAdapter *adapter, Message event);
extern "C" intptr_t plugin_process_param(PluginAdapter *adapter, Message event);
extern "C" char *plugin_name_of(const PluginAdapter *adapter, Message message);
extern "C" intptr_t plugin_dispatcher(PluginAdapter *adapter,
FlMessage message);
extern "C" intptr_t plugin_process_event(PluginAdapter *adapter,
FlMessage event);
extern "C" intptr_t plugin_process_param(PluginAdapter *adapter,
FlMessage event);
extern "C" char *plugin_name_of(const PluginAdapter *adapter,
FlMessage message);
extern "C" void plugin_idle(PluginAdapter *adapter);
extern "C" void plugin_tick(PluginAdapter *adapter);
extern "C" void plugin_midi_tick(PluginAdapter *adapter);
Expand All @@ -94,7 +98,7 @@ extern "C" void plugin_eff_render(PluginAdapter *adapter,
int len);
extern "C" void plugin_gen_render(PluginAdapter *adapter, float dest[1][2],
int len);
extern "C" void plugin_midi_in(PluginAdapter *adapter, MidiMessage message);
extern "C" void plugin_midi_in(PluginAdapter *adapter, int &message);
extern "C" void plugin_save_state(PluginAdapter *adapter, IStream *istream);
extern "C" void plugin_load_state(PluginAdapter *adapter, IStream *istream);
extern "C" void plugin_loop_in(PluginAdapter *adapter, intptr_t message);
Expand All @@ -105,10 +109,10 @@ extern "C" intptr_t voice_handler_trigger(PluginAdapter *adapter, Params params,
extern "C" void voice_handler_release(PluginAdapter *adapter, void *voice);
extern "C" void voice_handler_kill(PluginAdapter *adapter, void *voice);
extern "C" intptr_t voice_handler_on_event(PluginAdapter *adapter, void *voice,
Message message);
FlMessage message);
extern "C" void out_voice_handler_kill(PluginAdapter *adapter, intptr_t tag);
extern "C" intptr_t out_voice_handler_on_event(PluginAdapter *adapter,
intptr_t tag, Message message);
intptr_t tag, FlMessage message);

// IStream
extern "C" int32_t istream_read(void *istream, uint8_t *data, uint32_t size,
Expand All @@ -118,7 +122,7 @@ extern "C" int32_t istream_write(void *istream, const uint8_t *data,

// Host
extern "C" intptr_t host_on_message(void *host, TPluginTag tag,
Message message);
FlMessage message);
extern "C" void host_on_parameter(void *host, TPluginTag tag, int index,
int value);
extern "C" void host_on_controller(void *host, TPluginTag tag, intptr_t index,
Expand Down Expand Up @@ -154,15 +158,16 @@ extern "C" bool prompt_show(void *host, int x, int y, char *msg, char *result,
extern "C" void host_release_voice(void *host, intptr_t tag);
extern "C" void host_kill_voice(void *host, intptr_t tag);
extern "C" intptr_t host_on_voice_event(void *host, intptr_t tag,
Message message);
FlMessage message);
extern "C" intptr_t host_trig_out_voice(void *host, Params *params,
int32_t index, intptr_t tag);
extern "C" void host_release_out_voice(void *host, intptr_t tag);
extern "C" void host_kill_out_voice(void *host, intptr_t tag);
extern "C" intptr_t host_on_out_voice_event(void *host, intptr_t tag,
Message message);
FlMessage message);

// Utility
extern "C" void fplog(const char *msg);
extern "C" intptr_t init_p_notes_params(int target, int flags, int ch_num,
int pat_num, TNoteParams *notes,
int len);
Expand Down
Loading

0 comments on commit db5affb

Please sign in to comment.