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 9.1KB

9 years ago
10 years ago
10 years ago
10 years ago
10 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 CARLA_OS_LINUX
  21. # include <signal.h>
  22. # include <sys/prctl.h>
  23. #endif
  24. // needed for atom-util
  25. #ifndef nullptr
  26. # undef NULL
  27. # define NULL nullptr
  28. #endif
  29. #include "lv2/atom-util.h"
  30. CARLA_BRIDGE_START_NAMESPACE
  31. // ---------------------------------------------------------------------
  32. CarlaBridgeUI::CarlaBridgeUI() noexcept
  33. : CarlaPipeClient(),
  34. fQuitReceived(false),
  35. fGotOptions(false),
  36. fLastMsgTimer(-1),
  37. fToolkit(nullptr),
  38. fLib(nullptr),
  39. fLibFilename()
  40. {
  41. carla_debug("CarlaBridgeUI::CarlaBridgeUI()");
  42. try {
  43. fToolkit = CarlaBridgeToolkit::createNew(this);
  44. } CARLA_SAFE_EXCEPTION_RETURN("CarlaBridgeToolkit::createNew",);
  45. }
  46. CarlaBridgeUI::~CarlaBridgeUI() /*noexcept*/
  47. {
  48. carla_debug("CarlaBridgeUI::~CarlaBridgeUI()");
  49. if (fLib != nullptr)
  50. {
  51. lib_close(fLib);
  52. fLib = nullptr;
  53. }
  54. if (isPipeRunning() && ! fQuitReceived)
  55. {
  56. const CarlaMutexLocker cml(getPipeLock());
  57. writeMessage("exiting\n", 8);
  58. flushMessages();
  59. }
  60. if (fToolkit != nullptr)
  61. {
  62. fToolkit->quit();
  63. delete fToolkit;
  64. fToolkit = nullptr;
  65. }
  66. closePipeClient();
  67. }
  68. // ---------------------------------------------------------------------
  69. bool CarlaBridgeUI::libOpen(const char* const filename) noexcept
  70. {
  71. CARLA_SAFE_ASSERT_RETURN(fLib == nullptr, false);
  72. fLib = lib_open(filename);
  73. if (fLib != nullptr)
  74. {
  75. fLibFilename = filename;
  76. return true;
  77. }
  78. return false;
  79. }
  80. void* CarlaBridgeUI::libSymbol(const char* const symbol) const noexcept
  81. {
  82. CARLA_SAFE_ASSERT_RETURN(fLib != nullptr, nullptr);
  83. return lib_symbol<void*>(fLib, symbol);
  84. }
  85. const char* CarlaBridgeUI::libError() const noexcept
  86. {
  87. CARLA_SAFE_ASSERT_RETURN(fLibFilename.isNotEmpty(), nullptr);
  88. return lib_error(fLibFilename);
  89. }
  90. // ---------------------------------------------------------------------
  91. bool CarlaBridgeUI::msgReceived(const char* const msg) noexcept
  92. {
  93. carla_debug("CarlaBridgeUI::msgReceived(\"%s\")", msg);
  94. if (! fGotOptions && std::strcmp(msg, "urid") != 0 && std::strcmp(msg, "uiOptions") != 0)
  95. {
  96. carla_stderr2("CarlaBridgeUI::msgReceived(\"%s\") - invalid message while waiting for options", msg);
  97. return true;
  98. }
  99. if (fLastMsgTimer > 0)
  100. --fLastMsgTimer;
  101. if (std::strcmp(msg, "control") == 0)
  102. {
  103. uint32_t index;
  104. float value;
  105. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  106. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  107. dspParameterChanged(index, value);
  108. return true;
  109. }
  110. if (std::strcmp(msg, "program") == 0)
  111. {
  112. uint32_t index;
  113. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  114. dspProgramChanged(index);
  115. return true;
  116. }
  117. if (std::strcmp(msg, "midiprogram") == 0)
  118. {
  119. uint32_t bank, program;
  120. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(bank), true);
  121. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(program), true);
  122. dspMidiProgramChanged(bank, program);
  123. return true;
  124. }
  125. if (std::strcmp(msg, "configure") == 0)
  126. {
  127. const char* key;
  128. const char* value;
  129. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(key), true);
  130. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(value), true);
  131. dspStateChanged(key, value);
  132. delete[] key;
  133. delete[] value;
  134. return true;
  135. }
  136. if (std::strcmp(msg, "note") == 0)
  137. {
  138. bool onOff;
  139. uint8_t channel, note, velocity;
  140. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(onOff), true);
  141. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(channel), true);
  142. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(note), true);
  143. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(velocity), true);
  144. dspNoteReceived(onOff, channel, note, velocity);
  145. return true;
  146. }
  147. if (std::strcmp(msg, "atom") == 0)
  148. {
  149. uint32_t index, atomTotalSize;
  150. const char* base64atom;
  151. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  152. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(atomTotalSize), true);
  153. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(base64atom), true);
  154. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(base64atom));
  155. delete[] base64atom;
  156. CARLA_SAFE_ASSERT_RETURN(chunk.size() >= sizeof(LV2_Atom), true);
  157. #ifdef CARLA_PROPER_CPP11_SUPPORT
  158. const LV2_Atom* const atom((const LV2_Atom*)chunk.data());
  159. #else
  160. const LV2_Atom* const atom((const LV2_Atom*)&chunk.front());
  161. #endif
  162. const uint32_t atomTotalSizeCheck(lv2_atom_total_size(atom));
  163. CARLA_SAFE_ASSERT_RETURN(atomTotalSizeCheck == atomTotalSize, true);
  164. CARLA_SAFE_ASSERT_RETURN(atomTotalSizeCheck == chunk.size(), true);
  165. dspAtomReceived(index, atom);
  166. return true;
  167. }
  168. if (std::strcmp(msg, "urid") == 0)
  169. {
  170. uint32_t urid;
  171. const char* uri;
  172. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(urid), true);
  173. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(uri), true);
  174. if (urid != 0)
  175. dspURIDReceived(urid, uri);
  176. delete[] uri;
  177. return true;
  178. }
  179. if (std::strcmp(msg, "uiOptions") == 0)
  180. {
  181. double sampleRate;
  182. bool useTheme, useThemeColors;
  183. const char* windowTitle;
  184. uint64_t transientWindowId;
  185. CARLA_SAFE_ASSERT_RETURN(readNextLineAsDouble(sampleRate), true);
  186. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(useTheme), true);
  187. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(useThemeColors), true);
  188. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(windowTitle), true);
  189. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(transientWindowId), true);
  190. fGotOptions = true;
  191. uiOptionsChanged(sampleRate, useTheme, useThemeColors, windowTitle, static_cast<uintptr_t>(transientWindowId));
  192. delete[] windowTitle;
  193. return true;
  194. }
  195. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr, true);
  196. if (std::strcmp(msg, "show") == 0)
  197. {
  198. fToolkit->show();
  199. return true;
  200. }
  201. if (std::strcmp(msg, "focus") == 0)
  202. {
  203. fToolkit->focus();
  204. return true;
  205. }
  206. if (std::strcmp(msg, "hide") == 0)
  207. {
  208. fToolkit->hide();
  209. return true;
  210. }
  211. if (std::strcmp(msg, "quit") == 0)
  212. {
  213. fQuitReceived = true;
  214. fToolkit->quit();
  215. delete fToolkit;
  216. fToolkit = nullptr;
  217. return true;
  218. }
  219. if (std::strcmp(msg, "uiTitle") == 0)
  220. {
  221. const char* title;
  222. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(title), true);
  223. fToolkit->setTitle(title);
  224. delete[] title;
  225. return true;
  226. }
  227. carla_stderr("CarlaBridgeUI::msgReceived : %s", msg);
  228. return false;
  229. }
  230. // ---------------------------------------------------------------------
  231. bool CarlaBridgeUI::init(const int argc, const char* argv[])
  232. {
  233. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr, false);
  234. if (argc == 7)
  235. {
  236. if (! initPipeClient(argv))
  237. return false;
  238. fLastMsgTimer = 0;
  239. // wait for ui options
  240. for (; ++fLastMsgTimer < 50 && ! fGotOptions;)
  241. {
  242. idlePipe(true);
  243. carla_msleep(20);
  244. }
  245. if (! fGotOptions)
  246. {
  247. carla_stderr2("CarlaBridgeUI::init() - did not get options on time, quitting...");
  248. {
  249. const CarlaMutexLocker cml(getPipeLock());
  250. writeMessage("exiting\n", 8);
  251. flushMessages();
  252. }
  253. closePipeClient();
  254. return false;
  255. }
  256. }
  257. if (! fToolkit->init(argc, argv))
  258. {
  259. if (argc == 7)
  260. closePipeClient();
  261. return false;
  262. }
  263. return true;
  264. }
  265. void CarlaBridgeUI::exec(const bool showUI)
  266. {
  267. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr,);
  268. #ifdef CARLA_OS_LINUX
  269. // kill ourselves if main carla dies
  270. ::prctl(PR_SET_PDEATHSIG, SIGKILL);
  271. // TODO, osx version too, see https://stackoverflow.com/questions/284325/how-to-make-child-process-die-after-parent-exits
  272. #endif
  273. fToolkit->exec(showUI);
  274. }
  275. // ---------------------------------------------------------------------
  276. CARLA_BRIDGE_END_NAMESPACE
  277. #include "CarlaPipeUtils.cpp"
  278. #if defined(CARLA_OS_MAC) || defined(CARLA_OS_WIN)
  279. # include "water/misc/Time.cpp"
  280. #endif
  281. // ---------------------------------------------------------------------