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.

840 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/3 support</li>"
  390. #endif
  391. // misc libs
  392. "<li>base64 utilities based on code by Ren\u00E9 Nyffenegger</li>"
  393. "<li>liblo library for OSC support</li>"
  394. "<li>rtmempool library by Nedko Arnaudov"
  395. "<li>serd, sord, sratom and lilv libraries for LV2 discovery</li>"
  396. "<li>RtAudio v" RTAUDIO_VERSION " and RtMidi v" RTMIDI_VERSION " for native Audio and MIDI support</li>"
  397. // Internal plugins
  398. "<li>MIDI Sequencer UI code by Perry Nguyen</li>"
  399. // External plugins
  400. #ifdef HAVE_EXTERNAL_PLUGINS
  401. # ifdef HAVE_EXPERIMENTAL_PLUGINS
  402. "<li>AT1, BLS1 and REV1 plugin code by Fons Adriaensen</li>"
  403. # endif
  404. "<li>Nekobi plugin code based on nekobee by Sean Bolton and others</li>"
  405. "<li>VectorJuice and WobbleJuice plugin code by Andre Sklenar</li>"
  406. # ifdef HAVE_ZYN_DEPS
  407. "<li>ZynAddSubFX plugin code by Mark McCurry and Nasca Octavian Paul</li>"
  408. # endif
  409. #endif // HAVE_EXTERNAL_PLUGINS
  410. // end
  411. "</ul>";
  412. }
  413. return retText;
  414. }
  415. const char* const* carla_get_supported_file_extensions()
  416. {
  417. carla_debug("carla_get_supported_file_extensions()");
  418. // NOTE: please keep in sync with CarlaEngine::loadFile!!
  419. static const char* const extensions[] = {
  420. // Base types
  421. "carxp", "carxs",
  422. // plugin files and resources
  423. #ifdef HAVE_FLUIDSYNTH
  424. "sf2", "sf3",
  425. #endif
  426. #ifdef HAVE_ZYN_DEPS
  427. "xmz", "xiz",
  428. #endif
  429. #if defined(CARLA_OS_MAC)
  430. "vst",
  431. #else
  432. "dll",
  433. "so",
  434. #endif
  435. // Audio files
  436. #ifdef HAVE_SNDFILE
  437. "aif", "aifc", "aiff", "au", "bwf", "flac", "htk", "iff", "mat4", "mat5", "oga", "ogg",
  438. "paf", "pvf", "pvf5", "sd2", "sf", "snd", "svx", "vcc", "w64", "wav", "xi",
  439. #endif
  440. #ifdef HAVE_FFMPEG
  441. "3g2", "3gp", "aac", "ac3", "amr", "ape", "mp2", "mp3", "mpc", "wma",
  442. # ifdef HAVE_SNDFILE
  443. // FFmpeg without sndfile
  444. "flac", "oga", "ogg", "w64", "wav",
  445. # endif
  446. #endif
  447. // MIDI files
  448. "mid", "midi",
  449. // SFZ
  450. "sfz",
  451. // terminator
  452. nullptr
  453. };
  454. return extensions;
  455. }
  456. const char* const* carla_get_supported_features()
  457. {
  458. carla_debug("carla_get_supported_features()");
  459. static const char* const features[] = {
  460. #ifdef HAVE_FLUIDSYNTH
  461. "sf2",
  462. #endif
  463. #ifdef HAVE_HYLIA
  464. "link",
  465. #endif
  466. #ifdef HAVE_LIBLO
  467. "osc",
  468. #endif
  469. #if defined(HAVE_LIBMAGIC) || defined(CARLA_OS_WIN)
  470. "bridges",
  471. #endif
  472. #ifdef HAVE_PYQT
  473. "gui",
  474. #endif
  475. nullptr
  476. };
  477. return features;
  478. }
  479. // -------------------------------------------------------------------------------------------------------------------
  480. void carla_fflush(bool err)
  481. {
  482. std::fflush(err ? stderr : stdout);
  483. }
  484. void carla_fputs(bool err, const char* string)
  485. {
  486. std::fputs(string, err ? stderr : stdout);
  487. }
  488. void carla_set_process_name(const char* name)
  489. {
  490. carla_debug("carla_set_process_name(\"%s\")", name);
  491. CarlaThread::setCurrentThreadName(name);
  492. }
  493. // -------------------------------------------------------------------------------------------------------------------
  494. class CarlaPipeClientPlugin : public CarlaPipeClient
  495. {
  496. public:
  497. CarlaPipeClientPlugin(const CarlaPipeCallbackFunc callbackFunc, void* const callbackPtr) noexcept
  498. : CarlaPipeClient(),
  499. fCallbackFunc(callbackFunc),
  500. fCallbackPtr(callbackPtr),
  501. fLastReadLine(nullptr)
  502. {
  503. CARLA_SAFE_ASSERT(fCallbackFunc != nullptr);
  504. }
  505. ~CarlaPipeClientPlugin() override
  506. {
  507. if (fLastReadLine != nullptr)
  508. {
  509. delete[] fLastReadLine;
  510. fLastReadLine = nullptr;
  511. }
  512. }
  513. const char* readlineblock(const uint timeout) noexcept
  514. {
  515. delete[] fLastReadLine;
  516. fLastReadLine = CarlaPipeClient::_readlineblock(timeout);
  517. return fLastReadLine;
  518. }
  519. bool msgReceived(const char* const msg) noexcept override
  520. {
  521. if (fCallbackFunc != nullptr)
  522. {
  523. try {
  524. fCallbackFunc(fCallbackPtr, msg);
  525. } CARLA_SAFE_EXCEPTION("msgReceived");
  526. }
  527. return true;
  528. }
  529. private:
  530. const CarlaPipeCallbackFunc fCallbackFunc;
  531. void* const fCallbackPtr;
  532. const char* fLastReadLine;
  533. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaPipeClientPlugin)
  534. };
  535. CarlaPipeClientHandle carla_pipe_client_new(const char* argv[], CarlaPipeCallbackFunc callbackFunc, void* callbackPtr)
  536. {
  537. carla_debug("carla_pipe_client_new(%p, %p, %p)", argv, callbackFunc, callbackPtr);
  538. CarlaPipeClientPlugin* const pipe(new CarlaPipeClientPlugin(callbackFunc, callbackPtr));
  539. if (! pipe->initPipeClient(argv))
  540. {
  541. delete pipe;
  542. return nullptr;
  543. }
  544. return pipe;
  545. }
  546. void carla_pipe_client_idle(CarlaPipeClientHandle handle)
  547. {
  548. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  549. ((CarlaPipeClientPlugin*)handle)->idlePipe();
  550. }
  551. bool carla_pipe_client_is_running(CarlaPipeClientHandle handle)
  552. {
  553. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  554. return ((CarlaPipeClientPlugin*)handle)->isPipeRunning();
  555. }
  556. void carla_pipe_client_lock(CarlaPipeClientHandle handle)
  557. {
  558. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  559. return ((CarlaPipeClientPlugin*)handle)->lockPipe();
  560. }
  561. void carla_pipe_client_unlock(CarlaPipeClientHandle handle)
  562. {
  563. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  564. return ((CarlaPipeClientPlugin*)handle)->unlockPipe();
  565. }
  566. const char* carla_pipe_client_readlineblock(CarlaPipeClientHandle handle, uint timeout)
  567. {
  568. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  569. return ((CarlaPipeClientPlugin*)handle)->readlineblock(timeout);
  570. }
  571. bool carla_pipe_client_write_msg(CarlaPipeClientHandle handle, const char* msg)
  572. {
  573. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  574. return ((CarlaPipeClientPlugin*)handle)->writeMessage(msg);
  575. }
  576. bool carla_pipe_client_write_and_fix_msg(CarlaPipeClientHandle handle, const char* msg)
  577. {
  578. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  579. return ((CarlaPipeClientPlugin*)handle)->writeAndFixMessage(msg);
  580. }
  581. bool carla_pipe_client_flush(CarlaPipeClientHandle handle)
  582. {
  583. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  584. return ((CarlaPipeClientPlugin*)handle)->flushMessages();
  585. }
  586. bool carla_pipe_client_flush_and_unlock(CarlaPipeClientHandle handle)
  587. {
  588. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, false);
  589. CarlaPipeClientPlugin* const pipe((CarlaPipeClientPlugin*)handle);
  590. const bool ret(pipe->flushMessages());
  591. pipe->unlockPipe();
  592. return ret;
  593. }
  594. void carla_pipe_client_destroy(CarlaPipeClientHandle handle)
  595. {
  596. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  597. carla_debug("carla_pipe_client_destroy(%p)", handle);
  598. CarlaPipeClientPlugin* const pipe((CarlaPipeClientPlugin*)handle);
  599. pipe->closePipeClient();
  600. delete pipe;
  601. }
  602. // -------------------------------------------------------------------------------------------------------------------
  603. const char* carla_get_library_filename()
  604. {
  605. carla_debug("carla_get_library_filename()");
  606. static CarlaString ret;
  607. if (ret.isEmpty())
  608. {
  609. using water::File;
  610. ret = File(File::getSpecialLocation(File::currentExecutableFile)).getFullPathName().toRawUTF8();
  611. }
  612. return ret;
  613. }
  614. const char* carla_get_library_folder()
  615. {
  616. carla_debug("carla_get_library_folder()");
  617. static CarlaString ret;
  618. if (ret.isEmpty())
  619. {
  620. using water::File;
  621. ret = File(File::getSpecialLocation(File::currentExecutableFile).getParentDirectory()).getFullPathName().toRawUTF8();
  622. }
  623. return ret;
  624. }
  625. // -------------------------------------------------------------------------------------------------------------------
  626. void carla_x11_reparent_window(uintptr_t winId1, uintptr_t winId2)
  627. {
  628. CARLA_SAFE_ASSERT_RETURN(winId1 != 0,);
  629. CARLA_SAFE_ASSERT_RETURN(winId2 != 0,);
  630. #ifdef HAVE_X11
  631. if (::Display* const disp = XOpenDisplay(nullptr))
  632. {
  633. XReparentWindow(disp, winId1, winId2, 0, 0);
  634. XMapWindow(disp, winId1);
  635. XCloseDisplay(disp);
  636. }
  637. #endif
  638. }
  639. void carla_x11_move_window(uintptr_t winId, int x, int y)
  640. {
  641. CARLA_SAFE_ASSERT_RETURN(winId != 0,);
  642. #ifdef HAVE_X11
  643. if (::Display* const disp = XOpenDisplay(nullptr))
  644. {
  645. XMoveWindow(disp, winId, x, y);
  646. XCloseDisplay(disp);
  647. }
  648. #else
  649. // unused
  650. return; (void)x; (void)y;
  651. #endif
  652. }
  653. int* carla_x11_get_window_pos(uintptr_t winId)
  654. {
  655. static int pos[2];
  656. if (winId == 0)
  657. {
  658. pos[0] = 0;
  659. pos[1] = 0;
  660. }
  661. #ifdef HAVE_X11
  662. else if (::Display* const disp = XOpenDisplay(nullptr))
  663. {
  664. int x, y;
  665. Window child;
  666. XWindowAttributes xwa;
  667. XTranslateCoordinates(disp, winId, XRootWindow(disp, 0), 0, 0, &x, &y, &child);
  668. XGetWindowAttributes(disp, winId, &xwa);
  669. XCloseDisplay(disp);
  670. pos[0] = x - xwa.x;
  671. pos[1] = y - xwa.y;
  672. }
  673. #endif
  674. else
  675. {
  676. pos[0] = 0;
  677. pos[1] = 0;
  678. }
  679. return pos;
  680. }
  681. int carla_cocoa_get_window(void* nsViewPtr)
  682. {
  683. CARLA_SAFE_ASSERT_RETURN(nsViewPtr != nullptr, 0);
  684. #ifdef CARLA_OS_MAC
  685. NSView* nsView = (NSView*)nsViewPtr;
  686. return [[nsView window] windowNumber];
  687. #else
  688. return 0;
  689. #endif
  690. }
  691. // -------------------------------------------------------------------------------------------------------------------
  692. #include "CarlaPipeUtils.cpp"
  693. #if !(defined(DEBUG) || defined(BUILDING_CARLA_FOR_WINDOWS))
  694. # include "water/misc/Time.cpp"
  695. #endif
  696. // -------------------------------------------------------------------------------------------------------------------
  697. #endif // CARLA_UTILS_CACHED_PLUGINS_ONLY