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.

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