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.

CarlaBridgeFormat.cpp 9.0KB

9 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Carla Bridge UI
  3. * Copyright (C) 2011-2017 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 "CarlaMIDI.h"
  21. // FIXME move this into utils
  22. #ifdef CARLA_OS_LINUX
  23. # include <signal.h>
  24. # include <sys/prctl.h>
  25. #endif
  26. // needed for atom-util
  27. #ifndef nullptr
  28. # undef NULL
  29. # define NULL nullptr
  30. #endif
  31. #include "lv2/atom-util.h"
  32. CARLA_BRIDGE_UI_START_NAMESPACE
  33. // ---------------------------------------------------------------------
  34. CarlaBridgeFormat::CarlaBridgeFormat() noexcept
  35. : CarlaPipeClient(),
  36. fQuitReceived(false),
  37. fGotOptions(false),
  38. fLastMsgTimer(-1),
  39. fToolkit(nullptr),
  40. fLib(nullptr),
  41. fLibFilename()
  42. {
  43. carla_debug("CarlaBridgeFormat::CarlaBridgeFormat()");
  44. try {
  45. fToolkit = CarlaBridgeToolkit::createNew(this);
  46. } CARLA_SAFE_EXCEPTION_RETURN("CarlaBridgeToolkit::createNew",);
  47. }
  48. CarlaBridgeFormat::~CarlaBridgeFormat() /*noexcept*/
  49. {
  50. carla_debug("CarlaBridgeFormat::~CarlaBridgeFormat()");
  51. if (isPipeRunning() && ! fQuitReceived)
  52. writeExitingMessageAndWait();
  53. if (fLib != nullptr)
  54. {
  55. lib_close(fLib);
  56. fLib = nullptr;
  57. }
  58. if (fToolkit != nullptr)
  59. {
  60. fToolkit->quit();
  61. delete fToolkit;
  62. fToolkit = nullptr;
  63. }
  64. closePipeClient();
  65. }
  66. // ---------------------------------------------------------------------
  67. bool CarlaBridgeFormat::libOpen(const char* const filename) noexcept
  68. {
  69. CARLA_SAFE_ASSERT_RETURN(fLib == nullptr, false);
  70. fLib = lib_open(filename);
  71. if (fLib != nullptr)
  72. {
  73. fLibFilename = filename;
  74. return true;
  75. }
  76. return false;
  77. }
  78. void* CarlaBridgeFormat::libSymbol(const char* const symbol) const noexcept
  79. {
  80. CARLA_SAFE_ASSERT_RETURN(fLib != nullptr, nullptr);
  81. return lib_symbol<void*>(fLib, symbol);
  82. }
  83. const char* CarlaBridgeFormat::libError() const noexcept
  84. {
  85. CARLA_SAFE_ASSERT_RETURN(fLibFilename.isNotEmpty(), nullptr);
  86. return lib_error(fLibFilename);
  87. }
  88. // ---------------------------------------------------------------------
  89. bool CarlaBridgeFormat::msgReceived(const char* const msg) noexcept
  90. {
  91. carla_debug("CarlaBridgeFormat::msgReceived(\"%s\")", msg);
  92. if (! fGotOptions && std::strcmp(msg, "urid") != 0 && std::strcmp(msg, "uiOptions") != 0)
  93. {
  94. carla_stderr2("CarlaBridgeFormat::msgReceived(\"%s\") - invalid message while waiting for options", msg);
  95. return true;
  96. }
  97. if (fLastMsgTimer > 0)
  98. --fLastMsgTimer;
  99. if (std::strcmp(msg, "control") == 0)
  100. {
  101. uint32_t index;
  102. float value;
  103. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  104. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  105. dspParameterChanged(index, value);
  106. return true;
  107. }
  108. if (std::strcmp(msg, "program") == 0)
  109. {
  110. uint32_t index;
  111. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  112. dspProgramChanged(index);
  113. return true;
  114. }
  115. if (std::strcmp(msg, "midiprogram") == 0)
  116. {
  117. uint32_t bank, program;
  118. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(bank), true);
  119. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(program), true);
  120. dspMidiProgramChanged(bank, program);
  121. return true;
  122. }
  123. if (std::strcmp(msg, "configure") == 0)
  124. {
  125. const char* key;
  126. const char* value;
  127. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(key), true);
  128. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(value), true);
  129. dspStateChanged(key, value);
  130. delete[] key;
  131. delete[] value;
  132. return true;
  133. }
  134. if (std::strcmp(msg, "note") == 0)
  135. {
  136. bool onOff;
  137. uint8_t channel, note, velocity;
  138. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(onOff), true);
  139. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(channel), true);
  140. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(note), true);
  141. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(velocity), true);
  142. dspNoteReceived(onOff, channel, note, velocity);
  143. return true;
  144. }
  145. if (std::strcmp(msg, "atom") == 0)
  146. {
  147. uint32_t index, atomTotalSize;
  148. const char* base64atom;
  149. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  150. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(atomTotalSize), true);
  151. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(base64atom), true);
  152. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(base64atom));
  153. delete[] base64atom;
  154. CARLA_SAFE_ASSERT_RETURN(chunk.size() >= sizeof(LV2_Atom), true);
  155. #ifdef CARLA_PROPER_CPP11_SUPPORT
  156. const LV2_Atom* const atom((const LV2_Atom*)chunk.data());
  157. #else
  158. const LV2_Atom* const atom((const LV2_Atom*)&chunk.front());
  159. #endif
  160. const uint32_t atomTotalSizeCheck(lv2_atom_total_size(atom));
  161. CARLA_SAFE_ASSERT_RETURN(atomTotalSizeCheck == atomTotalSize, true);
  162. CARLA_SAFE_ASSERT_RETURN(atomTotalSizeCheck == chunk.size(), true);
  163. dspAtomReceived(index, atom);
  164. return true;
  165. }
  166. if (std::strcmp(msg, "urid") == 0)
  167. {
  168. uint32_t urid;
  169. const char* uri;
  170. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(urid), true);
  171. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(uri), true);
  172. if (urid != 0)
  173. dspURIDReceived(urid, uri);
  174. delete[] uri;
  175. return true;
  176. }
  177. if (std::strcmp(msg, "uiOptions") == 0)
  178. {
  179. double sampleRate;
  180. bool useTheme, useThemeColors;
  181. const char* windowTitle;
  182. uint64_t transientWindowId;
  183. CARLA_SAFE_ASSERT_RETURN(readNextLineAsDouble(sampleRate), true);
  184. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(useTheme), true);
  185. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(useThemeColors), true);
  186. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(windowTitle), true);
  187. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(transientWindowId), true);
  188. fGotOptions = true;
  189. uiOptionsChanged(sampleRate, useTheme, useThemeColors, 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. #ifdef CARLA_OS_LINUX
  263. // kill ourselves if main carla dies
  264. ::prctl(PR_SET_PDEATHSIG, SIGKILL);
  265. // TODO, osx version too, see https://stackoverflow.com/questions/284325/how-to-make-child-process-die-after-parent-exits
  266. #endif
  267. fToolkit->exec(showUI);
  268. }
  269. // ---------------------------------------------------------------------
  270. CARLA_BRIDGE_UI_END_NAMESPACE
  271. #include "CarlaPipeUtils.cpp"
  272. // ---------------------------------------------------------------------