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.

325 lines
8.0KB

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