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.

350 lines
8.9KB

  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);
  124. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(value), true);
  125. dspStateChanged(key, value);
  126. delete[] key;
  127. delete[] value;
  128. return true;
  129. }
  130. if (std::strcmp(msg, "note") == 0)
  131. {
  132. bool onOff;
  133. uint8_t channel, note, velocity;
  134. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(onOff), true);
  135. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(channel), true);
  136. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(note), true);
  137. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(velocity), true);
  138. dspNoteReceived(onOff, channel, note, velocity);
  139. return true;
  140. }
  141. if (std::strcmp(msg, "atom") == 0)
  142. {
  143. uint32_t index, atomTotalSize;
  144. const char* base64atom;
  145. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  146. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(atomTotalSize), true);
  147. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(base64atom), true);
  148. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(base64atom));
  149. delete[] base64atom;
  150. CARLA_SAFE_ASSERT_RETURN(chunk.size() >= sizeof(LV2_Atom), true);
  151. #ifdef CARLA_PROPER_CPP11_SUPPORT
  152. const LV2_Atom* const atom((const LV2_Atom*)chunk.data());
  153. #else
  154. const LV2_Atom* const atom((const LV2_Atom*)&chunk.front());
  155. #endif
  156. const uint32_t atomTotalSizeCheck(lv2_atom_total_size(atom));
  157. CARLA_SAFE_ASSERT_RETURN(atomTotalSizeCheck == atomTotalSize, true);
  158. CARLA_SAFE_ASSERT_RETURN(atomTotalSizeCheck == chunk.size(), true);
  159. dspAtomReceived(index, atom);
  160. return true;
  161. }
  162. if (std::strcmp(msg, "urid") == 0)
  163. {
  164. uint32_t urid;
  165. const char* uri;
  166. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(urid), true);
  167. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(uri), true);
  168. if (urid != 0)
  169. dspURIDReceived(urid, uri);
  170. delete[] uri;
  171. return true;
  172. }
  173. if (std::strcmp(msg, "uiOptions") == 0)
  174. {
  175. double sampleRate;
  176. bool useTheme, useThemeColors;
  177. float uiScale;
  178. const char* windowTitle;
  179. uint64_t transientWindowId;
  180. CARLA_SAFE_ASSERT_RETURN(readNextLineAsDouble(sampleRate), true);
  181. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(uiScale), true);
  182. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(useTheme), true);
  183. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(useThemeColors), true);
  184. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(windowTitle), true);
  185. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(transientWindowId), true);
  186. fGotOptions = true;
  187. uiOptionsChanged(sampleRate, uiScale,
  188. useTheme, useThemeColors,
  189. windowTitle, static_cast<uintptr_t>(transientWindowId));
  190. delete[] 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. return true;
  198. }
  199. if (std::strcmp(msg, "focus") == 0)
  200. {
  201. fToolkit->focus();
  202. return true;
  203. }
  204. if (std::strcmp(msg, "hide") == 0)
  205. {
  206. fToolkit->hide();
  207. return true;
  208. }
  209. if (std::strcmp(msg, "quit") == 0)
  210. {
  211. fQuitReceived = true;
  212. fToolkit->quit();
  213. delete fToolkit;
  214. fToolkit = nullptr;
  215. return true;
  216. }
  217. if (std::strcmp(msg, "uiTitle") == 0)
  218. {
  219. const char* title;
  220. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(title), true);
  221. fToolkit->setTitle(title);
  222. delete[] 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. // ---------------------------------------------------------------------