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.

357 lines
8.9KB

  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 (isPipeRunning() && ! fQuitReceived)
  50. writeExitingMessageAndWait();
  51. if (fLib != nullptr)
  52. {
  53. lib_close(fLib);
  54. fLib = nullptr;
  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 && std::strcmp(msg, "urid") != 0 && std::strcmp(msg, "uiOptions") != 0)
  91. {
  92. carla_stderr2("CarlaBridgeUI::msgReceived(\"%s\") - invalid message while waiting for options", msg);
  93. return true;
  94. }
  95. if (fLastMsgTimer > 0)
  96. --fLastMsgTimer;
  97. if (std::strcmp(msg, "control") == 0)
  98. {
  99. uint32_t index;
  100. float value;
  101. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  102. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  103. dspParameterChanged(index, value);
  104. return true;
  105. }
  106. if (std::strcmp(msg, "program") == 0)
  107. {
  108. uint32_t index;
  109. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  110. dspProgramChanged(index);
  111. return true;
  112. }
  113. if (std::strcmp(msg, "midiprogram") == 0)
  114. {
  115. uint32_t bank, program;
  116. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(bank), true);
  117. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(program), true);
  118. dspMidiProgramChanged(bank, program);
  119. return true;
  120. }
  121. if (std::strcmp(msg, "configure") == 0)
  122. {
  123. const char* key;
  124. const char* value;
  125. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(key), true);
  126. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(value), true);
  127. dspStateChanged(key, value);
  128. delete[] key;
  129. delete[] value;
  130. return true;
  131. }
  132. if (std::strcmp(msg, "note") == 0)
  133. {
  134. bool onOff;
  135. uint8_t channel, note, velocity;
  136. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(onOff), true);
  137. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(channel), true);
  138. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(note), true);
  139. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(velocity), true);
  140. dspNoteReceived(onOff, channel, note, velocity);
  141. return true;
  142. }
  143. if (std::strcmp(msg, "atom") == 0)
  144. {
  145. uint32_t index, atomTotalSize;
  146. const char* base64atom;
  147. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  148. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(atomTotalSize), true);
  149. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(base64atom), true);
  150. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(base64atom));
  151. delete[] base64atom;
  152. CARLA_SAFE_ASSERT_RETURN(chunk.size() >= sizeof(LV2_Atom), true);
  153. #ifdef CARLA_PROPER_CPP11_SUPPORT
  154. const LV2_Atom* const atom((const LV2_Atom*)chunk.data());
  155. #else
  156. const LV2_Atom* const atom((const LV2_Atom*)&chunk.front());
  157. #endif
  158. const uint32_t atomTotalSizeCheck(lv2_atom_total_size(atom));
  159. CARLA_SAFE_ASSERT_RETURN(atomTotalSizeCheck == atomTotalSize, true);
  160. CARLA_SAFE_ASSERT_RETURN(atomTotalSizeCheck == chunk.size(), true);
  161. dspAtomReceived(index, atom);
  162. return true;
  163. }
  164. if (std::strcmp(msg, "urid") == 0)
  165. {
  166. uint32_t urid;
  167. const char* uri;
  168. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(urid), true);
  169. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(uri), true);
  170. if (urid != 0)
  171. dspURIDReceived(urid, uri);
  172. delete[] uri;
  173. return true;
  174. }
  175. if (std::strcmp(msg, "uiOptions") == 0)
  176. {
  177. double sampleRate;
  178. bool useTheme, useThemeColors;
  179. const char* windowTitle;
  180. uint64_t transientWindowId;
  181. CARLA_SAFE_ASSERT_RETURN(readNextLineAsDouble(sampleRate), true);
  182. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(useTheme), true);
  183. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(useThemeColors), true);
  184. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(windowTitle), true);
  185. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(transientWindowId), true);
  186. fGotOptions = true;
  187. uiOptionsChanged(sampleRate, useTheme, useThemeColors, windowTitle, static_cast<uintptr_t>(transientWindowId));
  188. delete[] windowTitle;
  189. return true;
  190. }
  191. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr, true);
  192. if (std::strcmp(msg, "show") == 0)
  193. {
  194. fToolkit->show();
  195. return true;
  196. }
  197. if (std::strcmp(msg, "focus") == 0)
  198. {
  199. fToolkit->focus();
  200. return true;
  201. }
  202. if (std::strcmp(msg, "hide") == 0)
  203. {
  204. fToolkit->hide();
  205. return true;
  206. }
  207. if (std::strcmp(msg, "quit") == 0)
  208. {
  209. fQuitReceived = true;
  210. fToolkit->quit();
  211. delete fToolkit;
  212. fToolkit = nullptr;
  213. return true;
  214. }
  215. if (std::strcmp(msg, "uiTitle") == 0)
  216. {
  217. const char* title;
  218. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(title), true);
  219. fToolkit->setTitle(title);
  220. delete[] title;
  221. return true;
  222. }
  223. carla_stderr("CarlaBridgeUI::msgReceived : %s", msg);
  224. return false;
  225. }
  226. // ---------------------------------------------------------------------
  227. bool CarlaBridgeUI::init(const int argc, const char* argv[])
  228. {
  229. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr, false);
  230. if (argc == 7)
  231. {
  232. if (! initPipeClient(argv))
  233. return false;
  234. fLastMsgTimer = 0;
  235. // wait for ui options
  236. for (; ++fLastMsgTimer < 50 && ! fGotOptions;)
  237. {
  238. idlePipe(true);
  239. carla_msleep(20);
  240. }
  241. if (! fGotOptions)
  242. {
  243. carla_stderr2("CarlaBridgeUI::init() - did not get options on time, quitting...");
  244. writeExitingMessageAndWait();
  245. closePipeClient();
  246. return false;
  247. }
  248. }
  249. if (! fToolkit->init(argc, argv))
  250. {
  251. if (argc == 7)
  252. closePipeClient();
  253. return false;
  254. }
  255. return true;
  256. }
  257. void CarlaBridgeUI::exec(const bool showUI)
  258. {
  259. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr,);
  260. #ifdef CARLA_OS_LINUX
  261. // kill ourselves if main carla dies
  262. ::prctl(PR_SET_PDEATHSIG, SIGKILL);
  263. // TODO, osx version too, see https://stackoverflow.com/questions/284325/how-to-make-child-process-die-after-parent-exits
  264. #endif
  265. fToolkit->exec(showUI);
  266. }
  267. // ---------------------------------------------------------------------
  268. CARLA_BRIDGE_END_NAMESPACE
  269. #include "CarlaPipeUtils.cpp"
  270. #ifndef DEBUG
  271. # include "water/misc/Time.cpp"
  272. #endif
  273. // ---------------------------------------------------------------------