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.

346 lines
8.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. {
  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. const char* windowTitle;
  178. uint64_t transientWindowId;
  179. CARLA_SAFE_ASSERT_RETURN(readNextLineAsDouble(sampleRate), true);
  180. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(useTheme), true);
  181. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(useThemeColors), true);
  182. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(windowTitle), true);
  183. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(transientWindowId), true);
  184. fGotOptions = true;
  185. uiOptionsChanged(sampleRate, useTheme, useThemeColors, windowTitle, static_cast<uintptr_t>(transientWindowId));
  186. delete[] windowTitle;
  187. return true;
  188. }
  189. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr, true);
  190. if (std::strcmp(msg, "show") == 0)
  191. {
  192. fToolkit->show();
  193. return true;
  194. }
  195. if (std::strcmp(msg, "focus") == 0)
  196. {
  197. fToolkit->focus();
  198. return true;
  199. }
  200. if (std::strcmp(msg, "hide") == 0)
  201. {
  202. fToolkit->hide();
  203. return true;
  204. }
  205. if (std::strcmp(msg, "quit") == 0)
  206. {
  207. fQuitReceived = true;
  208. fToolkit->quit();
  209. delete fToolkit;
  210. fToolkit = nullptr;
  211. return true;
  212. }
  213. if (std::strcmp(msg, "uiTitle") == 0)
  214. {
  215. const char* title;
  216. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(title), true);
  217. fToolkit->setTitle(title);
  218. delete[] title;
  219. return true;
  220. }
  221. carla_stderr("CarlaBridgeFormat::msgReceived : %s", msg);
  222. return false;
  223. }
  224. // ---------------------------------------------------------------------
  225. bool CarlaBridgeFormat::init(const int argc, const char* argv[])
  226. {
  227. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr, false);
  228. if (argc == 7)
  229. {
  230. if (! initPipeClient(argv))
  231. return false;
  232. fLastMsgTimer = 0;
  233. // wait for ui options
  234. for (; ++fLastMsgTimer < 50 && ! fGotOptions;)
  235. {
  236. idlePipe(true);
  237. carla_msleep(20);
  238. }
  239. if (! fGotOptions)
  240. {
  241. carla_stderr2("CarlaBridgeFormat::init() - did not get options on time, quitting...");
  242. writeExitingMessageAndWait();
  243. closePipeClient();
  244. return false;
  245. }
  246. }
  247. if (! fToolkit->init(argc, argv))
  248. {
  249. if (argc == 7)
  250. closePipeClient();
  251. return false;
  252. }
  253. return true;
  254. }
  255. void CarlaBridgeFormat::exec(const bool showUI)
  256. {
  257. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr,);
  258. carla_terminateProcessOnParentExit(true);
  259. fToolkit->exec(showUI);
  260. }
  261. // ---------------------------------------------------------------------
  262. CARLA_BRIDGE_UI_END_NAMESPACE
  263. #include "CarlaPipeUtils.cpp"
  264. // ---------------------------------------------------------------------