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.

808 lines
20KB

  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. QString chunkFilePath(filePath);
  406. #ifdef Q_OS_WIN
  407. if (chunkFilePath.startsWith("/"))
  408. {
  409. // running under Wine, posix host
  410. chunkFilePath = chunkFilePath.replace(0, 1, "Z:/");
  411. chunkFilePath = QDir::toNativeSeparators(chunkFilePath);
  412. }
  413. #endif
  414. QFile chunkFile(chunkFilePath);
  415. if (plugin && chunkFile.open(QIODevice::ReadOnly | QIODevice::Text))
  416. {
  417. QTextStream in(&chunkFile);
  418. QString stringData(in.readAll());
  419. chunkFile.close();
  420. chunkFile.remove();
  421. plugin->setChunkData(stringData.toUtf8().constData());
  422. }
  423. }
  424. // ---------------------------------------------------------------------
  425. // callback
  426. void handleCallback(const CarlaBackend::CallbackType action, const int value1, const int value2, const double value3)
  427. {
  428. qDebug("CarlaPluginClient::handleCallback(%s, %i, %i, %g)", CarlaBackend::CallbackType2str(action), value1, value2, value3);
  429. if (! engine)
  430. return;
  431. switch (action)
  432. {
  433. case CarlaBackend::CALLBACK_PARAMETER_VALUE_CHANGED:
  434. engine->osc_send_bridge_set_parameter_value(value1, value3);
  435. break;
  436. case CarlaBackend::CALLBACK_PROGRAM_CHANGED:
  437. engine->osc_send_bridge_set_program(value1);
  438. break;
  439. case CarlaBackend::CALLBACK_MIDI_PROGRAM_CHANGED:
  440. engine->osc_send_bridge_set_midi_program(value1);
  441. break;
  442. case CarlaBackend::CALLBACK_NOTE_ON:
  443. {
  444. //uint8_t mdata[4] = { 0, MIDI_STATUS_NOTE_ON, (uint8_t)value1, (uint8_t)value2 };
  445. //osc_send_midi(mdata);
  446. break;
  447. }
  448. case CarlaBackend::CALLBACK_NOTE_OFF:
  449. {
  450. //uint8_t mdata[4] = { 0, MIDI_STATUS_NOTE_OFF, (uint8_t)value1, (uint8_t)value2 };
  451. //osc_send_midi(mdata);
  452. break;
  453. }
  454. case CarlaBackend::CALLBACK_SHOW_GUI:
  455. if (value1 == 0)
  456. engine->osc_send_bridge_configure(CarlaBackend::CARLA_BRIDGE_MSG_HIDE_GUI, "");
  457. break;
  458. case CarlaBackend::CALLBACK_RESIZE_GUI:
  459. CARLA_ASSERT(value1 > 0 && value2 > 0);
  460. if (value3 == 1.0)
  461. {
  462. nextWidth = 0;
  463. nextHeight = 0;
  464. pluginGui->setFixedSize(value1, value2);
  465. }
  466. else if (nextWidth != value1 && nextHeight != value2)
  467. {
  468. nextWidth = value1;
  469. nextHeight = value2;
  470. }
  471. break;
  472. case CarlaBackend::CALLBACK_RELOAD_PARAMETERS:
  473. //if (CARLA_PLUGIN)
  474. //{
  475. // for (uint32_t i=0; i < CARLA_PLUGIN->parameterCount(); i++)
  476. // {
  477. // osc_send_control(i, CARLA_PLUGIN->getParameterValue(i));
  478. // }
  479. //}
  480. break;
  481. case CarlaBackend::CALLBACK_QUIT:
  482. //QApplication::quit();
  483. break;
  484. default:
  485. break;
  486. }
  487. Q_UNUSED(value3);
  488. }
  489. // ---------------------------------------------------------------------
  490. static void callback(void* const ptr, CarlaBackend::CallbackType const action, const unsigned short, const int value1, const int value2, const double value3)
  491. {
  492. CARLA_ASSERT(ptr);
  493. if (! ptr)
  494. return;
  495. BridgePluginClient* const _this_ = (BridgePluginClient*)ptr;
  496. _this_->handleCallback(action, value1, value2, value3);
  497. }
  498. protected:
  499. void guiClosedCallback()
  500. {
  501. }
  502. void timerEvent(QTimerEvent* const event)
  503. {
  504. if (qCloseNow)
  505. return quit();
  506. if (event->timerId() == msgTimer)
  507. {
  508. if (nextWidth > 0 && nextHeight > 0 && pluginGui)
  509. {
  510. pluginGui->setNewSize(nextWidth, nextHeight);
  511. nextWidth = 0;
  512. nextHeight = 0;
  513. }
  514. if (plugin)
  515. plugin->idleGui();
  516. if (! CarlaClient::oscIdle())
  517. {
  518. CARLA_ASSERT(msgTimer == 0);
  519. msgTimer = 0;
  520. return;
  521. }
  522. }
  523. QApplication::timerEvent(event);
  524. }
  525. // ---------------------------------------------------------------------
  526. private:
  527. bool hasUI;
  528. int msgTimer;
  529. int nextWidth, nextHeight;
  530. CarlaBackend::CarlaEngine* engine;
  531. CarlaBackend::CarlaPlugin* plugin;
  532. BridgePluginGUI* pluginGui;
  533. };
  534. // -------------------------------------------------------------------------
  535. CARLA_BRIDGE_END_NAMESPACE
  536. int main(int argc, char* argv[])
  537. {
  538. if (argc != 6)
  539. {
  540. qWarning("usage: %s <osc-url|\"null\"> <type> <filename> <name|\"(none)\"> <label>", argv[0]);
  541. return 1;
  542. }
  543. qargc = argc;
  544. qargv = argv;
  545. const char* const oscUrl = argv[1];
  546. const char* const stype = argv[2];
  547. const char* const filename = argv[3];
  548. const char* name = argv[4];
  549. const char* const label = argv[5];
  550. const bool useOsc = strcmp(oscUrl, "null");
  551. if (strcmp(name, "(none)") == 0)
  552. name = nullptr;
  553. CarlaBackend::PluginType itype;
  554. if (strcmp(stype, "LADSPA") == 0)
  555. itype = CarlaBackend::PLUGIN_LADSPA;
  556. else if (strcmp(stype, "DSSI") == 0)
  557. itype = CarlaBackend::PLUGIN_DSSI;
  558. else if (strcmp(stype, "LV2") == 0)
  559. itype = CarlaBackend::PLUGIN_LV2;
  560. else if (strcmp(stype, "VST") == 0)
  561. itype = CarlaBackend::PLUGIN_VST;
  562. else
  563. {
  564. itype = CarlaBackend::PLUGIN_NONE;
  565. qWarning("Invalid plugin type '%s'", stype);
  566. return 1;
  567. }
  568. // Init bridge client
  569. CarlaBridge::BridgePluginClient client;
  570. client.init();
  571. // Init OSC
  572. if (useOsc && ! client.oscInit(oscUrl))
  573. {
  574. client.quit();
  575. return -1;
  576. }
  577. // Listen for ctrl+c or sigint/sigterm events
  578. initSignalHandler();
  579. // Init backend engine
  580. CarlaBackend::CarlaEngine* engine = CarlaBackend::CarlaEngine::newDriverByName("JACK");
  581. engine->setCallback(client.callback, &client);
  582. // bridge client <-> engine
  583. client.registerOscEngine(engine);
  584. // Init engine
  585. QString engName = QString("%1 (master)").arg(name ? name : label);
  586. engName.truncate(engine->maxClientNameSize());
  587. if (! engine->init(engName.toUtf8().constData()))
  588. {
  589. const char* const lastError = CarlaBackend::getLastError();
  590. qWarning("Bridge engine failed to start, error was:\n%s", lastError);
  591. engine->close();
  592. delete engine;
  593. client.sendOscBridgeError(lastError);
  594. client.quit();
  595. return 2;
  596. }
  597. // Init plugin
  598. short id = engine->addPlugin(itype, filename, name, label);
  599. int ret;
  600. if (id >= 0 && id < CarlaBackend::MAX_PLUGINS)
  601. {
  602. CarlaBackend::CarlaPlugin* const plugin = engine->getPlugin(id);
  603. client.setStuff(engine, plugin);
  604. // create window if needed
  605. bool guiResizable;
  606. CarlaBackend::GuiType guiType;
  607. plugin->getGuiInfo(&guiType, &guiResizable);
  608. if (guiType == CarlaBackend::GUI_INTERNAL_QT4 || guiType == CarlaBackend::GUI_INTERNAL_COCOA || guiType == CarlaBackend::GUI_INTERNAL_HWND || guiType == CarlaBackend::GUI_INTERNAL_X11)
  609. {
  610. client.createWindow(guiResizable);
  611. }
  612. if (! useOsc)
  613. plugin->setActive(true, false, false);
  614. client.exec(nullptr, !useOsc);
  615. ret = 0;
  616. }
  617. else
  618. {
  619. const char* const lastError = CarlaBackend::getLastError();
  620. qWarning("Plugin failed to load, error was:\n%s", lastError);
  621. if (useOsc)
  622. client.sendOscBridgeError(lastError);
  623. ret = 1;
  624. }
  625. engine->removeAllPlugins();
  626. engine->close();
  627. delete engine;
  628. if (useOsc)
  629. {
  630. // Close OSC
  631. client.oscClose();
  632. // bridge client can't be closed manually, only by host
  633. }
  634. else
  635. {
  636. // Close bridge client
  637. client.quit();
  638. }
  639. return ret;
  640. }
  641. #endif // BUILD_BRIDGE_PLUGIN