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.

CarlaBridgeUI.cpp 8.4KB

9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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. // needed for atom-util
  21. #ifndef nullptr
  22. # undef NULL
  23. # define NULL nullptr
  24. #endif
  25. #include "lv2/atom-util.h"
  26. CARLA_BRIDGE_START_NAMESPACE
  27. // ---------------------------------------------------------------------
  28. CarlaBridgeUI::CarlaBridgeUI() noexcept
  29. : CarlaPipeClient(),
  30. fQuitReceived(false),
  31. fGotOptions(false),
  32. fLastMsgTimer(-1),
  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. carla_debug("CarlaBridgeUI::msgReceived(\"%s\")", msg);
  91. if (! fGotOptions) {
  92. CARLA_SAFE_ASSERT_RETURN(std::strcmp(msg, "urid") == 0 || std::strcmp(msg, "uiOptions") == 0, true);
  93. }
  94. if (fLastMsgTimer > 0)
  95. --fLastMsgTimer;
  96. if (std::strcmp(msg, "control") == 0)
  97. {
  98. uint32_t index;
  99. float value;
  100. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  101. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  102. dspParameterChanged(index, value);
  103. return true;
  104. }
  105. if (std::strcmp(msg, "program") == 0)
  106. {
  107. uint32_t index;
  108. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  109. dspProgramChanged(index);
  110. return true;
  111. }
  112. if (std::strcmp(msg, "midiprogram") == 0)
  113. {
  114. uint32_t bank, program;
  115. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(bank), true);
  116. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(program), true);
  117. dspMidiProgramChanged(bank, program);
  118. return true;
  119. }
  120. if (std::strcmp(msg, "configure") == 0)
  121. {
  122. const char* key;
  123. const char* value;
  124. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(key), true);
  125. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(value), true);
  126. dspStateChanged(key, value);
  127. delete[] key;
  128. delete[] value;
  129. return true;
  130. }
  131. if (std::strcmp(msg, "note") == 0)
  132. {
  133. bool onOff;
  134. uint8_t channel, note, velocity;
  135. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(onOff), true);
  136. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(channel), true);
  137. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(note), true);
  138. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(velocity), true);
  139. dspNoteReceived(onOff, channel, note, velocity);
  140. }
  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. dspURIDReceived(urid, uri);
  164. delete[] uri;
  165. return true;
  166. }
  167. if (std::strcmp(msg, "uiOptions") == 0)
  168. {
  169. double sampleRate;
  170. bool useTheme, useThemeColors;
  171. const char* windowTitle;
  172. uint64_t transientWindowId;
  173. CARLA_SAFE_ASSERT_RETURN(readNextLineAsDouble(sampleRate), true);
  174. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(useTheme), true);
  175. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(useThemeColors), true);
  176. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(windowTitle), true);
  177. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(transientWindowId), true);
  178. fGotOptions = true;
  179. uiOptionsChanged(sampleRate, useTheme, useThemeColors, windowTitle, static_cast<uintptr_t>(transientWindowId));
  180. delete[] windowTitle;
  181. return true;
  182. }
  183. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr, true);
  184. if (std::strcmp(msg, "show") == 0)
  185. {
  186. fToolkit->show();
  187. return true;
  188. }
  189. if (std::strcmp(msg, "focus") == 0)
  190. {
  191. fToolkit->focus();
  192. return true;
  193. }
  194. if (std::strcmp(msg, "hide") == 0)
  195. {
  196. fToolkit->hide();
  197. return true;
  198. }
  199. if (std::strcmp(msg, "quit") == 0)
  200. {
  201. fQuitReceived = true;
  202. fToolkit->quit();
  203. delete fToolkit;
  204. fToolkit = nullptr;
  205. return true;
  206. }
  207. if (std::strcmp(msg, "uiTitle") == 0)
  208. {
  209. const char* title;
  210. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(title), true);
  211. fToolkit->setTitle(title);
  212. delete[] title;
  213. return true;
  214. }
  215. carla_stderr("CarlaBridgeUI::msgReceived : %s", msg);
  216. return false;
  217. }
  218. // ---------------------------------------------------------------------
  219. bool CarlaBridgeUI::init(const int argc, const char* argv[])
  220. {
  221. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr, false);
  222. if (argc == 7)
  223. {
  224. if (! initPipeClient(argv))
  225. return false;
  226. fLastMsgTimer = 0;
  227. // wait for ui options
  228. for (; ++fLastMsgTimer < 50 && ! fGotOptions;)
  229. {
  230. idlePipe(true);
  231. carla_msleep(20);
  232. }
  233. if (! fGotOptions)
  234. {
  235. carla_stderr2("CarlaBridgeUI::init() - did not get options on time, quitting...");
  236. {
  237. const CarlaMutexLocker cml(getPipeLock());
  238. writeMessage("exiting\n", 8);
  239. flushMessages();
  240. }
  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 CarlaBridgeUI::exec(const bool showUI)
  254. {
  255. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr,);
  256. fToolkit->exec(showUI);
  257. }
  258. // ---------------------------------------------------------------------
  259. CARLA_BRIDGE_END_NAMESPACE
  260. #include "CarlaPipeUtils.cpp"
  261. // ---------------------------------------------------------------------