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.

442 lines
12KB

  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. #include "juce_core.h"
  25. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  26. # include "juce_gui_basics.h"
  27. using juce::JUCEApplication;
  28. using juce::JUCEApplicationBase;
  29. using juce::Timer;
  30. #endif
  31. using CarlaBackend::CarlaEngine;
  32. using CarlaBackend::EngineCallbackOpcode;
  33. using CarlaBackend::EngineCallbackOpcode2Str;
  34. using juce::CharPointer_UTF8;
  35. using juce::File;
  36. using juce::String;
  37. // -------------------------------------------------------------------------
  38. static bool gIsInitiated = false;
  39. static volatile bool gCloseNow = false;
  40. static volatile bool gSaveNow = false;
  41. #ifdef CARLA_OS_WIN
  42. static BOOL WINAPI winSignalHandler(DWORD dwCtrlType) noexcept
  43. {
  44. if (dwCtrlType == CTRL_C_EVENT)
  45. {
  46. gCloseNow = true;
  47. return TRUE;
  48. }
  49. return FALSE;
  50. }
  51. #elif defined(CARLA_OS_LINUX)
  52. static void closeSignalHandler(int) noexcept
  53. {
  54. gCloseNow = true;
  55. }
  56. static void saveSignalHandler(int) noexcept
  57. {
  58. gSaveNow = true;
  59. }
  60. #endif
  61. static void initSignalHandler()
  62. {
  63. #ifdef CARLA_OS_WIN
  64. SetConsoleCtrlHandler(winSignalHandler, TRUE);
  65. #elif defined(CARLA_OS_LINUX)
  66. struct sigaction sint;
  67. struct sigaction sterm;
  68. struct sigaction susr1;
  69. sint.sa_handler = closeSignalHandler;
  70. sint.sa_flags = SA_RESTART;
  71. sint.sa_restorer = nullptr;
  72. sigemptyset(&sint.sa_mask);
  73. sigaction(SIGINT, &sint, nullptr);
  74. sterm.sa_handler = closeSignalHandler;
  75. sterm.sa_flags = SA_RESTART;
  76. sterm.sa_restorer = nullptr;
  77. sigemptyset(&sterm.sa_mask);
  78. sigaction(SIGTERM, &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. leakDetector_CarlaBridgePlugin()
  147. {
  148. CARLA_ASSERT(clientName != nullptr && clientName[0] != '\0');
  149. carla_debug("CarlaBridgePlugin::CarlaBridgePlugin(%s, \"%s\", %s, %s, %s, %s)", bool2str(useBridge), clientName, audioPoolBaseName, rtClientBaseName, nonRtClientBaseName, nonRtServerBaseName);
  150. carla_set_engine_callback(callback, this);
  151. if (useBridge)
  152. carla_engine_init_bridge(audioPoolBaseName, rtClientBaseName, nonRtClientBaseName, nonRtServerBaseName, clientName);
  153. else
  154. carla_engine_init("JACK", clientName);
  155. fEngine = carla_get_engine();
  156. }
  157. ~CarlaBridgePlugin()
  158. {
  159. carla_debug("CarlaBridgePlugin::~CarlaBridgePlugin()");
  160. carla_engine_close();
  161. }
  162. bool isOk() const noexcept
  163. {
  164. return (fEngine != nullptr);
  165. }
  166. // ---------------------------------------------------------------------
  167. void exec(const bool useBridge, int argc, char* argv[])
  168. {
  169. fUsingBridge = useBridge;
  170. if (! useBridge)
  171. {
  172. const CarlaPluginInfo* const pInfo(carla_get_plugin_info(0));
  173. CARLA_SAFE_ASSERT_RETURN(pInfo != nullptr,);
  174. gProjectFilename = CharPointer_UTF8(pInfo->name);
  175. gProjectFilename += ".carxs";
  176. if (! File::isAbsolutePath(gProjectFilename))
  177. gProjectFilename = File::getCurrentWorkingDirectory().getChildFile(gProjectFilename).getFullPathName();
  178. if (File(gProjectFilename).existsAsFile() && ! carla_load_plugin_state(0, gProjectFilename.toRawUTF8()))
  179. carla_stderr("Plugin preset load failed, error was:\n%s", carla_get_last_error());
  180. }
  181. gIsInitiated = true;
  182. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  183. JUCEApplicationBase::createInstance = &juce_CreateApplication;
  184. JUCEApplicationBase::main(JUCE_MAIN_FUNCTION_ARGS);
  185. #else
  186. for (; ! gCloseNow;)
  187. {
  188. gIdle();
  189. carla_msleep(8);
  190. }
  191. #endif
  192. carla_set_engine_about_to_close();
  193. carla_remove_plugin(0);
  194. // may be unused
  195. return; (void)argc; (void)argv;
  196. }
  197. // ---------------------------------------------------------------------
  198. protected:
  199. void handleCallback(const EngineCallbackOpcode action, const int value1, const int, const float, const char* const)
  200. {
  201. CARLA_BACKEND_USE_NAMESPACE;
  202. switch (action)
  203. {
  204. case ENGINE_CALLBACK_ENGINE_STOPPED:
  205. case ENGINE_CALLBACK_PLUGIN_REMOVED:
  206. case ENGINE_CALLBACK_QUIT:
  207. gCloseNow = true;
  208. break;
  209. case ENGINE_CALLBACK_UI_STATE_CHANGED:
  210. if (gIsInitiated && value1 != 1 && ! fUsingBridge)
  211. gCloseNow = true;
  212. break;
  213. default:
  214. break;
  215. }
  216. }
  217. private:
  218. const CarlaEngine* fEngine;
  219. bool fUsingBridge;
  220. static void callback(void* ptr, EngineCallbackOpcode action, unsigned int pluginId, int value1, int value2, float value3, const char* valueStr)
  221. {
  222. carla_debug("CarlaBridgePlugin::callback(%p, %i:%s, %i, %i, %i, %f, \"%s\")", ptr, action, EngineCallbackOpcode2Str(action), pluginId, value1, value2, value3, valueStr);
  223. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr,);
  224. CARLA_SAFE_ASSERT_RETURN(pluginId == 0,);
  225. return ((CarlaBridgePlugin*)ptr)->handleCallback(action, value1, value2, value3, valueStr);
  226. }
  227. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaBridgePlugin)
  228. };
  229. // -------------------------------------------------------------------------
  230. int main(int argc, char* argv[])
  231. {
  232. // ---------------------------------------------------------------------
  233. // Check argument count
  234. if (argc != 4 && argc != 5)
  235. {
  236. carla_stdout("usage: %s <type> <filename> <label> [uniqueId]", argv[0]);
  237. return 1;
  238. }
  239. // ---------------------------------------------------------------------
  240. // Get args
  241. const char* const stype = argv[1];
  242. const char* filename = argv[2];
  243. const char* label = argv[3];
  244. const int64_t uniqueId = (argc == 5) ? static_cast<int64_t>(std::atoll(argv[4])) : 0;
  245. if (filename[0] == '\0' || std::strcmp(filename, "(none)") == 0)
  246. filename = nullptr;
  247. if (label[0] == '\0' || std::strcmp(label, "(none)") == 0)
  248. label = nullptr;
  249. // ---------------------------------------------------------------------
  250. // Check plugin type
  251. CarlaBackend::PluginType itype(CarlaBackend::getPluginTypeFromString(stype));
  252. if (itype == CarlaBackend::PLUGIN_NONE)
  253. {
  254. carla_stderr("Invalid plugin type '%s'", stype);
  255. return 1;
  256. }
  257. // ---------------------------------------------------------------------
  258. // Set name
  259. const char* name(std::getenv("CARLA_CLIENT_NAME"));
  260. if (name != nullptr && (name[0] == '\0' || std::strcmp(name, "(none)") == 0))
  261. name = nullptr;
  262. // ---------------------------------------------------------------------
  263. // Setup options
  264. const char* const shmIds(std::getenv("ENGINE_BRIDGE_SHM_IDS"));
  265. const bool useBridge = (shmIds != nullptr);
  266. // ---------------------------------------------------------------------
  267. // Setup bridge ids
  268. char audioPoolBaseName[6+1];
  269. char rtClientBaseName[6+1];
  270. char nonRtClientBaseName[6+1];
  271. char nonRtServerBaseName[6+1];
  272. if (useBridge)
  273. {
  274. CARLA_SAFE_ASSERT_RETURN(std::strlen(shmIds) == 6*4, 1);
  275. std::strncpy(audioPoolBaseName, shmIds+6*0, 6);
  276. std::strncpy(rtClientBaseName, shmIds+6*1, 6);
  277. std::strncpy(nonRtClientBaseName, shmIds+6*2, 6);
  278. std::strncpy(nonRtServerBaseName, shmIds+6*3, 6);
  279. audioPoolBaseName[6] = '\0';
  280. rtClientBaseName[6] = '\0';
  281. nonRtClientBaseName[6] = '\0';
  282. nonRtServerBaseName[6] = '\0';
  283. }
  284. else
  285. {
  286. audioPoolBaseName[0] = '\0';
  287. rtClientBaseName[0] = '\0';
  288. nonRtClientBaseName[0] = '\0';
  289. nonRtServerBaseName[0] = '\0';
  290. }
  291. // ---------------------------------------------------------------------
  292. // Set client name
  293. CarlaString clientName(name != nullptr ? name : label);
  294. if (clientName.isEmpty())
  295. {
  296. const String jfilename = String(CharPointer_UTF8(filename));
  297. clientName = File(jfilename).getFileNameWithoutExtension().toRawUTF8();
  298. }
  299. // ---------------------------------------------------------------------
  300. // Set extraStuff
  301. const void* extraStuff = nullptr;
  302. if (itype == CarlaBackend::PLUGIN_GIG || itype == CarlaBackend::PLUGIN_SF2)
  303. {
  304. if (label == nullptr)
  305. label = clientName;
  306. if (std::strstr(label, " (16 outs)") != nullptr)
  307. extraStuff = "true";
  308. }
  309. // ---------------------------------------------------------------------
  310. // Init plugin bridge
  311. CarlaBridgePlugin bridge(useBridge, clientName, audioPoolBaseName, rtClientBaseName, nonRtClientBaseName, nonRtServerBaseName);
  312. if (! bridge.isOk())
  313. {
  314. carla_stderr("Failed to init engine, error was:\n%s", carla_get_last_error());
  315. return 1;
  316. }
  317. // ---------------------------------------------------------------------
  318. // Listen for ctrl+c or sigint/sigterm events
  319. initSignalHandler();
  320. // ---------------------------------------------------------------------
  321. // Init plugin
  322. int ret;
  323. if (carla_add_plugin(CarlaBackend::BINARY_NATIVE, itype, filename, name, label, uniqueId, extraStuff))
  324. {
  325. ret = 0;
  326. if (! useBridge)
  327. {
  328. carla_set_active(0, true);
  329. if (const CarlaPluginInfo* const pluginInfo = carla_get_plugin_info(0))
  330. {
  331. if (pluginInfo->hints & CarlaBackend::PLUGIN_HAS_CUSTOM_UI)
  332. carla_show_custom_ui(0, true);
  333. }
  334. }
  335. bridge.exec(useBridge, argc, argv);
  336. }
  337. else
  338. {
  339. ret = 1;
  340. const char* const lastError(carla_get_last_error());
  341. carla_stderr("Plugin failed to load, error was:\n%s", lastError);
  342. //if (useBridge)
  343. // bridge.sendOscBridgeError(lastError);
  344. }
  345. return ret;
  346. }