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

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