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.

CarlaBridgeFormat.cpp 9.7KB

9 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. /*
  2. * Carla Bridge UI
  3. * Copyright (C) 2011-2023 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 "CarlaBridgeFormat.hpp"
  18. #include "CarlaBridgeToolkit.hpp"
  19. #include "CarlaBase64Utils.hpp"
  20. #include "CarlaProcessUtils.hpp"
  21. #include "CarlaTimeUtils.hpp"
  22. #include "CarlaMIDI.h"
  23. // needed for atom-util
  24. #ifndef nullptr
  25. # undef NULL
  26. # define NULL nullptr
  27. #endif
  28. #include "lv2/atom-util.h"
  29. CARLA_BRIDGE_UI_START_NAMESPACE
  30. // ---------------------------------------------------------------------
  31. CarlaBridgeFormat::CarlaBridgeFormat() noexcept
  32. : CarlaPipeClient(),
  33. fQuitReceived(false),
  34. fGotOptions(false),
  35. fLastMsgTimer(-1),
  36. fToolkit(nullptr),
  37. fLib(nullptr),
  38. fLibFilename(),
  39. fBase64ReservedChunk()
  40. {
  41. carla_debug("CarlaBridgeFormat::CarlaBridgeFormat()");
  42. try {
  43. fToolkit = CarlaBridgeToolkit::createNew(this);
  44. } CARLA_SAFE_EXCEPTION_RETURN("CarlaBridgeToolkit::createNew",);
  45. }
  46. CarlaBridgeFormat::~CarlaBridgeFormat() /*noexcept*/
  47. {
  48. carla_debug("CarlaBridgeFormat::~CarlaBridgeFormat()");
  49. if (isPipeRunning() && ! fQuitReceived)
  50. writeExitingMessageAndWait();
  51. if (fLib != nullptr)
  52. {
  53. lib_close(fLib);
  54. fLib = nullptr;
  55. }
  56. if (fToolkit != nullptr)
  57. {
  58. fToolkit->quit();
  59. delete fToolkit;
  60. fToolkit = nullptr;
  61. }
  62. closePipeClient();
  63. }
  64. // ---------------------------------------------------------------------
  65. bool CarlaBridgeFormat::libOpen(const char* const filename) noexcept
  66. {
  67. CARLA_SAFE_ASSERT_RETURN(fLib == nullptr, false);
  68. fLib = lib_open(filename);
  69. fLibFilename = filename;
  70. return fLib != nullptr;
  71. }
  72. void* CarlaBridgeFormat::libSymbol(const char* const symbol) const noexcept
  73. {
  74. CARLA_SAFE_ASSERT_RETURN(fLib != nullptr, nullptr);
  75. return lib_symbol<void*>(fLib, symbol);
  76. }
  77. const char* CarlaBridgeFormat::libError() const noexcept
  78. {
  79. CARLA_SAFE_ASSERT_RETURN(fLibFilename.isNotEmpty(), nullptr);
  80. return lib_error(fLibFilename);
  81. }
  82. // ---------------------------------------------------------------------
  83. bool CarlaBridgeFormat::msgReceived(const char* const msg) noexcept
  84. {
  85. carla_debug("CarlaBridgeFormat::msgReceived(\"%s\")", msg);
  86. if (! fGotOptions && std::strcmp(msg, "urid") != 0 && std::strcmp(msg, "uiOptions") != 0)
  87. {
  88. carla_stderr2("CarlaBridgeFormat::msgReceived(\"%s\") - invalid message while waiting for options", msg);
  89. return true;
  90. }
  91. if (fLastMsgTimer > 0)
  92. --fLastMsgTimer;
  93. if (std::strcmp(msg, "atom") == 0)
  94. {
  95. uint32_t index, atomTotalSize, base64Size;
  96. const char* base64atom;
  97. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  98. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(atomTotalSize), true);
  99. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(base64Size), true);
  100. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(base64atom, false, base64Size), true);
  101. carla_getChunkFromBase64String_impl(fBase64ReservedChunk, base64atom);
  102. CARLA_SAFE_ASSERT_UINT2_RETURN(fBase64ReservedChunk.size() >= sizeof(LV2_Atom),
  103. fBase64ReservedChunk.size(), sizeof(LV2_Atom), true);
  104. #ifdef CARLA_PROPER_CPP11_SUPPORT
  105. const LV2_Atom* const atom((const LV2_Atom*)fBase64ReservedChunk.data());
  106. #else
  107. const LV2_Atom* const atom((const LV2_Atom*)&fBase64ReservedChunk.front());
  108. #endif
  109. const uint32_t atomTotalSizeCheck(lv2_atom_total_size(atom));
  110. CARLA_SAFE_ASSERT_UINT2_RETURN(atomTotalSizeCheck == atomTotalSize, atomTotalSizeCheck, atomTotalSize, true);
  111. CARLA_SAFE_ASSERT_UINT2_RETURN(atomTotalSizeCheck == fBase64ReservedChunk.size(),
  112. atomTotalSizeCheck, fBase64ReservedChunk.size(), true);
  113. dspAtomReceived(index, atom);
  114. return true;
  115. }
  116. if (std::strcmp(msg, "urid") == 0)
  117. {
  118. uint32_t urid, size;
  119. const char* uri;
  120. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(urid), true);
  121. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(size), true);
  122. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(uri, false, size), true);
  123. if (urid != 0)
  124. dspURIDReceived(urid, uri);
  125. return true;
  126. }
  127. if (std::strcmp(msg, "control") == 0)
  128. {
  129. uint32_t index;
  130. float value;
  131. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  132. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  133. dspParameterChanged(index, value);
  134. return true;
  135. }
  136. if (std::strcmp(msg, "parameter") == 0)
  137. {
  138. const char* uri;
  139. float value;
  140. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(uri, true), true);
  141. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  142. dspParameterChanged(uri, value);
  143. return true;
  144. }
  145. if (std::strcmp(msg, "program") == 0)
  146. {
  147. uint32_t index;
  148. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  149. dspProgramChanged(index);
  150. return true;
  151. }
  152. if (std::strcmp(msg, "midiprogram") == 0)
  153. {
  154. uint32_t bank, program;
  155. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(bank), true);
  156. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(program), true);
  157. dspMidiProgramChanged(bank, program);
  158. return true;
  159. }
  160. if (std::strcmp(msg, "configure") == 0)
  161. {
  162. const char* key;
  163. const char* value;
  164. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(key, true), true);
  165. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(value, false), true);
  166. dspStateChanged(key, value);
  167. delete[] key;
  168. return true;
  169. }
  170. if (std::strcmp(msg, "note") == 0)
  171. {
  172. bool onOff;
  173. uint8_t channel, note, velocity;
  174. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(onOff), true);
  175. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(channel), true);
  176. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(note), true);
  177. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(velocity), true);
  178. dspNoteReceived(onOff, channel, note, velocity);
  179. return true;
  180. }
  181. if (std::strcmp(msg, "uiOptions") == 0)
  182. {
  183. BridgeFormatOptions opts;
  184. uint64_t transientWindowId;
  185. CARLA_SAFE_ASSERT_RETURN(readNextLineAsDouble(opts.sampleRate), true);
  186. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(opts.bgColor), true);
  187. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(opts.fgColor), true);
  188. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(opts.uiScale), true);
  189. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(opts.useTheme), true);
  190. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(opts.useThemeColors), true);
  191. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(opts.windowTitle, true), true);
  192. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(transientWindowId), true);
  193. opts.transientWindowId = transientWindowId;
  194. // we can assume we are not standalone if we got options from controller side
  195. opts.isStandalone = false;
  196. fGotOptions = true;
  197. uiOptionsChanged(opts);
  198. delete[] opts.windowTitle;
  199. return true;
  200. }
  201. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr, true);
  202. if (std::strcmp(msg, "show") == 0)
  203. {
  204. fToolkit->show();
  205. fToolkit->focus();
  206. return true;
  207. }
  208. if (std::strcmp(msg, "focus") == 0)
  209. {
  210. fToolkit->focus();
  211. return true;
  212. }
  213. if (std::strcmp(msg, "hide") == 0)
  214. {
  215. fToolkit->hide();
  216. return true;
  217. }
  218. if (std::strcmp(msg, "quit") == 0)
  219. {
  220. fQuitReceived = true;
  221. fToolkit->quit();
  222. delete fToolkit;
  223. fToolkit = nullptr;
  224. return true;
  225. }
  226. if (std::strcmp(msg, "uiTitle") == 0)
  227. {
  228. const char* title;
  229. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(title, false), true);
  230. fToolkit->setTitle(title);
  231. return true;
  232. }
  233. carla_stderr("CarlaBridgeFormat::msgReceived : %s", msg);
  234. return false;
  235. }
  236. // ---------------------------------------------------------------------
  237. bool CarlaBridgeFormat::init(const int argc, const char* argv[])
  238. {
  239. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr, false);
  240. if (argc == 7)
  241. {
  242. if (! initPipeClient(argv))
  243. return false;
  244. fLastMsgTimer = 0;
  245. // wait for ui options
  246. for (; ++fLastMsgTimer < 50 && ! fGotOptions;)
  247. {
  248. idlePipe(true);
  249. carla_msleep(20);
  250. }
  251. if (! fGotOptions)
  252. {
  253. carla_stderr2("CarlaBridgeFormat::init() - did not get options on time, quitting...");
  254. writeExitingMessageAndWait();
  255. closePipeClient();
  256. return false;
  257. }
  258. }
  259. if (! fToolkit->init(argc, argv))
  260. {
  261. if (argc == 7)
  262. closePipeClient();
  263. return false;
  264. }
  265. return true;
  266. }
  267. void CarlaBridgeFormat::exec(const bool showUI)
  268. {
  269. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr,);
  270. carla_terminateProcessOnParentExit(true);
  271. fToolkit->exec(showUI);
  272. }
  273. // ---------------------------------------------------------------------
  274. CARLA_BRIDGE_UI_END_NAMESPACE
  275. #include "CarlaPipeUtils.cpp"
  276. // ---------------------------------------------------------------------