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.

893 lines
28KB

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