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.

364 lines
9.7KB

  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, "parameter") == 0)
  140. {
  141. const char* uri;
  142. float value;
  143. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(uri, true), true);
  144. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  145. dspParameterChanged(uri, value);
  146. return true;
  147. }
  148. if (std::strcmp(msg, "program") == 0)
  149. {
  150. uint32_t index;
  151. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  152. dspProgramChanged(index);
  153. return true;
  154. }
  155. if (std::strcmp(msg, "midiprogram") == 0)
  156. {
  157. uint32_t bank, program;
  158. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(bank), true);
  159. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(program), true);
  160. dspMidiProgramChanged(bank, program);
  161. return true;
  162. }
  163. if (std::strcmp(msg, "configure") == 0)
  164. {
  165. const char* key;
  166. const char* value;
  167. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(key, true), true);
  168. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(value, false), true);
  169. dspStateChanged(key, value);
  170. delete[] key;
  171. return true;
  172. }
  173. if (std::strcmp(msg, "note") == 0)
  174. {
  175. bool onOff;
  176. uint8_t channel, note, velocity;
  177. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(onOff), true);
  178. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(channel), true);
  179. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(note), true);
  180. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(velocity), true);
  181. dspNoteReceived(onOff, channel, note, velocity);
  182. return true;
  183. }
  184. if (std::strcmp(msg, "uiOptions") == 0)
  185. {
  186. BridgeFormatOptions opts;
  187. uint64_t transientWindowId;
  188. CARLA_SAFE_ASSERT_RETURN(readNextLineAsDouble(opts.sampleRate), true);
  189. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(opts.bgColor), true);
  190. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(opts.fgColor), true);
  191. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(opts.uiScale), true);
  192. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(opts.useTheme), true);
  193. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(opts.useThemeColors), true);
  194. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(opts.windowTitle, true), true);
  195. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(transientWindowId), true);
  196. opts.transientWindowId = transientWindowId;
  197. // we can assume we are not standalone if we got options from controller side
  198. opts.isStandalone = true;
  199. fGotOptions = true;
  200. uiOptionsChanged(opts);
  201. delete[] opts.windowTitle;
  202. return true;
  203. }
  204. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr, true);
  205. if (std::strcmp(msg, "show") == 0)
  206. {
  207. fToolkit->show();
  208. fToolkit->focus();
  209. return true;
  210. }
  211. if (std::strcmp(msg, "focus") == 0)
  212. {
  213. fToolkit->focus();
  214. return true;
  215. }
  216. if (std::strcmp(msg, "hide") == 0)
  217. {
  218. fToolkit->hide();
  219. return true;
  220. }
  221. if (std::strcmp(msg, "quit") == 0)
  222. {
  223. fQuitReceived = true;
  224. fToolkit->quit();
  225. delete fToolkit;
  226. fToolkit = nullptr;
  227. return true;
  228. }
  229. if (std::strcmp(msg, "uiTitle") == 0)
  230. {
  231. const char* title;
  232. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(title, false), true);
  233. fToolkit->setTitle(title);
  234. return true;
  235. }
  236. carla_stderr("CarlaBridgeFormat::msgReceived : %s", msg);
  237. return false;
  238. }
  239. // ---------------------------------------------------------------------
  240. bool CarlaBridgeFormat::init(const int argc, const char* argv[])
  241. {
  242. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr, false);
  243. if (argc == 7)
  244. {
  245. if (! initPipeClient(argv))
  246. return false;
  247. fLastMsgTimer = 0;
  248. // wait for ui options
  249. for (; ++fLastMsgTimer < 50 && ! fGotOptions;)
  250. {
  251. idlePipe(true);
  252. carla_msleep(20);
  253. }
  254. if (! fGotOptions)
  255. {
  256. carla_stderr2("CarlaBridgeFormat::init() - did not get options on time, quitting...");
  257. writeExitingMessageAndWait();
  258. closePipeClient();
  259. return false;
  260. }
  261. }
  262. if (! fToolkit->init(argc, argv))
  263. {
  264. if (argc == 7)
  265. closePipeClient();
  266. return false;
  267. }
  268. return true;
  269. }
  270. void CarlaBridgeFormat::exec(const bool showUI)
  271. {
  272. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr,);
  273. carla_terminateProcessOnParentExit(true);
  274. fToolkit->exec(showUI);
  275. }
  276. // ---------------------------------------------------------------------
  277. CARLA_BRIDGE_UI_END_NAMESPACE
  278. #include "CarlaPipeUtils.cpp"
  279. // ---------------------------------------------------------------------