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.

849 lines
27KB

  1. /*
  2. * Carla Plugin Host
  3. * Copyright (C) 2011-2018 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * For a full copy of the GNU General Public License see the doc/GPL.txt file.
  16. */
  17. #include "CarlaUtils.h"
  18. #include "CarlaNative.h"
  19. #include "CarlaBackendUtils.hpp"
  20. #include "CarlaLv2Utils.hpp"
  21. #include "CarlaPipeUtils.hpp"
  22. #include "CarlaThread.hpp"
  23. #include "LinkedList.hpp"
  24. #include "water/files/File.h"
  25. #ifdef CARLA_OS_MAC
  26. # import <Cocoa/Cocoa.h>
  27. #endif
  28. #ifndef CARLA_UTILS_CACHED_PLUGINS_ONLY
  29. # include "rtaudio/RtAudio.h"
  30. # include "rtmidi/RtMidi.h"
  31. # ifdef HAVE_X11
  32. # include <X11/Xlib.h>
  33. # endif
  34. #endif
  35. #include "../native-plugins/_data.all.cpp"
  36. namespace CB = CarlaBackend;
  37. static const char* const gNullCharPtr = "";
  38. // -------------------------------------------------------------------------------------------------------------------
  39. _CarlaCachedPluginInfo::_CarlaCachedPluginInfo() noexcept
  40. : category(CB::PLUGIN_CATEGORY_NONE),
  41. hints(0x0),
  42. audioIns(0),
  43. audioOuts(0),
  44. midiIns(0),
  45. midiOuts(0),
  46. parameterIns(0),
  47. parameterOuts(0),
  48. name(gNullCharPtr),
  49. label(gNullCharPtr),
  50. maker(gNullCharPtr),
  51. copyright(gNullCharPtr) {}
  52. // -------------------------------------------------------------------------------------------------------------------
  53. uint carla_get_cached_plugin_count(CB::PluginType ptype, const char* pluginPath)
  54. {
  55. CARLA_SAFE_ASSERT_RETURN(ptype == CB::PLUGIN_INTERNAL || ptype == CB::PLUGIN_LV2, 0);
  56. carla_debug("carla_get_cached_plugin_count(%i:%s)", ptype, CB::PluginType2Str(ptype));
  57. switch (ptype)
  58. {
  59. case CB::PLUGIN_INTERNAL: {
  60. uint32_t count = 0;
  61. carla_get_native_plugins_data(&count);
  62. return count;
  63. }
  64. case CB::PLUGIN_LV2: {
  65. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  66. lv2World.initIfNeeded(pluginPath);
  67. return lv2World.getPluginCount();
  68. }
  69. default:
  70. return 0;
  71. }
  72. }
  73. const CarlaCachedPluginInfo* carla_get_cached_plugin_info(CB::PluginType ptype, uint index)
  74. {
  75. carla_debug("carla_get_cached_plugin_info(%i:%s, %i)", ptype, CB::PluginType2Str(ptype), index);
  76. static CarlaCachedPluginInfo info;
  77. switch (ptype)
  78. {
  79. case CB::PLUGIN_INTERNAL: {
  80. uint32_t count = 0;
  81. const NativePluginDescriptor* const descs(carla_get_native_plugins_data(&count));
  82. CARLA_SAFE_ASSERT_BREAK(index < count);
  83. CARLA_SAFE_ASSERT_BREAK(descs != nullptr);
  84. const NativePluginDescriptor& desc(descs[index]);
  85. info.category = static_cast<CB::PluginCategory>(desc.category);
  86. info.hints = 0x0;
  87. if (desc.hints & NATIVE_PLUGIN_IS_RTSAFE)
  88. info.hints |= CB::PLUGIN_IS_RTSAFE;
  89. if (desc.hints & NATIVE_PLUGIN_IS_SYNTH)
  90. info.hints |= CB::PLUGIN_IS_SYNTH;
  91. if (desc.hints & NATIVE_PLUGIN_HAS_UI)
  92. info.hints |= CB::PLUGIN_HAS_CUSTOM_UI;
  93. if (desc.hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS)
  94. info.hints |= CB::PLUGIN_NEEDS_FIXED_BUFFERS;
  95. if (desc.hints & NATIVE_PLUGIN_NEEDS_UI_MAIN_THREAD)
  96. info.hints |= CB::PLUGIN_NEEDS_UI_MAIN_THREAD;
  97. if (desc.hints & NATIVE_PLUGIN_USES_MULTI_PROGS)
  98. info.hints |= CB::PLUGIN_USES_MULTI_PROGS;
  99. info.audioIns = desc.audioIns;
  100. info.audioOuts = desc.audioOuts;
  101. info.midiIns = desc.midiIns;
  102. info.midiOuts = desc.midiOuts;
  103. info.parameterIns = desc.paramIns;
  104. info.parameterOuts = desc.paramOuts;
  105. info.name = desc.name;
  106. info.label = desc.label;
  107. info.maker = desc.maker;
  108. info.copyright = desc.copyright;
  109. return &info;
  110. }
  111. case CB::PLUGIN_LV2: {
  112. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  113. const LilvPlugin* const cPlugin(lv2World.getPluginFromIndex(index));
  114. CARLA_SAFE_ASSERT_BREAK(cPlugin != nullptr);
  115. Lilv::Plugin lilvPlugin(cPlugin);
  116. CARLA_SAFE_ASSERT_BREAK(lilvPlugin.get_uri().is_uri());
  117. // features
  118. info.hints = 0x0;
  119. if (lilvPlugin.get_uis().size() > 0)
  120. info.hints |= CB::PLUGIN_HAS_CUSTOM_UI;
  121. {
  122. Lilv::Nodes lilvFeatureNodes(lilvPlugin.get_supported_features());
  123. LILV_FOREACH(nodes, it, lilvFeatureNodes)
  124. {
  125. Lilv::Node lilvFeatureNode(lilvFeatureNodes.get(it));
  126. const char* const featureURI(lilvFeatureNode.as_uri());
  127. CARLA_SAFE_ASSERT_CONTINUE(featureURI != nullptr);
  128. if (std::strcmp(featureURI, LV2_CORE__hardRTCapable) == 0)
  129. info.hints |= CB::PLUGIN_IS_RTSAFE;
  130. }
  131. lilv_nodes_free(const_cast<LilvNodes*>(lilvFeatureNodes.me));
  132. }
  133. // category
  134. info.category = CB::PLUGIN_CATEGORY_NONE;
  135. {
  136. Lilv::Nodes typeNodes(lilvPlugin.get_value(lv2World.rdf_type));
  137. if (typeNodes.size() > 0)
  138. {
  139. if (typeNodes.contains(lv2World.class_allpass))
  140. info.category = CB::PLUGIN_CATEGORY_FILTER;
  141. if (typeNodes.contains(lv2World.class_amplifier))
  142. info.category = CB::PLUGIN_CATEGORY_DYNAMICS;
  143. if (typeNodes.contains(lv2World.class_analyzer))
  144. info.category = CB::PLUGIN_CATEGORY_UTILITY;
  145. if (typeNodes.contains(lv2World.class_bandpass))
  146. info.category = CB::PLUGIN_CATEGORY_FILTER;
  147. if (typeNodes.contains(lv2World.class_chorus))
  148. info.category = CB::PLUGIN_CATEGORY_MODULATOR;
  149. if (typeNodes.contains(lv2World.class_comb))
  150. info.category = CB::PLUGIN_CATEGORY_FILTER;
  151. if (typeNodes.contains(lv2World.class_compressor))
  152. info.category = CB::PLUGIN_CATEGORY_DYNAMICS;
  153. if (typeNodes.contains(lv2World.class_constant))
  154. info.category = CB::PLUGIN_CATEGORY_OTHER;
  155. if (typeNodes.contains(lv2World.class_converter))
  156. info.category = CB::PLUGIN_CATEGORY_UTILITY;
  157. if (typeNodes.contains(lv2World.class_delay))
  158. info.category = CB::PLUGIN_CATEGORY_DELAY;
  159. if (typeNodes.contains(lv2World.class_distortion))
  160. info.category = CB::PLUGIN_CATEGORY_DISTORTION;
  161. if (typeNodes.contains(lv2World.class_dynamics))
  162. info.category = CB::PLUGIN_CATEGORY_DYNAMICS;
  163. if (typeNodes.contains(lv2World.class_eq))
  164. info.category = CB::PLUGIN_CATEGORY_EQ;
  165. if (typeNodes.contains(lv2World.class_envelope))
  166. info.category = CB::PLUGIN_CATEGORY_DYNAMICS;
  167. if (typeNodes.contains(lv2World.class_expander))
  168. info.category = CB::PLUGIN_CATEGORY_DYNAMICS;
  169. if (typeNodes.contains(lv2World.class_filter))
  170. info.category = CB::PLUGIN_CATEGORY_FILTER;
  171. if (typeNodes.contains(lv2World.class_flanger))
  172. info.category = CB::PLUGIN_CATEGORY_MODULATOR;
  173. if (typeNodes.contains(lv2World.class_function))
  174. info.category = CB::PLUGIN_CATEGORY_UTILITY;
  175. if (typeNodes.contains(lv2World.class_gate))
  176. info.category = CB::PLUGIN_CATEGORY_DYNAMICS;
  177. if (typeNodes.contains(lv2World.class_generator))
  178. info.category = CB::PLUGIN_CATEGORY_OTHER;
  179. if (typeNodes.contains(lv2World.class_highpass))
  180. info.category = CB::PLUGIN_CATEGORY_FILTER;
  181. if (typeNodes.contains(lv2World.class_limiter))
  182. info.category = CB::PLUGIN_CATEGORY_DYNAMICS;
  183. if (typeNodes.contains(lv2World.class_lowpass))
  184. info.category = CB::PLUGIN_CATEGORY_FILTER;
  185. if (typeNodes.contains(lv2World.class_mixer))
  186. info.category = CB::PLUGIN_CATEGORY_UTILITY;
  187. if (typeNodes.contains(lv2World.class_modulator))
  188. info.category = CB::PLUGIN_CATEGORY_MODULATOR;
  189. if (typeNodes.contains(lv2World.class_multiEQ))
  190. info.category = CB::PLUGIN_CATEGORY_EQ;
  191. if (typeNodes.contains(lv2World.class_oscillator))
  192. info.category = CB::PLUGIN_CATEGORY_OTHER;
  193. if (typeNodes.contains(lv2World.class_paraEQ))
  194. info.category = CB::PLUGIN_CATEGORY_EQ;
  195. if (typeNodes.contains(lv2World.class_phaser))
  196. info.category = CB::PLUGIN_CATEGORY_MODULATOR;
  197. if (typeNodes.contains(lv2World.class_pitch))
  198. info.category = CB::PLUGIN_CATEGORY_OTHER;
  199. if (typeNodes.contains(lv2World.class_reverb))
  200. info.category = CB::PLUGIN_CATEGORY_DELAY;
  201. if (typeNodes.contains(lv2World.class_simulator))
  202. info.category = CB::PLUGIN_CATEGORY_OTHER;
  203. if (typeNodes.contains(lv2World.class_spatial))
  204. info.category = CB::PLUGIN_CATEGORY_OTHER;
  205. if (typeNodes.contains(lv2World.class_spectral))
  206. info.category = CB::PLUGIN_CATEGORY_OTHER;
  207. if (typeNodes.contains(lv2World.class_utility))
  208. info.category = CB::PLUGIN_CATEGORY_UTILITY;
  209. if (typeNodes.contains(lv2World.class_waveshaper))
  210. info.category = CB::PLUGIN_CATEGORY_DISTORTION;
  211. if (typeNodes.contains(lv2World.class_instrument))
  212. {
  213. info.category = CB::PLUGIN_CATEGORY_SYNTH;
  214. info.hints |= CB::PLUGIN_IS_SYNTH;
  215. }
  216. }
  217. lilv_nodes_free(const_cast<LilvNodes*>(typeNodes.me));
  218. }
  219. // number data
  220. info.audioIns = 0;
  221. info.audioOuts = 0;
  222. info.midiIns = 0;
  223. info.midiOuts = 0;
  224. info.parameterIns = 0;
  225. info.parameterOuts = 0;
  226. for (uint i=0, count=lilvPlugin.get_num_ports(); i<count; ++i)
  227. {
  228. Lilv::Port lilvPort(lilvPlugin.get_port_by_index(i));
  229. bool isInput;
  230. /**/ if (lilvPort.is_a(lv2World.port_input))
  231. isInput = true;
  232. else if (lilvPort.is_a(lv2World.port_output))
  233. isInput = false;
  234. else
  235. continue;
  236. /**/ if (lilvPort.is_a(lv2World.port_control))
  237. {
  238. // skip some control ports
  239. if (lilvPort.has_property(lv2World.reportsLatency))
  240. continue;
  241. if (LilvNode* const designationNode = lilv_port_get(lilvPort.parent, lilvPort.me, lv2World.designation.me))
  242. {
  243. bool skip = false;
  244. if (const char* const designation = lilv_node_as_string(designationNode))
  245. {
  246. /**/ if (std::strcmp(designation, LV2_CORE__control) == 0)
  247. skip = true;
  248. else if (std::strcmp(designation, LV2_CORE__freeWheeling) == 0)
  249. skip = true;
  250. else if (std::strcmp(designation, LV2_CORE__latency) == 0)
  251. skip = true;
  252. else if (std::strcmp(designation, LV2_PARAMETERS__sampleRate) == 0)
  253. skip = true;
  254. else if (std::strcmp(designation, LV2_TIME__bar) == 0)
  255. skip = true;
  256. else if (std::strcmp(designation, LV2_TIME__barBeat) == 0)
  257. skip = true;
  258. else if (std::strcmp(designation, LV2_TIME__beat) == 0)
  259. skip = true;
  260. else if (std::strcmp(designation, LV2_TIME__beatUnit) == 0)
  261. skip = true;
  262. else if (std::strcmp(designation, LV2_TIME__beatsPerBar) == 0)
  263. skip = true;
  264. else if (std::strcmp(designation, LV2_TIME__beatsPerMinute) == 0)
  265. skip = true;
  266. else if (std::strcmp(designation, LV2_TIME__frame) == 0)
  267. skip = true;
  268. else if (std::strcmp(designation, LV2_TIME__framesPerSecond) == 0)
  269. skip = true;
  270. else if (std::strcmp(designation, LV2_TIME__speed) == 0)
  271. skip = true;
  272. else if (std::strcmp(designation, LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat) == 0)
  273. skip = true;
  274. }
  275. lilv_node_free(designationNode);
  276. if (skip)
  277. continue;
  278. }
  279. if (isInput)
  280. ++(info.parameterIns);
  281. else
  282. ++(info.parameterOuts);
  283. }
  284. else if (lilvPort.is_a(lv2World.port_audio))
  285. {
  286. if (isInput)
  287. ++(info.audioIns);
  288. else
  289. ++(info.audioOuts);
  290. }
  291. else if (lilvPort.is_a(lv2World.port_cv))
  292. {
  293. }
  294. else if (lilvPort.is_a(lv2World.port_atom))
  295. {
  296. Lilv::Nodes supportNodes(lilvPort.get_value(lv2World.atom_supports));
  297. for (LilvIter *it = lilv_nodes_begin(supportNodes.me); ! lilv_nodes_is_end(supportNodes.me, it); it = lilv_nodes_next(supportNodes.me, it))
  298. {
  299. const Lilv::Node node(lilv_nodes_get(supportNodes.me, it));
  300. CARLA_SAFE_ASSERT_CONTINUE(node.is_uri());
  301. if (node.equals(lv2World.midi_event))
  302. {
  303. if (isInput)
  304. ++(info.midiIns);
  305. else
  306. ++(info.midiOuts);
  307. }
  308. }
  309. lilv_nodes_free(const_cast<LilvNodes*>(supportNodes.me));
  310. }
  311. else if (lilvPort.is_a(lv2World.port_event))
  312. {
  313. if (lilvPort.supports_event(lv2World.midi_event))
  314. {
  315. if (isInput)
  316. ++(info.midiIns);
  317. else
  318. ++(info.midiOuts);
  319. }
  320. }
  321. else if (lilvPort.is_a(lv2World.port_midi))
  322. {
  323. if (isInput)
  324. ++(info.midiIns);
  325. else
  326. ++(info.midiOuts);
  327. }
  328. }
  329. // text data
  330. static CarlaString suri, sname, smaker, slicense;
  331. suri.clear(); sname.clear(); smaker.clear(); slicense.clear();
  332. suri = lilvPlugin.get_uri().as_uri();
  333. if (LilvNode* const nameNode = lilv_plugin_get_name(lilvPlugin.me))
  334. {
  335. if (const char* const name = lilv_node_as_string(nameNode))
  336. sname = name;
  337. lilv_node_free(nameNode);
  338. }
  339. if (const char* const author = lilvPlugin.get_author_name().as_string())
  340. smaker = author;
  341. Lilv::Nodes licenseNodes(lilvPlugin.get_value(lv2World.doap_license));
  342. if (licenseNodes.size() > 0)
  343. {
  344. if (const char* const license = licenseNodes.get_first().as_string())
  345. slicense = license;
  346. }
  347. lilv_nodes_free(const_cast<LilvNodes*>(licenseNodes.me));
  348. info.name = sname;
  349. info.label = suri;
  350. info.maker = smaker;
  351. info.copyright = slicense;
  352. return &info;
  353. }
  354. default:
  355. break;
  356. }
  357. info.category = CB::PLUGIN_CATEGORY_NONE;
  358. info.hints = 0x0;
  359. info.audioIns = 0;
  360. info.audioOuts = 0;
  361. info.midiIns = 0;
  362. info.midiOuts = 0;
  363. info.parameterIns = 0;
  364. info.parameterOuts = 0;
  365. info.name = gNullCharPtr;
  366. info.label = gNullCharPtr;
  367. info.maker = gNullCharPtr;
  368. info.copyright = gNullCharPtr;
  369. return &info;
  370. }
  371. #ifndef CARLA_UTILS_CACHED_PLUGINS_ONLY
  372. // -------------------------------------------------------------------------------------------------------------------
  373. const char* carla_get_complete_license_text()
  374. {
  375. carla_debug("carla_get_complete_license_text()");
  376. static CarlaString retText;
  377. if (retText.isEmpty())
  378. {
  379. retText =
  380. "<p>This current Carla build is using the following features and 3rd-party code:</p>"
  381. "<ul>"
  382. // Plugin formats
  383. "<li>LADSPA plugin support</li>"
  384. "<li>DSSI plugin support</li>"
  385. "<li>LV2 plugin support</li>"
  386. "<li>VST2 plugin support using VeSTige header by Javier Serrano Polo</li>"
  387. // Sample kit libraries
  388. #ifdef HAVE_FLUIDSYNTH
  389. "<li>FluidSynth library for SF2 support</li>"
  390. #endif
  391. #ifdef HAVE_LINUXSAMPLER
  392. "<li>LinuxSampler library for GIG and SFZ support [1]</li>"
  393. #endif
  394. // misc libs
  395. "<li>base64 utilities based on code by Ren\u00E9 Nyffenegger</li>"
  396. "<li>liblo library for OSC support</li>"
  397. "<li>rtmempool library by Nedko Arnaudov"
  398. "<li>serd, sord, sratom and lilv libraries for LV2 discovery</li>"
  399. "<li>RtAudio v" RTAUDIO_VERSION " and RtMidi v" RTMIDI_VERSION " for native Audio and MIDI support</li>"
  400. // Internal plugins
  401. "<li>MIDI Sequencer UI code by Perry Nguyen</li>"
  402. // External plugins
  403. #ifdef HAVE_EXTERNAL_PLUGINS
  404. # ifdef HAVE_EXPERIMENTAL_PLUGINS
  405. "<li>AT1, BLS1 and REV1 plugin code by Fons Adriaensen</li>"
  406. # endif
  407. "<li>Nekobi plugin code based on nekobee by Sean Bolton and others</li>"
  408. "<li>VectorJuice and WobbleJuice plugin code by Andre Sklenar</li>"
  409. # ifdef HAVE_ZYN_DEPS
  410. "<li>ZynAddSubFX plugin code by Mark McCurry and Nasca Octavian Paul</li>"
  411. # endif
  412. #endif // HAVE_EXTERNAL_PLUGINS
  413. // end
  414. "</ul>"
  415. "<p>"
  416. #ifdef HAVE_LINUXSAMPLER
  417. // LinuxSampler GPL exception
  418. "&nbsp;[1] Using LinuxSampler code in commercial hardware or software products is not allowed without prior written authorization by the authors."
  419. #endif
  420. "</p>"
  421. ;
  422. }
  423. return retText;
  424. }
  425. const char* const* carla_get_supported_file_extensions()
  426. {
  427. carla_debug("carla_get_supported_file_extensions()");
  428. // NOTE: please keep in sync with CarlaEngine::loadFile!!
  429. static const char* const extensions[] = {
  430. // Base types
  431. "carxp", "carxs",
  432. // plugin files and resources
  433. #ifdef HAVE_FLUIDSYNTH
  434. "sf2",
  435. #endif
  436. #ifdef HAVE_LINUXSAMPLER
  437. "gig", "sfz",
  438. #endif
  439. #ifdef HAVE_ZYN_DEPS
  440. "xmz", "xiz",
  441. #endif
  442. #if defined(CARLA_OS_MAC)
  443. "vst",
  444. #else
  445. "dll",
  446. "so",
  447. #endif
  448. // Audio files
  449. #ifdef HAVE_SNDFILE
  450. "aif", "aiff", "bwf", "flac", "oga", "ogg", "w64", "wav",
  451. #endif
  452. #ifdef HAVE_FFMPEG
  453. "3g2", "3gp", "aac", "ac3", "amr", "ape", "mp2", "mp3", "mpc", "wma",
  454. #endif
  455. // MIDI files
  456. "mid", "midi",
  457. // terminator
  458. nullptr
  459. };
  460. return extensions;
  461. }
  462. const char* const* carla_get_supported_features()
  463. {
  464. carla_debug("carla_get_supported_features()");
  465. static const char* const features[] = {
  466. #ifdef HAVE_FLUIDSYNTH
  467. "sf2",
  468. #endif
  469. #ifdef HAVE_HYLIA
  470. "link",
  471. #endif
  472. #ifdef HAVE_LIBLO
  473. "osc",
  474. #endif
  475. #if defined(HAVE_LIBMAGIC) || defined(CARLA_OS_WIN)
  476. "bridges",
  477. #endif
  478. #ifdef HAVE_LINUXSAMPLER
  479. "gig", "sfz",
  480. #endif
  481. #ifdef HAVE_PYQT
  482. "gui",
  483. #endif
  484. nullptr
  485. };
  486. return features;
  487. }
  488. // -------------------------------------------------------------------------------------------------------------------
  489. void carla_fflush(bool err)
  490. {
  491. std::fflush(err ? stderr : stdout);
  492. }
  493. void carla_fputs(bool err, const char* string)
  494. {
  495. std::fputs(string, err ? stderr : stdout);
  496. }
  497. void carla_set_process_name(const char* name)
  498. {
  499. carla_debug("carla_set_process_name(\"%s\")", name);
  500. CarlaThread::setCurrentThreadName(name);
  501. }
  502. // -------------------------------------------------------------------------------------------------------------------
  503. class CarlaPipeClientPlugin : public CarlaPipeClient
  504. {
  505. public:
  506. CarlaPipeClientPlugin(const CarlaPipeCallbackFunc callbackFunc, void* const callbackPtr) noexcept
  507. : CarlaPipeClient(),
  508. fCallbackFunc(callbackFunc),
  509. fCallbackPtr(callbackPtr),
  510. fLastReadLine(nullptr)
  511. {
  512. CARLA_SAFE_ASSERT(fCallbackFunc != nullptr);
  513. }
  514. ~CarlaPipeClientPlugin() override
  515. {
  516. if (fLastReadLine != nullptr)
  517. {
  518. delete[] fLastReadLine;
  519. fLastReadLine = nullptr;
  520. }
  521. }
  522. const char* readlineblock(const uint timeout) noexcept
  523. {
  524. delete[] fLastReadLine;
  525. fLastReadLine = CarlaPipeClient::_readlineblock(timeout);
  526. return fLastReadLine;
  527. }
  528. bool msgReceived(const char* const msg) noexcept override
  529. {
  530. if (fCallbackFunc != nullptr)
  531. {
  532. try {
  533. fCallbackFunc(fCallbackPtr, msg);
  534. } CARLA_SAFE_EXCEPTION("msgReceived");
  535. }
  536. return true;
  537. }
  538. private:
  539. const CarlaPipeCallbackFunc fCallbackFunc;
  540. void* const fCallbackPtr;
  541. const char* fLastReadLine;
  542. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPipeClientPlugin)
  543. };
  544. CarlaPipeClientHandle carla_pipe_client_new(const char* argv[], CarlaPipeCallbackFunc callbackFunc, void* callbackPtr)
  545. {
  546. carla_debug("carla_pipe_client_new(%p, %p, %p)", argv, callbackFunc, callbackPtr);
  547. CarlaPipeClientPlugin* const pipe(new CarlaPipeClientPlugin(callbackFunc, callbackPtr));
  548. if (! pipe->initPipeClient(argv))
  549. {
  550. delete pipe;
  551. return nullptr;
  552. }
  553. return pipe;
  554. }
  555. void carla_pipe_client_idle(CarlaPipeClientHandle handle)
  556. {
  557. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  558. ((CarlaPipeClientPlugin*)handle)->idlePipe();
  559. }
  560. bool carla_pipe_client_is_running(CarlaPipeClientHandle handle)
  561. {
  562. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  563. return ((CarlaPipeClientPlugin*)handle)->isPipeRunning();
  564. }
  565. void carla_pipe_client_lock(CarlaPipeClientHandle handle)
  566. {
  567. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  568. return ((CarlaPipeClientPlugin*)handle)->lockPipe();
  569. }
  570. void carla_pipe_client_unlock(CarlaPipeClientHandle handle)
  571. {
  572. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  573. return ((CarlaPipeClientPlugin*)handle)->unlockPipe();
  574. }
  575. const char* carla_pipe_client_readlineblock(CarlaPipeClientHandle handle, uint timeout)
  576. {
  577. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  578. return ((CarlaPipeClientPlugin*)handle)->readlineblock(timeout);
  579. }
  580. bool carla_pipe_client_write_msg(CarlaPipeClientHandle handle, const char* msg)
  581. {
  582. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  583. return ((CarlaPipeClientPlugin*)handle)->writeMessage(msg);
  584. }
  585. bool carla_pipe_client_write_and_fix_msg(CarlaPipeClientHandle handle, const char* msg)
  586. {
  587. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  588. return ((CarlaPipeClientPlugin*)handle)->writeAndFixMessage(msg);
  589. }
  590. bool carla_pipe_client_flush(CarlaPipeClientHandle handle)
  591. {
  592. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  593. return ((CarlaPipeClientPlugin*)handle)->flushMessages();
  594. }
  595. bool carla_pipe_client_flush_and_unlock(CarlaPipeClientHandle handle)
  596. {
  597. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  598. CarlaPipeClientPlugin* const pipe((CarlaPipeClientPlugin*)handle);
  599. const bool ret(pipe->flushMessages());
  600. pipe->unlockPipe();
  601. return ret;
  602. }
  603. void carla_pipe_client_destroy(CarlaPipeClientHandle handle)
  604. {
  605. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  606. carla_debug("carla_pipe_client_destroy(%p)", handle);
  607. CarlaPipeClientPlugin* const pipe((CarlaPipeClientPlugin*)handle);
  608. pipe->closePipeClient();
  609. delete pipe;
  610. }
  611. // -------------------------------------------------------------------------------------------------------------------
  612. const char* carla_get_library_filename()
  613. {
  614. carla_debug("carla_get_library_filename()");
  615. static CarlaString ret;
  616. if (ret.isEmpty())
  617. {
  618. using water::File;
  619. ret = File(File::getSpecialLocation(File::currentExecutableFile)).getFullPathName().toRawUTF8();
  620. }
  621. return ret;
  622. }
  623. const char* carla_get_library_folder()
  624. {
  625. carla_debug("carla_get_library_folder()");
  626. static CarlaString ret;
  627. if (ret.isEmpty())
  628. {
  629. using water::File;
  630. ret = File(File::getSpecialLocation(File::currentExecutableFile).getParentDirectory()).getFullPathName().toRawUTF8();
  631. }
  632. return ret;
  633. }
  634. // -------------------------------------------------------------------------------------------------------------------
  635. void carla_x11_reparent_window(uintptr_t winId1, uintptr_t winId2)
  636. {
  637. CARLA_SAFE_ASSERT_RETURN(winId1 != 0,);
  638. CARLA_SAFE_ASSERT_RETURN(winId2 != 0,);
  639. #ifdef HAVE_X11
  640. if (::Display* const disp = XOpenDisplay(nullptr))
  641. {
  642. XReparentWindow(disp, winId1, winId2, 0, 0);
  643. XMapWindow(disp, winId1);
  644. XCloseDisplay(disp);
  645. }
  646. #endif
  647. }
  648. void carla_x11_move_window(uintptr_t winId, int x, int y)
  649. {
  650. CARLA_SAFE_ASSERT_RETURN(winId != 0,);
  651. #ifdef HAVE_X11
  652. if (::Display* const disp = XOpenDisplay(nullptr))
  653. {
  654. XMoveWindow(disp, winId, x, y);
  655. XCloseDisplay(disp);
  656. }
  657. #else
  658. // unused
  659. return; (void)x; (void)y;
  660. #endif
  661. }
  662. int* carla_x11_get_window_pos(uintptr_t winId)
  663. {
  664. static int pos[2];
  665. if (winId == 0)
  666. {
  667. pos[0] = 0;
  668. pos[1] = 0;
  669. }
  670. #ifdef HAVE_X11
  671. else if (::Display* const disp = XOpenDisplay(nullptr))
  672. {
  673. int x, y;
  674. Window child;
  675. XWindowAttributes xwa;
  676. XTranslateCoordinates(disp, winId, XRootWindow(disp, 0), 0, 0, &x, &y, &child);
  677. XGetWindowAttributes(disp, winId, &xwa);
  678. XCloseDisplay(disp);
  679. pos[0] = x - xwa.x;
  680. pos[1] = y - xwa.y;
  681. }
  682. #endif
  683. else
  684. {
  685. pos[0] = 0;
  686. pos[1] = 0;
  687. }
  688. return pos;
  689. }
  690. int carla_cocoa_get_window(void* nsViewPtr)
  691. {
  692. CARLA_SAFE_ASSERT_RETURN(nsViewPtr != nullptr, 0);
  693. #ifdef CARLA_OS_MAC
  694. NSView* nsView = (NSView*)nsViewPtr;
  695. return [[nsView window] windowNumber];
  696. #else
  697. return 0;
  698. #endif
  699. }
  700. // -------------------------------------------------------------------------------------------------------------------
  701. #include "CarlaPipeUtils.cpp"
  702. #if !(defined(DEBUG) || defined(BUILDING_CARLA_FOR_WINDOWS))
  703. # include "water/misc/Time.cpp"
  704. #endif
  705. // -------------------------------------------------------------------------------------------------------------------
  706. #endif // CARLA_UTILS_CACHED_PLUGINS_ONLY