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.

328 lines
7.6KB

  1. /*
  2. * Carla Bridge UI
  3. * Copyright (C) 2011-2014 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 "CarlaBridgeUI.hpp"
  18. #include "CarlaMIDI.h"
  19. #include "CarlaBase64Utils.hpp"
  20. #ifdef BRIDGE_LV2
  21. # undef NULL
  22. # define NULL nullptr
  23. # include "lv2/atom-util.h"
  24. #endif
  25. CARLA_BRIDGE_START_NAMESPACE
  26. // ---------------------------------------------------------------------
  27. CarlaBridgeUI::CarlaBridgeUI() noexcept
  28. : CarlaPipeClient(),
  29. fQuitReceived(false),
  30. #ifdef BRIDGE_LV2
  31. fUridMapComplete(false),
  32. #endif
  33. fToolkit(nullptr),
  34. fLib(nullptr),
  35. fLibFilename(),
  36. leakDetector_CarlaBridgeUI()
  37. {
  38. carla_debug("CarlaBridgeUI::CarlaBridgeUI()");
  39. try {
  40. fToolkit = CarlaBridgeToolkit::createNew(this);
  41. } CARLA_SAFE_EXCEPTION_RETURN("CarlaBridgeToolkit::createNew",);
  42. }
  43. CarlaBridgeUI::~CarlaBridgeUI() /*noexcept*/
  44. {
  45. carla_debug("CarlaBridgeUI::~CarlaBridgeUI()");
  46. if (fLib != nullptr)
  47. {
  48. lib_close(fLib);
  49. fLib = nullptr;
  50. }
  51. if (isPipeRunning() && ! fQuitReceived)
  52. {
  53. const CarlaMutexLocker cml(getPipeLock());
  54. writeMessage("exiting\n", 8);
  55. flushMessages();
  56. }
  57. if (fToolkit != nullptr)
  58. {
  59. fToolkit->quit();
  60. delete fToolkit;
  61. fToolkit = nullptr;
  62. }
  63. closePipeClient();
  64. }
  65. // ---------------------------------------------------------------------
  66. bool CarlaBridgeUI::libOpen(const char* const filename) noexcept
  67. {
  68. CARLA_SAFE_ASSERT_RETURN(fLib == nullptr, false);
  69. fLib = lib_open(filename);
  70. if (fLib != nullptr)
  71. {
  72. fLibFilename = filename;
  73. return true;
  74. }
  75. return false;
  76. }
  77. void* CarlaBridgeUI::libSymbol(const char* const symbol) const noexcept
  78. {
  79. CARLA_SAFE_ASSERT_RETURN(fLib != nullptr, nullptr);
  80. return lib_symbol<void*>(fLib, symbol);
  81. }
  82. const char* CarlaBridgeUI::libError() const noexcept
  83. {
  84. CARLA_SAFE_ASSERT_RETURN(fLibFilename.isNotEmpty(), nullptr);
  85. return lib_error(fLibFilename);
  86. }
  87. // ---------------------------------------------------------------------
  88. bool CarlaBridgeUI::msgReceived(const char* const msg) noexcept
  89. {
  90. #ifdef BRIDGE_LV2
  91. if (! fUridMapComplete) {
  92. CARLA_SAFE_ASSERT_RETURN(std::strcmp(msg, "urid") == 0, true);
  93. }
  94. #endif
  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, "mprogram") == 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. }
  140. #ifdef BRIDGE_LV2
  141. if (std::strcmp(msg, "atom") == 0)
  142. {
  143. uint32_t index, size;
  144. const char* base64atom;
  145. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  146. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(size), 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. const LV2_Atom* const atom((const LV2_Atom*)chunk.data());
  152. CARLA_SAFE_ASSERT_RETURN(lv2_atom_total_size(atom) == chunk.size(), true);
  153. dspAtomReceived(index, atom);
  154. return true;
  155. }
  156. if (std::strcmp(msg, "urid") == 0)
  157. {
  158. uint32_t urid;
  159. const char* uri;
  160. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(urid), true);
  161. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(uri), true);
  162. if (urid == 0)
  163. {
  164. CARLA_SAFE_ASSERT_RETURN(std::strcmp(uri, "Complete") == 0, true);
  165. fUridMapComplete = true;
  166. }
  167. else
  168. {
  169. dspURIDReceived(urid, uri);
  170. }
  171. delete[] uri;
  172. return true;
  173. }
  174. #endif
  175. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr, true);
  176. if (std::strcmp(msg, "show") == 0)
  177. {
  178. fToolkit->show();
  179. return true;
  180. }
  181. if (std::strcmp(msg, "focus") == 0)
  182. {
  183. fToolkit->focus();
  184. return true;
  185. }
  186. if (std::strcmp(msg, "hide") == 0)
  187. {
  188. fToolkit->hide();
  189. return true;
  190. }
  191. if (std::strcmp(msg, "quit") == 0)
  192. {
  193. fQuitReceived = true;
  194. fToolkit->quit();
  195. delete fToolkit;
  196. fToolkit = nullptr;
  197. return true;
  198. }
  199. if (std::strcmp(msg, "uiTitle") == 0)
  200. {
  201. const char* title;
  202. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(title), true);
  203. fToolkit->setTitle(title);
  204. delete[] title;
  205. return true;
  206. }
  207. carla_stderr("CarlaBridgeUI::msgReceived : %s", msg);
  208. return false;
  209. }
  210. // ---------------------------------------------------------------------
  211. bool CarlaBridgeUI::init(const int argc, const char* argv[])
  212. {
  213. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr, false);
  214. if (! fToolkit->init(argc, argv))
  215. return false;
  216. if (argc == 7)
  217. {
  218. if (! initPipeClient(argv))
  219. {
  220. fToolkit->quit();
  221. delete fToolkit;
  222. fToolkit = nullptr;
  223. return false;
  224. }
  225. #ifdef BRIDGE_LV2
  226. // wait for URID map to complete
  227. for (int i=0; i<20 && ! fUridMapComplete; ++i)
  228. {
  229. idlePipe();
  230. carla_msleep(100);
  231. }
  232. if (! fUridMapComplete)
  233. {
  234. fToolkit->quit();
  235. delete fToolkit;
  236. fToolkit = nullptr;
  237. closePipeClient();
  238. }
  239. #endif
  240. }
  241. else
  242. {
  243. // no mapping needed
  244. fUridMapComplete = true;
  245. }
  246. return true;
  247. }
  248. void CarlaBridgeUI::exec(const bool showUI)
  249. {
  250. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr,);
  251. fToolkit->exec(showUI);
  252. }
  253. // ---------------------------------------------------------------------
  254. CARLA_BRIDGE_END_NAMESPACE
  255. #include "CarlaPipeUtils.cpp"
  256. // ---------------------------------------------------------------------