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.

1232 lines
46KB

  1. /*
  2. * Carla Bridge UI, LV2 version
  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 "CarlaLibUtils.hpp"
  19. #include "CarlaLv2Utils.hpp"
  20. #include "CarlaMIDI.h"
  21. #include "LinkedList.hpp"
  22. #include "juce_core/juce_core.h"
  23. #define URI_CARLA_ATOM_WORKER "http://kxstudio.sf.net/ns/carla/atomWorker"
  24. using juce::File;
  25. CARLA_BRIDGE_START_NAMESPACE
  26. // -----------------------------------------------------
  27. static int gBufferSize = 1024;
  28. static double gSampleRate = 44100.0;
  29. // Maximum default buffer size
  30. const unsigned int MAX_DEFAULT_BUFFER_SIZE = 8192; // 0x2000
  31. // LV2 URI Map Ids
  32. const uint32_t CARLA_URI_MAP_ID_NULL = 0;
  33. const uint32_t CARLA_URI_MAP_ID_ATOM_BLANK = 1;
  34. const uint32_t CARLA_URI_MAP_ID_ATOM_BOOL = 2;
  35. const uint32_t CARLA_URI_MAP_ID_ATOM_CHUNK = 3;
  36. const uint32_t CARLA_URI_MAP_ID_ATOM_DOUBLE = 4;
  37. const uint32_t CARLA_URI_MAP_ID_ATOM_EVENT = 5;
  38. const uint32_t CARLA_URI_MAP_ID_ATOM_FLOAT = 6;
  39. const uint32_t CARLA_URI_MAP_ID_ATOM_INT = 7;
  40. const uint32_t CARLA_URI_MAP_ID_ATOM_LITERAL = 8;
  41. const uint32_t CARLA_URI_MAP_ID_ATOM_LONG = 9;
  42. const uint32_t CARLA_URI_MAP_ID_ATOM_NUMBER = 10;
  43. const uint32_t CARLA_URI_MAP_ID_ATOM_OBJECT = 11;
  44. const uint32_t CARLA_URI_MAP_ID_ATOM_PATH = 12;
  45. const uint32_t CARLA_URI_MAP_ID_ATOM_PROPERTY = 13;
  46. const uint32_t CARLA_URI_MAP_ID_ATOM_RESOURCE = 14;
  47. const uint32_t CARLA_URI_MAP_ID_ATOM_SEQUENCE = 15;
  48. const uint32_t CARLA_URI_MAP_ID_ATOM_SOUND = 16;
  49. const uint32_t CARLA_URI_MAP_ID_ATOM_STRING = 17;
  50. const uint32_t CARLA_URI_MAP_ID_ATOM_TUPLE = 18;
  51. const uint32_t CARLA_URI_MAP_ID_ATOM_URI = 19;
  52. const uint32_t CARLA_URI_MAP_ID_ATOM_URID = 20;
  53. const uint32_t CARLA_URI_MAP_ID_ATOM_VECTOR = 21;
  54. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM = 22;
  55. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT = 23;
  56. const uint32_t CARLA_URI_MAP_ID_BUF_MAX_LENGTH = 24;
  57. const uint32_t CARLA_URI_MAP_ID_BUF_MIN_LENGTH = 25;
  58. const uint32_t CARLA_URI_MAP_ID_BUF_NOMINAL_LENGTH = 26;
  59. const uint32_t CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE = 27;
  60. const uint32_t CARLA_URI_MAP_ID_LOG_ERROR = 28;
  61. const uint32_t CARLA_URI_MAP_ID_LOG_NOTE = 29;
  62. const uint32_t CARLA_URI_MAP_ID_LOG_TRACE = 30;
  63. const uint32_t CARLA_URI_MAP_ID_LOG_WARNING = 31;
  64. const uint32_t CARLA_URI_MAP_ID_TIME_POSITION = 32; // base type
  65. const uint32_t CARLA_URI_MAP_ID_TIME_BAR = 33; // values
  66. const uint32_t CARLA_URI_MAP_ID_TIME_BAR_BEAT = 34;
  67. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT = 35;
  68. const uint32_t CARLA_URI_MAP_ID_TIME_BEAT_UNIT = 36;
  69. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR = 37;
  70. const uint32_t CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE = 38;
  71. const uint32_t CARLA_URI_MAP_ID_TIME_FRAME = 39;
  72. const uint32_t CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND = 40;
  73. const uint32_t CARLA_URI_MAP_ID_TIME_SPEED = 41;
  74. const uint32_t CARLA_URI_MAP_ID_TIME_TICKS_PER_BEAT = 42;
  75. const uint32_t CARLA_URI_MAP_ID_MIDI_EVENT = 43;
  76. const uint32_t CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE = 44;
  77. const uint32_t CARLA_URI_MAP_ID_UI_WINDOW_TITLE = 45;
  78. const uint32_t CARLA_URI_MAP_ID_CARLA_ATOM_WORKER = 46;
  79. const uint32_t CARLA_URI_MAP_ID_CARLA_TRANSIENT_WIN_ID = 47;
  80. const uint32_t CARLA_URI_MAP_ID_COUNT = 48;
  81. // LV2 Feature Ids
  82. const uint32_t kFeatureIdLogs = 0;
  83. const uint32_t kFeatureIdOptions = 1;
  84. const uint32_t kFeatureIdPrograms = 2;
  85. const uint32_t kFeatureIdStateMakePath = 3;
  86. const uint32_t kFeatureIdStateMapPath = 4;
  87. const uint32_t kFeatureIdUriMap = 5;
  88. const uint32_t kFeatureIdUridMap = 6;
  89. const uint32_t kFeatureIdUridUnmap = 7;
  90. const uint32_t kFeatureIdUiIdleInterface = 8;
  91. const uint32_t kFeatureIdUiFixedSize = 9;
  92. const uint32_t kFeatureIdUiMakeResident = 10;
  93. const uint32_t kFeatureIdUiMakeResident2 = 11;
  94. const uint32_t kFeatureIdUiNoUserResize = 12;
  95. const uint32_t kFeatureIdUiParent = 13;
  96. const uint32_t kFeatureIdUiPortMap = 14;
  97. const uint32_t kFeatureIdUiPortSubscribe = 15;
  98. const uint32_t kFeatureIdUiResize = 16;
  99. const uint32_t kFeatureIdUiTouch = 17;
  100. const uint32_t kFeatureCount = 18;
  101. // -------------------------------------------------------------------------
  102. struct Lv2PluginOptions {
  103. enum OptIndex {
  104. MaxBlockLenth = 0,
  105. MinBlockLenth,
  106. NominalBlockLenth,
  107. SequenceSize,
  108. SampleRate,
  109. FrontendWinId,
  110. //WindowTitle,
  111. Null,
  112. Count
  113. };
  114. int maxBufferSize;
  115. int minBufferSize;
  116. int nominalBufferSize;
  117. int sequenceSize;
  118. double sampleRate;
  119. int64_t frontendWinId;
  120. const char* windowTitle;
  121. LV2_Options_Option opts[Count];
  122. Lv2PluginOptions() noexcept
  123. : maxBufferSize(0),
  124. minBufferSize(0),
  125. nominalBufferSize(0),
  126. sequenceSize(MAX_DEFAULT_BUFFER_SIZE),
  127. sampleRate(0.0),
  128. frontendWinId(0),
  129. windowTitle(nullptr)
  130. {
  131. LV2_Options_Option& optMaxBlockLenth(opts[MaxBlockLenth]);
  132. optMaxBlockLenth.context = LV2_OPTIONS_INSTANCE;
  133. optMaxBlockLenth.subject = 0;
  134. optMaxBlockLenth.key = CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  135. optMaxBlockLenth.size = sizeof(int);
  136. optMaxBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  137. optMaxBlockLenth.value = &maxBufferSize;
  138. LV2_Options_Option& optMinBlockLenth(opts[MinBlockLenth]);
  139. optMinBlockLenth.context = LV2_OPTIONS_INSTANCE;
  140. optMinBlockLenth.subject = 0;
  141. optMinBlockLenth.key = CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  142. optMinBlockLenth.size = sizeof(int);
  143. optMinBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  144. optMinBlockLenth.value = &minBufferSize;
  145. LV2_Options_Option& optNominalBlockLenth(opts[NominalBlockLenth]);
  146. optNominalBlockLenth.context = LV2_OPTIONS_INSTANCE;
  147. optNominalBlockLenth.subject = 0;
  148. optNominalBlockLenth.key = CARLA_URI_MAP_ID_BUF_NOMINAL_LENGTH;
  149. optNominalBlockLenth.size = sizeof(int);
  150. optNominalBlockLenth.type = CARLA_URI_MAP_ID_ATOM_INT;
  151. optNominalBlockLenth.value = &nominalBufferSize;
  152. LV2_Options_Option& optSequenceSize(opts[SequenceSize]);
  153. optSequenceSize.context = LV2_OPTIONS_INSTANCE;
  154. optSequenceSize.subject = 0;
  155. optSequenceSize.key = CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  156. optSequenceSize.size = sizeof(int);
  157. optSequenceSize.type = CARLA_URI_MAP_ID_ATOM_INT;
  158. optSequenceSize.value = &sequenceSize;
  159. LV2_Options_Option& optSampleRate(opts[SampleRate]);
  160. optSampleRate.context = LV2_OPTIONS_INSTANCE;
  161. optSampleRate.subject = 0;
  162. optSampleRate.key = CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  163. optSampleRate.size = sizeof(double);
  164. optSampleRate.type = CARLA_URI_MAP_ID_ATOM_DOUBLE;
  165. optSampleRate.value = &sampleRate;
  166. LV2_Options_Option& optFrontendWinId(opts[FrontendWinId]);
  167. optFrontendWinId.context = LV2_OPTIONS_INSTANCE;
  168. optFrontendWinId.subject = 0;
  169. optFrontendWinId.key = CARLA_URI_MAP_ID_CARLA_TRANSIENT_WIN_ID;
  170. optFrontendWinId.size = sizeof(int64_t);
  171. optFrontendWinId.type = CARLA_URI_MAP_ID_ATOM_LONG;
  172. optFrontendWinId.value = &frontendWinId;
  173. /*
  174. LV2_Options_Option& optWindowTitle(opts[WindowTitle]);
  175. optWindowTitle.context = LV2_OPTIONS_INSTANCE;
  176. optWindowTitle.subject = 0;
  177. optWindowTitle.key = CARLA_URI_MAP_ID_UI_WINDOW_TITLE;
  178. optWindowTitle.size = 0;
  179. optWindowTitle.type = CARLA_URI_MAP_ID_ATOM_STRING;
  180. optWindowTitle.value = nullptr;
  181. */
  182. LV2_Options_Option& optNull(opts[Null]);
  183. optNull.context = LV2_OPTIONS_INSTANCE;
  184. optNull.subject = 0;
  185. optNull.key = CARLA_URI_MAP_ID_NULL;
  186. optNull.size = 0;
  187. optNull.type = CARLA_URI_MAP_ID_NULL;
  188. optNull.value = nullptr;
  189. }
  190. };
  191. // -------------------------------------------------------------------------
  192. class CarlaLv2Client : public CarlaBridgeUI
  193. {
  194. public:
  195. CarlaLv2Client()
  196. : CarlaBridgeUI(),
  197. fHandle(nullptr),
  198. fWidget(nullptr),
  199. fDescriptor(nullptr),
  200. fRdfDescriptor(nullptr),
  201. fRdfUiDescriptor(nullptr),
  202. fLv2Options(),
  203. fUiOptions(),
  204. fCustomURIDs(),
  205. fExt()
  206. {
  207. carla_zeroPointers(fFeatures, kFeatureCount+1);
  208. for (uint32_t i=0; i < CARLA_URI_MAP_ID_COUNT; ++i)
  209. fCustomURIDs.append(nullptr);
  210. // ---------------------------------------------------------------
  211. // initialize options
  212. fLv2Options.minBufferSize = gBufferSize;
  213. fLv2Options.maxBufferSize = gBufferSize;
  214. fLv2Options.nominalBufferSize = gBufferSize;
  215. fLv2Options.sampleRate = gSampleRate;
  216. // ---------------------------------------------------------------
  217. // initialize features (part 1)
  218. LV2_Log_Log* const logFt = new LV2_Log_Log;
  219. logFt->handle = this;
  220. logFt->printf = carla_lv2_log_printf;
  221. logFt->vprintf = carla_lv2_log_vprintf;
  222. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  223. stateMakePathFt->handle = this;
  224. stateMakePathFt->path = carla_lv2_state_make_path;
  225. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  226. stateMapPathFt->handle = this;
  227. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  228. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  229. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  230. programsFt->handle = this;
  231. programsFt->program_changed = carla_lv2_program_changed;
  232. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  233. uriMapFt->callback_data = this;
  234. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  235. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  236. uridMapFt->handle = this;
  237. uridMapFt->map = carla_lv2_urid_map;
  238. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  239. uridUnmapFt->handle = this;
  240. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  241. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  242. uiPortMapFt->handle = this;
  243. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  244. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  245. uiResizeFt->handle = this;
  246. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  247. // ---------------------------------------------------------------
  248. // initialize features (part 2)
  249. for (uint32_t i=0; i < kFeatureCount; ++i)
  250. fFeatures[i] = new LV2_Feature;
  251. fFeatures[kFeatureIdLogs]->URI = LV2_LOG__log;
  252. fFeatures[kFeatureIdLogs]->data = logFt;
  253. fFeatures[kFeatureIdOptions]->URI = LV2_OPTIONS__options;
  254. fFeatures[kFeatureIdOptions]->data = fLv2Options.opts;
  255. fFeatures[kFeatureIdPrograms]->URI = LV2_PROGRAMS__Host;
  256. fFeatures[kFeatureIdPrograms]->data = programsFt;
  257. fFeatures[kFeatureIdStateMakePath]->URI = LV2_STATE__makePath;
  258. fFeatures[kFeatureIdStateMakePath]->data = stateMakePathFt;
  259. fFeatures[kFeatureIdStateMapPath]->URI = LV2_STATE__mapPath;
  260. fFeatures[kFeatureIdStateMapPath]->data = stateMapPathFt;
  261. fFeatures[kFeatureIdUriMap]->URI = LV2_URI_MAP_URI;
  262. fFeatures[kFeatureIdUriMap]->data = uriMapFt;
  263. fFeatures[kFeatureIdUridMap]->URI = LV2_URID__map;
  264. fFeatures[kFeatureIdUridMap]->data = uridMapFt;
  265. fFeatures[kFeatureIdUridUnmap]->URI = LV2_URID__unmap;
  266. fFeatures[kFeatureIdUridUnmap]->data = uridUnmapFt;
  267. fFeatures[kFeatureIdUiIdleInterface]->URI = LV2_UI__idleInterface;
  268. fFeatures[kFeatureIdUiIdleInterface]->data = nullptr;
  269. fFeatures[kFeatureIdUiFixedSize]->URI = LV2_UI__fixedSize;
  270. fFeatures[kFeatureIdUiFixedSize]->data = nullptr;
  271. fFeatures[kFeatureIdUiMakeResident]->URI = LV2_UI__makeResident;
  272. fFeatures[kFeatureIdUiMakeResident]->data = nullptr;
  273. fFeatures[kFeatureIdUiMakeResident2]->URI = LV2_UI__makeSONameResident;
  274. fFeatures[kFeatureIdUiMakeResident2]->data = nullptr;
  275. fFeatures[kFeatureIdUiNoUserResize]->URI = LV2_UI__noUserResize;
  276. fFeatures[kFeatureIdUiNoUserResize]->data = nullptr;
  277. fFeatures[kFeatureIdUiParent]->URI = LV2_UI__parent;
  278. fFeatures[kFeatureIdUiParent]->data = nullptr;
  279. fFeatures[kFeatureIdUiPortMap]->URI = LV2_UI__portMap;
  280. fFeatures[kFeatureIdUiPortMap]->data = uiPortMapFt;
  281. fFeatures[kFeatureIdUiPortSubscribe]->URI = LV2_UI__portSubscribe;
  282. fFeatures[kFeatureIdUiPortSubscribe]->data = nullptr;
  283. fFeatures[kFeatureIdUiResize]->URI = LV2_UI__resize;
  284. fFeatures[kFeatureIdUiResize]->data = uiResizeFt;
  285. fFeatures[kFeatureIdUiTouch]->URI = LV2_UI__touch;
  286. fFeatures[kFeatureIdUiTouch]->data = nullptr;
  287. }
  288. ~CarlaLv2Client() override
  289. {
  290. if (fHandle != nullptr && fDescriptor != nullptr && fDescriptor->cleanup != nullptr)
  291. {
  292. fDescriptor->cleanup(fHandle);
  293. fHandle = nullptr;
  294. }
  295. if (fRdfDescriptor != nullptr)
  296. {
  297. delete fRdfDescriptor;
  298. fRdfDescriptor = nullptr;
  299. }
  300. fRdfUiDescriptor = nullptr;
  301. delete (LV2_Log_Log*)fFeatures[kFeatureIdLogs]->data;
  302. delete (LV2_State_Make_Path*)fFeatures[kFeatureIdStateMakePath]->data;
  303. delete (LV2_State_Map_Path*)fFeatures[kFeatureIdStateMapPath]->data;
  304. delete (LV2_Programs_Host*)fFeatures[kFeatureIdPrograms]->data;
  305. delete (LV2_URI_Map_Feature*)fFeatures[kFeatureIdUriMap]->data;
  306. delete (LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data;
  307. delete (LV2_URID_Unmap*)fFeatures[kFeatureIdUridUnmap]->data;
  308. delete (LV2UI_Port_Map*)fFeatures[kFeatureIdUiPortMap]->data;
  309. delete (LV2UI_Resize*)fFeatures[kFeatureIdUiResize]->data;
  310. for (uint32_t i=0; i < kFeatureCount; ++i)
  311. {
  312. if (fFeatures[i] != nullptr)
  313. {
  314. delete fFeatures[i];
  315. fFeatures[i] = nullptr;
  316. }
  317. }
  318. for (LinkedList<const char*>::Itenerator it = fCustomURIDs.begin2(); it.valid(); it.next())
  319. {
  320. const char* const uri(it.getValue(nullptr));
  321. if (uri != nullptr)
  322. delete[] uri;
  323. }
  324. fCustomURIDs.clear();
  325. }
  326. // ---------------------------------------------------------------------
  327. // UI initialization
  328. bool init(const int argc, const char* argv[]) override
  329. {
  330. const char* pluginURI = argv[1];
  331. const char* uiURI = argv[2];
  332. // -----------------------------------------------------------------
  333. // load plugin
  334. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  335. lv2World.initIfNeeded(std::getenv("LV2_PATH"));
  336. //Lilv::Node bundleNode(lv2World.new_file_uri(nullptr, uiBundle));
  337. //CARLA_SAFE_ASSERT_RETURN(bundleNode.is_uri(), false);
  338. //CarlaString sBundle(bundleNode.as_uri());
  339. //if (! sBundle.endsWith("/"))
  340. // sBundle += "/";
  341. //lv2World.load_bundle(sBundle);
  342. // -----------------------------------------------------------------
  343. // get plugin from lv2_rdf (lilv)
  344. fRdfDescriptor = lv2_rdf_new(pluginURI, true);
  345. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  346. // -----------------------------------------------------------------
  347. // find requested UI
  348. for (uint32_t i=0; i < fRdfDescriptor->UICount; ++i)
  349. {
  350. if (std::strcmp(fRdfDescriptor->UIs[i].URI, uiURI) == 0)
  351. {
  352. fRdfUiDescriptor = &fRdfDescriptor->UIs[i];
  353. break;
  354. }
  355. }
  356. CARLA_SAFE_ASSERT_RETURN(fRdfUiDescriptor != nullptr, false);
  357. // -----------------------------------------------------------
  358. // check if not resizable
  359. #if defined(BRIDGE_COCOA) || defined(BRIDGE_HWND) || defined(BRIDGE_X11)
  360. // embed UIs can only be resizable if they provide resize extension
  361. fUiOptions.isResizable = false;
  362. // TODO: put this trick into main carla
  363. for (uint32_t i=0; i < fRdfUiDescriptor->ExtensionCount; ++i)
  364. {
  365. carla_stdout("Test UI extension %s", fRdfUiDescriptor->Extensions[i]);
  366. if (std::strcmp(fRdfUiDescriptor->Extensions[i], LV2_UI__resize) == 0)
  367. {
  368. fUiOptions.isResizable = true;
  369. break;
  370. }
  371. }
  372. #endif
  373. for (uint32_t i=0; i < fRdfUiDescriptor->FeatureCount; ++i)
  374. {
  375. carla_stdout("Test UI feature %s", fRdfUiDescriptor->Features[i].URI);
  376. if (std::strcmp(fRdfUiDescriptor->Features[i].URI, LV2_UI__fixedSize ) == 0 ||
  377. std::strcmp(fRdfUiDescriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  378. {
  379. fUiOptions.isResizable = false;
  380. break;
  381. }
  382. }
  383. carla_stdout("Is resizable => %s", bool2str(fUiOptions.isResizable));
  384. // -----------------------------------------------------------------
  385. // init UI
  386. if (! CarlaBridgeUI::init(argc, argv))
  387. return false;
  388. // -----------------------------------------------------------------
  389. // open DLL
  390. if (! libOpen(fRdfUiDescriptor->Binary))
  391. {
  392. carla_stderr("Failed to load UI binary, error was:\n%s", libError());
  393. return false;
  394. }
  395. // -----------------------------------------------------------------
  396. // get DLL main entry
  397. const LV2UI_DescriptorFunction ui_descFn = (LV2UI_DescriptorFunction)libSymbol("lv2ui_descriptor");
  398. if (ui_descFn == nullptr)
  399. return false;
  400. // -----------------------------------------------------------
  401. // get descriptor that matches URI
  402. uint32_t i = 0;
  403. while ((fDescriptor = ui_descFn(i++)))
  404. {
  405. if (std::strcmp(fDescriptor->URI, uiURI) == 0)
  406. break;
  407. }
  408. if (fDescriptor == nullptr)
  409. {
  410. carla_stderr("Failed to find UI descriptor");
  411. return false;
  412. }
  413. // -----------------------------------------------------------
  414. // initialize UI
  415. #if defined(BRIDGE_COCOA) || defined(BRIDGE_HWND) || defined(BRIDGE_X11)
  416. fFeatures[kFeatureIdUiParent]->data = fToolkit->getContainerId();
  417. #endif
  418. fHandle = fDescriptor->instantiate(fDescriptor, fRdfDescriptor->URI, fRdfUiDescriptor->Bundle, carla_lv2_ui_write_function, this, &fWidget, fFeatures);
  419. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr, false);
  420. // -----------------------------------------------------------
  421. // check for known extensions
  422. if (fDescriptor->extension_data != nullptr)
  423. {
  424. fExt.options = (const LV2_Options_Interface*)fDescriptor->extension_data(LV2_OPTIONS__interface);
  425. fExt.programs = (const LV2_Programs_UI_Interface*)fDescriptor->extension_data(LV2_PROGRAMS__UIInterface);
  426. fExt.idle = (const LV2UI_Idle_Interface*)fDescriptor->extension_data(LV2_UI__idleInterface);
  427. fExt.resize = (const LV2UI_Resize*)fDescriptor->extension_data(LV2_UI__resize);
  428. // check if invalid
  429. if (fExt.programs != nullptr && fExt.programs->select_program == nullptr)
  430. fExt.programs = nullptr;
  431. if (fExt.idle != nullptr && fExt.idle->idle == nullptr)
  432. fExt.idle = nullptr;
  433. if (fExt.resize != nullptr && fExt.resize->ui_resize == nullptr)
  434. fExt.resize = nullptr;
  435. }
  436. return true;
  437. }
  438. void idleUI() override
  439. {
  440. #if defined(BRIDGE_COCOA) || defined(BRIDGE_HWND) || defined(BRIDGE_X11)
  441. if (fHandle != nullptr && fExt.idle != nullptr)
  442. fExt.idle->idle(fHandle);
  443. #endif
  444. }
  445. // ---------------------------------------------------------------------
  446. // UI management
  447. void* getWidget() const noexcept override
  448. {
  449. return fWidget;
  450. }
  451. const Options& getOptions() const noexcept override
  452. {
  453. return fUiOptions;
  454. }
  455. // ---------------------------------------------------------------------
  456. // DSP Callbacks
  457. void dspParameterChanged(const uint32_t index, const float value) override
  458. {
  459. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,)
  460. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  461. if (fDescriptor->port_event == nullptr)
  462. return;
  463. fDescriptor->port_event(fHandle, index, sizeof(float), CARLA_URI_MAP_ID_NULL, &value);
  464. }
  465. void dspProgramChanged(const uint32_t) override
  466. {
  467. }
  468. void dspMidiProgramChanged(const uint32_t bank, const uint32_t program) override
  469. {
  470. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,)
  471. if (fExt.programs == nullptr)
  472. return;
  473. fExt.programs->select_program(fHandle, bank, program);
  474. }
  475. void dspStateChanged(const char* const, const char* const) override
  476. {
  477. }
  478. void dspNoteReceived(const bool onOff, const uint8_t channel, const uint8_t note, const uint8_t velocity) override
  479. {
  480. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,)
  481. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  482. if (fDescriptor->port_event == nullptr)
  483. return;
  484. LV2_Atom_MidiEvent midiEv;
  485. midiEv.atom.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  486. midiEv.atom.size = 3;
  487. midiEv.data[0] = uint8_t((onOff ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (channel & MIDI_CHANNEL_BIT));
  488. midiEv.data[1] = note;
  489. midiEv.data[2] = velocity;
  490. fDescriptor->port_event(fHandle, /* TODO */ 0, lv2_atom_total_size(midiEv), CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  491. }
  492. void dspAtomReceived(const uint32_t portIndex, const LV2_Atom* const atom) override
  493. {
  494. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  495. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  496. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  497. if (fDescriptor->port_event == nullptr)
  498. return;
  499. fDescriptor->port_event(fHandle, portIndex, lv2_atom_total_size(atom), CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  500. }
  501. void dspURIDReceived(const LV2_URID urid, const char* const uri)
  502. {
  503. CARLA_SAFE_ASSERT_RETURN(urid == fCustomURIDs.count(),);
  504. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0',);
  505. fCustomURIDs.append(carla_strdup(uri));
  506. }
  507. void uiOptionsChanged(const double sampleRate, const bool useTheme, const bool useThemeColors, const char* const windowTitle, uintptr_t transientWindowId) override
  508. {
  509. carla_debug("CarlaLv2Client::uiOptionsChanged(%g, %s, %s, \"%s\", " P_UINTPTR ")", sampleRate, bool2str(useTheme), bool2str(useThemeColors), windowTitle, transientWindowId);
  510. fLv2Options.sampleRate = gSampleRate = sampleRate;
  511. fUiOptions.useTheme = useTheme;
  512. fUiOptions.useThemeColors = useThemeColors;
  513. fUiOptions.windowTitle = windowTitle;
  514. fUiOptions.transientWindowId = transientWindowId;
  515. }
  516. void uiResized(const uint width, const uint height) override
  517. {
  518. if (fHandle != nullptr && fExt.resize != nullptr)
  519. fExt.resize->ui_resize(fHandle, static_cast<int>(width), static_cast<int>(height));
  520. }
  521. // ---------------------------------------------------------------------
  522. LV2_URID getCustomURID(const char* const uri)
  523. {
  524. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  525. carla_debug("CarlaLv2Client::getCustomURID(\"%s\")", uri);
  526. LV2_URID urid = CARLA_URI_MAP_ID_NULL;
  527. for (uint32_t i=0, count=static_cast<uint32_t>(fCustomURIDs.count()); i<count; ++i)
  528. {
  529. const char* const thisUri(fCustomURIDs.getAt(i, nullptr));
  530. if (thisUri != nullptr && std::strcmp(thisUri, uri) == 0)
  531. {
  532. urid = i;
  533. break;
  534. }
  535. }
  536. if (urid == CARLA_URI_MAP_ID_NULL)
  537. {
  538. urid = static_cast<LV2_URID>(fCustomURIDs.count());
  539. fCustomURIDs.append(carla_strdup(uri));
  540. }
  541. if (isPipeRunning())
  542. writeLv2UridMessage(urid, uri);
  543. return urid;
  544. }
  545. const char* getCustomURIDString(const LV2_URID urid) const noexcept
  546. {
  547. static const char* const sFallback = "urn:null";
  548. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, sFallback);
  549. CARLA_SAFE_ASSERT_RETURN(urid < fCustomURIDs.count(), sFallback);
  550. carla_debug("CarlaLv2Client::getCustomURIDString(%i)", urid);
  551. return fCustomURIDs.getAt(urid, sFallback);
  552. }
  553. // ---------------------------------------------------------------------
  554. void handleProgramChanged(const int32_t /*index*/)
  555. {
  556. if (isPipeRunning())
  557. writeConfigureMessage("reloadprograms", "");
  558. }
  559. uint32_t handleUiPortMap(const char* const symbol)
  560. {
  561. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr && symbol[0] != '\0', LV2UI_INVALID_PORT_INDEX);
  562. carla_debug("CarlaLv2Client::handleUiPortMap(\"%s\")", symbol);
  563. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  564. {
  565. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, symbol) == 0)
  566. return i;
  567. }
  568. return LV2UI_INVALID_PORT_INDEX;
  569. }
  570. int handleUiResize(const int width, const int height)
  571. {
  572. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr, 1);
  573. CARLA_SAFE_ASSERT_RETURN(width > 0, 1);
  574. CARLA_SAFE_ASSERT_RETURN(height > 0, 1);
  575. carla_debug("CarlaLv2Client::handleUiResize(%i, %i)", width, height);
  576. fToolkit->setSize(static_cast<uint>(width), static_cast<uint>(height));
  577. return 0;
  578. }
  579. void handleUiWrite(uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  580. {
  581. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  582. CARLA_SAFE_ASSERT_RETURN(bufferSize > 0,);
  583. carla_debug("CarlaLv2Client::handleUiWrite(%i, %i, %i, %p)", portIndex, bufferSize, format, buffer);
  584. if (format == 0)
  585. {
  586. CARLA_SAFE_ASSERT_RETURN(bufferSize == sizeof(float),);
  587. const float value(*(const float*)buffer);
  588. if (isPipeRunning())
  589. writeControlMessage(portIndex, value);
  590. }
  591. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM || CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  592. {
  593. CARLA_SAFE_ASSERT_RETURN(bufferSize >= sizeof(LV2_Atom),);
  594. const LV2_Atom* const atom((const LV2_Atom*)buffer);
  595. if (isPipeRunning())
  596. writeLv2AtomMessage(portIndex, atom);
  597. }
  598. else
  599. {
  600. carla_stdout("CarlaLv2Client::handleUiWrite(%i, %i, %i:\"%s\", %p) - unknown format", portIndex, bufferSize, format, carla_lv2_urid_unmap(this, format), buffer);
  601. }
  602. }
  603. // ---------------------------------------------------------------------
  604. private:
  605. LV2UI_Handle fHandle;
  606. LV2UI_Widget fWidget;
  607. LV2_Feature* fFeatures[kFeatureCount+1];
  608. const LV2UI_Descriptor* fDescriptor;
  609. const LV2_RDF_Descriptor* fRdfDescriptor;
  610. const LV2_RDF_UI* fRdfUiDescriptor;
  611. Lv2PluginOptions fLv2Options;
  612. Options fUiOptions;
  613. LinkedList<const char*> fCustomURIDs;
  614. struct Extensions {
  615. const LV2_Options_Interface* options;
  616. const LV2_Programs_UI_Interface* programs;
  617. const LV2UI_Idle_Interface* idle;
  618. const LV2UI_Resize* resize;
  619. Extensions()
  620. : options(nullptr),
  621. programs(nullptr),
  622. idle(nullptr),
  623. resize(nullptr) {}
  624. } fExt;
  625. // -------------------------------------------------------------------
  626. // Logs Feature
  627. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  628. {
  629. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  630. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  631. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  632. #ifndef DEBUG
  633. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  634. return 0;
  635. #endif
  636. va_list args;
  637. va_start(args, fmt);
  638. const int ret(carla_lv2_log_vprintf(handle, type, fmt, args));
  639. va_end(args);
  640. return ret;
  641. }
  642. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  643. {
  644. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  645. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  646. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  647. #ifndef DEBUG
  648. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  649. return 0;
  650. #endif
  651. int ret = 0;
  652. switch (type)
  653. {
  654. case CARLA_URI_MAP_ID_LOG_ERROR:
  655. std::fprintf(stderr, "\x1b[31m");
  656. ret = std::vfprintf(stderr, fmt, ap);
  657. std::fprintf(stderr, "\x1b[0m");
  658. break;
  659. case CARLA_URI_MAP_ID_LOG_NOTE:
  660. ret = std::vfprintf(stdout, fmt, ap);
  661. break;
  662. case CARLA_URI_MAP_ID_LOG_TRACE:
  663. #ifdef DEBUG
  664. std::fprintf(stdout, "\x1b[30;1m");
  665. ret = std::vfprintf(stdout, fmt, ap);
  666. std::fprintf(stdout, "\x1b[0m");
  667. #endif
  668. break;
  669. case CARLA_URI_MAP_ID_LOG_WARNING:
  670. ret = std::vfprintf(stderr, fmt, ap);
  671. break;
  672. default:
  673. break;
  674. }
  675. return ret;
  676. }
  677. // -------------------------------------------------------------------
  678. // Programs Feature
  679. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  680. {
  681. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  682. carla_debug("carla_lv2_program_changed(%p, %i)", handle, index);
  683. ((CarlaLv2Client*)handle)->handleProgramChanged(index);
  684. }
  685. // -------------------------------------------------------------------
  686. // State Feature
  687. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  688. {
  689. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  690. CARLA_SAFE_ASSERT_RETURN(path != nullptr && path[0] != '\0', nullptr);
  691. carla_debug("carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  692. File file;
  693. if (File::isAbsolutePath(path))
  694. file = File(path);
  695. else
  696. file = File::getCurrentWorkingDirectory().getChildFile(path);
  697. file.getParentDirectory().createDirectory();
  698. return strdup(file.getFullPathName().toRawUTF8());
  699. }
  700. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  701. {
  702. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  703. CARLA_SAFE_ASSERT_RETURN(absolute_path != nullptr && absolute_path[0] != '\0', nullptr);
  704. carla_debug("carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  705. // may already be an abstract path
  706. if (! File::isAbsolutePath(absolute_path))
  707. return strdup(absolute_path);
  708. return strdup(File(absolute_path).getRelativePathFrom(File::getCurrentWorkingDirectory()).toRawUTF8());
  709. }
  710. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  711. {
  712. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  713. CARLA_SAFE_ASSERT_RETURN(abstract_path != nullptr && abstract_path[0] != '\0', nullptr);
  714. carla_debug("carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  715. // may already be an absolute path
  716. if (File::isAbsolutePath(abstract_path))
  717. return strdup(abstract_path);
  718. return strdup(File::getCurrentWorkingDirectory().getChildFile(abstract_path).getFullPathName().toRawUTF8());
  719. }
  720. // -------------------------------------------------------------------
  721. // URI-Map Feature
  722. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  723. {
  724. carla_debug("carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  725. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  726. // unused
  727. (void)map;
  728. }
  729. // -------------------------------------------------------------------
  730. // URID Feature
  731. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  732. {
  733. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, CARLA_URI_MAP_ID_NULL);
  734. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  735. carla_debug("carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  736. // Atom types
  737. if (std::strcmp(uri, LV2_ATOM__Blank) == 0)
  738. return CARLA_URI_MAP_ID_ATOM_BLANK;
  739. if (std::strcmp(uri, LV2_ATOM__Bool) == 0)
  740. return CARLA_URI_MAP_ID_ATOM_BOOL;
  741. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  742. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  743. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  744. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  745. if (std::strcmp(uri, LV2_ATOM__Event) == 0)
  746. return CARLA_URI_MAP_ID_ATOM_EVENT;
  747. if (std::strcmp(uri, LV2_ATOM__Float) == 0)
  748. return CARLA_URI_MAP_ID_ATOM_FLOAT;
  749. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  750. return CARLA_URI_MAP_ID_ATOM_INT;
  751. if (std::strcmp(uri, LV2_ATOM__Literal) == 0)
  752. return CARLA_URI_MAP_ID_ATOM_LITERAL;
  753. if (std::strcmp(uri, LV2_ATOM__Long) == 0)
  754. return CARLA_URI_MAP_ID_ATOM_LONG;
  755. if (std::strcmp(uri, LV2_ATOM__Number) == 0)
  756. return CARLA_URI_MAP_ID_ATOM_NUMBER;
  757. if (std::strcmp(uri, LV2_ATOM__Object) == 0)
  758. return CARLA_URI_MAP_ID_ATOM_OBJECT;
  759. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  760. return CARLA_URI_MAP_ID_ATOM_PATH;
  761. if (std::strcmp(uri, LV2_ATOM__Property) == 0)
  762. return CARLA_URI_MAP_ID_ATOM_PROPERTY;
  763. if (std::strcmp(uri, LV2_ATOM__Resource) == 0)
  764. return CARLA_URI_MAP_ID_ATOM_RESOURCE;
  765. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  766. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  767. if (std::strcmp(uri, LV2_ATOM__Sound) == 0)
  768. return CARLA_URI_MAP_ID_ATOM_SOUND;
  769. if (std::strcmp(uri, LV2_ATOM__String) == 0)
  770. return CARLA_URI_MAP_ID_ATOM_STRING;
  771. if (std::strcmp(uri, LV2_ATOM__Tuple) == 0)
  772. return CARLA_URI_MAP_ID_ATOM_TUPLE;
  773. if (std::strcmp(uri, LV2_ATOM__URI) == 0)
  774. return CARLA_URI_MAP_ID_ATOM_URI;
  775. if (std::strcmp(uri, LV2_ATOM__URID) == 0)
  776. return CARLA_URI_MAP_ID_ATOM_URID;
  777. if (std::strcmp(uri, LV2_ATOM__Vector) == 0)
  778. return CARLA_URI_MAP_ID_ATOM_VECTOR;
  779. if (std::strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  780. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  781. if (std::strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  782. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  783. // BufSize types
  784. if (std::strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  785. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  786. if (std::strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  787. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  788. if (std::strcmp(uri, LV2_BUF_SIZE__nominalBlockLength) == 0)
  789. return CARLA_URI_MAP_ID_BUF_NOMINAL_LENGTH;
  790. if (std::strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  791. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  792. // Log types
  793. if (std::strcmp(uri, LV2_LOG__Error) == 0)
  794. return CARLA_URI_MAP_ID_LOG_ERROR;
  795. if (std::strcmp(uri, LV2_LOG__Note) == 0)
  796. return CARLA_URI_MAP_ID_LOG_NOTE;
  797. if (std::strcmp(uri, LV2_LOG__Trace) == 0)
  798. return CARLA_URI_MAP_ID_LOG_TRACE;
  799. if (std::strcmp(uri, LV2_LOG__Warning) == 0)
  800. return CARLA_URI_MAP_ID_LOG_WARNING;
  801. // Time types
  802. if (std::strcmp(uri, LV2_TIME__Position) == 0)
  803. return CARLA_URI_MAP_ID_TIME_POSITION;
  804. if (std::strcmp(uri, LV2_TIME__bar) == 0)
  805. return CARLA_URI_MAP_ID_TIME_BAR;
  806. if (std::strcmp(uri, LV2_TIME__barBeat) == 0)
  807. return CARLA_URI_MAP_ID_TIME_BAR_BEAT;
  808. if (std::strcmp(uri, LV2_TIME__beat) == 0)
  809. return CARLA_URI_MAP_ID_TIME_BEAT;
  810. if (std::strcmp(uri, LV2_TIME__beatUnit) == 0)
  811. return CARLA_URI_MAP_ID_TIME_BEAT_UNIT;
  812. if (std::strcmp(uri, LV2_TIME__beatsPerBar) == 0)
  813. return CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR;
  814. if (std::strcmp(uri, LV2_TIME__beatsPerMinute) == 0)
  815. return CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE;
  816. if (std::strcmp(uri, LV2_TIME__frame) == 0)
  817. return CARLA_URI_MAP_ID_TIME_FRAME;
  818. if (std::strcmp(uri, LV2_TIME__framesPerSecond) == 0)
  819. return CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND;
  820. if (std::strcmp(uri, LV2_TIME__speed) == 0)
  821. return CARLA_URI_MAP_ID_TIME_SPEED;
  822. if (std::strcmp(uri, LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat) == 0)
  823. return CARLA_URI_MAP_ID_TIME_TICKS_PER_BEAT;
  824. // Others
  825. if (std::strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  826. return CARLA_URI_MAP_ID_MIDI_EVENT;
  827. if (std::strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  828. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  829. if (std::strcmp(uri, LV2_UI__windowTitle) == 0)
  830. return CARLA_URI_MAP_ID_UI_WINDOW_TITLE;
  831. // Custom
  832. if (std::strcmp(uri, LV2_KXSTUDIO_PROPERTIES__TransientWindowId) == 0)
  833. return CARLA_URI_MAP_ID_CARLA_TRANSIENT_WIN_ID;
  834. if (std::strcmp(uri, URI_CARLA_ATOM_WORKER) == 0)
  835. return CARLA_URI_MAP_ID_CARLA_ATOM_WORKER;
  836. // Custom types
  837. return ((CarlaLv2Client*)handle)->getCustomURID(uri);
  838. }
  839. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  840. {
  841. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  842. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, nullptr);
  843. carla_debug("carla_lv2_urid_unmap(%p, %i)", handle, urid);
  844. // Atom types
  845. if (urid == CARLA_URI_MAP_ID_ATOM_BLANK)
  846. return LV2_ATOM__Blank;
  847. if (urid == CARLA_URI_MAP_ID_ATOM_BOOL)
  848. return LV2_ATOM__Bool;
  849. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  850. return LV2_ATOM__Chunk;
  851. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  852. return LV2_ATOM__Double;
  853. if (urid == CARLA_URI_MAP_ID_ATOM_EVENT)
  854. return LV2_ATOM__Event;
  855. if (urid == CARLA_URI_MAP_ID_ATOM_FLOAT)
  856. return LV2_ATOM__Float;
  857. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  858. return LV2_ATOM__Int;
  859. if (urid == CARLA_URI_MAP_ID_ATOM_LITERAL)
  860. return LV2_ATOM__Literal;
  861. if (urid == CARLA_URI_MAP_ID_ATOM_LONG)
  862. return LV2_ATOM__Long;
  863. if (urid == CARLA_URI_MAP_ID_ATOM_NUMBER)
  864. return LV2_ATOM__Number;
  865. if (urid == CARLA_URI_MAP_ID_ATOM_OBJECT)
  866. return LV2_ATOM__Object;
  867. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  868. return LV2_ATOM__Path;
  869. if (urid == CARLA_URI_MAP_ID_ATOM_PROPERTY)
  870. return LV2_ATOM__Property;
  871. if (urid == CARLA_URI_MAP_ID_ATOM_RESOURCE)
  872. return LV2_ATOM__Resource;
  873. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  874. return LV2_ATOM__Sequence;
  875. if (urid == CARLA_URI_MAP_ID_ATOM_SOUND)
  876. return LV2_ATOM__Sound;
  877. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  878. return LV2_ATOM__String;
  879. if (urid == CARLA_URI_MAP_ID_ATOM_TUPLE)
  880. return LV2_ATOM__Tuple;
  881. if (urid == CARLA_URI_MAP_ID_ATOM_URI)
  882. return LV2_ATOM__URI;
  883. if (urid == CARLA_URI_MAP_ID_ATOM_URID)
  884. return LV2_ATOM__URID;
  885. if (urid == CARLA_URI_MAP_ID_ATOM_VECTOR)
  886. return LV2_ATOM__Vector;
  887. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  888. return LV2_ATOM__atomTransfer;
  889. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  890. return LV2_ATOM__eventTransfer;
  891. // BufSize types
  892. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  893. return LV2_BUF_SIZE__maxBlockLength;
  894. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  895. return LV2_BUF_SIZE__minBlockLength;
  896. if (urid == CARLA_URI_MAP_ID_BUF_NOMINAL_LENGTH)
  897. return LV2_BUF_SIZE__nominalBlockLength;
  898. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  899. return LV2_BUF_SIZE__sequenceSize;
  900. // Log types
  901. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  902. return LV2_LOG__Error;
  903. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  904. return LV2_LOG__Note;
  905. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  906. return LV2_LOG__Trace;
  907. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  908. return LV2_LOG__Warning;
  909. // Time types
  910. if (urid == CARLA_URI_MAP_ID_TIME_POSITION)
  911. return LV2_TIME__Position;
  912. if (urid == CARLA_URI_MAP_ID_TIME_BAR)
  913. return LV2_TIME__bar;
  914. if (urid == CARLA_URI_MAP_ID_TIME_BAR_BEAT)
  915. return LV2_TIME__barBeat;
  916. if (urid == CARLA_URI_MAP_ID_TIME_BEAT)
  917. return LV2_TIME__beat;
  918. if (urid == CARLA_URI_MAP_ID_TIME_BEAT_UNIT)
  919. return LV2_TIME__beatUnit;
  920. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR)
  921. return LV2_TIME__beatsPerBar;
  922. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE)
  923. return LV2_TIME__beatsPerMinute;
  924. if (urid == CARLA_URI_MAP_ID_TIME_FRAME)
  925. return LV2_TIME__frame;
  926. if (urid == CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND)
  927. return LV2_TIME__framesPerSecond;
  928. if (urid == CARLA_URI_MAP_ID_TIME_SPEED)
  929. return LV2_TIME__speed;
  930. if (urid == CARLA_URI_MAP_ID_TIME_TICKS_PER_BEAT)
  931. return LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat;
  932. // Others
  933. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  934. return LV2_MIDI__MidiEvent;
  935. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  936. return LV2_PARAMETERS__sampleRate;
  937. if (urid == CARLA_URI_MAP_ID_UI_WINDOW_TITLE)
  938. return LV2_UI__windowTitle;
  939. // Custom
  940. if (urid == CARLA_URI_MAP_ID_CARLA_ATOM_WORKER)
  941. return URI_CARLA_ATOM_WORKER;
  942. if (urid == CARLA_URI_MAP_ID_CARLA_TRANSIENT_WIN_ID)
  943. return LV2_KXSTUDIO_PROPERTIES__TransientWindowId;
  944. // Custom types
  945. return ((CarlaLv2Client*)handle)->getCustomURIDString(urid);
  946. }
  947. // -------------------------------------------------------------------
  948. // UI Port-Map Feature
  949. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  950. {
  951. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2UI_INVALID_PORT_INDEX);
  952. carla_debug("carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  953. return ((CarlaLv2Client*)handle)->handleUiPortMap(symbol);
  954. }
  955. // -------------------------------------------------------------------
  956. // UI Resize Feature
  957. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  958. {
  959. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 1);
  960. carla_debug("carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  961. return ((CarlaLv2Client*)handle)->handleUiResize(width, height);
  962. }
  963. // -------------------------------------------------------------------
  964. // UI Extension
  965. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  966. {
  967. CARLA_SAFE_ASSERT_RETURN(controller != nullptr,);
  968. carla_debug("carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  969. ((CarlaLv2Client*)controller)->handleUiWrite(port_index, buffer_size, format, buffer);
  970. }
  971. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaLv2Client)
  972. };
  973. // -----------------------------------------------------------------------
  974. CARLA_BRIDGE_END_NAMESPACE
  975. // -----------------------------------------------------------------------
  976. int main(int argc, const char* argv[])
  977. {
  978. CARLA_BRIDGE_USE_NAMESPACE
  979. if (argc < 3)
  980. {
  981. carla_stderr("usage: %s <plugin-uri> <ui-uri>", argv[0]);
  982. return 1;
  983. }
  984. const bool testingModeOnly = (argc != 7);
  985. // try to get sampleRate value
  986. if (const char* const sampleRateStr = std::getenv("CARLA_SAMPLE_RATE"))
  987. gSampleRate = std::atof(sampleRateStr);
  988. // Init LV2 client
  989. CarlaLv2Client client;
  990. // Load UI
  991. int ret;
  992. if (client.init(argc, argv))
  993. {
  994. client.exec(testingModeOnly);
  995. ret = 0;
  996. }
  997. else
  998. {
  999. ret = 1;
  1000. }
  1001. return ret;
  1002. }
  1003. // -----------------------------------------------------------------------