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.

768 lines
19KB

  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/QTimerEvent>
  23. #include <QtGui/QApplication>
  24. #include <QtGui/QDialog>
  25. #include <QtGui/QVBoxLayout>
  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, sterm;
  53. sint.sa_handler = closeSignalHandler;
  54. sigemptyset(&sint.sa_mask);
  55. sint.sa_flags |= SA_RESTART;
  56. sigaction(SIGINT, &sint, 0);
  57. sterm.sa_handler = closeSignalHandler;
  58. sigemptyset(&sterm.sa_mask);
  59. sterm.sa_flags |= SA_RESTART;
  60. sigaction(SIGTERM, &sterm, 0);
  61. #elif defined(Q_OS_WIN)
  62. SetConsoleCtrlHandler(closeSignalHandler, TRUE);
  63. #endif
  64. }
  65. CARLA_BRIDGE_START_NAMESPACE
  66. // -------------------------------------------------------------------------
  67. class BridgePluginGUI : public QDialog
  68. {
  69. public:
  70. class Callback
  71. {
  72. public:
  73. virtual ~Callback() {}
  74. virtual void guiClosedCallback() = 0;
  75. };
  76. BridgePluginGUI(QWidget* const parent, Callback* const callback_, const char* const pluginName, const bool resizable)
  77. : QDialog(parent),
  78. callback(callback_)
  79. {
  80. qDebug("BridgePluginGUI::BridgePluginGUI(%p, %p, \"%s\", %s", parent, callback, pluginName, bool2str(resizable));
  81. Q_ASSERT(callback);
  82. Q_ASSERT(pluginName);
  83. m_firstShow = true;
  84. m_resizable = resizable;
  85. vbLayout = new QVBoxLayout(this);
  86. vbLayout->setContentsMargins(0, 0, 0, 0);
  87. setLayout(vbLayout);
  88. container = new GuiContainer(this);
  89. vbLayout->addWidget(container);
  90. setNewSize(50, 50);
  91. setWindowTitle(QString("%1 (GUI)").arg(pluginName));
  92. #ifdef Q_OS_WIN
  93. if (! resizable)
  94. setWindowFlags(windowFlags() | Qt::MSWindowsFixedSizeDialogHint);
  95. #endif
  96. }
  97. ~BridgePluginGUI()
  98. {
  99. qDebug("BridgePluginGUI::~BridgePluginGUI()");
  100. Q_ASSERT(container);
  101. Q_ASSERT(vbLayout);
  102. delete container;
  103. delete vbLayout;
  104. }
  105. GuiContainer* getContainer()
  106. {
  107. return container;
  108. }
  109. void setNewSize(int width, int height)
  110. {
  111. qDebug("BridgePluginGUI::setNewSize(%i, %i)", width, height);
  112. if (width < 30)
  113. width = 30;
  114. if (height < 30)
  115. height = 30;
  116. if (m_resizable)
  117. {
  118. resize(width, height);
  119. }
  120. else
  121. {
  122. setFixedSize(width, height);
  123. container->setFixedSize(width, height);
  124. }
  125. }
  126. void setVisible(const bool yesNo)
  127. {
  128. qDebug("BridgePluginGUI::setVisible(%s)", bool2str(yesNo));
  129. if (yesNo)
  130. {
  131. if (m_firstShow)
  132. {
  133. m_firstShow = false;
  134. restoreGeometry(QByteArray());
  135. }
  136. else if (! m_geometry.isNull())
  137. restoreGeometry(m_geometry);
  138. }
  139. else
  140. m_geometry = saveGeometry();
  141. QDialog::setVisible(yesNo);
  142. }
  143. protected:
  144. void hideEvent(QHideEvent* const event)
  145. {
  146. qDebug("BridgePluginGUI::hideEvent(%p)", event);
  147. event->accept();
  148. close();
  149. }
  150. void closeEvent(QCloseEvent* const event)
  151. {
  152. qDebug("BridgePluginGUI::closeEvent(%p)", event);
  153. if (event->spontaneous())
  154. callback->guiClosedCallback();
  155. QDialog::closeEvent(event);
  156. }
  157. void done(int r)
  158. {
  159. QDialog::done(r);
  160. close();
  161. }
  162. private:
  163. Callback* const callback;
  164. QVBoxLayout* vbLayout;
  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)
  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. Q_ASSERT(msgTimer == 0);
  195. Q_ASSERT(! pluginGui);
  196. }
  197. void setStuff(CarlaBackend::CarlaEngine* const engine, CarlaBackend::CarlaPlugin* const plugin)
  198. {
  199. qDebug("BridgePluginClient::setStuff(%p, %p)", engine, plugin);
  200. Q_ASSERT(engine);
  201. Q_ASSERT(plugin);
  202. this->engine = engine;
  203. this->plugin = plugin;
  204. }
  205. // ---------------------------------------------------------------------
  206. // toolkit
  207. void init()
  208. {
  209. qDebug("BridgePluginClient::init()");
  210. }
  211. void exec(CarlaClient* const, const bool showGui)
  212. {
  213. qDebug("BridgePluginClient::exec()");
  214. if (showGui)
  215. {
  216. show();
  217. }
  218. else
  219. {
  220. CarlaClient::sendOscUpdate();
  221. CarlaClient::sendOscBridgeUpdate();
  222. QApplication::setQuitOnLastWindowClosed(false);
  223. }
  224. msgTimer = startTimer(50);
  225. QApplication::exec();
  226. }
  227. void quit()
  228. {
  229. qDebug("BridgePluginClient::quit()");
  230. if (msgTimer != 0)
  231. {
  232. QApplication::killTimer(msgTimer);
  233. msgTimer = 0;
  234. }
  235. if (pluginGui)
  236. {
  237. if (pluginGui->isVisible())
  238. hide();
  239. pluginGui->close();
  240. delete pluginGui;
  241. pluginGui = nullptr;
  242. }
  243. if (! QApplication::closingDown())
  244. QApplication::quit();
  245. }
  246. void show()
  247. {
  248. qDebug("BridgePluginClient::show()");
  249. Q_ASSERT(pluginGui);
  250. if (plugin)
  251. plugin->showGui(true);
  252. if (pluginGui)
  253. pluginGui->show();
  254. }
  255. void hide()
  256. {
  257. qDebug("BridgePluginClient::hide()");
  258. Q_ASSERT(pluginGui);
  259. if (pluginGui)
  260. pluginGui->hide();
  261. if (plugin)
  262. plugin->showGui(false);
  263. }
  264. void resize(int width, int height)
  265. {
  266. qDebug("BridgePluginClient::resize(%i, %i)", width, height);
  267. Q_ASSERT(pluginGui);
  268. if (pluginGui)
  269. pluginGui->setNewSize(width, height);
  270. }
  271. // ---------------------------------------------------------------------
  272. void createWindow(const bool resizable)
  273. {
  274. qDebug("BridgePluginClient::createWindow(%s)", bool2str(resizable));
  275. Q_ASSERT(plugin);
  276. pluginGui = new BridgePluginGUI(nullptr, this, plugin->name(), resizable);
  277. plugin->setGuiContainer(pluginGui->getContainer());
  278. }
  279. // ---------------------------------------------------------------------
  280. // processing
  281. void setParameter(const int32_t rindex, const double value)
  282. {
  283. qDebug("CarlaPluginClient::setParameter(%i, %g)", rindex, value);
  284. Q_ASSERT(plugin);
  285. if (! plugin)
  286. return;
  287. plugin->setParameterValueByRIndex(rindex, value, true, true, false);
  288. }
  289. void setProgram(const uint32_t index)
  290. {
  291. qDebug("CarlaPluginClient::setProgram(%i)", index);
  292. Q_ASSERT(engine);
  293. Q_ASSERT(plugin);
  294. Q_ASSERT(index < plugin->programCount());
  295. if (! (plugin && engine))
  296. return;
  297. if (index >= plugin->programCount())
  298. return;
  299. plugin->setProgram(index, true, true, false, true);
  300. double value;
  301. for (uint32_t i=0; i < plugin->parameterCount(); i++)
  302. {
  303. value = plugin->getParameterValue(i);
  304. engine->osc_send_bridge_set_parameter_value(i, value);
  305. engine->osc_send_bridge_set_default_value(i, value);
  306. }
  307. }
  308. void setMidiProgram(const uint32_t index)
  309. {
  310. qDebug("CarlaPluginClient::setMidiProgram(%i)", index);
  311. Q_ASSERT(engine);
  312. Q_ASSERT(plugin);
  313. if (! (plugin && engine))
  314. return;
  315. plugin->setMidiProgram(index, true, true, false, true);
  316. double value;
  317. for (uint32_t i=0; i < plugin->parameterCount(); i++)
  318. {
  319. value = plugin->getParameterValue(i);
  320. engine->osc_send_bridge_set_parameter_value(i, value);
  321. engine->osc_send_bridge_set_default_value(i, value);
  322. }
  323. }
  324. void noteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  325. {
  326. qDebug("CarlaPluginClient::noteOn(%i, %i, %i)", channel, note, velo);
  327. Q_ASSERT(plugin);
  328. Q_ASSERT(velo > 0);
  329. if (! plugin)
  330. return;
  331. plugin->sendMidiSingleNote(channel, note, velo, true, true, false);
  332. }
  333. void noteOff(const uint8_t channel, const uint8_t note)
  334. {
  335. qDebug("CarlaPluginClient::noteOff(%i, %i)", channel, note);
  336. Q_ASSERT(plugin);
  337. if (! plugin)
  338. return;
  339. plugin->sendMidiSingleNote(channel, note, 0, true, true, false);
  340. }
  341. // ---------------------------------------------------------------------
  342. // plugin
  343. void saveNow()
  344. {
  345. qDebug("CarlaPluginClient::saveNow()");
  346. Q_ASSERT(plugin);
  347. Q_ASSERT(engine);
  348. if (! (plugin && engine))
  349. return;
  350. plugin->prepareForSave();
  351. for (uint32_t i=0; i < plugin->customDataCount(); i++)
  352. {
  353. const CarlaBackend::CustomData* const cdata = plugin->customData(i);
  354. engine->osc_send_bridge_set_custom_data(CarlaBackend::getCustomDataTypeString(cdata->type), cdata->key, cdata->value);
  355. }
  356. if (plugin->hints() & CarlaBackend::PLUGIN_USES_CHUNKS)
  357. {
  358. void* data = nullptr;
  359. int32_t dataSize = plugin->chunkData(&data);
  360. if (data && dataSize >= 4)
  361. {
  362. QString filePath;
  363. filePath = QDir::tempPath();
  364. #ifdef Q_OS_WIN
  365. filePath += "\\.CarlaChunk_";
  366. #else
  367. filePath += "/.CarlaChunk_";
  368. #endif
  369. filePath += plugin->name();
  370. QFile file(filePath);
  371. if (file.open(QIODevice::WriteOnly))
  372. {
  373. QByteArray chunk((const char*)data, dataSize);
  374. file.write(chunk);
  375. file.close();
  376. engine->osc_send_bridge_set_chunk_data(filePath.toUtf8().constData());
  377. }
  378. }
  379. }
  380. engine->osc_send_bridge_configure(CarlaBackend::CARLA_BRIDGE_MSG_SAVED, "");
  381. }
  382. void setCustomData(const char* const type, const char* const key, const char* const value)
  383. {
  384. qDebug("CarlaPluginClient::setCustomData(\"%s\", \"%s\", \"%s\")", type, key, value);
  385. Q_ASSERT(plugin);
  386. if (! plugin)
  387. return;
  388. plugin->setCustomData(CarlaBackend::getCustomDataStringType(type), key, value, true);
  389. }
  390. void setChunkData(const char* const filePath)
  391. {
  392. qDebug("CarlaPluginClient::setChunkData(\"%s\")", filePath);
  393. Q_ASSERT(plugin);
  394. if (! plugin)
  395. return;
  396. Q_UNUSED(filePath);
  397. #if 0
  398. nextChunkFilePath = strdup(filePath);
  399. while (nextChunkFilePath)
  400. carla_msleep(25);
  401. #endif
  402. }
  403. // ---------------------------------------------------------------------
  404. // callback
  405. void handleCallback(const CarlaBackend::CallbackType action, const int value1, const int value2, const double value3)
  406. {
  407. qDebug("CarlaPluginClient::handleCallback(%s, %i, %i, %g)", CarlaBackend::CallbackType2str(action), value1, value2, value3);
  408. if (! engine)
  409. return;
  410. switch (action)
  411. {
  412. case CarlaBackend::CALLBACK_PARAMETER_VALUE_CHANGED:
  413. engine->osc_send_bridge_set_parameter_value(value1, value3);
  414. break;
  415. case CarlaBackend::CALLBACK_PROGRAM_CHANGED:
  416. engine->osc_send_bridge_set_program(value1);
  417. break;
  418. case CarlaBackend::CALLBACK_MIDI_PROGRAM_CHANGED:
  419. engine->osc_send_bridge_set_midi_program(value1);
  420. break;
  421. case CarlaBackend::CALLBACK_NOTE_ON:
  422. {
  423. //uint8_t mdata[4] = { 0, MIDI_STATUS_NOTE_ON, (uint8_t)value1, (uint8_t)value2 };
  424. //osc_send_midi(mdata);
  425. break;
  426. }
  427. case CarlaBackend::CALLBACK_NOTE_OFF:
  428. {
  429. //uint8_t mdata[4] = { 0, MIDI_STATUS_NOTE_OFF, (uint8_t)value1, (uint8_t)value2 };
  430. //osc_send_midi(mdata);
  431. break;
  432. }
  433. case CarlaBackend::CALLBACK_SHOW_GUI:
  434. if (value1 == 0)
  435. engine->osc_send_bridge_configure(CarlaBackend::CARLA_BRIDGE_MSG_HIDE_GUI, "");
  436. break;
  437. case CarlaBackend::CALLBACK_RESIZE_GUI:
  438. Q_ASSERT(value1 > 0 && value2 > 0);
  439. nextWidth = value1;
  440. nextHeight = value2;
  441. break;
  442. case CarlaBackend::CALLBACK_RELOAD_PARAMETERS:
  443. //if (CARLA_PLUGIN)
  444. //{
  445. // for (uint32_t i=0; i < CARLA_PLUGIN->parameterCount(); i++)
  446. // {
  447. // osc_send_control(i, CARLA_PLUGIN->getParameterValue(i));
  448. // }
  449. //}
  450. break;
  451. case CarlaBackend::CALLBACK_QUIT:
  452. //QApplication::quit();
  453. break;
  454. default:
  455. break;
  456. }
  457. Q_UNUSED(value3);
  458. }
  459. // ---------------------------------------------------------------------
  460. static void callback(void* const ptr, CarlaBackend::CallbackType const action, const unsigned short, const int value1, const int value2, const double value3)
  461. {
  462. Q_ASSERT(ptr);
  463. if (! ptr)
  464. return;
  465. BridgePluginClient* const _this_ = (BridgePluginClient*)ptr;
  466. _this_->handleCallback(action, value1, value2, value3);
  467. }
  468. protected:
  469. void guiClosedCallback()
  470. {
  471. }
  472. void timerEvent(QTimerEvent* const event)
  473. {
  474. if (qCloseNow)
  475. return quit();
  476. if (event->timerId() == msgTimer)
  477. {
  478. if (nextWidth > 0 && nextHeight > 0 && pluginGui)
  479. {
  480. pluginGui->setNewSize(nextWidth, nextHeight);
  481. nextWidth = 0;
  482. nextHeight = 0;
  483. }
  484. if (plugin)
  485. plugin->idleGui();
  486. if (! CarlaClient::runMessages())
  487. {
  488. Q_ASSERT(msgTimer == 0);
  489. msgTimer = 0;
  490. return;
  491. }
  492. }
  493. QApplication::timerEvent(event);
  494. }
  495. // ---------------------------------------------------------------------
  496. private:
  497. int msgTimer;
  498. int nextWidth, nextHeight;
  499. CarlaBackend::CarlaEngine* engine;
  500. CarlaBackend::CarlaPlugin* plugin;
  501. BridgePluginGUI* pluginGui;
  502. };
  503. // -------------------------------------------------------------------------
  504. CARLA_BRIDGE_END_NAMESPACE
  505. int main(int argc, char* argv[])
  506. {
  507. if (argc != 6)
  508. {
  509. qWarning("usage: %s <osc-url|\"null\"> <type> <filename> <name|\"(none)\"> <label>", argv[0]);
  510. return 1;
  511. }
  512. qargc = argc;
  513. qargv = argv;
  514. const char* const oscUrl = argv[1];
  515. const char* const stype = argv[2];
  516. const char* const filename = argv[3];
  517. const char* name = argv[4];
  518. const char* const label = argv[5];
  519. const bool useOsc = strcmp(oscUrl, "null");
  520. if (strcmp(name, "(none)") == 0)
  521. name = nullptr;
  522. CarlaBackend::PluginType itype;
  523. if (strcmp(stype, "LADSPA") == 0)
  524. itype = CarlaBackend::PLUGIN_LADSPA;
  525. else if (strcmp(stype, "DSSI") == 0)
  526. itype = CarlaBackend::PLUGIN_DSSI;
  527. else if (strcmp(stype, "LV2") == 0)
  528. itype = CarlaBackend::PLUGIN_LV2;
  529. else if (strcmp(stype, "VST") == 0)
  530. itype = CarlaBackend::PLUGIN_VST;
  531. else
  532. {
  533. itype = CarlaBackend::PLUGIN_NONE;
  534. qWarning("Invalid plugin type '%s'", stype);
  535. return 1;
  536. }
  537. // Init bridge client
  538. CarlaBridge::BridgePluginClient client;
  539. client.init();
  540. // Init OSC
  541. if (useOsc && ! client.oscInit(oscUrl))
  542. {
  543. client.quit();
  544. return -1;
  545. }
  546. // Init backend engine
  547. CarlaBackend::CarlaEngineJack engine;
  548. engine.setCallback(client.callback, &client);
  549. // bridge client <-> engine
  550. client.registerOscEngine(&engine);
  551. // Init engine
  552. QString engName = QString("%1 (master)").arg(name ? name : label);
  553. engName.truncate(engine.maxClientNameSize());
  554. if (! engine.init(engName.toUtf8().constData()))
  555. {
  556. const char* const lastError = CarlaBackend::getLastError();
  557. qWarning("Bridge engine failed to start, error was:\n%s", lastError);
  558. engine.close();
  559. client.sendOscBridgeError(lastError);
  560. client.quit();
  561. return 2;
  562. }
  563. /// Init plugin
  564. short id = engine.addPlugin(itype, filename, name, label);
  565. int ret;
  566. if (id >= 0 && id < CarlaBackend::MAX_PLUGINS)
  567. {
  568. CarlaBackend::CarlaPlugin* const plugin = engine.getPlugin(id);
  569. client.setStuff(&engine, plugin);
  570. // create window if needed
  571. bool guiResizable;
  572. CarlaBackend::GuiType guiType;
  573. plugin->getGuiInfo(&guiType, &guiResizable);
  574. if (guiType == CarlaBackend::GUI_INTERNAL_QT4 || guiType == CarlaBackend::GUI_INTERNAL_COCOA || guiType == CarlaBackend::GUI_INTERNAL_HWND || guiType == CarlaBackend::GUI_INTERNAL_X11)
  575. {
  576. client.createWindow(guiResizable);
  577. }
  578. if (! useOsc)
  579. plugin->setActive(true, false, false);
  580. ret = 0;
  581. }
  582. else
  583. {
  584. const char* const lastError = CarlaBackend::getLastError();
  585. qWarning("Plugin failed to load, error was:\n%s", lastError);
  586. client.sendOscBridgeError(lastError);
  587. ret = 1;
  588. }
  589. if (ret == 0)
  590. {
  591. initSignalHandler();
  592. client.exec(nullptr, !useOsc);
  593. }
  594. engine.removeAllPlugins();
  595. engine.close();
  596. if (useOsc)
  597. {
  598. // Close OSC
  599. client.sendOscExiting();
  600. client.oscClose();
  601. // bridge client can't be closed manually, only by host
  602. }
  603. else
  604. {
  605. // Close bridge client
  606. client.quit();
  607. }
  608. return ret;
  609. }
  610. #endif // BUILD_BRIDGE_PLUGIN