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.

359 lines
9.7KB

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