Audio plugin host https://kx.studio/carla
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.

CarlaBridgePlugin.cpp 15KB

8 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  1. /*
  2. * Carla Bridge Plugin
  3. * Copyright (C) 2012-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or 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 doc/GPL.txt file.
  16. */
  17. #include "CarlaEngine.hpp"
  18. #include "CarlaHost.h"
  19. #include "CarlaBackendUtils.hpp"
  20. #include "CarlaMIDI.h"
  21. #ifdef CARLA_OS_UNIX
  22. # include <signal.h>
  23. #endif
  24. #ifdef HAVE_X11
  25. # include <X11/Xlib.h>
  26. #endif
  27. #include "jackbridge/JackBridge.hpp"
  28. #include "juce_core/juce_core.h"
  29. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  30. # include "juce_gui_basics/juce_gui_basics.h"
  31. using juce::JUCEApplication;
  32. using juce::JUCEApplicationBase;
  33. using juce::ScopedJuceInitialiser_GUI;
  34. using juce::Timer;
  35. #endif
  36. using CarlaBackend::CarlaEngine;
  37. using CarlaBackend::EngineCallbackOpcode;
  38. using CarlaBackend::EngineCallbackOpcode2Str;
  39. using juce::CharPointer_UTF8;
  40. using juce::File;
  41. using juce::String;
  42. // -------------------------------------------------------------------------
  43. static bool gIsInitiated = false;
  44. static volatile bool gCloseNow = false;
  45. static volatile bool gSaveNow = false;
  46. #ifdef CARLA_OS_WIN
  47. static BOOL WINAPI winSignalHandler(DWORD dwCtrlType) noexcept
  48. {
  49. if (dwCtrlType == CTRL_C_EVENT)
  50. {
  51. gCloseNow = true;
  52. return TRUE;
  53. }
  54. return FALSE;
  55. }
  56. #elif defined(CARLA_OS_LINUX)
  57. static void closeSignalHandler(int) noexcept
  58. {
  59. gCloseNow = true;
  60. }
  61. static void saveSignalHandler(int) noexcept
  62. {
  63. gSaveNow = true;
  64. }
  65. #endif
  66. static void initSignalHandler()
  67. {
  68. #ifdef CARLA_OS_WIN
  69. SetConsoleCtrlHandler(winSignalHandler, TRUE);
  70. #elif defined(CARLA_OS_LINUX)
  71. struct sigaction sterm;
  72. struct sigaction susr1;
  73. sterm.sa_handler = closeSignalHandler;
  74. sterm.sa_flags = SA_RESTART;
  75. sterm.sa_restorer = nullptr;
  76. sigemptyset(&sterm.sa_mask);
  77. sigaction(SIGTERM, &sterm, nullptr);
  78. sigaction(SIGINT, &sterm, nullptr);
  79. susr1.sa_handler = saveSignalHandler;
  80. susr1.sa_flags = SA_RESTART;
  81. susr1.sa_restorer = nullptr;
  82. sigemptyset(&susr1.sa_mask);
  83. sigaction(SIGUSR1, &susr1, nullptr);
  84. #endif
  85. }
  86. // -------------------------------------------------------------------------
  87. static String gProjectFilename;
  88. static void gIdle()
  89. {
  90. carla_engine_idle();
  91. if (gSaveNow)
  92. {
  93. gSaveNow = false;
  94. if (gProjectFilename.isNotEmpty())
  95. {
  96. if (! carla_save_plugin_state(0, gProjectFilename.toRawUTF8()))
  97. carla_stderr("Plugin preset save failed, error was:\n%s", carla_get_last_error());
  98. }
  99. }
  100. }
  101. // -------------------------------------------------------------------------
  102. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  103. class CarlaJuceApp : public JUCEApplication,
  104. private Timer
  105. {
  106. public:
  107. CarlaJuceApp() {}
  108. ~CarlaJuceApp() {}
  109. void initialise(const String&) override
  110. {
  111. startTimer(8);
  112. }
  113. void shutdown() override
  114. {
  115. gCloseNow = true;
  116. stopTimer();
  117. }
  118. const String getApplicationName() override
  119. {
  120. return "CarlaPlugin";
  121. }
  122. const String getApplicationVersion() override
  123. {
  124. return CARLA_VERSION_STRING;
  125. }
  126. void timerCallback() override
  127. {
  128. gIdle();
  129. if (gCloseNow)
  130. {
  131. quit();
  132. gCloseNow = false;
  133. }
  134. }
  135. };
  136. static JUCEApplicationBase* juce_CreateApplication() { return new CarlaJuceApp(); }
  137. #endif
  138. // -------------------------------------------------------------------------
  139. class CarlaBridgePlugin
  140. {
  141. public:
  142. CarlaBridgePlugin(const bool useBridge, const char* const clientName, const char* const audioPoolBaseName,
  143. const char* const rtClientBaseName, const char* const nonRtClientBaseName, const char* const nonRtServerBaseName)
  144. : fEngine(nullptr),
  145. fUsingBridge(false)
  146. {
  147. CARLA_ASSERT(clientName != nullptr && clientName[0] != '\0');
  148. carla_debug("CarlaBridgePlugin::CarlaBridgePlugin(%s, \"%s\", %s, %s, %s, %s)", bool2str(useBridge), clientName, audioPoolBaseName, rtClientBaseName, nonRtClientBaseName, nonRtServerBaseName);
  149. carla_set_engine_callback(callback, this);
  150. if (useBridge)
  151. carla_engine_init_bridge(audioPoolBaseName, rtClientBaseName, nonRtClientBaseName, nonRtServerBaseName, clientName);
  152. else
  153. carla_engine_init("JACK", clientName);
  154. fEngine = carla_get_engine();
  155. }
  156. ~CarlaBridgePlugin()
  157. {
  158. carla_debug("CarlaBridgePlugin::~CarlaBridgePlugin()");
  159. carla_engine_close();
  160. }
  161. bool isOk() const noexcept
  162. {
  163. return (fEngine != nullptr);
  164. }
  165. // ---------------------------------------------------------------------
  166. void exec(const bool useBridge, int argc, char* argv[])
  167. {
  168. fUsingBridge = useBridge;
  169. if (! useBridge)
  170. {
  171. const CarlaPluginInfo* const pInfo(carla_get_plugin_info(0));
  172. CARLA_SAFE_ASSERT_RETURN(pInfo != nullptr,);
  173. gProjectFilename = CharPointer_UTF8(pInfo->name);
  174. gProjectFilename += ".carxs";
  175. if (! File::isAbsolutePath(gProjectFilename))
  176. gProjectFilename = File::getCurrentWorkingDirectory().getChildFile(gProjectFilename).getFullPathName();
  177. if (File(gProjectFilename).existsAsFile() && ! carla_load_plugin_state(0, gProjectFilename.toRawUTF8()))
  178. carla_stderr("Plugin preset load failed, error was:\n%s", carla_get_last_error());
  179. }
  180. gIsInitiated = true;
  181. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  182. JUCEApplicationBase::createInstance = &juce_CreateApplication;
  183. JUCEApplicationBase::main(JUCE_MAIN_FUNCTION_ARGS);
  184. #else
  185. for (; ! gCloseNow;)
  186. {
  187. gIdle();
  188. carla_msleep(8);
  189. }
  190. #endif
  191. carla_set_engine_about_to_close();
  192. carla_remove_plugin(0);
  193. // may be unused
  194. return; (void)argc; (void)argv;
  195. }
  196. // ---------------------------------------------------------------------
  197. protected:
  198. void handleCallback(const EngineCallbackOpcode action, const int value1, const int, const float, const char* const)
  199. {
  200. CARLA_BACKEND_USE_NAMESPACE;
  201. switch (action)
  202. {
  203. case ENGINE_CALLBACK_ENGINE_STOPPED:
  204. case ENGINE_CALLBACK_PLUGIN_REMOVED:
  205. case ENGINE_CALLBACK_QUIT:
  206. gCloseNow = true;
  207. break;
  208. case ENGINE_CALLBACK_UI_STATE_CHANGED:
  209. if (gIsInitiated && value1 != 1 && ! fUsingBridge)
  210. gCloseNow = true;
  211. break;
  212. default:
  213. break;
  214. }
  215. }
  216. private:
  217. const CarlaEngine* fEngine;
  218. bool fUsingBridge;
  219. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  220. const ScopedJuceInitialiser_GUI kJuceInitialiser;
  221. #endif
  222. static void callback(void* ptr, EngineCallbackOpcode action, unsigned int pluginId, int value1, int value2, float value3, const char* valueStr)
  223. {
  224. carla_debug("CarlaBridgePlugin::callback(%p, %i:%s, %i, %i, %i, %f, \"%s\")", ptr, action, EngineCallbackOpcode2Str(action), pluginId, value1, value2, value3, valueStr);
  225. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr,);
  226. CARLA_SAFE_ASSERT_RETURN(pluginId == 0,);
  227. return ((CarlaBridgePlugin*)ptr)->handleCallback(action, value1, value2, value3, valueStr);
  228. }
  229. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaBridgePlugin)
  230. };
  231. // -------------------------------------------------------------------------
  232. int main(int argc, char* argv[])
  233. {
  234. // ---------------------------------------------------------------------
  235. // Check argument count
  236. if (argc != 4 && argc != 5)
  237. {
  238. carla_stdout("usage: %s <type> <filename> <label> [uniqueId]", argv[0]);
  239. return 1;
  240. }
  241. #if defined(CARLA_OS_WIN) && ! defined(BUILDING_CARLA_FOR_WINDOWS)
  242. // ---------------------------------------------------------------------
  243. // Test if bridge is working
  244. if (! jackbridge_is_ok())
  245. {
  246. carla_stderr("A JACK or Wine library is missing, cannot continue");
  247. return 1;
  248. }
  249. #endif
  250. // ---------------------------------------------------------------------
  251. // Get args
  252. const char* const stype = argv[1];
  253. const char* filename = argv[2];
  254. const char* label = argv[3];
  255. const int64_t uniqueId = (argc == 5) ? static_cast<int64_t>(std::atoll(argv[4])) : 0;
  256. if (filename[0] == '\0' || std::strcmp(filename, "(none)") == 0)
  257. filename = nullptr;
  258. if (label[0] == '\0' || std::strcmp(label, "(none)") == 0)
  259. label = nullptr;
  260. // ---------------------------------------------------------------------
  261. // Check binary type
  262. CarlaBackend::BinaryType btype = CarlaBackend::BINARY_NATIVE;
  263. if (const char* const binaryTypeStr = std::getenv("CARLA_BRIDGE_PLUGIN_BINARY_TYPE"))
  264. btype = CarlaBackend::getBinaryTypeFromString(binaryTypeStr);
  265. if (btype == CarlaBackend::BINARY_NONE)
  266. {
  267. carla_stderr("Invalid binary type '%i'", btype);
  268. return 1;
  269. }
  270. // ---------------------------------------------------------------------
  271. // Check plugin type
  272. CarlaBackend::PluginType itype(CarlaBackend::getPluginTypeFromString(stype));
  273. if (itype == CarlaBackend::PLUGIN_NONE)
  274. {
  275. carla_stderr("Invalid plugin type '%s'", stype);
  276. return 1;
  277. }
  278. // ---------------------------------------------------------------------
  279. // Set name
  280. const char* name(std::getenv("CARLA_CLIENT_NAME"));
  281. if (name != nullptr && (name[0] == '\0' || std::strcmp(name, "(none)") == 0))
  282. name = nullptr;
  283. // ---------------------------------------------------------------------
  284. // Setup options
  285. const char* const shmIds(std::getenv("ENGINE_BRIDGE_SHM_IDS"));
  286. const bool useBridge = (shmIds != nullptr);
  287. // ---------------------------------------------------------------------
  288. // Setup bridge ids
  289. char audioPoolBaseName[6+1];
  290. char rtClientBaseName[6+1];
  291. char nonRtClientBaseName[6+1];
  292. char nonRtServerBaseName[6+1];
  293. if (useBridge)
  294. {
  295. CARLA_SAFE_ASSERT_RETURN(std::strlen(shmIds) == 6*4, 1);
  296. std::strncpy(audioPoolBaseName, shmIds+6*0, 6);
  297. std::strncpy(rtClientBaseName, shmIds+6*1, 6);
  298. std::strncpy(nonRtClientBaseName, shmIds+6*2, 6);
  299. std::strncpy(nonRtServerBaseName, shmIds+6*3, 6);
  300. audioPoolBaseName[6] = '\0';
  301. rtClientBaseName[6] = '\0';
  302. nonRtClientBaseName[6] = '\0';
  303. nonRtServerBaseName[6] = '\0';
  304. }
  305. else
  306. {
  307. audioPoolBaseName[0] = '\0';
  308. rtClientBaseName[0] = '\0';
  309. nonRtClientBaseName[0] = '\0';
  310. nonRtServerBaseName[0] = '\0';
  311. jackbridge_init();
  312. }
  313. // ---------------------------------------------------------------------
  314. // Set client name
  315. CarlaString clientName;
  316. if (name != nullptr)
  317. {
  318. clientName = name;
  319. }
  320. else if (itype == CarlaBackend::PLUGIN_LV2)
  321. {
  322. // LV2 requires URI
  323. CARLA_SAFE_ASSERT_RETURN(label != nullptr && label[0] != '\0', 1);
  324. // LV2 URI is not usable as client name, create a usable name from URI
  325. CarlaString label2(label);
  326. // truncate until last valid char
  327. for (std::size_t i=label2.length()-1; i != 0; --i)
  328. {
  329. if (! std::isalnum(label2[i]))
  330. continue;
  331. label2.truncate(i+1);
  332. break;
  333. }
  334. // get last used separator
  335. bool found;
  336. std::size_t septmp, sep = 0;
  337. septmp = label2.rfind('#', &found)+1;
  338. if (found && septmp > sep)
  339. sep = septmp;
  340. septmp = label2.rfind('/', &found)+1;
  341. if (found && septmp > sep)
  342. sep = septmp;
  343. septmp = label2.rfind('=', &found)+1;
  344. if (found && septmp > sep)
  345. sep = septmp;
  346. septmp = label2.rfind(':', &found)+1;
  347. if (found && septmp > sep)
  348. sep = septmp;
  349. // make name starting from the separator and first valid char
  350. const char* name2 = label2.buffer() + sep;
  351. for (; *name2 != '\0' && ! std::isalnum(*name2); ++name2) {}
  352. if (*name2 != '\0')
  353. clientName = name2;
  354. }
  355. else if (label != nullptr)
  356. {
  357. clientName = label;
  358. }
  359. else
  360. {
  361. const String jfilename = String(CharPointer_UTF8(filename));
  362. clientName = File(jfilename).getFileNameWithoutExtension().toRawUTF8();
  363. }
  364. // if we still have no client name by now, use a dummy one
  365. if (clientName.isEmpty())
  366. clientName = "carla-plugin";
  367. // just to be safe
  368. clientName.toBasic();
  369. // ---------------------------------------------------------------------
  370. // Set extraStuff
  371. const void* extraStuff = nullptr;
  372. if (itype == CarlaBackend::PLUGIN_GIG || itype == CarlaBackend::PLUGIN_SF2)
  373. {
  374. if (label == nullptr)
  375. label = clientName;
  376. if (std::strstr(label, " (16 outs)") != nullptr)
  377. extraStuff = "true";
  378. }
  379. #ifdef HAVE_X11
  380. if (std::getenv("DISPLAY") != nullptr)
  381. XInitThreads();
  382. #endif
  383. // ---------------------------------------------------------------------
  384. // Init plugin bridge
  385. CarlaBridgePlugin bridge(useBridge, clientName, audioPoolBaseName, rtClientBaseName, nonRtClientBaseName, nonRtServerBaseName);
  386. if (! bridge.isOk())
  387. {
  388. carla_stderr("Failed to init engine, error was:\n%s", carla_get_last_error());
  389. return 1;
  390. }
  391. // ---------------------------------------------------------------------
  392. // Listen for ctrl+c or sigint/sigterm events
  393. initSignalHandler();
  394. // ---------------------------------------------------------------------
  395. // Init plugin
  396. int ret;
  397. if (carla_add_plugin(btype, itype, filename, name, label, uniqueId, extraStuff, 0x0))
  398. {
  399. ret = 0;
  400. if (! useBridge)
  401. {
  402. carla_set_active(0, true);
  403. if (const CarlaPluginInfo* const pluginInfo = carla_get_plugin_info(0))
  404. {
  405. if (pluginInfo->hints & CarlaBackend::PLUGIN_HAS_CUSTOM_UI)
  406. {
  407. #ifdef HAVE_X11
  408. if (std::getenv("DISPLAY") != nullptr)
  409. #endif
  410. carla_show_custom_ui(0, true);
  411. }
  412. }
  413. }
  414. bridge.exec(useBridge, argc, argv);
  415. }
  416. else
  417. {
  418. ret = 1;
  419. const char* const lastError(carla_get_last_error());
  420. carla_stderr("Plugin failed to load, error was:\n%s", lastError);
  421. //if (useBridge)
  422. // bridge.sendOscBridgeError(lastError);
  423. }
  424. return ret;
  425. }