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.

337 lines
8.3KB

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