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.3KB

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