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.

321 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. #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. if (! fGotOptions) {
  87. CARLA_SAFE_ASSERT_RETURN(std::strcmp(msg, "urid") == 0 || std::strcmp(msg, "uiOptions") == 0, true);
  88. }
  89. if (std::strcmp(msg, "control") == 0)
  90. {
  91. uint32_t index;
  92. float value;
  93. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  94. CARLA_SAFE_ASSERT_RETURN(readNextLineAsFloat(value), true);
  95. dspParameterChanged(index, value);
  96. return true;
  97. }
  98. if (std::strcmp(msg, "program") == 0)
  99. {
  100. uint32_t index;
  101. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  102. dspProgramChanged(index);
  103. return true;
  104. }
  105. if (std::strcmp(msg, "mprogram") == 0)
  106. {
  107. uint32_t bank, program;
  108. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(bank), true);
  109. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(program), true);
  110. dspMidiProgramChanged(bank, program);
  111. return true;
  112. }
  113. if (std::strcmp(msg, "configure") == 0)
  114. {
  115. const char* key;
  116. const char* value;
  117. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(key), true);
  118. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(value), true);
  119. dspStateChanged(key, value);
  120. delete[] key;
  121. delete[] value;
  122. return true;
  123. }
  124. if (std::strcmp(msg, "note") == 0)
  125. {
  126. bool onOff;
  127. uint8_t channel, note, velocity;
  128. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(onOff), true);
  129. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(channel), true);
  130. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(note), true);
  131. CARLA_SAFE_ASSERT_RETURN(readNextLineAsByte(velocity), true);
  132. dspNoteReceived(onOff, channel, note, velocity);
  133. }
  134. if (std::strcmp(msg, "atom") == 0)
  135. {
  136. uint32_t index, size;
  137. const char* base64atom;
  138. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(index), true);
  139. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(size), true);
  140. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(base64atom), true);
  141. std::vector<uint8_t> chunk(carla_getChunkFromBase64String(base64atom));
  142. delete[] base64atom;
  143. CARLA_SAFE_ASSERT_RETURN(chunk.size() >= sizeof(LV2_Atom), true);
  144. const LV2_Atom* const atom((const LV2_Atom*)chunk.data());
  145. CARLA_SAFE_ASSERT_RETURN(lv2_atom_total_size(atom) == chunk.size(), true);
  146. dspAtomReceived(index, atom);
  147. return true;
  148. }
  149. if (std::strcmp(msg, "urid") == 0)
  150. {
  151. uint32_t urid;
  152. const char* uri;
  153. CARLA_SAFE_ASSERT_RETURN(readNextLineAsUInt(urid), true);
  154. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(uri), true);
  155. if (urid != 0)
  156. dspURIDReceived(urid, uri);
  157. delete[] uri;
  158. return true;
  159. }
  160. if (std::strcmp(msg, "uiOptions") == 0)
  161. {
  162. bool useTheme, useThemeColors;
  163. const char* windowTitle;
  164. uint64_t transientWindowId;
  165. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(useTheme), true);
  166. CARLA_SAFE_ASSERT_RETURN(readNextLineAsBool(useThemeColors), true);
  167. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(windowTitle), true);
  168. CARLA_SAFE_ASSERT_RETURN(readNextLineAsULong(transientWindowId), true);
  169. fGotOptions = true;
  170. uiOptionsChanged(useTheme, useThemeColors, windowTitle, static_cast<uintptr_t>(transientWindowId));
  171. delete[] windowTitle;
  172. return true;
  173. }
  174. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr, true);
  175. if (std::strcmp(msg, "show") == 0)
  176. {
  177. fToolkit->show();
  178. return true;
  179. }
  180. if (std::strcmp(msg, "focus") == 0)
  181. {
  182. fToolkit->focus();
  183. return true;
  184. }
  185. if (std::strcmp(msg, "hide") == 0)
  186. {
  187. fToolkit->hide();
  188. return true;
  189. }
  190. if (std::strcmp(msg, "quit") == 0)
  191. {
  192. fQuitReceived = true;
  193. fToolkit->quit();
  194. delete fToolkit;
  195. fToolkit = nullptr;
  196. return true;
  197. }
  198. if (std::strcmp(msg, "uiTitle") == 0)
  199. {
  200. const char* title;
  201. CARLA_SAFE_ASSERT_RETURN(readNextLineAsString(title), true);
  202. fToolkit->setTitle(title);
  203. delete[] title;
  204. return true;
  205. }
  206. carla_stderr("CarlaBridgeUI::msgReceived : %s", msg);
  207. return false;
  208. }
  209. // ---------------------------------------------------------------------
  210. bool CarlaBridgeUI::init(const int argc, const char* argv[])
  211. {
  212. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr, false);
  213. if (argc == 7)
  214. {
  215. if (! initPipeClient(argv))
  216. return false;
  217. // wait for ui options, FIXME
  218. for (int i=0; i<20 && ! fGotOptions; ++i)
  219. {
  220. idlePipe(true);
  221. carla_msleep(100);
  222. }
  223. if (! fGotOptions)
  224. {
  225. closePipeClient();
  226. return false;
  227. }
  228. }
  229. if (! fToolkit->init(argc, argv))
  230. {
  231. if (argc == 7)
  232. closePipeClient();
  233. return false;
  234. }
  235. return true;
  236. }
  237. void CarlaBridgeUI::exec(const bool showUI)
  238. {
  239. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr,);
  240. fToolkit->exec(showUI);
  241. }
  242. // ---------------------------------------------------------------------
  243. CARLA_BRIDGE_END_NAMESPACE
  244. #include "CarlaPipeUtils.cpp"
  245. // ---------------------------------------------------------------------