diff --git a/include/bridge.hpp b/include/bridge.hpp index ec333446..2e97dccd 100644 --- a/include/bridge.hpp +++ b/include/bridge.hpp @@ -26,7 +26,6 @@ void bridgeInit(); void bridgeDestroy(); void bridgeAudioSubscribe(int channel, AudioIO *audio); void bridgeAudioUnsubscribe(int channel, AudioIO *audio); -BridgeMidiDriver *bridgeGetMidiDriver(); } // namespace rack diff --git a/include/gamepad.hpp b/include/gamepad.hpp index c618f176..3db2696f 100644 --- a/include/gamepad.hpp +++ b/include/gamepad.hpp @@ -7,9 +7,6 @@ namespace rack { -const int GAMEPAD_DRIVER = -10; - - struct GamepadInputDevice : MidiInputDevice { int deviceId; std::vector ccs; @@ -30,8 +27,8 @@ struct GamepadDriver : MidiDriver { }; +void gamepadInit(); void gamepadStep(); -GamepadDriver *gamepadGetDriver(); } // namespace rack diff --git a/include/keyboard.hpp b/include/keyboard.hpp index 0e84b736..03c3087d 100644 --- a/include/keyboard.hpp +++ b/include/keyboard.hpp @@ -7,9 +7,6 @@ namespace rack { -const int KEYBOARD_DRIVER = -11; - - struct KeyboardInputDevice : MidiInputDevice { int octave = 5; void processKey(int key, bool released); @@ -27,9 +24,9 @@ struct KeyboardDriver : MidiDriver { }; +void keyboardInit(); void keyboardPress(int key); void keyboardRelease(int key); -KeyboardDriver *keyboardGetDriver(); } // namespace rack diff --git a/include/midi.hpp b/include/midi.hpp index 23a20e81..7d113b42 100644 --- a/include/midi.hpp +++ b/include/midi.hpp @@ -127,7 +127,9 @@ struct MidiOutput : MidiIO { }; -void midiInit(); +void midiDestroy(); +/** Registers a new MIDI driver. Takes pointer ownership. */ +void midiDriverAdd(int driverId, MidiDriver *driver); } // namespace rack diff --git a/include/rtmidi.hpp b/include/rtmidi.hpp index 3b9d8489..bc74be01 100644 --- a/include/rtmidi.hpp +++ b/include/rtmidi.hpp @@ -1,6 +1,5 @@ #pragma once -#include "util/common.hpp" #include "midi.hpp" #include @@ -40,8 +39,7 @@ struct RtMidiDriver : MidiDriver { }; -std::vector rtmidiGetDrivers(); -RtMidiDriver *rtmidiCreateDriver(int driverId); +void rtmidiInit(); } // namespace rack diff --git a/src/bridge.cpp b/src/bridge.cpp index 8c979751..e839587d 100644 --- a/src/bridge.cpp +++ b/src/bridge.cpp @@ -25,7 +25,7 @@ static BridgeClientConnection *connections[BRIDGE_NUM_PORTS] = {}; static AudioIO *audioListeners[BRIDGE_NUM_PORTS] = {}; static std::thread serverThread; static bool serverRunning = false; -static BridgeMidiDriver driver; +static BridgeMidiDriver *driver = NULL; struct BridgeClientConnection { @@ -206,7 +206,9 @@ struct BridgeClientConnection { void processMidi(MidiMessage message) { if (!(0 <= port && port < BRIDGE_NUM_PORTS)) return; - driver.devices[port].onMessage(message); + if (!driver) + return; + driver->devices[port].onMessage(message); } void setSampleRate(int sampleRate) { @@ -402,6 +404,9 @@ void BridgeMidiDriver::unsubscribeInputDevice(int deviceId, MidiInput *midiInput void bridgeInit() { serverRunning = true; serverThread = std::thread(serverRun); + + driver = new BridgeMidiDriver(); + midiDriverAdd(BRIDGE_DRIVER, driver); } void bridgeDestroy() { @@ -428,9 +433,5 @@ void bridgeAudioUnsubscribe(int port, AudioIO *audio) { audioListeners[port] = NULL; } -BridgeMidiDriver *bridgeGetMidiDriver() { - return &driver; -} - } // namespace rack diff --git a/src/gamepad.cpp b/src/gamepad.cpp index 420e8303..ea818a5b 100644 --- a/src/gamepad.cpp +++ b/src/gamepad.cpp @@ -5,6 +5,10 @@ namespace rack { +static const int GAMEPAD_DRIVER = -10; +static GamepadDriver *driver = NULL; + + void GamepadInputDevice::step() { if (!glfwJoystickPresent(deviceId)) return; @@ -92,20 +96,20 @@ void GamepadDriver::unsubscribeInputDevice(int deviceId, MidiInput *midiInput) { } -static GamepadDriver driver; - +void gamepadInit() { + driver = new GamepadDriver(); + midiDriverAdd(GAMEPAD_DRIVER, driver); +} void gamepadStep() { + if (!driver) + return; for (int i = 0; i < 16; i++) { if (glfwJoystickPresent(i)) { - driver.devices[i].step(); + driver->devices[i].step(); } } } -GamepadDriver *gamepadGetDriver() { - return &driver; -} - } // namespace rack diff --git a/src/keyboard.cpp b/src/keyboard.cpp index 61c5608a..e81648e9 100644 --- a/src/keyboard.cpp +++ b/src/keyboard.cpp @@ -5,6 +5,10 @@ namespace rack { +static const int KEYBOARD_DRIVER = -11; +static KeyboardDriver *driver = NULL; + + void KeyboardInputDevice::processKey(int key, bool released) { int note = -1; switch (key) { @@ -100,18 +104,21 @@ void KeyboardDriver::unsubscribeInputDevice(int deviceId, MidiInput *midiInput) } -static KeyboardDriver driver; +void keyboardInit() { + driver = new KeyboardDriver(); + midiDriverAdd(KEYBOARD_DRIVER, driver); +} void keyboardPress(int key) { - driver.device.processKey(key, false); + if (!driver) + return; + driver->device.processKey(key, false); } void keyboardRelease(int key) { - driver.device.processKey(key, true); -} - -KeyboardDriver *keyboardGetDriver() { - return &driver; + if (!driver) + return; + driver->device.processKey(key, true); } diff --git a/src/main.cpp b/src/main.cpp index fa5e28b5..bbcd820b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,9 +7,12 @@ #include "asset.hpp" #include "bridge.hpp" #include "midi.hpp" -#include "osdialog.h" +#include "rtmidi.hpp" +#include "keyboard.hpp" +#include "gamepad.hpp" #include "util/color.hpp" +#include "osdialog.h" #include @@ -35,8 +38,10 @@ int main(int argc, char* argv[]) { // Initialize namespaces pluginInit(); engineInit(); - midiInit(); + rtmidiInit(); bridgeInit(); + keyboardInit(); + gamepadInit(); windowInit(); appInit(); settingsLoad(assetLocal("settings.json")); @@ -73,6 +78,7 @@ int main(int argc, char* argv[]) { windowDestroy(); bridgeDestroy(); engineDestroy(); + midiDestroy(); pluginDestroy(); loggerDestroy(); diff --git a/src/midi.cpp b/src/midi.cpp index 91fc235d..93864b8d 100644 --- a/src/midi.cpp +++ b/src/midi.cpp @@ -192,20 +192,19 @@ void MidiOutput::setDeviceId(int deviceId) { // midi //////////////////// -static void midiAddDriver(int driverId, MidiDriver *driver) { +void midiDestroy() { + driverIds.clear(); + for (auto &pair : drivers) { + delete pair.second; + } + drivers.clear(); +} + +void midiDriverAdd(int driverId, MidiDriver *driver) { assert(driver); driverIds.push_back(driverId); drivers[driverId] = driver; } -void midiInit() { - for (int driverId : rtmidiGetDrivers()) { - midiAddDriver(driverId, rtmidiCreateDriver(driverId)); - } - midiAddDriver(BRIDGE_DRIVER, bridgeGetMidiDriver()); - midiAddDriver(KEYBOARD_DRIVER, keyboardGetDriver()); - midiAddDriver(GAMEPAD_DRIVER, gamepadGetDriver()); -} - } // namespace rack diff --git a/src/rtmidi.cpp b/src/rtmidi.cpp index 21eb13eb..2d385aa0 100644 --- a/src/rtmidi.cpp +++ b/src/rtmidi.cpp @@ -103,19 +103,14 @@ void RtMidiDriver::unsubscribeInputDevice(int deviceId, MidiInput *midiInput) { } -std::vector rtmidiGetDrivers() { +void rtmidiInit() { std::vector rtApis; RtMidi::getCompiledApi(rtApis); - - std::vector drivers; for (RtMidi::Api api : rtApis) { - drivers.push_back((int) api); + int driverId = (int) api; + MidiDriver *driver = new RtMidiDriver(driverId); + midiDriverAdd(driverId, driver); } - return drivers; -} - -RtMidiDriver *rtmidiCreateDriver(int driverId) { - return new RtMidiDriver(driverId); }