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.

817 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. msgTimer = 0;
  185. nextWidth = 0;
  186. nextHeight = 0;
  187. engine = nullptr;
  188. plugin = nullptr;
  189. pluginGui = nullptr;
  190. m_client = this;
  191. }
  192. ~BridgePluginClient()
  193. {
  194. qDebug("BridgePluginClient::~BridgePluginClient()");
  195. CARLA_ASSERT(msgTimer == 0);
  196. CARLA_ASSERT(! pluginGui);
  197. }
  198. void setStuff(CarlaBackend::CarlaEngine* const engine, CarlaBackend::CarlaPlugin* const plugin)
  199. {
  200. qDebug("BridgePluginClient::setStuff(%p, %p)", engine, plugin);
  201. CARLA_ASSERT(engine);
  202. CARLA_ASSERT(plugin);
  203. this->engine = engine;
  204. this->plugin = plugin;
  205. }
  206. // ---------------------------------------------------------------------
  207. // toolkit
  208. void init()
  209. {
  210. qDebug("BridgePluginClient::init()");
  211. pluginGui = new BridgePluginGUI(nullptr, this);
  212. pluginGui->hide();
  213. }
  214. void exec(CarlaClient* const, const bool showGui)
  215. {
  216. qDebug("BridgePluginClient::exec()");
  217. if (showGui)
  218. {
  219. if (hasUI)
  220. show();
  221. }
  222. else
  223. {
  224. CarlaClient::sendOscUpdate();
  225. CarlaClient::sendOscBridgeUpdate();
  226. QApplication::setQuitOnLastWindowClosed(false);
  227. }
  228. msgTimer = startTimer(50);
  229. QApplication::exec();
  230. }
  231. void quit()
  232. {
  233. qDebug("BridgePluginClient::quit()");
  234. if (msgTimer != 0)
  235. {
  236. QApplication::killTimer(msgTimer);
  237. msgTimer = 0;
  238. }
  239. if (pluginGui)
  240. {
  241. if (pluginGui->isVisible())
  242. hide();
  243. pluginGui->close();
  244. delete pluginGui;
  245. pluginGui = nullptr;
  246. }
  247. if (! QApplication::closingDown())
  248. QApplication::quit();
  249. }
  250. void show()
  251. {
  252. qDebug("BridgePluginClient::show()");
  253. CARLA_ASSERT(pluginGui);
  254. if (plugin)
  255. plugin->showGui(true);
  256. if (pluginGui)
  257. pluginGui->show();
  258. }
  259. void hide()
  260. {
  261. qDebug("BridgePluginClient::hide()");
  262. CARLA_ASSERT(pluginGui);
  263. if (pluginGui)
  264. pluginGui->hide();
  265. if (plugin)
  266. plugin->showGui(false);
  267. }
  268. void resize(int width, int height)
  269. {
  270. qDebug("BridgePluginClient::resize(%i, %i)", width, height);
  271. CARLA_ASSERT(pluginGui);
  272. if (pluginGui)
  273. pluginGui->setNewSize(width, height);
  274. }
  275. // ---------------------------------------------------------------------
  276. void createWindow(const bool resizable)
  277. {
  278. qDebug("BridgePluginClient::createWindow(%s)", bool2str(resizable));
  279. CARLA_ASSERT(plugin);
  280. CARLA_ASSERT(pluginGui);
  281. if (! (plugin && pluginGui))
  282. return;
  283. hasUI = true;
  284. pluginGui->setResizable(resizable);
  285. pluginGui->setTitle(plugin->name());
  286. plugin->setGuiContainer(pluginGui->getContainer());
  287. }
  288. // ---------------------------------------------------------------------
  289. // processing
  290. void setParameter(const int32_t rindex, const double value)
  291. {
  292. qDebug("CarlaPluginClient::setParameter(%i, %g)", rindex, value);
  293. CARLA_ASSERT(plugin);
  294. if (! plugin)
  295. return;
  296. plugin->setParameterValueByRIndex(rindex, value, true, true, false);
  297. }
  298. void setProgram(const uint32_t index)
  299. {
  300. qDebug("CarlaPluginClient::setProgram(%i)", index);
  301. CARLA_ASSERT(engine);
  302. CARLA_ASSERT(plugin);
  303. CARLA_ASSERT(index < plugin->programCount());
  304. if (! (plugin && engine))
  305. return;
  306. if (index >= plugin->programCount())
  307. return;
  308. plugin->setProgram(index, true, true, false, true);
  309. double value;
  310. for (uint32_t i=0; i < plugin->parameterCount(); i++)
  311. {
  312. value = plugin->getParameterValue(i);
  313. engine->osc_send_bridge_set_parameter_value(i, value);
  314. engine->osc_send_bridge_set_default_value(i, value);
  315. }
  316. }
  317. void setMidiProgram(const uint32_t index)
  318. {
  319. qDebug("CarlaPluginClient::setMidiProgram(%i)", index);
  320. CARLA_ASSERT(engine);
  321. CARLA_ASSERT(plugin);
  322. if (! (plugin && engine))
  323. return;
  324. plugin->setMidiProgram(index, true, true, false, true);
  325. double value;
  326. for (uint32_t i=0; i < plugin->parameterCount(); i++)
  327. {
  328. value = plugin->getParameterValue(i);
  329. engine->osc_send_bridge_set_parameter_value(i, value);
  330. engine->osc_send_bridge_set_default_value(i, value);
  331. }
  332. }
  333. void noteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  334. {
  335. qDebug("CarlaPluginClient::noteOn(%i, %i, %i)", channel, note, velo);
  336. CARLA_ASSERT(plugin);
  337. CARLA_ASSERT(velo > 0);
  338. if (! plugin)
  339. return;
  340. plugin->sendMidiSingleNote(channel, note, velo, true, true, false);
  341. }
  342. void noteOff(const uint8_t channel, const uint8_t note)
  343. {
  344. qDebug("CarlaPluginClient::noteOff(%i, %i)", channel, note);
  345. CARLA_ASSERT(plugin);
  346. if (! plugin)
  347. return;
  348. plugin->sendMidiSingleNote(channel, note, 0, true, true, false);
  349. }
  350. // ---------------------------------------------------------------------
  351. // plugin
  352. void saveNow()
  353. {
  354. qDebug("CarlaPluginClient::saveNow()");
  355. CARLA_ASSERT(plugin);
  356. CARLA_ASSERT(engine);
  357. if (! (plugin && engine))
  358. return;
  359. plugin->prepareForSave();
  360. for (uint32_t i=0; i < plugin->customDataCount(); i++)
  361. {
  362. const CarlaBackend::CustomData* const cdata = plugin->customData(i);
  363. engine->osc_send_bridge_set_custom_data(CarlaBackend::getCustomDataTypeString(cdata->type), cdata->key, cdata->value);
  364. }
  365. if (plugin->hints() & CarlaBackend::PLUGIN_USES_CHUNKS)
  366. {
  367. void* data = nullptr;
  368. int32_t dataSize = plugin->chunkData(&data);
  369. if (data && dataSize >= 4)
  370. {
  371. QString filePath;
  372. filePath = QDir::tempPath();
  373. #ifdef Q_OS_WIN
  374. filePath += "\\.CarlaChunk_";
  375. #else
  376. filePath += "/.CarlaChunk_";
  377. #endif
  378. filePath += plugin->name();
  379. QFile file(filePath);
  380. if (file.open(QIODevice::WriteOnly))
  381. {
  382. QByteArray chunk((const char*)data, dataSize);
  383. file.write(chunk);
  384. file.close();
  385. engine->osc_send_bridge_set_chunk_data(filePath.toUtf8().constData());
  386. }
  387. }
  388. }
  389. engine->osc_send_bridge_configure(CarlaBackend::CARLA_BRIDGE_MSG_SAVED, "");
  390. }
  391. void setCustomData(const char* const type, const char* const key, const char* const value)
  392. {
  393. qDebug("CarlaPluginClient::setCustomData(\"%s\", \"%s\", \"%s\")", type, key, value);
  394. CARLA_ASSERT(plugin);
  395. if (! plugin)
  396. return;
  397. plugin->setCustomData(CarlaBackend::getCustomDataStringType(type), key, value, true);
  398. }
  399. void setChunkData(const char* const filePath)
  400. {
  401. qDebug("CarlaPluginClient::setChunkData(\"%s\")", filePath);
  402. CARLA_ASSERT(plugin);
  403. if (! plugin)
  404. return;
  405. nextChunkFilePath = QString(filePath);
  406. while (! nextChunkFilePath.isEmpty())
  407. carla_msleep(25);
  408. }
  409. // ---------------------------------------------------------------------
  410. // callback
  411. void handleCallback(const CarlaBackend::CallbackType action, const int value1, const int value2, const double value3)
  412. {
  413. qDebug("CarlaPluginClient::handleCallback(%s, %i, %i, %g)", CarlaBackend::CallbackType2str(action), value1, value2, value3);
  414. if (! engine)
  415. return;
  416. switch (action)
  417. {
  418. case CarlaBackend::CALLBACK_PARAMETER_VALUE_CHANGED:
  419. engine->osc_send_bridge_set_parameter_value(value1, value3);
  420. break;
  421. case CarlaBackend::CALLBACK_PROGRAM_CHANGED:
  422. engine->osc_send_bridge_set_program(value1);
  423. break;
  424. case CarlaBackend::CALLBACK_MIDI_PROGRAM_CHANGED:
  425. engine->osc_send_bridge_set_midi_program(value1);
  426. break;
  427. case CarlaBackend::CALLBACK_NOTE_ON:
  428. {
  429. //uint8_t mdata[4] = { 0, MIDI_STATUS_NOTE_ON, (uint8_t)value1, (uint8_t)value2 };
  430. //osc_send_midi(mdata);
  431. break;
  432. }
  433. case CarlaBackend::CALLBACK_NOTE_OFF:
  434. {
  435. //uint8_t mdata[4] = { 0, MIDI_STATUS_NOTE_OFF, (uint8_t)value1, (uint8_t)value2 };
  436. //osc_send_midi(mdata);
  437. break;
  438. }
  439. case CarlaBackend::CALLBACK_SHOW_GUI:
  440. if (value1 == 0)
  441. engine->osc_send_bridge_configure(CarlaBackend::CARLA_BRIDGE_MSG_HIDE_GUI, "");
  442. break;
  443. case CarlaBackend::CALLBACK_RESIZE_GUI:
  444. CARLA_ASSERT(value1 > 0 && value2 > 0);
  445. if (value3 == 1.0)
  446. {
  447. nextWidth = 0;
  448. nextHeight = 0;
  449. pluginGui->setFixedSize(value1, value2);
  450. }
  451. else if (nextWidth != value1 && nextHeight != value2)
  452. {
  453. nextWidth = value1;
  454. nextHeight = value2;
  455. }
  456. break;
  457. case CarlaBackend::CALLBACK_RELOAD_PARAMETERS:
  458. //if (CARLA_PLUGIN)
  459. //{
  460. // for (uint32_t i=0; i < CARLA_PLUGIN->parameterCount(); i++)
  461. // {
  462. // osc_send_control(i, CARLA_PLUGIN->getParameterValue(i));
  463. // }
  464. //}
  465. break;
  466. case CarlaBackend::CALLBACK_QUIT:
  467. //QApplication::quit();
  468. break;
  469. default:
  470. break;
  471. }
  472. Q_UNUSED(value3);
  473. }
  474. // ---------------------------------------------------------------------
  475. static void callback(void* const ptr, CarlaBackend::CallbackType const action, const unsigned short, const int value1, const int value2, const double value3)
  476. {
  477. CARLA_ASSERT(ptr);
  478. if (! ptr)
  479. return;
  480. BridgePluginClient* const _this_ = (BridgePluginClient*)ptr;
  481. _this_->handleCallback(action, value1, value2, value3);
  482. }
  483. protected:
  484. void guiClosedCallback()
  485. {
  486. }
  487. void timerEvent(QTimerEvent* const event)
  488. {
  489. if (qCloseNow)
  490. return quit();
  491. if (event->timerId() == msgTimer)
  492. {
  493. if (! nextChunkFilePath.isEmpty())
  494. {
  495. #ifdef Q_OS_WIN
  496. if (nextChunkFilePath.startsWith("/"))
  497. {
  498. // running under Wine, posix host
  499. nextChunkFilePath = nextChunkFilePath.replace(0, 1, "Z:/");
  500. nextChunkFilePath = QDir::toNativeSeparators(nextChunkFilePath);
  501. }
  502. #endif
  503. QFile chunkFile(nextChunkFilePath);
  504. if (plugin && chunkFile.open(QIODevice::ReadOnly | QIODevice::Text))
  505. {
  506. QTextStream in(&chunkFile);
  507. QString stringData(in.readAll());
  508. chunkFile.close();
  509. chunkFile.remove();
  510. plugin->setChunkData(stringData.toUtf8().constData());
  511. }
  512. nextChunkFilePath.clear();
  513. }
  514. if (nextWidth > 0 && nextHeight > 0 && pluginGui)
  515. {
  516. pluginGui->setNewSize(nextWidth, nextHeight);
  517. nextWidth = 0;
  518. nextHeight = 0;
  519. }
  520. if (plugin)
  521. plugin->idleGui();
  522. if (! CarlaClient::oscIdle())
  523. {
  524. CARLA_ASSERT(msgTimer == 0);
  525. msgTimer = 0;
  526. return;
  527. }
  528. }
  529. QApplication::timerEvent(event);
  530. }
  531. // ---------------------------------------------------------------------
  532. private:
  533. bool hasUI;
  534. int msgTimer;
  535. int nextWidth, nextHeight;
  536. QString nextChunkFilePath;
  537. CarlaBackend::CarlaEngine* engine;
  538. CarlaBackend::CarlaPlugin* plugin;
  539. BridgePluginGUI* pluginGui;
  540. };
  541. // -------------------------------------------------------------------------
  542. CARLA_BRIDGE_END_NAMESPACE
  543. int main(int argc, char* argv[])
  544. {
  545. if (argc != 6)
  546. {
  547. qWarning("usage: %s <osc-url|\"null\"> <type> <filename> <name|\"(none)\"> <label>", argv[0]);
  548. return 1;
  549. }
  550. qargc = argc;
  551. qargv = argv;
  552. const char* const oscUrl = argv[1];
  553. const char* const stype = argv[2];
  554. const char* const filename = argv[3];
  555. const char* name = argv[4];
  556. const char* const label = argv[5];
  557. const bool useOsc = strcmp(oscUrl, "null");
  558. if (strcmp(name, "(none)") == 0)
  559. name = nullptr;
  560. CarlaBackend::PluginType itype;
  561. if (strcmp(stype, "LADSPA") == 0)
  562. itype = CarlaBackend::PLUGIN_LADSPA;
  563. else if (strcmp(stype, "DSSI") == 0)
  564. itype = CarlaBackend::PLUGIN_DSSI;
  565. else if (strcmp(stype, "LV2") == 0)
  566. itype = CarlaBackend::PLUGIN_LV2;
  567. else if (strcmp(stype, "VST") == 0)
  568. itype = CarlaBackend::PLUGIN_VST;
  569. else
  570. {
  571. itype = CarlaBackend::PLUGIN_NONE;
  572. qWarning("Invalid plugin type '%s'", stype);
  573. return 1;
  574. }
  575. // Init bridge client
  576. CarlaBridge::BridgePluginClient client;
  577. client.init();
  578. // Init OSC
  579. if (useOsc && ! client.oscInit(oscUrl))
  580. {
  581. client.quit();
  582. return -1;
  583. }
  584. // Listen for ctrl+c or sigint/sigterm events
  585. initSignalHandler();
  586. // Init backend engine
  587. CarlaBackend::CarlaEngine* engine = CarlaBackend::CarlaEngine::newDriverByName("JACK");
  588. engine->setCallback(client.callback, &client);
  589. // bridge client <-> engine
  590. client.registerOscEngine(engine);
  591. // Init engine
  592. QString engName = QString("%1 (master)").arg(name ? name : label);
  593. engName.truncate(engine->maxClientNameSize());
  594. if (! engine->init(engName.toUtf8().constData()))
  595. {
  596. const char* const lastError = CarlaBackend::getLastError();
  597. qWarning("Bridge engine failed to start, error was:\n%s", lastError);
  598. engine->close();
  599. delete engine;
  600. client.sendOscBridgeError(lastError);
  601. client.quit();
  602. return 2;
  603. }
  604. // Init plugin
  605. short id = engine->addPlugin(itype, filename, name, label);
  606. int ret;
  607. if (id >= 0 && id < CarlaBackend::MAX_PLUGINS)
  608. {
  609. CarlaBackend::CarlaPlugin* const plugin = engine->getPlugin(id);
  610. client.setStuff(engine, plugin);
  611. // create window if needed
  612. bool guiResizable;
  613. CarlaBackend::GuiType guiType;
  614. plugin->getGuiInfo(&guiType, &guiResizable);
  615. if (guiType == CarlaBackend::GUI_INTERNAL_QT4 || guiType == CarlaBackend::GUI_INTERNAL_COCOA || guiType == CarlaBackend::GUI_INTERNAL_HWND || guiType == CarlaBackend::GUI_INTERNAL_X11)
  616. {
  617. client.createWindow(guiResizable);
  618. }
  619. if (! useOsc)
  620. plugin->setActive(true, false, false);
  621. client.exec(nullptr, !useOsc);
  622. ret = 0;
  623. }
  624. else
  625. {
  626. const char* const lastError = CarlaBackend::getLastError();
  627. qWarning("Plugin failed to load, error was:\n%s", lastError);
  628. if (useOsc)
  629. client.sendOscBridgeError(lastError);
  630. ret = 1;
  631. }
  632. engine->removeAllPlugins();
  633. engine->close();
  634. delete engine;
  635. if (useOsc)
  636. {
  637. // Close OSC
  638. client.oscClose();
  639. // bridge client can't be closed manually, only by host
  640. }
  641. else
  642. {
  643. // Close bridge client
  644. client.quit();
  645. }
  646. return ret;
  647. }
  648. #endif // BUILD_BRIDGE_PLUGIN