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.

886 lines
27KB

  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[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 != nullptr && options.wine.fallbackPrefix[0] != '\0')
  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. if (fBinaries.empty())
  380. {
  381. if (fBinaryType == CB::BINARY_NATIVE)
  382. {
  383. switch (fPluginType)
  384. {
  385. default:
  386. break;
  387. case CB::PLUGIN_INTERNAL:
  388. case CB::PLUGIN_LV2:
  389. case CB::PLUGIN_JSFX:
  390. case CB::PLUGIN_SFZ:
  391. if (const uint count = carla_get_cached_plugin_count(fPluginType, fPluginPath))
  392. {
  393. for (uint i=0; i<count; ++i)
  394. {
  395. const CarlaCachedPluginInfo* const pinfo = carla_get_cached_plugin_info(fPluginType, i);
  396. if (pinfo == nullptr || !pinfo->valid)
  397. continue;
  398. char* filename = nullptr;
  399. CarlaPluginDiscoveryInfo info = {};
  400. info.btype = CB::BINARY_NATIVE;
  401. info.ptype = fPluginType;
  402. info.metadata.name = pinfo->name;
  403. info.metadata.maker = pinfo->maker;
  404. info.metadata.category = pinfo->category;
  405. info.metadata.hints = pinfo->hints;
  406. info.io.audioIns = pinfo->audioIns;
  407. info.io.audioOuts = pinfo->audioOuts;
  408. info.io.cvIns = pinfo->cvIns;
  409. info.io.cvOuts = pinfo->cvOuts;
  410. info.io.midiIns = pinfo->midiIns;
  411. info.io.midiOuts = pinfo->midiOuts;
  412. info.io.parameterIns = pinfo->parameterIns;
  413. info.io.parameterOuts = pinfo->parameterOuts;
  414. if (fPluginType == CB::PLUGIN_LV2)
  415. {
  416. const char* const slash = std::strchr(pinfo->label, CARLA_OS_SEP);
  417. CARLA_SAFE_ASSERT_BREAK(slash != nullptr);
  418. filename = strdup(pinfo->label);
  419. filename[slash - pinfo->label] = '\0';
  420. info.filename = filename;
  421. info.label = slash + 1;
  422. }
  423. else
  424. {
  425. info.filename = gPluginsDiscoveryNullCharPtr;
  426. info.label = pinfo->label;
  427. }
  428. fDiscoveryCallback(fCallbackPtr, &info, nullptr);
  429. std::free(filename);
  430. }
  431. }
  432. return;
  433. }
  434. }
  435. #ifndef CARLA_OS_WIN
  436. if (helperTool.isNotEmpty())
  437. startPipeServer(helperTool.toRawUTF8(), fDiscoveryTool, getPluginTypeAsString(fPluginType), ":all");
  438. else
  439. #endif
  440. startPipeServer(fDiscoveryTool, getPluginTypeAsString(fPluginType), ":all");
  441. }
  442. else
  443. {
  444. const File file(fBinaries[fBinaryIndex]);
  445. const String filename(file.getFullPathName());
  446. if (fCheckCacheCallback != nullptr)
  447. {
  448. makeHash(file, filename);
  449. if (fCheckCacheCallback(fCallbackPtr, filename.toRawUTF8(), fNextSha1Sum))
  450. {
  451. fPluginsFoundInBinary = true;
  452. carla_debug("Skipping \"%s\", using cache", filename.toRawUTF8());
  453. return;
  454. }
  455. }
  456. carla_stdout("Scanning \"%s\"...", filename.toRawUTF8());
  457. #ifndef CARLA_OS_WIN
  458. if (helperTool.isNotEmpty())
  459. startPipeServer(helperTool.toRawUTF8(), fDiscoveryTool, getPluginTypeAsString(fPluginType), filename.toRawUTF8());
  460. else
  461. #endif
  462. startPipeServer(fDiscoveryTool, getPluginTypeAsString(fPluginType), filename.toRawUTF8());
  463. }
  464. }
  465. void makeHash(const water::File& file, const water::String& filename)
  466. {
  467. CarlaSha1 sha1;
  468. /* do we want this? it is not exactly needed and makes discovery slow..
  469. if (file.existsAsFile() && file.getSize() < 20*1024*1024) // dont bother hashing > 20Mb files
  470. {
  471. water::FileInputStream stream(file);
  472. if (stream.openedOk())
  473. {
  474. uint8_t block[8192];
  475. for (int r; r = stream.read(block, sizeof(block)), r > 0;)
  476. sha1.write(block, r);
  477. }
  478. }
  479. */
  480. sha1.write(filename.toRawUTF8(), filename.length());
  481. const int64_t mtime = file.getLastModificationTime();
  482. sha1.write(&mtime, sizeof(mtime));
  483. fNextSha1Sum = sha1.resultAsString();
  484. }
  485. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPluginDiscovery)
  486. };
  487. // --------------------------------------------------------------------------------------------------------------------
  488. static bool findDirectories(std::vector<water::File>& files, const char* const pluginPath, const char* const wildcard)
  489. {
  490. CARLA_SAFE_ASSERT_RETURN(pluginPath != nullptr, true);
  491. if (pluginPath[0] == '\0')
  492. return true;
  493. using water::File;
  494. using water::String;
  495. using water::StringArray;
  496. const StringArray splitPaths(StringArray::fromTokens(pluginPath, CARLA_OS_SPLIT_STR, ""));
  497. if (splitPaths.size() == 0)
  498. return true;
  499. for (String *it = splitPaths.begin(), *end = splitPaths.end(); it != end; ++it)
  500. {
  501. const File dir(*it);
  502. std::vector<File> results;
  503. if (dir.findChildFiles(results, File::findDirectories|File::ignoreHiddenFiles, true, wildcard) > 0)
  504. {
  505. files.reserve(files.size() + results.size());
  506. files.insert(files.end(), results.begin(), results.end());
  507. }
  508. }
  509. return files.empty();
  510. }
  511. static bool findFiles(std::vector<water::File>& files,
  512. const BinaryType btype, const char* const pluginPath, const char* const wildcard)
  513. {
  514. CARLA_SAFE_ASSERT_RETURN(pluginPath != nullptr, true);
  515. if (pluginPath[0] == '\0')
  516. return true;
  517. using water::File;
  518. using water::String;
  519. using water::StringArray;
  520. const StringArray splitPaths(StringArray::fromTokens(pluginPath, CARLA_OS_SPLIT_STR, ""));
  521. if (splitPaths.size() == 0)
  522. return true;
  523. for (String *it = splitPaths.begin(), *end = splitPaths.end(); it != end; ++it)
  524. {
  525. const File dir(*it);
  526. std::vector<File> results;
  527. if (dir.findChildFiles(results, File::findFiles|File::ignoreHiddenFiles, true, wildcard) > 0)
  528. {
  529. files.reserve(files.size() + results.size());
  530. for (std::vector<File>::const_iterator cit = results.begin(); cit != results.end(); ++cit)
  531. {
  532. const File file(*cit);
  533. if (CB::getBinaryTypeFromFile(file.getFullPathName().toRawUTF8()) == btype)
  534. files.push_back(file);
  535. }
  536. }
  537. }
  538. return files.empty();
  539. }
  540. static bool findVST3s(std::vector<water::File>& files,
  541. const BinaryType btype, const char* const pluginPath)
  542. {
  543. CARLA_SAFE_ASSERT_RETURN(pluginPath != nullptr, true);
  544. if (pluginPath[0] == '\0')
  545. return true;
  546. using water::File;
  547. using water::String;
  548. using water::StringArray;
  549. const StringArray splitPaths(StringArray::fromTokens(pluginPath, CARLA_OS_SPLIT_STR, ""));
  550. if (splitPaths.size() == 0)
  551. return true;
  552. const uint flags = btype == CB::BINARY_WIN32 || btype == CB::BINARY_WIN64
  553. ? File::findDirectories|File::findFiles
  554. : File::findDirectories;
  555. for (String *it = splitPaths.begin(), *end = splitPaths.end(); it != end; ++it)
  556. {
  557. const File dir(*it);
  558. std::vector<File> results;
  559. if (dir.findChildFiles(results, flags|File::ignoreHiddenFiles, true, "*.vst3") > 0)
  560. {
  561. files.reserve(files.size() + results.size());
  562. for (std::vector<File>::const_iterator cit = results.begin(); cit != results.end(); ++cit)
  563. {
  564. const File file(*cit);
  565. if (CB::getBinaryTypeFromFile(file.getFullPathName().toRawUTF8()) == btype)
  566. files.push_back(file);
  567. }
  568. }
  569. }
  570. return files.empty();
  571. }
  572. CarlaPluginDiscoveryHandle carla_plugin_discovery_start(const char* const discoveryTool,
  573. const BinaryType btype,
  574. const PluginType ptype,
  575. const char* const pluginPath,
  576. const CarlaPluginDiscoveryCallback discoveryCb,
  577. const CarlaPluginCheckCacheCallback checkCacheCb,
  578. void* const callbackPtr)
  579. {
  580. CARLA_SAFE_ASSERT_RETURN(btype != CB::BINARY_NONE, nullptr);
  581. CARLA_SAFE_ASSERT_RETURN(ptype != CB::PLUGIN_NONE, nullptr);
  582. CARLA_SAFE_ASSERT_RETURN(discoveryTool != nullptr && discoveryTool[0] != '\0', nullptr);
  583. CARLA_SAFE_ASSERT_RETURN(discoveryCb != nullptr, nullptr);
  584. bool directories = false;
  585. const char* wildcard = nullptr;
  586. switch (ptype)
  587. {
  588. case CB::PLUGIN_INTERNAL:
  589. case CB::PLUGIN_LV2:
  590. case CB::PLUGIN_SFZ:
  591. case CB::PLUGIN_JSFX:
  592. case CB::PLUGIN_DLS:
  593. case CB::PLUGIN_GIG:
  594. case CB::PLUGIN_SF2:
  595. CARLA_SAFE_ASSERT_UINT_RETURN(btype == CB::BINARY_NATIVE, btype, nullptr);
  596. break;
  597. default:
  598. break;
  599. }
  600. switch (ptype)
  601. {
  602. case CB::PLUGIN_NONE:
  603. case CB::PLUGIN_JACK:
  604. case CB::PLUGIN_TYPE_COUNT:
  605. return nullptr;
  606. case CB::PLUGIN_LV2:
  607. case CB::PLUGIN_SFZ:
  608. case CB::PLUGIN_JSFX:
  609. {
  610. const CarlaScopedEnvVar csev("CARLA_DISCOVERY_PATH", pluginPath);
  611. return new CarlaPluginDiscovery(discoveryTool, btype, ptype, discoveryCb, checkCacheCb, callbackPtr, pluginPath);
  612. }
  613. case CB::PLUGIN_INTERNAL:
  614. return new CarlaPluginDiscovery(discoveryTool, btype, ptype, discoveryCb, checkCacheCb, callbackPtr);
  615. case CB::PLUGIN_LADSPA:
  616. case CB::PLUGIN_DSSI:
  617. #ifdef CARLA_OS_WIN
  618. wildcard = "*.dll";
  619. #else
  620. if (btype == CB::BINARY_WIN32 || btype == CB::BINARY_WIN64)
  621. {
  622. wildcard = "*.dll";
  623. }
  624. else
  625. {
  626. #ifdef CARLA_OS_MAC
  627. wildcard = "*.dylib";
  628. #else
  629. wildcard = "*.so";
  630. #endif
  631. }
  632. #endif
  633. break;
  634. case CB::PLUGIN_VST2:
  635. #ifdef CARLA_OS_WIN
  636. wildcard = "*.dll";
  637. #else
  638. if (btype == CB::BINARY_WIN32 || btype == CB::BINARY_WIN64)
  639. {
  640. wildcard = "*.dll";
  641. }
  642. else
  643. {
  644. #ifdef CARLA_OS_MAC
  645. directories = true;
  646. wildcard = "*.vst";
  647. #else
  648. wildcard = "*.so";
  649. #endif
  650. }
  651. #endif
  652. break;
  653. case CB::PLUGIN_VST3:
  654. directories = true;
  655. wildcard = "*.vst3";
  656. break;
  657. case CB::PLUGIN_AU:
  658. directories = true;
  659. wildcard = "*.component";
  660. break;
  661. case CB::PLUGIN_CLAP:
  662. wildcard = "*.clap";
  663. #ifdef CARLA_OS_MAC
  664. directories = true;
  665. #endif
  666. break;
  667. case CB::PLUGIN_DLS:
  668. wildcard = "*.dls";
  669. break;
  670. case CB::PLUGIN_GIG:
  671. wildcard = "*.gig";
  672. break;
  673. case CB::PLUGIN_SF2:
  674. wildcard = "*.sf2";
  675. break;
  676. }
  677. CARLA_SAFE_ASSERT_RETURN(wildcard != nullptr, nullptr);
  678. std::vector<water::File> files;
  679. if (ptype == CB::PLUGIN_VST3)
  680. {
  681. if (findVST3s(files, btype, pluginPath))
  682. return nullptr;
  683. }
  684. else if (directories)
  685. {
  686. if (findDirectories(files, pluginPath, wildcard))
  687. return nullptr;
  688. }
  689. else
  690. {
  691. if (findFiles(files, btype, pluginPath, wildcard))
  692. return nullptr;
  693. }
  694. return new CarlaPluginDiscovery(discoveryTool, btype, ptype, std::move(files),
  695. discoveryCb, checkCacheCb, callbackPtr);
  696. }
  697. bool carla_plugin_discovery_idle(const CarlaPluginDiscoveryHandle handle)
  698. {
  699. return static_cast<CarlaPluginDiscovery*>(handle)->idle();
  700. }
  701. void carla_plugin_discovery_skip(const CarlaPluginDiscoveryHandle handle)
  702. {
  703. static_cast<CarlaPluginDiscovery*>(handle)->skip();
  704. }
  705. void carla_plugin_discovery_stop(const CarlaPluginDiscoveryHandle handle)
  706. {
  707. delete static_cast<CarlaPluginDiscovery*>(handle);
  708. }
  709. void carla_plugin_discovery_set_option(const EngineOption option, const int value, const char* const valueStr)
  710. {
  711. switch (option)
  712. {
  713. #if !defined(BUILD_BRIDGE_ALTERNATIVE_ARCH) && !defined(CARLA_OS_WIN)
  714. case CB::ENGINE_OPTION_WINE_EXECUTABLE:
  715. if (valueStr != nullptr && valueStr[0] != '\0')
  716. CarlaPluginDiscoveryOptions::getInstance().wine.executable = valueStr;
  717. else
  718. CarlaPluginDiscoveryOptions::getInstance().wine.executable.clear();
  719. break;
  720. case CB::ENGINE_OPTION_WINE_AUTO_PREFIX:
  721. CARLA_SAFE_ASSERT_RETURN(value == 0 || value == 1,);
  722. CarlaPluginDiscoveryOptions::getInstance().wine.autoPrefix = value != 0;
  723. break;
  724. case CB::ENGINE_OPTION_WINE_FALLBACK_PREFIX:
  725. if (valueStr != nullptr && valueStr[0] != '\0')
  726. CarlaPluginDiscoveryOptions::getInstance().wine.fallbackPrefix = valueStr;
  727. else
  728. CarlaPluginDiscoveryOptions::getInstance().wine.fallbackPrefix.clear();
  729. break;
  730. #endif
  731. default:
  732. break;
  733. }
  734. }
  735. // --------------------------------------------------------------------------------------------------------------------