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.

PluginDiscovery.cpp 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. // SPDX-FileCopyrightText: 2011-2024 Filipe Coelho <falktx@falktx.com>
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #include "CarlaUtils.h"
  4. #include "CarlaBackendUtils.hpp"
  5. #include "CarlaBinaryUtils.hpp"
  6. #include "CarlaJuceUtils.hpp"
  7. #include "CarlaPipeUtils.hpp"
  8. #include "CarlaSha1Utils.hpp"
  9. #include "CarlaTimeUtils.hpp"
  10. #include "water/files/File.h"
  11. #include "water/files/FileInputStream.h"
  12. #include "water/threads/ChildProcess.h"
  13. #include "water/text/StringArray.h"
  14. namespace CB = CARLA_BACKEND_NAMESPACE;
  15. // --------------------------------------------------------------------------------------------------------------------
  16. #ifndef CARLA_OS_WIN
  17. static water::String findWinePrefix(const water::String filename, const int recursionLimit = 10)
  18. {
  19. if (recursionLimit == 0 || filename.length() < 5 || ! filename.contains("/"))
  20. return "";
  21. const water::String path(filename.upToLastOccurrenceOf("/", false, false));
  22. if (water::File(path + "/dosdevices").isDirectory())
  23. return path;
  24. return findWinePrefix(path, recursionLimit-1);
  25. }
  26. #endif
  27. // --------------------------------------------------------------------------------------------------------------------
  28. static const char* const gPluginsDiscoveryNullCharPtr = "";
  29. _CarlaPluginDiscoveryMetadata::_CarlaPluginDiscoveryMetadata() noexcept
  30. : name(gPluginsDiscoveryNullCharPtr),
  31. maker(gPluginsDiscoveryNullCharPtr),
  32. category(CB::PLUGIN_CATEGORY_NONE),
  33. hints(0x0) {}
  34. _CarlaPluginDiscoveryIO::_CarlaPluginDiscoveryIO() noexcept
  35. : audioIns(0),
  36. audioOuts(0),
  37. cvIns(0),
  38. cvOuts(0),
  39. midiIns(0),
  40. midiOuts(0),
  41. parameterIns(0),
  42. parameterOuts(0) {}
  43. _CarlaPluginDiscoveryInfo::_CarlaPluginDiscoveryInfo() noexcept
  44. : btype(CB::BINARY_NONE),
  45. ptype(CB::PLUGIN_NONE),
  46. filename(gPluginsDiscoveryNullCharPtr),
  47. label(gPluginsDiscoveryNullCharPtr),
  48. uniqueId(0),
  49. metadata() {}
  50. // --------------------------------------------------------------------------------------------------------------------
  51. struct CarlaPluginDiscoveryOptions {
  52. #if !defined(BUILD_BRIDGE_ALTERNATIVE_ARCH) && !defined(CARLA_OS_WIN)
  53. struct {
  54. bool autoPrefix;
  55. CarlaString executable;
  56. CarlaString fallbackPrefix;
  57. } wine;
  58. #endif
  59. static CarlaPluginDiscoveryOptions& getInstance() noexcept
  60. {
  61. static CarlaPluginDiscoveryOptions instance;
  62. return instance;
  63. }
  64. };
  65. // --------------------------------------------------------------------------------------------------------------------
  66. class CarlaPluginDiscovery : private CarlaPipeServer
  67. {
  68. public:
  69. CarlaPluginDiscovery(const char* const discoveryTool,
  70. const BinaryType btype,
  71. const PluginType ptype,
  72. const std::vector<water::File>&& binaries,
  73. const CarlaPluginDiscoveryCallback discoveryCb,
  74. const CarlaPluginCheckCacheCallback checkCacheCb,
  75. void* const callbackPtr)
  76. : fBinaryType(btype),
  77. fPluginType(ptype),
  78. fDiscoveryCallback(discoveryCb),
  79. fCheckCacheCallback(checkCacheCb),
  80. fCallbackPtr(callbackPtr),
  81. fPluginPath(nullptr),
  82. fPluginsFoundInBinary(false),
  83. fBinaryIndex(0),
  84. fBinaryCount(static_cast<uint>(binaries.size())),
  85. fBinaries(binaries),
  86. fDiscoveryTool(discoveryTool),
  87. fLastMessageTime(0),
  88. fNextLabel(nullptr),
  89. fNextMaker(nullptr),
  90. fNextName(nullptr)
  91. {
  92. start();
  93. }
  94. CarlaPluginDiscovery(const char* const discoveryTool,
  95. const BinaryType btype,
  96. const PluginType ptype,
  97. const CarlaPluginDiscoveryCallback discoveryCb,
  98. const CarlaPluginCheckCacheCallback checkCacheCb,
  99. void* const callbackPtr,
  100. const char* const pluginPath = nullptr)
  101. : fBinaryType(btype),
  102. fPluginType(ptype),
  103. fDiscoveryCallback(discoveryCb),
  104. fCheckCacheCallback(checkCacheCb),
  105. fCallbackPtr(callbackPtr),
  106. fPluginPath(pluginPath != nullptr ? carla_strdup_safe(pluginPath) : nullptr),
  107. fPluginsFoundInBinary(false),
  108. fBinaryIndex(0),
  109. fBinaryCount(1),
  110. fDiscoveryTool(discoveryTool),
  111. fLastMessageTime(0),
  112. fNextLabel(nullptr),
  113. fNextMaker(nullptr),
  114. fNextName(nullptr)
  115. {
  116. start();
  117. }
  118. ~CarlaPluginDiscovery()
  119. {
  120. stopPipeServer(5000);
  121. std::free(fNextLabel);
  122. std::free(fNextMaker);
  123. std::free(fNextName);
  124. delete[] fPluginPath;
  125. }
  126. bool idle()
  127. {
  128. if (isPipeRunning())
  129. {
  130. idlePipe();
  131. // automatically skip a plugin if 30s passes without a reply
  132. const uint32_t timeNow = carla_gettime_ms();
  133. if (timeNow - fLastMessageTime < 30000)
  134. return true;
  135. carla_stdout("Plugin took too long to respond, skipping...");
  136. stopPipeServer(1000);
  137. }
  138. // report binary as having no plugins
  139. if (fCheckCacheCallback != nullptr && !fPluginsFoundInBinary && !fBinaries.empty())
  140. {
  141. const water::File file(fBinaries[fBinaryIndex]);
  142. const water::String filename(file.getFullPathName());
  143. makeHash(file, filename);
  144. if (! fCheckCacheCallback(fCallbackPtr, filename.toRawUTF8(), fNextSha1Sum))
  145. fDiscoveryCallback(fCallbackPtr, nullptr, fNextSha1Sum);
  146. }
  147. if (++fBinaryIndex == fBinaryCount)
  148. return false;
  149. start();
  150. return true;
  151. }
  152. void skip()
  153. {
  154. if (isPipeRunning())
  155. stopPipeServer(1000);
  156. }
  157. protected:
  158. bool msgReceived(const char* const msg) noexcept
  159. {
  160. fLastMessageTime = carla_gettime_ms();
  161. if (std::strcmp(msg, "warning") == 0 || std::strcmp(msg, "error") == 0)
  162. {
  163. const char* text = nullptr;
  164. readNextLineAsString(text, false);
  165. carla_stdout("discovery: %s", text);
  166. return true;
  167. }
  168. if (std::strcmp(msg, "init") == 0)
  169. {
  170. const char* _;
  171. readNextLineAsString(_, false);
  172. new (&fNextInfo) _CarlaPluginDiscoveryInfo();
  173. return true;
  174. }
  175. if (std::strcmp(msg, "end") == 0)
  176. {
  177. const char* _;
  178. readNextLineAsString(_, false);
  179. if (fNextInfo.label == nullptr)
  180. fNextInfo.label = gPluginsDiscoveryNullCharPtr;
  181. if (fNextInfo.metadata.maker == nullptr)
  182. fNextInfo.metadata.maker = gPluginsDiscoveryNullCharPtr;
  183. if (fNextInfo.metadata.name == nullptr)
  184. fNextInfo.metadata.name = gPluginsDiscoveryNullCharPtr;
  185. if (fBinaries.empty())
  186. {
  187. char* filename = nullptr;
  188. if (fPluginType == CB::PLUGIN_LV2)
  189. {
  190. do {
  191. const char* const slash = std::strchr(fNextLabel, CARLA_OS_SEP);
  192. CARLA_SAFE_ASSERT_BREAK(slash != nullptr);
  193. filename = strdup(fNextLabel);
  194. filename[slash - fNextLabel] = '\0';
  195. fNextInfo.filename = filename;
  196. fNextInfo.label = slash + 1;
  197. } while (false);
  198. }
  199. fNextInfo.ptype = fPluginType;
  200. fDiscoveryCallback(fCallbackPtr, &fNextInfo, nullptr);
  201. std::free(filename);
  202. }
  203. else
  204. {
  205. CARLA_SAFE_ASSERT(fNextSha1Sum.isNotEmpty());
  206. const water::String filename(fBinaries[fBinaryIndex].getFullPathName());
  207. fNextInfo.filename = filename.toRawUTF8();
  208. fNextInfo.ptype = fPluginType;
  209. fPluginsFoundInBinary = true;
  210. carla_stdout("Found %s from %s", fNextInfo.metadata.name, fNextInfo.filename);
  211. fDiscoveryCallback(fCallbackPtr, &fNextInfo, fNextSha1Sum);
  212. }
  213. std::free(fNextLabel);
  214. fNextLabel = nullptr;
  215. std::free(fNextMaker);
  216. fNextMaker = nullptr;
  217. std::free(fNextName);
  218. fNextName = nullptr;
  219. return true;
  220. }
  221. if (std::strcmp(msg, "build") == 0)
  222. {
  223. uint8_t btype = 0;
  224. readNextLineAsByte(btype);
  225. fNextInfo.btype = static_cast<BinaryType>(btype);
  226. return true;
  227. }
  228. if (std::strcmp(msg, "hints") == 0)
  229. {
  230. readNextLineAsUInt(fNextInfo.metadata.hints);
  231. return true;
  232. }
  233. if (std::strcmp(msg, "category") == 0)
  234. {
  235. const char* category = nullptr;
  236. readNextLineAsString(category, false);
  237. fNextInfo.metadata.category = CB::getPluginCategoryFromString(category);
  238. return true;
  239. }
  240. if (std::strcmp(msg, "name") == 0)
  241. {
  242. fNextInfo.metadata.name = fNextName = readNextLineAsString();
  243. return true;
  244. }
  245. if (std::strcmp(msg, "label") == 0)
  246. {
  247. fNextInfo.label = fNextLabel = readNextLineAsString();
  248. return true;
  249. }
  250. if (std::strcmp(msg, "maker") == 0)
  251. {
  252. fNextInfo.metadata.maker = fNextMaker = readNextLineAsString();
  253. return true;
  254. }
  255. if (std::strcmp(msg, "uniqueId") == 0)
  256. {
  257. readNextLineAsULong(fNextInfo.uniqueId);
  258. return true;
  259. }
  260. if (std::strcmp(msg, "audio.ins") == 0)
  261. {
  262. readNextLineAsUInt(fNextInfo.io.audioIns);
  263. return true;
  264. }
  265. if (std::strcmp(msg, "audio.outs") == 0)
  266. {
  267. readNextLineAsUInt(fNextInfo.io.audioOuts);
  268. return true;
  269. }
  270. if (std::strcmp(msg, "cv.ins") == 0)
  271. {
  272. readNextLineAsUInt(fNextInfo.io.cvIns);
  273. return true;
  274. }
  275. if (std::strcmp(msg, "cv.outs") == 0)
  276. {
  277. readNextLineAsUInt(fNextInfo.io.cvOuts);
  278. return true;
  279. }
  280. if (std::strcmp(msg, "midi.ins") == 0)
  281. {
  282. readNextLineAsUInt(fNextInfo.io.midiIns);
  283. return true;
  284. }
  285. if (std::strcmp(msg, "midi.outs") == 0)
  286. {
  287. readNextLineAsUInt(fNextInfo.io.midiOuts);
  288. return true;
  289. }
  290. if (std::strcmp(msg, "parameters.ins") == 0)
  291. {
  292. readNextLineAsUInt(fNextInfo.io.parameterIns);
  293. return true;
  294. }
  295. if (std::strcmp(msg, "parameters.outs") == 0)
  296. {
  297. readNextLineAsUInt(fNextInfo.io.parameterOuts);
  298. return true;
  299. }
  300. if (std::strcmp(msg, "exiting") == 0)
  301. {
  302. stopPipeServer(1000);
  303. return true;
  304. }
  305. carla_stdout("discovery: unknown message '%s' received", msg);
  306. return true;
  307. }
  308. private:
  309. const BinaryType fBinaryType;
  310. const PluginType fPluginType;
  311. const CarlaPluginDiscoveryCallback fDiscoveryCallback;
  312. const CarlaPluginCheckCacheCallback fCheckCacheCallback;
  313. void* const fCallbackPtr;
  314. const char* fPluginPath;
  315. bool fPluginsFoundInBinary;
  316. uint fBinaryIndex;
  317. const uint fBinaryCount;
  318. const std::vector<water::File> fBinaries;
  319. const CarlaString fDiscoveryTool;
  320. uint32_t fLastMessageTime;
  321. CarlaPluginDiscoveryInfo fNextInfo;
  322. CarlaString fNextSha1Sum;
  323. char* fNextLabel;
  324. char* fNextMaker;
  325. char* fNextName;
  326. void start()
  327. {
  328. using water::File;
  329. using water::String;
  330. fLastMessageTime = carla_gettime_ms();
  331. fPluginsFoundInBinary = false;
  332. fNextSha1Sum.clear();
  333. #ifndef CARLA_OS_WIN
  334. const CarlaPluginDiscoveryOptions& options(CarlaPluginDiscoveryOptions::getInstance());
  335. String helperTool;
  336. switch (fBinaryType)
  337. {
  338. case CB::BINARY_WIN32:
  339. if (options.wine.executable.isNotEmpty())
  340. helperTool = options.wine.executable.buffer();
  341. else
  342. helperTool = "wine";
  343. break;
  344. case CB::BINARY_WIN64:
  345. if (options.wine.executable.isNotEmpty())
  346. {
  347. helperTool = options.wine.executable.buffer();
  348. if (helperTool.isNotEmpty() && helperTool[0] == CARLA_OS_SEP && File(helperTool + "64").existsAsFile())
  349. helperTool += "64";
  350. }
  351. else
  352. {
  353. helperTool = "wine";
  354. }
  355. break;
  356. default:
  357. break;
  358. }
  359. String winePrefix;
  360. if (options.wine.autoPrefix && !fBinaries.empty())
  361. {
  362. const File file(fBinaries[fBinaryIndex]);
  363. const String filename(file.getFullPathName());
  364. winePrefix = findWinePrefix(filename);
  365. }
  366. if (winePrefix.isEmpty())
  367. {
  368. const char* const envWinePrefix = std::getenv("WINEPREFIX");
  369. if (envWinePrefix != nullptr && envWinePrefix[0] != '\0')
  370. winePrefix = envWinePrefix;
  371. else if (options.wine.fallbackPrefix.isNotEmpty())
  372. winePrefix = options.wine.fallbackPrefix.buffer();
  373. else
  374. winePrefix = File::getSpecialLocation(File::userHomeDirectory).getFullPathName() + "/.wine";
  375. }
  376. const CarlaScopedEnvVar sev1("WINEDEBUG", "-all");
  377. const CarlaScopedEnvVar sev2("WINEPREFIX", winePrefix.toRawUTF8());
  378. #endif
  379. const CarlaScopedEnvVar sev3("CARLA_DISCOVERY_NO_PROCESSING_CHECKS", "1");
  380. if (fBinaries.empty())
  381. {
  382. if (fBinaryType == CB::BINARY_NATIVE)
  383. {
  384. switch (fPluginType)
  385. {
  386. default:
  387. break;
  388. case CB::PLUGIN_INTERNAL:
  389. case CB::PLUGIN_LV2:
  390. case CB::PLUGIN_JSFX:
  391. case CB::PLUGIN_SFZ:
  392. if (const uint count = carla_get_cached_plugin_count(fPluginType, fPluginPath))
  393. {
  394. for (uint i=0; i<count; ++i)
  395. {
  396. const CarlaCachedPluginInfo* const pinfo = carla_get_cached_plugin_info(fPluginType, i);
  397. if (pinfo == nullptr || !pinfo->valid)
  398. continue;
  399. char* filename = nullptr;
  400. CarlaPluginDiscoveryInfo info = {};
  401. info.btype = CB::BINARY_NATIVE;
  402. info.ptype = fPluginType;
  403. info.metadata.name = pinfo->name;
  404. info.metadata.maker = pinfo->maker;
  405. info.metadata.category = pinfo->category;
  406. info.metadata.hints = pinfo->hints;
  407. info.io.audioIns = pinfo->audioIns;
  408. info.io.audioOuts = pinfo->audioOuts;
  409. info.io.cvIns = pinfo->cvIns;
  410. info.io.cvOuts = pinfo->cvOuts;
  411. info.io.midiIns = pinfo->midiIns;
  412. info.io.midiOuts = pinfo->midiOuts;
  413. info.io.parameterIns = pinfo->parameterIns;
  414. info.io.parameterOuts = pinfo->parameterOuts;
  415. if (fPluginType == CB::PLUGIN_LV2)
  416. {
  417. const char* const slash = std::strchr(pinfo->label, CARLA_OS_SEP);
  418. CARLA_SAFE_ASSERT_BREAK(slash != nullptr);
  419. filename = strdup(pinfo->label);
  420. filename[slash - pinfo->label] = '\0';
  421. info.filename = filename;
  422. info.label = slash + 1;
  423. }
  424. else
  425. {
  426. info.filename = gPluginsDiscoveryNullCharPtr;
  427. info.label = pinfo->label;
  428. }
  429. fDiscoveryCallback(fCallbackPtr, &info, nullptr);
  430. std::free(filename);
  431. }
  432. }
  433. return;
  434. }
  435. }
  436. #ifndef CARLA_OS_WIN
  437. if (helperTool.isNotEmpty())
  438. startPipeServer(helperTool.toRawUTF8(), fDiscoveryTool, getPluginTypeAsString(fPluginType), ":all", -1, 2000);
  439. else
  440. #endif
  441. startPipeServer(fDiscoveryTool, getPluginTypeAsString(fPluginType), ":all", -1, 2000);
  442. }
  443. else
  444. {
  445. const File file(fBinaries[fBinaryIndex]);
  446. const String filename(file.getFullPathName());
  447. if (fCheckCacheCallback != nullptr)
  448. {
  449. makeHash(file, filename);
  450. if (fCheckCacheCallback(fCallbackPtr, filename.toRawUTF8(), fNextSha1Sum))
  451. {
  452. fPluginsFoundInBinary = true;
  453. carla_debug("Skipping \"%s\", using cache", filename.toRawUTF8());
  454. return;
  455. }
  456. }
  457. carla_stdout("Scanning \"%s\"...", filename.toRawUTF8());
  458. #ifndef CARLA_OS_WIN
  459. if (helperTool.isNotEmpty())
  460. startPipeServer(helperTool.toRawUTF8(), fDiscoveryTool, getPluginTypeAsString(fPluginType), filename.toRawUTF8(), -1, 2000);
  461. else
  462. #endif
  463. startPipeServer(fDiscoveryTool, getPluginTypeAsString(fPluginType), filename.toRawUTF8(), -1, 2000);
  464. }
  465. }
  466. void makeHash(const water::File& file, const water::String& filename)
  467. {
  468. CarlaSha1 sha1;
  469. /* do we want this? it is not exactly needed and makes discovery slow..
  470. if (file.existsAsFile() && file.getSize() < 20*1024*1024) // dont bother hashing > 20Mb files
  471. {
  472. water::FileInputStream stream(file);
  473. if (stream.openedOk())
  474. {
  475. uint8_t block[8192];
  476. for (int r; r = stream.read(block, sizeof(block)), r > 0;)
  477. sha1.write(block, r);
  478. }
  479. }
  480. */
  481. sha1.write(filename.toRawUTF8(), filename.length());
  482. const int64_t mtime = file.getLastModificationTime();
  483. sha1.write(&mtime, sizeof(mtime));
  484. fNextSha1Sum = sha1.resultAsString();
  485. }
  486. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginDiscovery)
  487. };
  488. // --------------------------------------------------------------------------------------------------------------------
  489. static bool findDirectories(std::vector<water::File>& files, const char* const pluginPath, const char* const wildcard)
  490. {
  491. CARLA_SAFE_ASSERT_RETURN(pluginPath != nullptr, true);
  492. if (pluginPath[0] == '\0')
  493. return true;
  494. using water::File;
  495. using water::String;
  496. using water::StringArray;
  497. const StringArray splitPaths(StringArray::fromTokens(pluginPath, CARLA_OS_SPLIT_STR, ""));
  498. if (splitPaths.size() == 0)
  499. return true;
  500. for (String *it = splitPaths.begin(), *end = splitPaths.end(); it != end; ++it)
  501. {
  502. const File dir(*it);
  503. std::vector<File> results;
  504. if (dir.findChildFiles(results, File::findDirectories|File::ignoreHiddenFiles, true, wildcard) > 0)
  505. {
  506. files.reserve(files.size() + results.size());
  507. files.insert(files.end(), results.begin(), results.end());
  508. }
  509. }
  510. return files.empty();
  511. }
  512. static bool findFiles(std::vector<water::File>& files,
  513. const BinaryType btype, const char* const pluginPath, const char* const wildcard)
  514. {
  515. CARLA_SAFE_ASSERT_RETURN(pluginPath != nullptr, true);
  516. if (pluginPath[0] == '\0')
  517. return true;
  518. using water::File;
  519. using water::String;
  520. using water::StringArray;
  521. const StringArray splitPaths(StringArray::fromTokens(pluginPath, CARLA_OS_SPLIT_STR, ""));
  522. if (splitPaths.size() == 0)
  523. return true;
  524. for (String *it = splitPaths.begin(), *end = splitPaths.end(); it != end; ++it)
  525. {
  526. const File dir(*it);
  527. std::vector<File> results;
  528. if (dir.findChildFiles(results, File::findFiles|File::ignoreHiddenFiles, true, wildcard) > 0)
  529. {
  530. files.reserve(files.size() + results.size());
  531. for (std::vector<File>::const_iterator cit = results.begin(); cit != results.end(); ++cit)
  532. {
  533. const File file(*cit);
  534. if (CB::getBinaryTypeFromFile(file.getFullPathName().toRawUTF8()) == btype)
  535. files.push_back(file);
  536. }
  537. }
  538. }
  539. return files.empty();
  540. }
  541. static bool findVST3s(std::vector<water::File>& files,
  542. const BinaryType btype, const char* const pluginPath)
  543. {
  544. CARLA_SAFE_ASSERT_RETURN(pluginPath != nullptr, true);
  545. if (pluginPath[0] == '\0')
  546. return true;
  547. using water::File;
  548. using water::String;
  549. using water::StringArray;
  550. const StringArray splitPaths(StringArray::fromTokens(pluginPath, CARLA_OS_SPLIT_STR, ""));
  551. if (splitPaths.size() == 0)
  552. return true;
  553. const uint flags = btype == CB::BINARY_WIN32 || btype == CB::BINARY_WIN64
  554. ? File::findDirectories|File::findFiles
  555. : File::findDirectories;
  556. for (String *it = splitPaths.begin(), *end = splitPaths.end(); it != end; ++it)
  557. {
  558. const File dir(*it);
  559. std::vector<File> results;
  560. if (dir.findChildFiles(results, flags|File::ignoreHiddenFiles, true, "*.vst3") > 0)
  561. {
  562. files.reserve(files.size() + results.size());
  563. for (std::vector<File>::const_iterator cit = results.begin(); cit != results.end(); ++cit)
  564. {
  565. const File file(*cit);
  566. if (CB::getBinaryTypeFromFile(file.getFullPathName().toRawUTF8()) == btype)
  567. files.push_back(file);
  568. }
  569. }
  570. }
  571. return files.empty();
  572. }
  573. CarlaPluginDiscoveryHandle carla_plugin_discovery_start(const char* const discoveryTool,
  574. const BinaryType btype,
  575. const PluginType ptype,
  576. const char* const pluginPath,
  577. const CarlaPluginDiscoveryCallback discoveryCb,
  578. const CarlaPluginCheckCacheCallback checkCacheCb,
  579. void* const callbackPtr)
  580. {
  581. CARLA_SAFE_ASSERT_RETURN(btype != CB::BINARY_NONE, nullptr);
  582. CARLA_SAFE_ASSERT_RETURN(ptype != CB::PLUGIN_NONE, nullptr);
  583. CARLA_SAFE_ASSERT_RETURN(discoveryTool != nullptr && discoveryTool[0] != '\0', nullptr);
  584. CARLA_SAFE_ASSERT_RETURN(discoveryCb != nullptr, nullptr);
  585. carla_debug("carla_plugin_discovery_start(%s, %d:%s, %d:%s, %s, %p, %p, %p)",
  586. discoveryTool, btype, BinaryType2Str(btype), ptype, PluginType2Str(ptype), pluginPath,
  587. discoveryCb, checkCacheCb, callbackPtr);
  588. bool directories = false;
  589. const char* wildcard = nullptr;
  590. switch (ptype)
  591. {
  592. case CB::PLUGIN_INTERNAL:
  593. case CB::PLUGIN_LV2:
  594. case CB::PLUGIN_SFZ:
  595. case CB::PLUGIN_JSFX:
  596. case CB::PLUGIN_DLS:
  597. case CB::PLUGIN_GIG:
  598. case CB::PLUGIN_SF2:
  599. CARLA_SAFE_ASSERT_UINT_RETURN(btype == CB::BINARY_NATIVE, btype, nullptr);
  600. break;
  601. default:
  602. break;
  603. }
  604. switch (ptype)
  605. {
  606. case CB::PLUGIN_NONE:
  607. case CB::PLUGIN_JACK:
  608. case CB::PLUGIN_TYPE_COUNT:
  609. return nullptr;
  610. case CB::PLUGIN_LV2:
  611. case CB::PLUGIN_SFZ:
  612. case CB::PLUGIN_JSFX:
  613. {
  614. const CarlaScopedEnvVar csev("CARLA_DISCOVERY_PATH", pluginPath);
  615. return new CarlaPluginDiscovery(discoveryTool, btype, ptype, discoveryCb, checkCacheCb, callbackPtr, pluginPath);
  616. }
  617. case CB::PLUGIN_INTERNAL:
  618. return new CarlaPluginDiscovery(discoveryTool, btype, ptype, discoveryCb, checkCacheCb, callbackPtr);
  619. case CB::PLUGIN_LADSPA:
  620. case CB::PLUGIN_DSSI:
  621. #ifdef CARLA_OS_WIN
  622. wildcard = "*.dll";
  623. #else
  624. if (btype == CB::BINARY_WIN32 || btype == CB::BINARY_WIN64)
  625. {
  626. wildcard = "*.dll";
  627. }
  628. else
  629. {
  630. #ifdef CARLA_OS_MAC
  631. wildcard = "*.dylib";
  632. #else
  633. wildcard = "*.so";
  634. #endif
  635. }
  636. #endif
  637. break;
  638. case CB::PLUGIN_VST2:
  639. #ifdef CARLA_OS_WIN
  640. wildcard = "*.dll";
  641. #else
  642. if (btype == CB::BINARY_WIN32 || btype == CB::BINARY_WIN64)
  643. {
  644. wildcard = "*.dll";
  645. }
  646. else
  647. {
  648. #ifdef CARLA_OS_MAC
  649. directories = true;
  650. wildcard = "*.vst";
  651. #else
  652. wildcard = "*.so";
  653. #endif
  654. }
  655. #endif
  656. break;
  657. case CB::PLUGIN_VST3:
  658. directories = true;
  659. wildcard = "*.vst3";
  660. break;
  661. case CB::PLUGIN_AU:
  662. directories = true;
  663. wildcard = "*.component";
  664. break;
  665. case CB::PLUGIN_CLAP:
  666. wildcard = "*.clap";
  667. #ifdef CARLA_OS_MAC
  668. directories = true;
  669. #endif
  670. break;
  671. case CB::PLUGIN_DLS:
  672. wildcard = "*.dls";
  673. break;
  674. case CB::PLUGIN_GIG:
  675. wildcard = "*.gig";
  676. break;
  677. case CB::PLUGIN_SF2:
  678. wildcard = "*.sf2";
  679. break;
  680. }
  681. CARLA_SAFE_ASSERT_RETURN(wildcard != nullptr, nullptr);
  682. std::vector<water::File> files;
  683. if (ptype == CB::PLUGIN_VST3)
  684. {
  685. if (findVST3s(files, btype, pluginPath))
  686. return nullptr;
  687. }
  688. else if (directories)
  689. {
  690. if (findDirectories(files, pluginPath, wildcard))
  691. return nullptr;
  692. }
  693. else
  694. {
  695. if (findFiles(files, btype, pluginPath, wildcard))
  696. return nullptr;
  697. }
  698. return new CarlaPluginDiscovery(discoveryTool, btype, ptype, std::move(files),
  699. discoveryCb, checkCacheCb, callbackPtr);
  700. }
  701. bool carla_plugin_discovery_idle(const CarlaPluginDiscoveryHandle handle)
  702. {
  703. return static_cast<CarlaPluginDiscovery*>(handle)->idle();
  704. }
  705. void carla_plugin_discovery_skip(const CarlaPluginDiscoveryHandle handle)
  706. {
  707. static_cast<CarlaPluginDiscovery*>(handle)->skip();
  708. }
  709. void carla_plugin_discovery_stop(const CarlaPluginDiscoveryHandle handle)
  710. {
  711. delete static_cast<CarlaPluginDiscovery*>(handle);
  712. }
  713. void carla_plugin_discovery_set_option(const EngineOption option, const int value, const char* const valueStr)
  714. {
  715. switch (option)
  716. {
  717. #if !defined(BUILD_BRIDGE_ALTERNATIVE_ARCH) && !defined(CARLA_OS_WIN)
  718. case CB::ENGINE_OPTION_WINE_EXECUTABLE:
  719. if (valueStr != nullptr && valueStr[0] != '\0')
  720. CarlaPluginDiscoveryOptions::getInstance().wine.executable = valueStr;
  721. else
  722. CarlaPluginDiscoveryOptions::getInstance().wine.executable.clear();
  723. break;
  724. case CB::ENGINE_OPTION_WINE_AUTO_PREFIX:
  725. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  726. CarlaPluginDiscoveryOptions::getInstance().wine.autoPrefix = value != 0;
  727. break;
  728. case CB::ENGINE_OPTION_WINE_FALLBACK_PREFIX:
  729. if (valueStr != nullptr && valueStr[0] != '\0')
  730. CarlaPluginDiscoveryOptions::getInstance().wine.fallbackPrefix = valueStr;
  731. else
  732. CarlaPluginDiscoveryOptions::getInstance().wine.fallbackPrefix.clear();
  733. break;
  734. #endif
  735. default:
  736. break;
  737. }
  738. }
  739. // --------------------------------------------------------------------------------------------------------------------