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.

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