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.

818 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/QDialog>
  26. #include <QtGui/QVBoxLayout>
  27. #include <QtGui/QtEvents>
  28. #ifdef Q_OS_UNIX
  29. #include <signal.h>
  30. #endif
  31. static int qargc = 0;
  32. static char** qargv = nullptr;
  33. static bool qCloseNow = false;
  34. #if defined(Q_OS_UNIX)
  35. void closeSignalHandler(int)
  36. {
  37. qCloseNow = true;
  38. }
  39. #elif defined(Q_OS_WIN)
  40. BOOL WINAPI closeSignalHandler(DWORD dwCtrlType)
  41. {
  42. if (dwCtrlType == CTRL_C_EVENT)
  43. {
  44. qCloseNow = true;
  45. return TRUE;
  46. }
  47. return FALSE;
  48. }
  49. #endif
  50. void initSignalHandler()
  51. {
  52. #if defined(Q_OS_UNIX)
  53. struct sigaction sint, sterm;
  54. sint.sa_handler = closeSignalHandler;
  55. sigemptyset(&sint.sa_mask);
  56. sint.sa_flags |= SA_RESTART;
  57. sigaction(SIGINT, &sint, 0);
  58. sterm.sa_handler = closeSignalHandler;
  59. sigemptyset(&sterm.sa_mask);
  60. sterm.sa_flags |= SA_RESTART;
  61. sigaction(SIGTERM, &sterm, 0);
  62. #elif defined(Q_OS_WIN)
  63. SetConsoleCtrlHandler(closeSignalHandler, TRUE);
  64. #endif
  65. }
  66. #ifdef PTW32_STATIC_LIB
  67. #include <pthread.h>
  68. class PThreadScopedInitializer
  69. {
  70. public:
  71. PThreadScopedInitializer()
  72. {
  73. pthread_win32_process_attach_np();
  74. pthread_win32_thread_attach_np();
  75. };
  76. ~PThreadScopedInitializer()
  77. {
  78. pthread_win32_thread_detach_np();
  79. pthread_win32_process_detach_np();
  80. };
  81. };
  82. #endif
  83. CARLA_BRIDGE_START_NAMESPACE
  84. // -------------------------------------------------------------------------
  85. class BridgePluginGUI : public QDialog
  86. {
  87. public:
  88. class Callback
  89. {
  90. public:
  91. virtual ~Callback() {}
  92. virtual void guiClosedCallback() = 0;
  93. };
  94. BridgePluginGUI(QWidget* const parent, Callback* const callback_, const char* const pluginName, const bool resizable)
  95. : QDialog(parent),
  96. callback(callback_)
  97. {
  98. qDebug("BridgePluginGUI::BridgePluginGUI(%p, %p, \"%s\", %s", parent, callback, pluginName, bool2str(resizable));
  99. Q_ASSERT(callback);
  100. Q_ASSERT(pluginName);
  101. m_firstShow = true;
  102. m_resizable = resizable;
  103. vbLayout = new QVBoxLayout(this);
  104. vbLayout->setContentsMargins(0, 0, 0, 0);
  105. setLayout(vbLayout);
  106. container = new GuiContainer(this);
  107. vbLayout->addWidget(container);
  108. setNewSize(50, 50);
  109. setWindowTitle(QString("%1 (GUI)").arg(pluginName));
  110. #ifdef Q_OS_WIN
  111. if (! resizable)
  112. setWindowFlags(windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
  113. #endif
  114. }
  115. ~BridgePluginGUI()
  116. {
  117. qDebug("BridgePluginGUI::~BridgePluginGUI()");
  118. Q_ASSERT(container);
  119. Q_ASSERT(vbLayout);
  120. delete container;
  121. delete vbLayout;
  122. }
  123. GuiContainer* getContainer()
  124. {
  125. return container;
  126. }
  127. void setNewSize(int width, int height)
  128. {
  129. qDebug("BridgePluginGUI::setNewSize(%i, %i)", width, height);
  130. if (width < 30)
  131. width = 30;
  132. if (height < 30)
  133. height = 30;
  134. if (m_resizable)
  135. {
  136. resize(width, height);
  137. }
  138. else
  139. {
  140. setFixedSize(width, height);
  141. container->setFixedSize(width, height);
  142. }
  143. }
  144. void setVisible(const bool yesNo)
  145. {
  146. qDebug("BridgePluginGUI::setVisible(%s)", bool2str(yesNo));
  147. if (yesNo)
  148. {
  149. if (m_firstShow)
  150. {
  151. m_firstShow = false;
  152. restoreGeometry(QByteArray());
  153. }
  154. else if (! m_geometry.isNull())
  155. restoreGeometry(m_geometry);
  156. }
  157. else
  158. m_geometry = saveGeometry();
  159. QDialog::setVisible(yesNo);
  160. }
  161. protected:
  162. void hideEvent(QHideEvent* const event)
  163. {
  164. qDebug("BridgePluginGUI::hideEvent(%p)", event);
  165. event->accept();
  166. close();
  167. }
  168. void closeEvent(QCloseEvent* const event)
  169. {
  170. qDebug("BridgePluginGUI::closeEvent(%p)", event);
  171. if (event->spontaneous())
  172. callback->guiClosedCallback();
  173. QDialog::closeEvent(event);
  174. }
  175. void done(int r)
  176. {
  177. QDialog::done(r);
  178. close();
  179. }
  180. private:
  181. Callback* const callback;
  182. QVBoxLayout* vbLayout;
  183. GuiContainer* container;
  184. bool m_firstShow;
  185. bool m_resizable;
  186. QByteArray m_geometry;
  187. };
  188. // -------------------------------------------------------------------------
  189. class BridgePluginClient : public CarlaToolkit,
  190. public CarlaClient,
  191. public BridgePluginGUI::Callback,
  192. public QApplication
  193. {
  194. public:
  195. BridgePluginClient()
  196. : CarlaToolkit("carla-bridge-plugin"),
  197. CarlaClient(this),
  198. QApplication(qargc, qargv)
  199. {
  200. qDebug("BridgePluginClient::BridgePluginClient()");
  201. msgTimer = 0;
  202. nextWidth = 0;
  203. nextHeight = 0;
  204. engine = nullptr;
  205. plugin = nullptr;
  206. pluginGui = nullptr;
  207. m_client = this;
  208. }
  209. ~BridgePluginClient()
  210. {
  211. qDebug("BridgePluginClient::~BridgePluginClient()");
  212. Q_ASSERT(msgTimer == 0);
  213. Q_ASSERT(! pluginGui);
  214. }
  215. void setStuff(CarlaBackend::CarlaEngine* const engine, CarlaBackend::CarlaPlugin* const plugin)
  216. {
  217. qDebug("BridgePluginClient::setStuff(%p, %p)", engine, plugin);
  218. Q_ASSERT(engine);
  219. Q_ASSERT(plugin);
  220. this->engine = engine;
  221. this->plugin = plugin;
  222. }
  223. // ---------------------------------------------------------------------
  224. // toolkit
  225. void init()
  226. {
  227. qDebug("BridgePluginClient::init()");
  228. }
  229. void exec(CarlaClient* const, const bool showGui)
  230. {
  231. qDebug("BridgePluginClient::exec()");
  232. if (showGui)
  233. {
  234. show();
  235. }
  236. else
  237. {
  238. CarlaClient::sendOscUpdate();
  239. CarlaClient::sendOscBridgeUpdate();
  240. QApplication::setQuitOnLastWindowClosed(false);
  241. }
  242. msgTimer = startTimer(50);
  243. QApplication::exec();
  244. }
  245. void quit()
  246. {
  247. qDebug("BridgePluginClient::quit()");
  248. if (msgTimer != 0)
  249. {
  250. QApplication::killTimer(msgTimer);
  251. msgTimer = 0;
  252. }
  253. if (pluginGui)
  254. {
  255. if (pluginGui->isVisible())
  256. hide();
  257. pluginGui->close();
  258. delete pluginGui;
  259. pluginGui = nullptr;
  260. }
  261. if (! QApplication::closingDown())
  262. QApplication::quit();
  263. }
  264. void show()
  265. {
  266. qDebug("BridgePluginClient::show()");
  267. Q_ASSERT(pluginGui);
  268. if (plugin)
  269. plugin->showGui(true);
  270. if (pluginGui)
  271. pluginGui->show();
  272. }
  273. void hide()
  274. {
  275. qDebug("BridgePluginClient::hide()");
  276. Q_ASSERT(pluginGui);
  277. if (pluginGui)
  278. pluginGui->hide();
  279. if (plugin)
  280. plugin->showGui(false);
  281. }
  282. void resize(int width, int height)
  283. {
  284. qDebug("BridgePluginClient::resize(%i, %i)", width, height);
  285. Q_ASSERT(pluginGui);
  286. if (pluginGui)
  287. pluginGui->setNewSize(width, height);
  288. }
  289. // ---------------------------------------------------------------------
  290. void createWindow(const bool resizable)
  291. {
  292. qDebug("BridgePluginClient::createWindow(%s)", bool2str(resizable));
  293. Q_ASSERT(plugin);
  294. pluginGui = new BridgePluginGUI(nullptr, this, plugin->name(), resizable);
  295. plugin->setGuiContainer(pluginGui->getContainer());
  296. }
  297. // ---------------------------------------------------------------------
  298. // processing
  299. void setParameter(const int32_t rindex, const double value)
  300. {
  301. qDebug("CarlaPluginClient::setParameter(%i, %g)", rindex, value);
  302. Q_ASSERT(plugin);
  303. if (! plugin)
  304. return;
  305. plugin->setParameterValueByRIndex(rindex, value, true, true, false);
  306. }
  307. void setProgram(const uint32_t index)
  308. {
  309. qDebug("CarlaPluginClient::setProgram(%i)", index);
  310. Q_ASSERT(engine);
  311. Q_ASSERT(plugin);
  312. Q_ASSERT(index < plugin->programCount());
  313. if (! (plugin && engine))
  314. return;
  315. if (index >= plugin->programCount())
  316. return;
  317. plugin->setProgram(index, true, true, false, true);
  318. double value;
  319. for (uint32_t i=0; i < plugin->parameterCount(); i++)
  320. {
  321. value = plugin->getParameterValue(i);
  322. engine->osc_send_bridge_set_parameter_value(i, value);
  323. engine->osc_send_bridge_set_default_value(i, value);
  324. }
  325. }
  326. void setMidiProgram(const uint32_t index)
  327. {
  328. qDebug("CarlaPluginClient::setMidiProgram(%i)", index);
  329. Q_ASSERT(engine);
  330. Q_ASSERT(plugin);
  331. if (! (plugin && engine))
  332. return;
  333. plugin->setMidiProgram(index, true, true, false, true);
  334. double value;
  335. for (uint32_t i=0; i < plugin->parameterCount(); i++)
  336. {
  337. value = plugin->getParameterValue(i);
  338. engine->osc_send_bridge_set_parameter_value(i, value);
  339. engine->osc_send_bridge_set_default_value(i, value);
  340. }
  341. }
  342. void noteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  343. {
  344. qDebug("CarlaPluginClient::noteOn(%i, %i, %i)", channel, note, velo);
  345. Q_ASSERT(plugin);
  346. Q_ASSERT(velo > 0);
  347. if (! plugin)
  348. return;
  349. plugin->sendMidiSingleNote(channel, note, velo, true, true, false);
  350. }
  351. void noteOff(const uint8_t channel, const uint8_t note)
  352. {
  353. qDebug("CarlaPluginClient::noteOff(%i, %i)", channel, note);
  354. Q_ASSERT(plugin);
  355. if (! plugin)
  356. return;
  357. plugin->sendMidiSingleNote(channel, note, 0, true, true, false);
  358. }
  359. // ---------------------------------------------------------------------
  360. // plugin
  361. void saveNow()
  362. {
  363. qDebug("CarlaPluginClient::saveNow()");
  364. Q_ASSERT(plugin);
  365. Q_ASSERT(engine);
  366. if (! (plugin && engine))
  367. return;
  368. plugin->prepareForSave();
  369. for (uint32_t i=0; i < plugin->customDataCount(); i++)
  370. {
  371. const CarlaBackend::CustomData* const cdata = plugin->customData(i);
  372. engine->osc_send_bridge_set_custom_data(CarlaBackend::getCustomDataTypeString(cdata->type), cdata->key, cdata->value);
  373. }
  374. if (plugin->hints() & CarlaBackend::PLUGIN_USES_CHUNKS)
  375. {
  376. void* data = nullptr;
  377. int32_t dataSize = plugin->chunkData(&data);
  378. if (data && dataSize >= 4)
  379. {
  380. QString filePath;
  381. filePath = QDir::tempPath();
  382. #ifdef Q_OS_WIN
  383. filePath += "\\.CarlaChunk_";
  384. #else
  385. filePath += "/.CarlaChunk_";
  386. #endif
  387. filePath += plugin->name();
  388. QFile file(filePath);
  389. if (file.open(QIODevice::WriteOnly))
  390. {
  391. QByteArray chunk((const char*)data, dataSize);
  392. file.write(chunk);
  393. file.close();
  394. engine->osc_send_bridge_set_chunk_data(filePath.toUtf8().constData());
  395. }
  396. }
  397. }
  398. engine->osc_send_bridge_configure(CarlaBackend::CARLA_BRIDGE_MSG_SAVED, "");
  399. }
  400. void setCustomData(const char* const type, const char* const key, const char* const value)
  401. {
  402. qDebug("CarlaPluginClient::setCustomData(\"%s\", \"%s\", \"%s\")", type, key, value);
  403. Q_ASSERT(plugin);
  404. if (! plugin)
  405. return;
  406. plugin->setCustomData(CarlaBackend::getCustomDataStringType(type), key, value, true);
  407. }
  408. void setChunkData(const char* const filePath)
  409. {
  410. qDebug("CarlaPluginClient::setChunkData(\"%s\")", filePath);
  411. Q_ASSERT(plugin);
  412. if (! plugin)
  413. return;
  414. nextChunkFilePath = QString(filePath);
  415. while (! nextChunkFilePath.isEmpty())
  416. carla_msleep(25);
  417. }
  418. // ---------------------------------------------------------------------
  419. // callback
  420. void handleCallback(const CarlaBackend::CallbackType action, const int value1, const int value2, const double value3)
  421. {
  422. qDebug("CarlaPluginClient::handleCallback(%s, %i, %i, %g)", CarlaBackend::CallbackType2str(action), value1, value2, value3);
  423. if (! engine)
  424. return;
  425. switch (action)
  426. {
  427. case CarlaBackend::CALLBACK_PARAMETER_VALUE_CHANGED:
  428. engine->osc_send_bridge_set_parameter_value(value1, value3);
  429. break;
  430. case CarlaBackend::CALLBACK_PROGRAM_CHANGED:
  431. engine->osc_send_bridge_set_program(value1);
  432. break;
  433. case CarlaBackend::CALLBACK_MIDI_PROGRAM_CHANGED:
  434. engine->osc_send_bridge_set_midi_program(value1);
  435. break;
  436. case CarlaBackend::CALLBACK_NOTE_ON:
  437. {
  438. //uint8_t mdata[4] = { 0, MIDI_STATUS_NOTE_ON, (uint8_t)value1, (uint8_t)value2 };
  439. //osc_send_midi(mdata);
  440. break;
  441. }
  442. case CarlaBackend::CALLBACK_NOTE_OFF:
  443. {
  444. //uint8_t mdata[4] = { 0, MIDI_STATUS_NOTE_OFF, (uint8_t)value1, (uint8_t)value2 };
  445. //osc_send_midi(mdata);
  446. break;
  447. }
  448. case CarlaBackend::CALLBACK_SHOW_GUI:
  449. if (value1 == 0)
  450. engine->osc_send_bridge_configure(CarlaBackend::CARLA_BRIDGE_MSG_HIDE_GUI, "");
  451. break;
  452. case CarlaBackend::CALLBACK_RESIZE_GUI:
  453. Q_ASSERT(value1 > 0 && value2 > 0);
  454. nextWidth = value1;
  455. nextHeight = value2;
  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. Q_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::runMessages())
  523. {
  524. Q_ASSERT(msgTimer == 0);
  525. msgTimer = 0;
  526. return;
  527. }
  528. }
  529. QApplication::timerEvent(event);
  530. }
  531. // ---------------------------------------------------------------------
  532. private:
  533. int msgTimer;
  534. int nextWidth, nextHeight;
  535. QString nextChunkFilePath;
  536. CarlaBackend::CarlaEngine* engine;
  537. CarlaBackend::CarlaPlugin* plugin;
  538. BridgePluginGUI* pluginGui;
  539. };
  540. // -------------------------------------------------------------------------
  541. CARLA_BRIDGE_END_NAMESPACE
  542. int main(int argc, char* argv[])
  543. {
  544. if (argc != 6)
  545. {
  546. qWarning("usage: %s <osc-url|\"null\"> <type> <filename> <name|\"(none)\"> <label>", argv[0]);
  547. return 1;
  548. }
  549. qargc = argc;
  550. qargv = argv;
  551. const char* const oscUrl = argv[1];
  552. const char* const stype = argv[2];
  553. const char* const filename = argv[3];
  554. const char* name = argv[4];
  555. const char* const label = argv[5];
  556. const bool useOsc = strcmp(oscUrl, "null");
  557. if (strcmp(name, "(none)") == 0)
  558. name = nullptr;
  559. CarlaBackend::PluginType itype;
  560. if (strcmp(stype, "LADSPA") == 0)
  561. itype = CarlaBackend::PLUGIN_LADSPA;
  562. else if (strcmp(stype, "DSSI") == 0)
  563. itype = CarlaBackend::PLUGIN_DSSI;
  564. else if (strcmp(stype, "LV2") == 0)
  565. itype = CarlaBackend::PLUGIN_LV2;
  566. else if (strcmp(stype, "VST") == 0)
  567. itype = CarlaBackend::PLUGIN_VST;
  568. else
  569. {
  570. itype = CarlaBackend::PLUGIN_NONE;
  571. qWarning("Invalid plugin type '%s'", stype);
  572. return 1;
  573. }
  574. #ifdef PTW32_STATIC_LIB
  575. PThreadScopedInitializer pthreadScopedInitializer();
  576. #endif
  577. // Init bridge client
  578. CarlaBridge::BridgePluginClient client;
  579. client.init();
  580. // Init OSC
  581. if (useOsc && ! client.oscInit(oscUrl))
  582. {
  583. client.quit();
  584. return -1;
  585. }
  586. // Init backend engine
  587. CarlaBackend::CarlaEngineJack engine;
  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. client.sendOscBridgeError(lastError);
  600. client.quit();
  601. return 2;
  602. }
  603. /// Init plugin
  604. short id = engine.addPlugin(itype, filename, name, label);
  605. int ret;
  606. if (id >= 0 && id < CarlaBackend::MAX_PLUGINS)
  607. {
  608. CarlaBackend::CarlaPlugin* const plugin = engine.getPlugin(id);
  609. client.setStuff(&engine, plugin);
  610. // create window if needed
  611. bool guiResizable;
  612. CarlaBackend::GuiType guiType;
  613. plugin->getGuiInfo(&guiType, &guiResizable);
  614. if (guiType == CarlaBackend::GUI_INTERNAL_QT4 || guiType == CarlaBackend::GUI_INTERNAL_COCOA || guiType == CarlaBackend::GUI_INTERNAL_HWND || guiType == CarlaBackend::GUI_INTERNAL_X11)
  615. {
  616. client.createWindow(guiResizable);
  617. }
  618. if (! useOsc)
  619. plugin->setActive(true, false, false);
  620. ret = 0;
  621. }
  622. else
  623. {
  624. const char* const lastError = CarlaBackend::getLastError();
  625. qWarning("Plugin failed to load, error was:\n%s", lastError);
  626. if (useOsc)
  627. client.sendOscBridgeError(lastError);
  628. ret = 1;
  629. }
  630. if (ret == 0)
  631. {
  632. initSignalHandler();
  633. client.exec(nullptr, !useOsc);
  634. }
  635. engine.removeAllPlugins();
  636. engine.close();
  637. if (useOsc)
  638. {
  639. // Close OSC
  640. client.sendOscExiting();
  641. client.oscClose();
  642. // bridge client can't be closed manually, only by host
  643. }
  644. else
  645. {
  646. // Close bridge client
  647. client.quit();
  648. }
  649. return ret;
  650. }
  651. #endif // BUILD_BRIDGE_PLUGIN