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 22KB

3 years ago
3 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /*
  2. * Carla Bridge Plugin
  3. * Copyright (C) 2012-2021 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. #ifndef BUILD_BRIDGE
  18. # error This file should not be compiled if not building bridge
  19. #endif
  20. #include "CarlaEngine.hpp"
  21. #include "CarlaHost.h"
  22. #include "CarlaUtils.h"
  23. #include "CarlaBackendUtils.hpp"
  24. #include "CarlaJuceUtils.hpp"
  25. #include "CarlaMainLoop.hpp"
  26. #include "CarlaMIDI.h"
  27. #ifdef CARLA_OS_MAC
  28. # include "CarlaMacUtils.hpp"
  29. #endif
  30. #ifdef CARLA_OS_UNIX
  31. # include <signal.h>
  32. #endif
  33. #ifdef CARLA_OS_LINUX
  34. # include <sched.h>
  35. # define SCHED_RESET_ON_FORK 0x40000000
  36. #endif
  37. #ifdef CARLA_OS_WIN
  38. # include <pthread.h>
  39. # include <objbase.h>
  40. #endif
  41. #ifdef HAVE_X11
  42. # include <X11/Xlib.h>
  43. #endif
  44. #ifdef USING_JUCE
  45. # if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  46. # pragma GCC diagnostic push
  47. # pragma GCC diagnostic ignored "-Wconversion"
  48. # pragma GCC diagnostic ignored "-Weffc++"
  49. # pragma GCC diagnostic ignored "-Wsign-conversion"
  50. # pragma GCC diagnostic ignored "-Wundef"
  51. # pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
  52. # endif
  53. # include "AppConfig.h"
  54. # if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  55. # include "juce_gui_basics/juce_gui_basics.h"
  56. # else
  57. # include "juce_events/juce_events.h"
  58. # endif
  59. # if defined(__GNUC__) && (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6))
  60. # pragma GCC diagnostic pop
  61. # endif
  62. #endif
  63. #include "jackbridge/JackBridge.hpp"
  64. #include "water/files/File.h"
  65. #include "water/misc/Time.h"
  66. using CarlaBackend::CarlaEngine;
  67. using CarlaBackend::EngineCallbackOpcode;
  68. using CarlaBackend::EngineCallbackOpcode2Str;
  69. using CarlaBackend::runMainLoopOnce;
  70. using water::CharPointer_UTF8;
  71. using water::File;
  72. using water::String;
  73. // -------------------------------------------------------------------------
  74. static bool gIsInitiated = false;
  75. static volatile bool gCloseNow = false;
  76. static volatile bool gSaveNow = false;
  77. #if defined(CARLA_OS_UNIX)
  78. static void closeSignalHandler(int) noexcept
  79. {
  80. gCloseNow = true;
  81. }
  82. static void saveSignalHandler(int) noexcept
  83. {
  84. gSaveNow = true;
  85. }
  86. #elif defined(CARLA_OS_WIN)
  87. static BOOL WINAPI winSignalHandler(DWORD dwCtrlType) noexcept
  88. {
  89. if (dwCtrlType == CTRL_C_EVENT)
  90. {
  91. gCloseNow = true;
  92. return TRUE;
  93. }
  94. return FALSE;
  95. }
  96. #endif
  97. static void initSignalHandler()
  98. {
  99. #if defined(CARLA_OS_UNIX)
  100. struct sigaction sig;
  101. carla_zeroStruct(sig);
  102. sig.sa_handler = closeSignalHandler;
  103. sig.sa_flags = SA_RESTART;
  104. sigemptyset(&sig.sa_mask);
  105. sigaction(SIGTERM, &sig, nullptr);
  106. sigaction(SIGINT, &sig, nullptr);
  107. sig.sa_handler = saveSignalHandler;
  108. sig.sa_flags = SA_RESTART;
  109. sigemptyset(&sig.sa_mask);
  110. sigaction(SIGUSR1, &sig, nullptr);
  111. #elif defined(CARLA_OS_WIN)
  112. SetConsoleCtrlHandler(winSignalHandler, TRUE);
  113. #endif
  114. }
  115. // -------------------------------------------------------------------------
  116. static String gProjectFilename;
  117. static CarlaHostHandle gHostHandle;
  118. static void gIdle()
  119. {
  120. carla_engine_idle(gHostHandle);
  121. if (gSaveNow)
  122. {
  123. gSaveNow = false;
  124. if (gProjectFilename.isNotEmpty())
  125. {
  126. if (! carla_save_plugin_state(gHostHandle, 0, gProjectFilename.toRawUTF8()))
  127. carla_stderr("Plugin preset save failed, error was:\n%s", carla_get_last_error(gHostHandle));
  128. }
  129. }
  130. }
  131. // -------------------------------------------------------------------------
  132. #if defined(USING_JUCE) && (defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN))
  133. class CarlaJuceApp : public juce::JUCEApplication,
  134. private juce::Timer
  135. {
  136. public:
  137. CarlaJuceApp() {}
  138. ~CarlaJuceApp() {}
  139. void initialise(const juce::String&) override
  140. {
  141. startTimer(8);
  142. }
  143. void shutdown() override
  144. {
  145. gCloseNow = true;
  146. stopTimer();
  147. }
  148. const juce::String getApplicationName() override
  149. {
  150. return "CarlaPlugin";
  151. }
  152. const juce::String getApplicationVersion() override
  153. {
  154. return CARLA_VERSION_STRING;
  155. }
  156. void timerCallback() override
  157. {
  158. gIdle();
  159. if (gCloseNow)
  160. {
  161. quit();
  162. gCloseNow = false;
  163. }
  164. }
  165. };
  166. static juce::JUCEApplicationBase* juce_CreateApplication() { return new CarlaJuceApp(); }
  167. #endif
  168. // -------------------------------------------------------------------------
  169. class CarlaBridgePlugin
  170. {
  171. public:
  172. CarlaBridgePlugin(const bool useBridge, const char* const clientName, const char* const audioPoolBaseName,
  173. const char* const rtClientBaseName, const char* const nonRtClientBaseName, const char* const nonRtServerBaseName)
  174. : fEngine(nullptr),
  175. #ifdef USING_JUCE
  176. fJuceInitialiser(),
  177. #endif
  178. fUsingBridge(false),
  179. fUsingExec(false)
  180. {
  181. CARLA_ASSERT(clientName != nullptr && clientName[0] != '\0');
  182. carla_debug("CarlaBridgePlugin::CarlaBridgePlugin(%s, \"%s\", %s, %s, %s, %s)",
  183. bool2str(useBridge), clientName, audioPoolBaseName, rtClientBaseName, nonRtClientBaseName, nonRtServerBaseName);
  184. carla_set_engine_callback(gHostHandle, callback, this);
  185. if (useBridge)
  186. {
  187. carla_engine_init_bridge(gHostHandle,
  188. audioPoolBaseName,
  189. rtClientBaseName,
  190. nonRtClientBaseName,
  191. nonRtServerBaseName,
  192. clientName);
  193. }
  194. else if (std::getenv("CARLA_BRIDGE_DUMMY") != nullptr)
  195. {
  196. carla_engine_init(gHostHandle, "Dummy", clientName);
  197. }
  198. else
  199. {
  200. carla_engine_init(gHostHandle, "JACK", clientName);
  201. }
  202. fEngine = carla_get_engine_from_handle(gHostHandle);
  203. }
  204. ~CarlaBridgePlugin()
  205. {
  206. carla_debug("CarlaBridgePlugin::~CarlaBridgePlugin()");
  207. if (fEngine != nullptr && ! fUsingExec)
  208. carla_engine_close(gHostHandle);
  209. }
  210. bool isOk() const noexcept
  211. {
  212. return (fEngine != nullptr);
  213. }
  214. // ---------------------------------------------------------------------
  215. void exec(const bool useBridge)
  216. {
  217. fUsingBridge = useBridge;
  218. fUsingExec = true;
  219. const bool testing = std::getenv("CARLA_BRIDGE_TESTING") != nullptr;
  220. if (! useBridge && ! testing)
  221. {
  222. const CarlaPluginInfo* const pInfo = carla_get_plugin_info(gHostHandle, 0);
  223. CARLA_SAFE_ASSERT_RETURN(pInfo != nullptr,);
  224. gProjectFilename = CharPointer_UTF8(pInfo->name);
  225. gProjectFilename += ".carxs";
  226. if (! File::isAbsolutePath(gProjectFilename))
  227. gProjectFilename = File::getCurrentWorkingDirectory().getChildFile(gProjectFilename).getFullPathName();
  228. if (File(gProjectFilename).existsAsFile())
  229. {
  230. if (carla_load_plugin_state(gHostHandle, 0, gProjectFilename.toRawUTF8()))
  231. carla_stdout("Plugin state loaded successfully");
  232. else
  233. carla_stderr("Plugin state load failed, error was:\n%s", carla_get_last_error(gHostHandle));
  234. }
  235. else
  236. {
  237. carla_stdout("Previous plugin state in '%s' is non-existent, will start from default state",
  238. gProjectFilename.toRawUTF8());
  239. }
  240. }
  241. gIsInitiated = true;
  242. #if defined(USING_JUCE) && (defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN))
  243. # ifndef CARLA_OS_WIN
  244. static const int argc = 0;
  245. static const char* argv[] = {};
  246. # endif
  247. juce::JUCEApplicationBase::createInstance = &juce_CreateApplication;
  248. juce::JUCEApplicationBase::main(JUCE_MAIN_FUNCTION_ARGS);
  249. #else
  250. int64_t timeToEnd = 0;
  251. if (testing)
  252. {
  253. timeToEnd = water::Time::currentTimeMillis() + 5 * 1000;
  254. fEngine->transportPlay();
  255. }
  256. for (; runMainLoopOnce() && ! gCloseNow;)
  257. {
  258. gIdle();
  259. # if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  260. // MacOS and Win32 have event-loops to run, so minimize sleep time
  261. carla_msleep(1);
  262. # else
  263. carla_msleep(5);
  264. # endif
  265. if (testing && timeToEnd - water::Time::currentTimeMillis() < 0)
  266. break;
  267. }
  268. #endif
  269. carla_engine_close(gHostHandle);
  270. }
  271. // ---------------------------------------------------------------------
  272. protected:
  273. void handleCallback(const EngineCallbackOpcode action,
  274. const int value1,
  275. const int, const int, const float, const char* const)
  276. {
  277. CARLA_BACKEND_USE_NAMESPACE;
  278. switch (action)
  279. {
  280. case ENGINE_CALLBACK_ENGINE_STOPPED:
  281. case ENGINE_CALLBACK_PLUGIN_REMOVED:
  282. case ENGINE_CALLBACK_QUIT:
  283. gCloseNow = true;
  284. break;
  285. case ENGINE_CALLBACK_UI_STATE_CHANGED:
  286. if (gIsInitiated && value1 != 1 && ! fUsingBridge)
  287. gCloseNow = true;
  288. break;
  289. default:
  290. break;
  291. }
  292. }
  293. private:
  294. CarlaEngine* fEngine;
  295. #ifdef USING_JUCE
  296. const juce::ScopedJuceInitialiser_GUI fJuceInitialiser;
  297. #endif
  298. bool fUsingBridge;
  299. bool fUsingExec;
  300. static void callback(void* ptr, EngineCallbackOpcode action, unsigned int pluginId,
  301. int value1, int value2, int value3,
  302. float valuef, const char* valueStr)
  303. {
  304. carla_debug("CarlaBridgePlugin::callback(%p, %i:%s, %i, %i, %i, %i, %f, \"%s\")",
  305. ptr, action, EngineCallbackOpcode2Str(action),
  306. pluginId, value1, value2, value3, static_cast<double>(valuef), valueStr);
  307. // ptr must not be null
  308. CARLA_SAFE_ASSERT_RETURN(ptr != nullptr,);
  309. #ifndef BUILD_BRIDGE_ALTERNATIVE_ARCH
  310. // pluginId must be 0 (first), except for patchbay things
  311. if (action < CarlaBackend::ENGINE_CALLBACK_PATCHBAY_CLIENT_ADDED ||
  312. action > CarlaBackend::ENGINE_CALLBACK_PATCHBAY_CONNECTION_REMOVED)
  313. #endif
  314. {
  315. CARLA_SAFE_ASSERT_UINT_RETURN(pluginId == 0, pluginId,);
  316. }
  317. return ((CarlaBridgePlugin*)ptr)->handleCallback(action, value1, value2, value3, valuef, valueStr);
  318. }
  319. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaBridgePlugin)
  320. };
  321. // -------------------------------------------------------------------------
  322. int main(int argc, char* argv[])
  323. {
  324. // ---------------------------------------------------------------------
  325. // Check argument count
  326. if (argc != 4 && argc != 5)
  327. {
  328. carla_stdout("usage: %s <type> <filename> <label> [uniqueId]", argv[0]);
  329. return 1;
  330. }
  331. #if defined(CARLA_OS_WIN) && defined(BUILDING_CARLA_FOR_WINE)
  332. // ---------------------------------------------------------------------
  333. // Test if bridge is working
  334. if (! jackbridge_is_ok())
  335. {
  336. carla_stderr("A JACK or Wine library is missing, cannot continue");
  337. return 1;
  338. }
  339. #endif
  340. // ---------------------------------------------------------------------
  341. // Get args
  342. const char* const stype = argv[1];
  343. const char* filename = argv[2];
  344. const char* label = argv[3];
  345. const int64_t uniqueId = (argc == 5) ? static_cast<int64_t>(std::atoll(argv[4])) : 0;
  346. if (filename[0] == '\0' || std::strcmp(filename, "(none)") == 0)
  347. filename = nullptr;
  348. if (label[0] == '\0' || std::strcmp(label, "(none)") == 0)
  349. label = nullptr;
  350. // ---------------------------------------------------------------------
  351. // Check binary type
  352. CarlaBackend::BinaryType btype = CarlaBackend::BINARY_NATIVE;
  353. if (const char* const binaryTypeStr = std::getenv("CARLA_BRIDGE_PLUGIN_BINARY_TYPE"))
  354. btype = CarlaBackend::getBinaryTypeFromString(binaryTypeStr);
  355. if (btype == CarlaBackend::BINARY_NONE)
  356. {
  357. carla_stderr("Invalid binary type '%i'", btype);
  358. return 1;
  359. }
  360. // ---------------------------------------------------------------------
  361. // Check plugin type
  362. CarlaBackend::PluginType itype(CarlaBackend::getPluginTypeFromString(stype));
  363. if (itype == CarlaBackend::PLUGIN_NONE)
  364. {
  365. carla_stderr("Invalid plugin type '%s'", stype);
  366. return 1;
  367. }
  368. // ---------------------------------------------------------------------
  369. // Set file
  370. const File file(filename != nullptr ? filename : "");
  371. // ---------------------------------------------------------------------
  372. // Set name
  373. const char* name(std::getenv("CARLA_CLIENT_NAME"));
  374. if (name != nullptr && (name[0] == '\0' || std::strcmp(name, "(none)") == 0))
  375. name = nullptr;
  376. // ---------------------------------------------------------------------
  377. // Setup options
  378. const char* const shmIds(std::getenv("ENGINE_BRIDGE_SHM_IDS"));
  379. const bool useBridge = (shmIds != nullptr);
  380. // ---------------------------------------------------------------------
  381. // Setup bridge ids
  382. char audioPoolBaseName[6+1];
  383. char rtClientBaseName[6+1];
  384. char nonRtClientBaseName[6+1];
  385. char nonRtServerBaseName[6+1];
  386. if (useBridge)
  387. {
  388. CARLA_SAFE_ASSERT_RETURN(std::strlen(shmIds) == 6*4, 1);
  389. std::strncpy(audioPoolBaseName, shmIds+6*0, 6);
  390. std::strncpy(rtClientBaseName, shmIds+6*1, 6);
  391. std::strncpy(nonRtClientBaseName, shmIds+6*2, 6);
  392. std::strncpy(nonRtServerBaseName, shmIds+6*3, 6);
  393. audioPoolBaseName[6] = '\0';
  394. rtClientBaseName[6] = '\0';
  395. nonRtClientBaseName[6] = '\0';
  396. nonRtServerBaseName[6] = '\0';
  397. jackbridge_parent_deathsig(false);
  398. }
  399. else
  400. {
  401. audioPoolBaseName[0] = '\0';
  402. rtClientBaseName[0] = '\0';
  403. nonRtClientBaseName[0] = '\0';
  404. nonRtServerBaseName[0] = '\0';
  405. jackbridge_init();
  406. }
  407. // ---------------------------------------------------------------------
  408. // Set client name
  409. CarlaString clientName;
  410. if (name != nullptr)
  411. {
  412. clientName = name;
  413. }
  414. else if (itype == CarlaBackend::PLUGIN_LV2)
  415. {
  416. // LV2 requires URI
  417. CARLA_SAFE_ASSERT_RETURN(label != nullptr && label[0] != '\0', 1);
  418. // LV2 URI is not usable as client name, create a usable name from URI
  419. CarlaString label2(label);
  420. // truncate until last valid char
  421. for (std::size_t i=label2.length()-1; i != 0; --i)
  422. {
  423. if (! std::isalnum(label2[i]))
  424. continue;
  425. label2.truncate(i+1);
  426. break;
  427. }
  428. // get last used separator
  429. bool found;
  430. std::size_t septmp, sep = 0;
  431. septmp = label2.rfind('#', &found)+1;
  432. if (found && septmp > sep)
  433. sep = septmp;
  434. septmp = label2.rfind('/', &found)+1;
  435. if (found && septmp > sep)
  436. sep = septmp;
  437. septmp = label2.rfind('=', &found)+1;
  438. if (found && septmp > sep)
  439. sep = septmp;
  440. septmp = label2.rfind(':', &found)+1;
  441. if (found && septmp > sep)
  442. sep = septmp;
  443. // make name starting from the separator and first valid char
  444. const char* name2 = label2.buffer() + sep;
  445. for (; *name2 != '\0' && ! std::isalnum(*name2); ++name2) {}
  446. if (*name2 != '\0')
  447. clientName = name2;
  448. }
  449. else if (label != nullptr)
  450. {
  451. clientName = label;
  452. }
  453. else
  454. {
  455. clientName = file.getFileNameWithoutExtension().toRawUTF8();
  456. }
  457. // if we still have no client name by now, use a dummy one
  458. if (clientName.isEmpty())
  459. clientName = "carla-plugin";
  460. // just to be safe
  461. clientName.toBasic();
  462. // ---------------------------------------------------------------------
  463. // Set extraStuff
  464. const void* extraStuff = nullptr;
  465. if (itype == CarlaBackend::PLUGIN_SF2)
  466. {
  467. if (label == nullptr)
  468. label = clientName;
  469. if (std::strstr(label, " (16 outs)") != nullptr)
  470. extraStuff = "true";
  471. }
  472. // ---------------------------------------------------------------------
  473. // Initialize OS features
  474. const bool testing = std::getenv("CARLA_BRIDGE_TESTING") != nullptr;
  475. #ifdef CARLA_OS_MAC
  476. CarlaBackend::initStandaloneApplication();
  477. #endif
  478. #ifdef CARLA_OS_WIN
  479. OleInitialize(nullptr);
  480. CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
  481. # ifndef __WINPTHREADS_VERSION
  482. // (non-portable) initialization of statically linked pthread library
  483. pthread_win32_process_attach_np();
  484. pthread_win32_thread_attach_np();
  485. # endif
  486. #endif
  487. #ifdef HAVE_X11
  488. if (std::getenv("DISPLAY") != nullptr)
  489. XInitThreads();
  490. #endif
  491. // ---------------------------------------------------------------------
  492. // Set ourselves with high priority
  493. #ifdef CARLA_OS_LINUX
  494. // reset scheduler to normal mode
  495. struct sched_param sparam;
  496. carla_zeroStruct(sparam);
  497. sched_setscheduler(0, SCHED_OTHER|SCHED_RESET_ON_FORK, &sparam);
  498. // try niceness first, if it fails, try SCHED_RR
  499. if (nice(-5) < 0)
  500. {
  501. sparam.sched_priority = (sched_get_priority_max(SCHED_RR) + sched_get_priority_min(SCHED_RR*7)) / 8;
  502. if (sparam.sched_priority > 0)
  503. {
  504. if (sched_setscheduler(0, SCHED_RR|SCHED_RESET_ON_FORK, &sparam) < 0 && ! testing)
  505. {
  506. CarlaString error(std::strerror(errno));
  507. carla_stderr("Failed to set high priority, error %i: %s", errno, error.buffer());
  508. }
  509. }
  510. }
  511. #endif
  512. #ifdef CARLA_OS_WIN
  513. if (! SetPriorityClass(GetCurrentProcess(), ABOVE_NORMAL_PRIORITY_CLASS) && ! testing)
  514. carla_stderr("Failed to set high priority.");
  515. #endif
  516. // ---------------------------------------------------------------------
  517. // Listen for ctrl+c or sigint/sigterm events
  518. initSignalHandler();
  519. // ---------------------------------------------------------------------
  520. // Init plugin bridge
  521. int ret;
  522. {
  523. gHostHandle = carla_standalone_host_init();
  524. CarlaBridgePlugin bridge(useBridge, clientName,
  525. audioPoolBaseName, rtClientBaseName, nonRtClientBaseName, nonRtServerBaseName);
  526. if (! bridge.isOk())
  527. {
  528. carla_stderr("Failed to init engine, error was:\n%s", carla_get_last_error(gHostHandle));
  529. return 1;
  530. }
  531. if (! useBridge && ! testing)
  532. {
  533. #ifdef HAVE_X11
  534. if (std::getenv("DISPLAY") != nullptr)
  535. #endif
  536. carla_set_engine_option(gHostHandle,
  537. CarlaBackend::ENGINE_OPTION_FRONTEND_UI_SCALE,
  538. static_cast<int>(carla_get_desktop_scale_factor()*1000+0.5),
  539. nullptr);
  540. }
  541. // -----------------------------------------------------------------
  542. // Init plugin
  543. if (carla_add_plugin(gHostHandle,
  544. btype, itype,
  545. file.getFullPathName().toRawUTF8(), name, label, uniqueId, extraStuff, CarlaBackend::PLUGIN_OPTIONS_NULL))
  546. {
  547. ret = 0;
  548. if (! useBridge)
  549. {
  550. carla_set_active(gHostHandle, 0, true);
  551. carla_set_engine_option(gHostHandle, CarlaBackend::ENGINE_OPTION_PLUGINS_ARE_STANDALONE, 1, nullptr);
  552. if (const CarlaPluginInfo* const pluginInfo = carla_get_plugin_info(gHostHandle, 0))
  553. {
  554. if (itype == CarlaBackend::PLUGIN_INTERNAL && (std::strcmp(label, "audiofile") == 0 || std::strcmp(label, "midifile") == 0))
  555. {
  556. if (file.exists())
  557. carla_set_custom_data(gHostHandle, 0,
  558. CarlaBackend::CUSTOM_DATA_TYPE_STRING,
  559. "file", file.getFullPathName().toRawUTF8());
  560. }
  561. else if (pluginInfo->hints & CarlaBackend::PLUGIN_HAS_CUSTOM_UI)
  562. {
  563. #ifdef HAVE_X11
  564. if (std::getenv("DISPLAY") != nullptr)
  565. #endif
  566. if (! testing)
  567. carla_show_custom_ui(gHostHandle, 0, true);
  568. }
  569. // on standalone usage, enable everything that makes sense
  570. const uint optsAvailable = pluginInfo->optionsAvailable;
  571. if (optsAvailable & CarlaBackend::PLUGIN_OPTION_FIXED_BUFFERS)
  572. carla_set_option(gHostHandle, 0, CarlaBackend::PLUGIN_OPTION_FIXED_BUFFERS, true);
  573. }
  574. }
  575. bridge.exec(useBridge);
  576. }
  577. else
  578. {
  579. ret = 1;
  580. const char* const lastError(carla_get_last_error(gHostHandle));
  581. carla_stderr("Plugin failed to load, error was:\n%s", lastError);
  582. if (useBridge)
  583. {
  584. // do a single idle so that we can send error message to server
  585. gIdle();
  586. #ifdef CARLA_OS_UNIX
  587. // kill ourselves now if we can't load plugin in bridge mode
  588. ::kill(::getpid(), SIGKILL);
  589. #endif
  590. }
  591. }
  592. }
  593. #ifdef CARLA_OS_WIN
  594. #ifndef __WINPTHREADS_VERSION
  595. pthread_win32_thread_detach_np();
  596. pthread_win32_process_detach_np();
  597. #endif
  598. CoUninitialize();
  599. OleUninitialize();
  600. #endif
  601. return ret;
  602. }