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.

1199 lines
44KB

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