|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497 |
- /*
- * DISTRHO Plugin Toolkit (DPT)
- * Copyright (C) 2012-2013 Filipe Coelho <falktx@falktx.com>
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Lesser General Public License for more details.
- *
- * For a full copy of the license see the LGPL.txt file
- */
-
- #include "CarlaNative.hpp"
- #include "CarlaUtils.hpp"
-
- #include "DistrhoPluginMain.cpp"
-
- #include <QtCore/QSettings>
-
- #if DISTRHO_PLUGIN_HAS_UI
- # if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
- # include <QtWidgets/QMainWindow>
- # else
- # include <QtGui/QMainWindow>
- # endif
- # include "DistrhoUIMain.cpp"
- #endif
-
- // -----------------------------------------------------------------------
-
- START_NAMESPACE_DISTRHO
-
- #if DISTRHO_PLUGIN_HAS_UI
- // -----------------------------------------------------------------------
- // Carla UI
-
- class UICarla : public QMainWindow
- {
- public:
- UICarla(const HostDescriptor* const host, PluginInternal* const plugin)
- : QMainWindow(nullptr),
- kHost(host),
- kPlugin(plugin),
- #ifdef DISTRHO_UI_OPENGL
- fWidget(this),
- fUi(this, (intptr_t)fWidget.winId(), editParameterCallback, setParameterCallback, setStateCallback, sendNoteCallback, uiResizeCallback)
- #else
- fUi(this, 0, editParameterCallback, setParameterCallback, setStateCallback, sendNoteCallback, uiResizeCallback)
- #endif
- {
- #ifdef DISTRHO_UI_OPENGL
- setCentralWidget(&fWidget);
- #else
- QtUI* const qtUi(fUi.getQtUI());
- qtUi->setParent(this);
- setCentralWidget(qtUi);
- #endif
- setWindowTitle(QString("%1 (GUI)").arg(fUi.name()));
-
- uiResize(fUi.width(), fUi.height());
-
- {
- QSettings settings;
-
- if (settings.value("Engine/UIsAlwaysOnTop", true).toBool())
- setWindowFlags(windowFlags()|Qt::WindowStaysOnTopHint);
- }
- }
-
- ~UICarla()
- {
- }
-
- // ---------------------------------------------
-
- void carla_show(const bool yesNo)
- {
- setVisible(yesNo);
- }
-
- void carla_idle()
- {
- fUi.idle();
- }
-
- void carla_setParameterValue(const uint32_t index, const float value)
- {
- fUi.parameterChanged(index, value);
- }
-
- void carla_setMidiProgram(const uint32_t realProgram)
- {
- #if DISTRHO_PLUGIN_WANT_PROGRAMS
- fUi.programChanged(realProgram);
- #else
- return;
- // unused
- (void)realProgram;
- #endif
- }
-
- void carla_setCustomData(const char* const key, const char* const value)
- {
- #if DISTRHO_PLUGIN_WANT_STATE
- fUi.stateChanged(key, value);
- #else
- return;
- // unused
- (void)key;
- (void)value;
- #endif
- }
-
- // ---------------------------------------------
-
- protected:
- void editParameter(uint32_t, bool)
- {
- // TODO
- }
-
- void setParameterValue(uint32_t rindex, float value)
- {
- kHost->ui_parameter_changed(kHost->handle, rindex, value);
- }
-
- void setState(const char* key, const char* value)
- {
- kHost->ui_custom_data_changed(kHost->handle, key, value);
- }
-
- void sendNote(bool, uint8_t, uint8_t, uint8_t)
- {
- // TODO
- }
-
- void uiResize(unsigned int width, unsigned int height)
- {
- #ifdef DISTRHO_UI_OPENGL
- fWidget.setFixedSize(width, height);
- setFixedSize(width, height);
- #else
- if (fUi.resizable())
- resize(width, height);
- else
- setFixedSize(width, height);
- #endif
- }
-
- // ---------------------------------------------
-
- void closeEvent(QCloseEvent* event) override
- {
- kHost->ui_closed(kHost->handle);
-
- // FIXME - ignore event?
- QMainWindow::closeEvent(event);
- }
-
- // ---------------------------------------------
-
- private:
- // Plugin stuff
- const HostDescriptor* const kHost;
- PluginInternal* const kPlugin;
-
- #ifdef DISTRHO_UI_OPENGL
- // Qt stuff, used for GL
- QWidget fWidget;
- #endif
-
- // UI
- UIInternal fUi;
-
- // ---------------------------------------------
- // Callbacks
-
- #define handlePtr ((UICarla*)ptr)
-
- static void editParameterCallback(void* ptr, uint32_t index, bool started)
- {
- handlePtr->editParameter(index, started);
- }
-
- static void setParameterCallback(void* ptr, uint32_t rindex, float value)
- {
- handlePtr->setParameterValue(rindex, value);
- }
-
- static void setStateCallback(void* ptr, const char* key, const char* value)
- {
- handlePtr->setState(key, value);
- }
-
- static void sendNoteCallback(void* ptr, bool onOff, uint8_t channel, uint8_t note, uint8_t velocity)
- {
- handlePtr->sendNote(onOff, channel, note, velocity);
- }
-
- static void uiResizeCallback(void* ptr, unsigned int width, unsigned int height)
- {
- handlePtr->uiResize(width, height);
- }
-
- #undef handlePtr
-
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(UICarla)
- };
- #endif // DISTRHO_PLUGIN_HAS_UI
-
- // -----------------------------------------------------------------------
- // Carla Plugin
-
- class PluginCarla : public PluginDescriptorClass
- {
- public:
- PluginCarla(const HostDescriptor* const host)
- : PluginDescriptorClass(host)
- {
- #if DISTRHO_PLUGIN_HAS_UI
- fUiPtr = nullptr;
- #endif
- }
-
- ~PluginCarla() override
- {
- #if DISTRHO_PLUGIN_HAS_UI
- fUiPtr = nullptr;
- #endif
- }
-
- protected:
- // -------------------------------------------------------------------
- // Plugin parameter calls
-
- uint32_t getParameterCount() override
- {
- return fPlugin.parameterCount();
- }
-
- const ::Parameter* getParameterInfo(const uint32_t index) override
- {
- CARLA_ASSERT(index < getParameterCount());
-
- static ::Parameter param;
-
- // reset
- param.hints = ::PARAMETER_IS_ENABLED;
- param.scalePointCount = 0;
- param.scalePoints = nullptr;
-
- {
- int nativeParamHints = ::PARAMETER_IS_ENABLED;
- const uint32_t paramHints = fPlugin.parameterHints(index);
-
- if (paramHints & PARAMETER_IS_AUTOMABLE)
- nativeParamHints |= ::PARAMETER_IS_AUTOMABLE;
- if (paramHints & PARAMETER_IS_BOOLEAN)
- nativeParamHints |= ::PARAMETER_IS_BOOLEAN;
- if (paramHints & PARAMETER_IS_INTEGER)
- nativeParamHints |= ::PARAMETER_IS_INTEGER;
- if (paramHints & PARAMETER_IS_LOGARITHMIC)
- nativeParamHints |= ::PARAMETER_IS_LOGARITHMIC;
- if (paramHints & PARAMETER_IS_OUTPUT)
- nativeParamHints |= ::PARAMETER_IS_OUTPUT;
-
- param.hints = static_cast<ParameterHints>(nativeParamHints);
- }
-
- param.name = fPlugin.parameterName(index);
- param.unit = fPlugin.parameterUnit(index);
-
- {
- const ParameterRanges& ranges(fPlugin.parameterRanges(index));
-
- param.ranges.def = ranges.def;
- param.ranges.min = ranges.min;
- param.ranges.max = ranges.max;
- param.ranges.step = ranges.step;
- param.ranges.stepSmall = ranges.stepSmall;
- param.ranges.stepLarge = ranges.stepLarge;
- }
-
- return ¶m;
- }
-
- float getParameterValue(const uint32_t index) override
- {
- CARLA_ASSERT(index < getParameterCount());
-
- return fPlugin.parameterValue(index);
- }
-
- // getParameterText unused
-
- // -------------------------------------------------------------------
- // Plugin midi-program calls
-
- #if DISTRHO_PLUGIN_WANT_PROGRAMS
- uint32_t getMidiProgramCount() override
- {
- return fPlugin.programCount();
- }
-
- const ::MidiProgram* getMidiProgramInfo(const uint32_t index) override
- {
- CARLA_ASSERT(index < getMidiProgramCount());
-
- if (index >= fPlugin.programCount())
- return nullptr;
-
- static ::MidiProgram midiProgram;
-
- midiProgram.bank = index / 128;
- midiProgram.program = index % 128;
- midiProgram.name = fPlugin.programName(index);
-
- return &midiProgram;
- }
- #endif
-
- // -------------------------------------------------------------------
- // Plugin state calls
-
- void setParameterValue(const uint32_t index, const float value) override
- {
- CARLA_ASSERT(index < getParameterCount());
-
- fPlugin.setParameterValue(index, value);
- }
-
- #if DISTRHO_PLUGIN_WANT_PROGRAMS
- void setMidiProgram(const uint32_t bank, const uint32_t program) override
- {
- const uint32_t realProgram = bank * 128 + program;
-
- if (realProgram >= fPlugin.programCount())
- return;
-
- fPlugin.setProgram(realProgram);
- }
- #endif
-
- #if DISTRHO_PLUGIN_WANT_STATE
- void setCustomData(const char* const key, const char* const value) override
- {
- CARLA_ASSERT(key != nullptr);
- CARLA_ASSERT(value != nullptr);
-
- fPlugin.setState(key, value);
- }
- #endif
-
- // -------------------------------------------------------------------
- // Plugin process calls
-
- void activate() override
- {
- fPlugin.activate();
- }
-
- void deactivate() override
- {
- fPlugin.deactivate();
- }
-
- #if DISTRHO_PLUGIN_IS_SYNTH
- void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t midiEventCount, const ::MidiEvent* const midiEvents) override
- {
- uint32_t i;
-
- for (i=0; i < midiEventCount && i < MAX_MIDI_EVENTS; i++)
- {
- const ::MidiEvent* const midiEvent = &midiEvents[i];
- MidiEvent* const realMidiEvent = &fRealMidiEvents[i];
-
- realMidiEvent->buffer[0] = midiEvent->data[0];
- realMidiEvent->buffer[1] = midiEvent->data[1];
- realMidiEvent->buffer[2] = midiEvent->data[2];
- realMidiEvent->frame = midiEvent->time;
- }
-
- fPlugin.run(inBuffer, outBuffer, frames, i, fRealMidiEvents);
- }
- #else
- void process(float** const inBuffer, float** const outBuffer, const uint32_t frames, const uint32_t, const ::MidiEvent* const) override
- {
- fPlugin.run(inBuffer, outBuffer, frames, 0, nullptr);
- }
- #endif
-
- // -------------------------------------------------------------------
- // Plugin UI calls
-
- #if DISTRHO_PLUGIN_HAS_UI
- void uiShow(const bool show) override
- {
- if (show)
- createUiIfNeeded();
-
- if (fUiPtr != nullptr)
- fUiPtr->carla_show(show);
- }
-
- void uiIdle() override
- {
- CARLA_ASSERT(fUiPtr != nullptr);
-
- if (fUiPtr != nullptr)
- fUiPtr->carla_idle();
- }
-
- void uiSetParameterValue(const uint32_t index, const float value) override
- {
- CARLA_ASSERT(fUiPtr != nullptr);
- CARLA_ASSERT(index < getParameterCount());
-
- if (fUiPtr != nullptr)
- fUiPtr->carla_setParameterValue(index, value);
- }
-
- # if DISTRHO_PLUGIN_WANT_PROGRAMS
- void uiSetMidiProgram(const uint32_t bank, const uint32_t program) override
- {
- CARLA_ASSERT(fUiPtr != nullptr);
-
- uint32_t realProgram = bank * 128 + program;
-
- if (realProgram >= fPlugin.programCount())
- return;
-
- if (fUiPtr != nullptr)
- fUiPtr->carla_setMidiProgram(realProgram);
- }
- # endif
-
- # if DISTRHO_PLUGIN_WANT_STATE
- void uiSetCustomData(const char* const key, const char* const value) override
- {
- CARLA_ASSERT(fUiPtr != nullptr);
- CARLA_ASSERT(key != nullptr);
- CARLA_ASSERT(value != nullptr);
-
- if (fUiPtr != nullptr)
- fUiPtr->carla_setCustomData(key, value);
- }
- # endif
- #endif
-
- // -------------------------------------------------------------------
-
- private:
- PluginInternal fPlugin;
-
- #if DISTRHO_PLUGIN_IS_SYNTH
- MidiEvent fRealMidiEvents[MAX_MIDI_EVENTS];
- #endif
-
- #if DISTRHO_PLUGIN_HAS_UI
- // UI
- ScopedPointer<UICarla> fUiPtr;
-
- void createUiIfNeeded()
- {
- if (fUiPtr == nullptr)
- {
- d_lastUiSampleRate = getSampleRate();
- fUiPtr = new UICarla(getHostHandle(), &fPlugin);
- }
- }
- #endif
-
- CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(PluginCarla)
-
- // -------------------------------------------------------------------
-
- public:
- static PluginHandle _instantiate(const PluginDescriptor*, HostDescriptor* host)
- {
- d_lastBufferSize = host->get_buffer_size(host->handle);
- d_lastSampleRate = host->get_sample_rate(host->handle);
- return new PluginCarla(host);
- }
-
- static void _cleanup(PluginHandle handle)
- {
- delete (PluginCarla*)handle;
- }
- };
-
- END_NAMESPACE_DISTRHO
-
- // -----------------------------------------------------------------------
|