Collection of tools useful for audio production
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

820 lines
21KB

  1. /*
  2. * Carla Plugin bridge code
  3. * Copyright (C) 2012 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the COPYING file
  16. */
  17. #ifdef BUILD_BRIDGE_PLUGIN
  18. #include "carla_bridge_client.h"
  19. #include "carla_plugin.h"
  20. #include <QtCore/QDir>
  21. #include <QtCore/QFile>
  22. #include <QtCore/QTextStream>
  23. #include <QtCore/QTimerEvent>
  24. #include <QtGui/QApplication>
  25. #include <QtGui/QMainWindow>
  26. #include <QtGui/QtEvents>
  27. #ifdef Q_OS_UNIX
  28. # include <signal.h>
  29. #endif
  30. static int qargc = 0;
  31. static char** qargv = nullptr;
  32. static bool qCloseNow = false;
  33. #if defined(Q_OS_UNIX)
  34. void closeSignalHandler(int)
  35. {
  36. qCloseNow = true;
  37. }
  38. #elif defined(Q_OS_WIN)
  39. BOOL WINAPI closeSignalHandler(DWORD dwCtrlType)
  40. {
  41. if (dwCtrlType == CTRL_C_EVENT)
  42. {
  43. qCloseNow = true;
  44. return TRUE;
  45. }
  46. return FALSE;
  47. }
  48. #endif
  49. void initSignalHandler()
  50. {
  51. #if defined(Q_OS_UNIX)
  52. struct sigaction sint;
  53. struct sigaction sterm;
  54. sint.sa_handler = closeSignalHandler;
  55. sint.sa_flags = SA_RESTART;
  56. sint.sa_restorer = nullptr;
  57. sigemptyset(&sint.sa_mask);
  58. sigaction(SIGINT, &sint, nullptr);
  59. sterm.sa_handler = closeSignalHandler;
  60. sterm.sa_flags = SA_RESTART;
  61. sterm.sa_restorer = nullptr;
  62. sigemptyset(&sterm.sa_mask);
  63. sigaction(SIGTERM, &sterm, nullptr);
  64. #elif defined(Q_OS_WIN)
  65. SetConsoleCtrlHandler(closeSignalHandler, TRUE);
  66. #endif
  67. }
  68. CARLA_BRIDGE_START_NAMESPACE
  69. // -------------------------------------------------------------------------
  70. class BridgePluginGUI : public QMainWindow
  71. {
  72. public:
  73. class Callback
  74. {
  75. public:
  76. virtual ~Callback() {}
  77. virtual void guiClosedCallback() = 0;
  78. };
  79. BridgePluginGUI(QWidget* const parent, Callback* const callback_)
  80. : QMainWindow(parent),
  81. callback(callback_)
  82. {
  83. qDebug("BridgePluginGUI::BridgePluginGUI(%p, %p", parent, callback);
  84. CARLA_ASSERT(callback);
  85. m_firstShow = true;
  86. m_resizable = true;
  87. container = new GuiContainer(this);
  88. setCentralWidget(container);
  89. setNewSize(50, 50);
  90. }
  91. ~BridgePluginGUI()
  92. {
  93. qDebug("BridgePluginGUI::~BridgePluginGUI()");
  94. CARLA_ASSERT(container);
  95. delete container;
  96. }
  97. GuiContainer* getContainer()
  98. {
  99. return container;
  100. }
  101. void setResizable(bool resizable)
  102. {
  103. m_resizable = resizable;
  104. setNewSize(width(), height());
  105. #ifdef Q_OS_WIN
  106. if (! resizable)
  107. setWindowFlags(windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
  108. #endif
  109. }
  110. void setTitle(const char* title)
  111. {
  112. CARLA_ASSERT(title);
  113. setWindowTitle(QString("%1 (GUI)").arg(title));
  114. }
  115. void setNewSize(int width, int height)
  116. {
  117. qDebug("BridgePluginGUI::setNewSize(%i, %i)", width, height);
  118. if (width < 30)
  119. width = 30;
  120. if (height < 30)
  121. height = 30;
  122. if (m_resizable)
  123. {
  124. resize(width, height);
  125. }
  126. else
  127. {
  128. setFixedSize(width, height);
  129. container->setFixedSize(width, height);
  130. }
  131. }
  132. void setVisible(const bool yesNo)
  133. {
  134. qDebug("BridgePluginGUI::setVisible(%s)", bool2str(yesNo));
  135. if (yesNo)
  136. {
  137. if (m_firstShow)
  138. {
  139. m_firstShow = false;
  140. restoreGeometry(QByteArray());
  141. }
  142. else if (! m_geometry.isNull())
  143. restoreGeometry(m_geometry);
  144. }
  145. else
  146. m_geometry = saveGeometry();
  147. QMainWindow::setVisible(yesNo);
  148. }
  149. protected:
  150. void hideEvent(QHideEvent* const event)
  151. {
  152. qDebug("BridgePluginGUI::hideEvent(%p)", event);
  153. event->accept();
  154. close();
  155. }
  156. void closeEvent(QCloseEvent* const event)
  157. {
  158. qDebug("BridgePluginGUI::closeEvent(%p)", event);
  159. if (event->spontaneous())
  160. callback->guiClosedCallback();
  161. QMainWindow::closeEvent(event);
  162. }
  163. private:
  164. Callback* const callback;
  165. GuiContainer* container;
  166. bool m_firstShow;
  167. bool m_resizable;
  168. QByteArray m_geometry;
  169. };
  170. // -------------------------------------------------------------------------
  171. class BridgePluginClient : public CarlaToolkit,
  172. public CarlaClient,
  173. public BridgePluginGUI::Callback,
  174. public QApplication
  175. {
  176. public:
  177. BridgePluginClient()
  178. : CarlaToolkit("carla-bridge-plugin"),
  179. CarlaClient(this),
  180. QApplication(qargc, qargv, true)
  181. {
  182. qDebug("BridgePluginClient::BridgePluginClient()");
  183. hasUI = false;
  184. msgTimerGUI = 0;
  185. msgTimerOSC = 0;
  186. nextWidth = 0;
  187. nextHeight = 0;
  188. engine = nullptr;
  189. plugin = nullptr;
  190. pluginGui = nullptr;
  191. m_client = this;
  192. }
  193. ~BridgePluginClient()
  194. {
  195. qDebug("BridgePluginClient::~BridgePluginClient()");
  196. CARLA_ASSERT(msgTimerGUI == 0);
  197. CARLA_ASSERT(msgTimerOSC == 0);
  198. CARLA_ASSERT(! pluginGui);
  199. }
  200. void setStuff(CarlaBackend::CarlaEngine* const engine, CarlaBackend::CarlaPlugin* const plugin)
  201. {
  202. qDebug("BridgePluginClient::setStuff(%p, %p)", engine, plugin);
  203. CARLA_ASSERT(engine);
  204. CARLA_ASSERT(plugin);
  205. this->engine = engine;
  206. this->plugin = plugin;
  207. }
  208. // ---------------------------------------------------------------------
  209. // toolkit
  210. void init()
  211. {
  212. qDebug("BridgePluginClient::init()");
  213. pluginGui = new BridgePluginGUI(nullptr, this);
  214. pluginGui->hide();
  215. }
  216. void exec(CarlaClient* const, const bool showGui)
  217. {
  218. qDebug("BridgePluginClient::exec()");
  219. if (showGui)
  220. {
  221. if (hasUI)
  222. show();
  223. }
  224. else
  225. {
  226. CarlaClient::sendOscUpdate();
  227. CarlaClient::sendOscBridgeUpdate();
  228. QApplication::setQuitOnLastWindowClosed(false);
  229. }
  230. msgTimerGUI = startTimer(50);
  231. msgTimerOSC = startTimer(25);
  232. QApplication::exec();
  233. }
  234. void quit()
  235. {
  236. qDebug("BridgePluginClient::quit()");
  237. if (msgTimerGUI != 0)
  238. {
  239. QApplication::killTimer(msgTimerGUI);
  240. msgTimerGUI = 0;
  241. }
  242. if (msgTimerOSC != 0)
  243. {
  244. QApplication::killTimer(msgTimerOSC);
  245. msgTimerOSC = 0;
  246. }
  247. if (pluginGui)
  248. {
  249. if (pluginGui->isVisible())
  250. hide();
  251. pluginGui->close();
  252. delete pluginGui;
  253. pluginGui = nullptr;
  254. }
  255. if (! QApplication::closingDown())
  256. QApplication::quit();
  257. }
  258. void show()
  259. {
  260. qDebug("BridgePluginClient::show()");
  261. CARLA_ASSERT(pluginGui);
  262. if (plugin)
  263. plugin->showGui(true);
  264. if (pluginGui)
  265. pluginGui->show();
  266. }
  267. void hide()
  268. {
  269. qDebug("BridgePluginClient::hide()");
  270. CARLA_ASSERT(pluginGui);
  271. if (pluginGui)
  272. pluginGui->hide();
  273. if (plugin)
  274. plugin->showGui(false);
  275. }
  276. void resize(int width, int height)
  277. {
  278. qDebug("BridgePluginClient::resize(%i, %i)", width, height);
  279. CARLA_ASSERT(pluginGui);
  280. if (pluginGui)
  281. pluginGui->setNewSize(width, height);
  282. }
  283. // ---------------------------------------------------------------------
  284. void createWindow(const bool resizable)
  285. {
  286. qDebug("BridgePluginClient::createWindow(%s)", bool2str(resizable));
  287. CARLA_ASSERT(plugin);
  288. CARLA_ASSERT(pluginGui);
  289. if (! (plugin && pluginGui))
  290. return;
  291. hasUI = true;
  292. pluginGui->setResizable(resizable);
  293. pluginGui->setTitle(plugin->name());
  294. plugin->setGuiContainer(pluginGui->getContainer());
  295. }
  296. // ---------------------------------------------------------------------
  297. // processing
  298. void setParameter(const int32_t rindex, const double value)
  299. {
  300. qDebug("CarlaPluginClient::setParameter(%i, %g)", rindex, value);
  301. CARLA_ASSERT(plugin);
  302. if (! plugin)
  303. return;
  304. plugin->setParameterValueByRIndex(rindex, value, true, true, false);
  305. }
  306. void setProgram(const uint32_t index)
  307. {
  308. qDebug("CarlaPluginClient::setProgram(%i)", index);
  309. CARLA_ASSERT(engine);
  310. CARLA_ASSERT(plugin);
  311. CARLA_ASSERT(index < plugin->programCount());
  312. if (! (plugin && engine))
  313. return;
  314. if (index >= plugin->programCount())
  315. return;
  316. plugin->setProgram(index, true, true, false, true);
  317. double value;
  318. for (uint32_t i=0; i < plugin->parameterCount(); i++)
  319. {
  320. value = plugin->getParameterValue(i);
  321. engine->osc_send_bridge_set_parameter_value(i, value);
  322. engine->osc_send_bridge_set_default_value(i, value);
  323. }
  324. }
  325. void setMidiProgram(const uint32_t index)
  326. {
  327. qDebug("CarlaPluginClient::setMidiProgram(%i)", index);
  328. CARLA_ASSERT(engine);
  329. CARLA_ASSERT(plugin);
  330. if (! (plugin && engine))
  331. return;
  332. plugin->setMidiProgram(index, true, true, false, true);
  333. double value;
  334. for (uint32_t i=0; i < plugin->parameterCount(); i++)
  335. {
  336. value = plugin->getParameterValue(i);
  337. engine->osc_send_bridge_set_parameter_value(i, value);
  338. engine->osc_send_bridge_set_default_value(i, value);
  339. }
  340. }
  341. void noteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  342. {
  343. qDebug("CarlaPluginClient::noteOn(%i, %i, %i)", channel, note, velo);
  344. CARLA_ASSERT(plugin);
  345. CARLA_ASSERT(velo > 0);
  346. if (! plugin)
  347. return;
  348. plugin->sendMidiSingleNote(channel, note, velo, true, true, false);
  349. }
  350. void noteOff(const uint8_t channel, const uint8_t note)
  351. {
  352. qDebug("CarlaPluginClient::noteOff(%i, %i)", channel, note);
  353. CARLA_ASSERT(plugin);
  354. if (! plugin)
  355. return;
  356. plugin->sendMidiSingleNote(channel, note, 0, true, true, false);
  357. }
  358. // ---------------------------------------------------------------------
  359. // plugin
  360. void saveNow()
  361. {
  362. qDebug("CarlaPluginClient::saveNow()");
  363. CARLA_ASSERT(plugin);
  364. CARLA_ASSERT(engine);
  365. if (! (plugin && engine))
  366. return;
  367. plugin->prepareForSave();
  368. for (uint32_t i=0; i < plugin->customDataCount(); i++)
  369. {
  370. const CarlaBackend::CustomData* const cdata = plugin->customData(i);
  371. engine->osc_send_bridge_set_custom_data(CarlaBackend::getCustomDataTypeString(cdata->type), cdata->key, cdata->value);
  372. }
  373. if (plugin->hints() & CarlaBackend::PLUGIN_USES_CHUNKS)
  374. {
  375. void* data = nullptr;
  376. int32_t dataSize = plugin->chunkData(&data);
  377. if (data && dataSize >= 4)
  378. {
  379. QString filePath;
  380. filePath = QDir::tempPath();
  381. #ifdef Q_OS_WIN
  382. filePath += "\\.CarlaChunk_";
  383. #else
  384. filePath += "/.CarlaChunk_";
  385. #endif
  386. filePath += plugin->name();
  387. QFile file(filePath);
  388. if (file.open(QIODevice::WriteOnly))
  389. {
  390. QByteArray chunk((const char*)data, dataSize);
  391. file.write(chunk);
  392. file.close();
  393. engine->osc_send_bridge_set_chunk_data(filePath.toUtf8().constData());
  394. }
  395. }
  396. }
  397. engine->osc_send_bridge_configure(CarlaBackend::CARLA_BRIDGE_MSG_SAVED, "");
  398. }
  399. void setCustomData(const char* const type, const char* const key, const char* const value)
  400. {
  401. qDebug("CarlaPluginClient::setCustomData(\"%s\", \"%s\", \"%s\")", type, key, value);
  402. CARLA_ASSERT(plugin);
  403. if (! plugin)
  404. return;
  405. plugin->setCustomData(CarlaBackend::getCustomDataStringType(type), key, value, true);
  406. }
  407. void setChunkData(const char* const filePath)
  408. {
  409. qDebug("CarlaPluginClient::setChunkData(\"%s\")", filePath);
  410. CARLA_ASSERT(plugin);
  411. if (! plugin)
  412. return;
  413. QString chunkFilePath(filePath);
  414. #ifdef Q_OS_WIN
  415. if (chunkFilePath.startsWith("/"))
  416. {
  417. // running under Wine, posix host
  418. chunkFilePath = chunkFilePath.replace(0, 1, "Z:/");
  419. chunkFilePath = QDir::toNativeSeparators(chunkFilePath);
  420. }
  421. #endif
  422. QFile chunkFile(chunkFilePath);
  423. if (plugin && chunkFile.open(QIODevice::ReadOnly | QIODevice::Text))
  424. {
  425. QTextStream in(&chunkFile);
  426. QString stringData(in.readAll());
  427. chunkFile.close();
  428. chunkFile.remove();
  429. plugin->setChunkData(stringData.toUtf8().constData());
  430. }
  431. }
  432. // ---------------------------------------------------------------------
  433. // callback
  434. void handleCallback(const CarlaBackend::CallbackType action, const int value1, const int value2, const double value3)
  435. {
  436. qDebug("CarlaPluginClient::handleCallback(%s, %i, %i, %g)", CarlaBackend::CallbackType2str(action), value1, value2, value3);
  437. if (! engine)
  438. return;
  439. switch (action)
  440. {
  441. case CarlaBackend::CALLBACK_PARAMETER_VALUE_CHANGED:
  442. engine->osc_send_bridge_set_parameter_value(value1, value3);
  443. break;
  444. case CarlaBackend::CALLBACK_PROGRAM_CHANGED:
  445. engine->osc_send_bridge_set_program(value1);
  446. break;
  447. case CarlaBackend::CALLBACK_MIDI_PROGRAM_CHANGED:
  448. engine->osc_send_bridge_set_midi_program(value1);
  449. break;
  450. case CarlaBackend::CALLBACK_NOTE_ON:
  451. {
  452. //uint8_t mdata[4] = { 0, MIDI_STATUS_NOTE_ON, (uint8_t)value1, (uint8_t)value2 };
  453. //osc_send_midi(mdata);
  454. break;
  455. }
  456. case CarlaBackend::CALLBACK_NOTE_OFF:
  457. {
  458. //uint8_t mdata[4] = { 0, MIDI_STATUS_NOTE_OFF, (uint8_t)value1, (uint8_t)value2 };
  459. //osc_send_midi(mdata);
  460. break;
  461. }
  462. case CarlaBackend::CALLBACK_SHOW_GUI:
  463. if (value1 == 0)
  464. engine->osc_send_bridge_configure(CarlaBackend::CARLA_BRIDGE_MSG_HIDE_GUI, "");
  465. break;
  466. case CarlaBackend::CALLBACK_RESIZE_GUI:
  467. CARLA_ASSERT(value1 > 0 && value2 > 0);
  468. if (value3 == 1.0)
  469. {
  470. nextWidth = 0;
  471. nextHeight = 0;
  472. pluginGui->setFixedSize(value1, value2);
  473. }
  474. else if (nextWidth != value1 && nextHeight != value2)
  475. {
  476. nextWidth = value1;
  477. nextHeight = value2;
  478. }
  479. break;
  480. case CarlaBackend::CALLBACK_RELOAD_PARAMETERS:
  481. //if (CARLA_PLUGIN)
  482. //{
  483. // for (uint32_t i=0; i < CARLA_PLUGIN->parameterCount(); i++)
  484. // {
  485. // osc_send_control(i, CARLA_PLUGIN->getParameterValue(i));
  486. // }
  487. //}
  488. break;
  489. case CarlaBackend::CALLBACK_QUIT:
  490. //QApplication::quit();
  491. break;
  492. default:
  493. break;
  494. }
  495. Q_UNUSED(value3);
  496. }
  497. // ---------------------------------------------------------------------
  498. static void callback(void* const ptr, CarlaBackend::CallbackType const action, const unsigned short, const int value1, const int value2, const double value3)
  499. {
  500. CARLA_ASSERT(ptr);
  501. if (! ptr)
  502. return;
  503. BridgePluginClient* const _this_ = (BridgePluginClient*)ptr;
  504. _this_->handleCallback(action, value1, value2, value3);
  505. }
  506. protected:
  507. void guiClosedCallback()
  508. {
  509. }
  510. void timerEvent(QTimerEvent* const event)
  511. {
  512. if (qCloseNow)
  513. return quit();
  514. if (event->timerId() == msgTimerGUI)
  515. {
  516. if (nextWidth > 0 && nextHeight > 0 && pluginGui)
  517. {
  518. pluginGui->setNewSize(nextWidth, nextHeight);
  519. nextWidth = 0;
  520. nextHeight = 0;
  521. }
  522. if (plugin)
  523. plugin->idleGui();
  524. }
  525. else if (event->timerId() == msgTimerOSC)
  526. {
  527. if (! CarlaClient::oscIdle())
  528. {
  529. CARLA_ASSERT(msgTimerOSC == 0);
  530. msgTimerOSC = 0;
  531. return;
  532. }
  533. }
  534. QApplication::timerEvent(event);
  535. }
  536. // ---------------------------------------------------------------------
  537. private:
  538. bool hasUI;
  539. int msgTimerGUI, msgTimerOSC;
  540. int nextWidth, nextHeight;
  541. CarlaBackend::CarlaEngine* engine;
  542. CarlaBackend::CarlaPlugin* plugin;
  543. BridgePluginGUI* pluginGui;
  544. };
  545. // -------------------------------------------------------------------------
  546. CARLA_BRIDGE_END_NAMESPACE
  547. int main(int argc, char* argv[])
  548. {
  549. if (argc != 6)
  550. {
  551. qWarning("usage: %s <osc-url|\"null\"> <type> <filename> <name|\"(none)\"> <label>", argv[0]);
  552. return 1;
  553. }
  554. qargc = argc;
  555. qargv = argv;
  556. const char* const oscUrl = argv[1];
  557. const char* const stype = argv[2];
  558. const char* const filename = argv[3];
  559. const char* name = argv[4];
  560. const char* const label = argv[5];
  561. const bool useOsc = strcmp(oscUrl, "null");
  562. if (strcmp(name, "(none)") == 0)
  563. name = nullptr;
  564. CarlaBackend::PluginType itype;
  565. if (strcmp(stype, "LADSPA") == 0)
  566. itype = CarlaBackend::PLUGIN_LADSPA;
  567. else if (strcmp(stype, "DSSI") == 0)
  568. itype = CarlaBackend::PLUGIN_DSSI;
  569. else if (strcmp(stype, "LV2") == 0)
  570. itype = CarlaBackend::PLUGIN_LV2;
  571. else if (strcmp(stype, "VST") == 0)
  572. itype = CarlaBackend::PLUGIN_VST;
  573. else
  574. {
  575. itype = CarlaBackend::PLUGIN_NONE;
  576. qWarning("Invalid plugin type '%s'", stype);
  577. return 1;
  578. }
  579. // Init bridge client
  580. CarlaBridge::BridgePluginClient client;
  581. client.init();
  582. // Init OSC
  583. if (useOsc && ! client.oscInit(oscUrl))
  584. {
  585. client.quit();
  586. return -1;
  587. }
  588. // Listen for ctrl+c or sigint/sigterm events
  589. initSignalHandler();
  590. // Init backend engine
  591. CarlaBackend::CarlaEngine* engine = CarlaBackend::CarlaEngine::newDriverByName("JACK");
  592. engine->setCallback(client.callback, &client);
  593. // bridge client <-> engine
  594. client.registerOscEngine(engine);
  595. // Init engine
  596. QString engName = QString("%1 (master)").arg(name ? name : label);
  597. engName.truncate(engine->maxClientNameSize());
  598. if (! engine->init(engName.toUtf8().constData()))
  599. {
  600. const char* const lastError = CarlaBackend::getLastError();
  601. qWarning("Bridge engine failed to start, error was:\n%s", lastError);
  602. engine->close();
  603. delete engine;
  604. client.sendOscBridgeError(lastError);
  605. client.quit();
  606. return 2;
  607. }
  608. // Init plugin
  609. short id = engine->addPlugin(itype, filename, name, label);
  610. int ret;
  611. if (id >= 0 && id < CarlaBackend::MAX_PLUGINS)
  612. {
  613. CarlaBackend::CarlaPlugin* const plugin = engine->getPlugin(id);
  614. client.setStuff(engine, plugin);
  615. // create window if needed
  616. bool guiResizable;
  617. CarlaBackend::GuiType guiType;
  618. plugin->getGuiInfo(&guiType, &guiResizable);
  619. if (guiType == CarlaBackend::GUI_INTERNAL_QT4 || guiType == CarlaBackend::GUI_INTERNAL_COCOA || guiType == CarlaBackend::GUI_INTERNAL_HWND || guiType == CarlaBackend::GUI_INTERNAL_X11)
  620. {
  621. client.createWindow(guiResizable);
  622. }
  623. if (! useOsc)
  624. plugin->setActive(true, false, false);
  625. client.exec(nullptr, !useOsc);
  626. ret = 0;
  627. }
  628. else
  629. {
  630. const char* const lastError = CarlaBackend::getLastError();
  631. qWarning("Plugin failed to load, error was:\n%s", lastError);
  632. if (useOsc)
  633. client.sendOscBridgeError(lastError);
  634. ret = 1;
  635. }
  636. engine->removeAllPlugins();
  637. engine->close();
  638. delete engine;
  639. if (useOsc)
  640. {
  641. // Close OSC
  642. client.oscClose();
  643. // bridge client can't be closed manually, only by host
  644. }
  645. else
  646. {
  647. // Close bridge client
  648. client.quit();
  649. }
  650. return ret;
  651. }
  652. #endif // BUILD_BRIDGE_PLUGIN