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.

343 lines
8.8KB

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