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.5KB

9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  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. {
  37. carla_debug("CarlaBridgeUI::CarlaBridgeUI()");
  38. try {
  39. fToolkit = CarlaBridgeToolkit::createNew(this);
  40. } CARLA_SAFE_EXCEPTION_RETURN("CarlaBridgeToolkit::createNew",);
  41. }
  42. CarlaBridgeUI::~CarlaBridgeUI() /*noexcept*/
  43. {
  44. carla_debug("CarlaBridgeUI::~CarlaBridgeUI()");
  45. if (fLib != nullptr)
  46. {
  47. lib_close(fLib);
  48. fLib = nullptr;
  49. }
  50. if (isPipeRunning() && ! fQuitReceived)
  51. {
  52. const CarlaMutexLocker cml(getPipeLock());
  53. writeMessage("exiting\n", 8);
  54. flushMessages();
  55. }
  56. if (fToolkit != nullptr)
  57. {
  58. fToolkit->quit();
  59. delete fToolkit;
  60. fToolkit = nullptr;
  61. }
  62. closePipeClient();
  63. }
  64. // ---------------------------------------------------------------------
  65. bool CarlaBridgeUI::libOpen(const char* const filename) noexcept
  66. {
  67. CARLA_SAFE_ASSERT_RETURN(fLib == nullptr, false);
  68. fLib = lib_open(filename);
  69. if (fLib != nullptr)
  70. {
  71. fLibFilename = filename;
  72. return true;
  73. }
  74. return false;
  75. }
  76. void* CarlaBridgeUI::libSymbol(const char* const symbol) const noexcept
  77. {
  78. CARLA_SAFE_ASSERT_RETURN(fLib != nullptr, nullptr);
  79. return lib_symbol<void*>(fLib, symbol);
  80. }
  81. const char* CarlaBridgeUI::libError() const noexcept
  82. {
  83. CARLA_SAFE_ASSERT_RETURN(fLibFilename.isNotEmpty(), nullptr);
  84. return lib_error(fLibFilename);
  85. }
  86. // ---------------------------------------------------------------------
  87. bool CarlaBridgeUI::msgReceived(const char* const msg) noexcept
  88. {
  89. carla_debug("CarlaBridgeUI::msgReceived(\"%s\")", msg);
  90. if (! fGotOptions) {
  91. CARLA_SAFE_ASSERT_RETURN(std::strcmp(msg, "urid") == 0 || std::strcmp(msg, "uiOptions") == 0, 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. }
  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), true);
  147. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(base64atom));
  148. delete[] base64atom;
  149. CARLA_SAFE_ASSERT_RETURN(chunk.size() >= sizeof(LV2_Atom), true);
  150. const LV2_Atom* const atom((const LV2_Atom*)chunk.data());
  151. const uint32_t atomTotalSizeCheck(lv2_atom_total_size(atom));
  152. CARLA_SAFE_ASSERT_RETURN(atomTotalSizeCheck == atomTotalSize, true);
  153. CARLA_SAFE_ASSERT_RETURN(atomTotalSizeCheck == chunk.size(), true);
  154. dspAtomReceived(index, atom);
  155. return true;
  156. }
  157. if (std::strcmp(msg, "urid") == 0)
  158. {
  159. uint32_t urid;
  160. const char* uri;
  161. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(urid), true);
  162. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(uri), true);
  163. if (urid != 0)
  164. dspURIDReceived(urid, uri);
  165. delete[] uri;
  166. return true;
  167. }
  168. if (std::strcmp(msg, "uiOptions") == 0)
  169. {
  170. double sampleRate;
  171. bool useTheme, useThemeColors;
  172. const char* windowTitle;
  173. uint64_t transientWindowId;
  174. CARLA_SAFE_ASSERT_RETURN(readNextLineAsDouble(sampleRate), true);
  175. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(useTheme), true);
  176. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(useThemeColors), true);
  177. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(windowTitle), true);
  178. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(transientWindowId), true);
  179. fGotOptions = true;
  180. uiOptionsChanged(sampleRate, useTheme, useThemeColors, windowTitle, static_cast<uintptr_t>(transientWindowId));
  181. delete[] windowTitle;
  182. return true;
  183. }
  184. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr, true);
  185. if (std::strcmp(msg, "show") == 0)
  186. {
  187. fToolkit->show();
  188. return true;
  189. }
  190. if (std::strcmp(msg, "focus") == 0)
  191. {
  192. fToolkit->focus();
  193. return true;
  194. }
  195. if (std::strcmp(msg, "hide") == 0)
  196. {
  197. fToolkit->hide();
  198. return true;
  199. }
  200. if (std::strcmp(msg, "quit") == 0)
  201. {
  202. fQuitReceived = true;
  203. fToolkit->quit();
  204. delete fToolkit;
  205. fToolkit = nullptr;
  206. return true;
  207. }
  208. if (std::strcmp(msg, "uiTitle") == 0)
  209. {
  210. const char* title;
  211. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(title), true);
  212. fToolkit->setTitle(title);
  213. delete[] title;
  214. return true;
  215. }
  216. carla_stderr("CarlaBridgeUI::msgReceived : %s", msg);
  217. return false;
  218. }
  219. // ---------------------------------------------------------------------
  220. bool CarlaBridgeUI::init(const int argc, const char* argv[])
  221. {
  222. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr, false);
  223. if (argc == 7)
  224. {
  225. if (! initPipeClient(argv))
  226. return false;
  227. fLastMsgTimer = 0;
  228. // wait for ui options
  229. for (; ++fLastMsgTimer < 50 && ! fGotOptions;)
  230. {
  231. idlePipe(true);
  232. carla_msleep(20);
  233. }
  234. if (! fGotOptions)
  235. {
  236. carla_stderr2("CarlaBridgeUI::init() - did not get options on time, quitting...");
  237. {
  238. const CarlaMutexLocker cml(getPipeLock());
  239. writeMessage("exiting\n", 8);
  240. flushMessages();
  241. }
  242. closePipeClient();
  243. return false;
  244. }
  245. }
  246. if (! fToolkit->init(argc, argv))
  247. {
  248. if (argc == 7)
  249. closePipeClient();
  250. return false;
  251. }
  252. return true;
  253. }
  254. void CarlaBridgeUI::exec(const bool showUI)
  255. {
  256. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr,);
  257. fToolkit->exec(showUI);
  258. }
  259. // ---------------------------------------------------------------------
  260. CARLA_BRIDGE_END_NAMESPACE
  261. #include "CarlaPipeUtils.cpp"
  262. // ---------------------------------------------------------------------