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.

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