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.

1211 lines
45KB

  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. {
  195. carla_zeroPointers(fFeatures, kFeatureCount+1);
  196. for (uint32_t i=0; i < CARLA_URI_MAP_ID_COUNT; ++i)
  197. fCustomURIDs.append(nullptr);
  198. // ---------------------------------------------------------------
  199. // initialize options
  200. fLv2Options.minBufferSize = gBufferSize;
  201. fLv2Options.maxBufferSize = gBufferSize;
  202. fLv2Options.sampleRate = gSampleRate;
  203. // ---------------------------------------------------------------
  204. // initialize features (part 1)
  205. LV2_Log_Log* const logFt = new LV2_Log_Log;
  206. logFt->handle = this;
  207. logFt->printf = carla_lv2_log_printf;
  208. logFt->vprintf = carla_lv2_log_vprintf;
  209. LV2_State_Make_Path* const stateMakePathFt = new LV2_State_Make_Path;
  210. stateMakePathFt->handle = this;
  211. stateMakePathFt->path = carla_lv2_state_make_path;
  212. LV2_State_Map_Path* const stateMapPathFt = new LV2_State_Map_Path;
  213. stateMapPathFt->handle = this;
  214. stateMapPathFt->abstract_path = carla_lv2_state_map_abstract_path;
  215. stateMapPathFt->absolute_path = carla_lv2_state_map_absolute_path;
  216. LV2_Programs_Host* const programsFt = new LV2_Programs_Host;
  217. programsFt->handle = this;
  218. programsFt->program_changed = carla_lv2_program_changed;
  219. LV2_URI_Map_Feature* const uriMapFt = new LV2_URI_Map_Feature;
  220. uriMapFt->callback_data = this;
  221. uriMapFt->uri_to_id = carla_lv2_uri_to_id;
  222. LV2_URID_Map* const uridMapFt = new LV2_URID_Map;
  223. uridMapFt->handle = this;
  224. uridMapFt->map = carla_lv2_urid_map;
  225. LV2_URID_Unmap* const uridUnmapFt = new LV2_URID_Unmap;
  226. uridUnmapFt->handle = this;
  227. uridUnmapFt->unmap = carla_lv2_urid_unmap;
  228. LV2UI_Port_Map* const uiPortMapFt = new LV2UI_Port_Map;
  229. uiPortMapFt->handle = this;
  230. uiPortMapFt->port_index = carla_lv2_ui_port_map;
  231. LV2UI_Resize* const uiResizeFt = new LV2UI_Resize;
  232. uiResizeFt->handle = this;
  233. uiResizeFt->ui_resize = carla_lv2_ui_resize;
  234. // ---------------------------------------------------------------
  235. // initialize features (part 2)
  236. for (uint32_t i=0; i < kFeatureCount; ++i)
  237. fFeatures[i] = new LV2_Feature;
  238. fFeatures[kFeatureIdLogs]->URI = LV2_LOG__log;
  239. fFeatures[kFeatureIdLogs]->data = logFt;
  240. fFeatures[kFeatureIdOptions]->URI = LV2_OPTIONS__options;
  241. fFeatures[kFeatureIdOptions]->data = fLv2Options.opts;
  242. fFeatures[kFeatureIdPrograms]->URI = LV2_PROGRAMS__Host;
  243. fFeatures[kFeatureIdPrograms]->data = programsFt;
  244. fFeatures[kFeatureIdStateMakePath]->URI = LV2_STATE__makePath;
  245. fFeatures[kFeatureIdStateMakePath]->data = stateMakePathFt;
  246. fFeatures[kFeatureIdStateMapPath]->URI = LV2_STATE__mapPath;
  247. fFeatures[kFeatureIdStateMapPath]->data = stateMapPathFt;
  248. fFeatures[kFeatureIdUriMap]->URI = LV2_URI_MAP_URI;
  249. fFeatures[kFeatureIdUriMap]->data = uriMapFt;
  250. fFeatures[kFeatureIdUridMap]->URI = LV2_URID__map;
  251. fFeatures[kFeatureIdUridMap]->data = uridMapFt;
  252. fFeatures[kFeatureIdUridUnmap]->URI = LV2_URID__unmap;
  253. fFeatures[kFeatureIdUridUnmap]->data = uridUnmapFt;
  254. fFeatures[kFeatureIdUiIdleInterface]->URI = LV2_UI__idleInterface;
  255. fFeatures[kFeatureIdUiIdleInterface]->data = nullptr;
  256. fFeatures[kFeatureIdUiFixedSize]->URI = LV2_UI__fixedSize;
  257. fFeatures[kFeatureIdUiFixedSize]->data = nullptr;
  258. fFeatures[kFeatureIdUiMakeResident]->URI = LV2_UI__makeResident;
  259. fFeatures[kFeatureIdUiMakeResident]->data = nullptr;
  260. fFeatures[kFeatureIdUiNoUserResize]->URI = LV2_UI__noUserResize;
  261. fFeatures[kFeatureIdUiNoUserResize]->data = nullptr;
  262. fFeatures[kFeatureIdUiParent]->URI = LV2_UI__parent;
  263. fFeatures[kFeatureIdUiParent]->data = nullptr;
  264. fFeatures[kFeatureIdUiPortMap]->URI = LV2_UI__portMap;
  265. fFeatures[kFeatureIdUiPortMap]->data = uiPortMapFt;
  266. fFeatures[kFeatureIdUiPortSubscribe]->URI = LV2_UI__portSubscribe;
  267. fFeatures[kFeatureIdUiPortSubscribe]->data = nullptr;
  268. fFeatures[kFeatureIdUiResize]->URI = LV2_UI__resize;
  269. fFeatures[kFeatureIdUiResize]->data = uiResizeFt;
  270. fFeatures[kFeatureIdUiTouch]->URI = LV2_UI__touch;
  271. fFeatures[kFeatureIdUiTouch]->data = nullptr;
  272. }
  273. ~CarlaLv2Client() override
  274. {
  275. if (fHandle != nullptr && fDescriptor != nullptr && fDescriptor->cleanup != nullptr)
  276. {
  277. fDescriptor->cleanup(fHandle);
  278. fHandle = nullptr;
  279. }
  280. if (fRdfDescriptor != nullptr)
  281. {
  282. delete fRdfDescriptor;
  283. fRdfDescriptor = nullptr;
  284. }
  285. fRdfUiDescriptor = nullptr;
  286. delete (LV2_Log_Log*)fFeatures[kFeatureIdLogs]->data;
  287. delete (LV2_State_Make_Path*)fFeatures[kFeatureIdStateMakePath]->data;
  288. delete (LV2_State_Map_Path*)fFeatures[kFeatureIdStateMapPath]->data;
  289. delete (LV2_Programs_Host*)fFeatures[kFeatureIdPrograms]->data;
  290. delete (LV2_URI_Map_Feature*)fFeatures[kFeatureIdUriMap]->data;
  291. delete (LV2_URID_Map*)fFeatures[kFeatureIdUridMap]->data;
  292. delete (LV2_URID_Unmap*)fFeatures[kFeatureIdUridUnmap]->data;
  293. delete (LV2UI_Port_Map*)fFeatures[kFeatureIdUiPortMap]->data;
  294. delete (LV2UI_Resize*)fFeatures[kFeatureIdUiResize]->data;
  295. for (uint32_t i=0; i < kFeatureCount; ++i)
  296. {
  297. if (fFeatures[i] != nullptr)
  298. {
  299. delete fFeatures[i];
  300. fFeatures[i] = nullptr;
  301. }
  302. }
  303. for (LinkedList<const char*>::Itenerator it = fCustomURIDs.begin2(); it.valid(); it.next())
  304. {
  305. const char* const uri(it.getValue(nullptr));
  306. if (uri != nullptr)
  307. delete[] uri;
  308. }
  309. fCustomURIDs.clear();
  310. }
  311. // ---------------------------------------------------------------------
  312. // UI initialization
  313. bool init(const int argc, const char* argv[]) override
  314. {
  315. const char* pluginURI = argv[1];
  316. const char* uiURI = argv[2];
  317. // -----------------------------------------------------------------
  318. // load plugin
  319. Lv2WorldClass& lv2World(Lv2WorldClass::getInstance());
  320. lv2World.initIfNeeded(std::getenv("LV2_PATH"));
  321. //Lilv::Node bundleNode(lv2World.new_file_uri(nullptr, uiBundle));
  322. //CARLA_SAFE_ASSERT_RETURN(bundleNode.is_uri(), false);
  323. //CarlaString sBundle(bundleNode.as_uri());
  324. //if (! sBundle.endsWith("/"))
  325. // sBundle += "/";
  326. //lv2World.load_bundle(sBundle);
  327. // -----------------------------------------------------------------
  328. // get plugin from lv2_rdf (lilv)
  329. fRdfDescriptor = lv2_rdf_new(pluginURI, true);
  330. CARLA_SAFE_ASSERT_RETURN(fRdfDescriptor != nullptr, false);
  331. // -----------------------------------------------------------------
  332. // find requested UI
  333. for (uint32_t i=0; i < fRdfDescriptor->UICount; ++i)
  334. {
  335. if (std::strcmp(fRdfDescriptor->UIs[i].URI, uiURI) == 0)
  336. {
  337. fRdfUiDescriptor = &fRdfDescriptor->UIs[i];
  338. break;
  339. }
  340. }
  341. CARLA_SAFE_ASSERT_RETURN(fRdfUiDescriptor != nullptr, false);
  342. // -----------------------------------------------------------
  343. // check if not resizable
  344. #if defined(BRIDGE_COCOA) || defined(BRIDGE_HWND) || defined(BRIDGE_X11)
  345. // embed UIs can only be resizable if they provide resize extension
  346. fUiOptions.isResizable = false;
  347. // TODO: put this trick into main carla
  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.options = (const LV2_Options_Interface*)fDescriptor->extension_data(LV2_OPTIONS__interface);
  410. fExt.programs = (const LV2_Programs_UI_Interface*)fDescriptor->extension_data(LV2_PROGRAMS__UIInterface);
  411. fExt.idle = (const LV2UI_Idle_Interface*)fDescriptor->extension_data(LV2_UI__idleInterface);
  412. fExt.resize = (const LV2UI_Resize*)fDescriptor->extension_data(LV2_UI__resize);
  413. // check if invalid
  414. if (fExt.programs != nullptr && fExt.programs->select_program == nullptr)
  415. fExt.programs = nullptr;
  416. if (fExt.idle != nullptr && fExt.idle->idle == nullptr)
  417. fExt.idle = nullptr;
  418. if (fExt.resize != nullptr && fExt.resize->ui_resize == nullptr)
  419. fExt.resize = nullptr;
  420. }
  421. return true;
  422. }
  423. void idleUI() override
  424. {
  425. #if defined(BRIDGE_COCOA) || defined(BRIDGE_HWND) || defined(BRIDGE_X11)
  426. if (fHandle != nullptr && fExt.idle != nullptr)
  427. fExt.idle->idle(fHandle);
  428. #endif
  429. }
  430. // ---------------------------------------------------------------------
  431. // UI management
  432. void* getWidget() const noexcept override
  433. {
  434. return fWidget;
  435. }
  436. const Options& getOptions() const noexcept override
  437. {
  438. return fUiOptions;
  439. }
  440. // ---------------------------------------------------------------------
  441. // DSP Callbacks
  442. void dspParameterChanged(const uint32_t index, const float value) override
  443. {
  444. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,)
  445. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  446. if (fDescriptor->port_event == nullptr)
  447. return;
  448. fDescriptor->port_event(fHandle, index, sizeof(float), CARLA_URI_MAP_ID_NULL, &value);
  449. }
  450. void dspProgramChanged(const uint32_t) override
  451. {
  452. }
  453. void dspMidiProgramChanged(const uint32_t bank, const uint32_t program) override
  454. {
  455. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,)
  456. if (fExt.programs == nullptr)
  457. return;
  458. fExt.programs->select_program(fHandle, bank, program);
  459. }
  460. void dspStateChanged(const char* const, const char* const) override
  461. {
  462. }
  463. void dspNoteReceived(const bool onOff, const uint8_t channel, const uint8_t note, const uint8_t velocity) override
  464. {
  465. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,)
  466. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  467. if (fDescriptor->port_event == nullptr)
  468. return;
  469. LV2_Atom_MidiEvent midiEv;
  470. midiEv.atom.type = CARLA_URI_MAP_ID_MIDI_EVENT;
  471. midiEv.atom.size = 3;
  472. midiEv.data[0] = uint8_t((onOff ? MIDI_STATUS_NOTE_ON : MIDI_STATUS_NOTE_OFF) | (channel & MIDI_CHANNEL_BIT));
  473. midiEv.data[1] = note;
  474. midiEv.data[2] = velocity;
  475. fDescriptor->port_event(fHandle, /* TODO */ 0, lv2_atom_total_size(midiEv), CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, &midiEv);
  476. }
  477. void dspAtomReceived(const uint32_t portIndex, const LV2_Atom* const atom) override
  478. {
  479. CARLA_SAFE_ASSERT_RETURN(fHandle != nullptr,);
  480. CARLA_SAFE_ASSERT_RETURN(fDescriptor != nullptr,);
  481. CARLA_SAFE_ASSERT_RETURN(atom != nullptr,);
  482. if (fDescriptor->port_event == nullptr)
  483. return;
  484. fDescriptor->port_event(fHandle, portIndex, lv2_atom_total_size(atom), CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  485. }
  486. void dspURIDReceived(const LV2_URID urid, const char* const uri)
  487. {
  488. CARLA_SAFE_ASSERT_RETURN(urid == fCustomURIDs.count(),);
  489. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0',);
  490. fCustomURIDs.append(carla_strdup(uri));
  491. }
  492. void uiOptionsChanged(const double sampleRate, const bool useTheme, const bool useThemeColors, const char* const windowTitle, uintptr_t transientWindowId) override
  493. {
  494. carla_debug("CarlaLv2Client::uiOptionsChanged(%g, %s, %s, \"%s\", " P_UINTPTR ")", sampleRate, bool2str(useTheme), bool2str(useThemeColors), windowTitle, transientWindowId);
  495. fLv2Options.sampleRate = gSampleRate = sampleRate;
  496. fUiOptions.useTheme = useTheme;
  497. fUiOptions.useThemeColors = useThemeColors;
  498. fUiOptions.windowTitle = windowTitle;
  499. fUiOptions.transientWindowId = transientWindowId;
  500. }
  501. void uiResized(const uint width, const uint height) override
  502. {
  503. if (fHandle != nullptr && fExt.resize != nullptr)
  504. fExt.resize->ui_resize(fHandle, static_cast<int>(width), static_cast<int>(height));
  505. }
  506. // ---------------------------------------------------------------------
  507. LV2_URID getCustomURID(const char* const uri)
  508. {
  509. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  510. carla_debug("CarlaLv2Client::getCustomURID(\"%s\")", uri);
  511. LV2_URID urid = CARLA_URI_MAP_ID_NULL;
  512. for (uint32_t i=0, count=static_cast<uint32_t>(fCustomURIDs.count()); i<count; ++i)
  513. {
  514. const char* const thisUri(fCustomURIDs.getAt(i, nullptr));
  515. if (thisUri != nullptr && std::strcmp(thisUri, uri) == 0)
  516. {
  517. urid = i;
  518. break;
  519. }
  520. }
  521. if (urid == CARLA_URI_MAP_ID_NULL)
  522. {
  523. urid = static_cast<LV2_URID>(fCustomURIDs.count());
  524. fCustomURIDs.append(carla_strdup(uri));
  525. }
  526. if (isPipeRunning())
  527. writeLv2UridMessage(urid, uri);
  528. return urid;
  529. }
  530. const char* getCustomURIDString(const LV2_URID urid) const noexcept
  531. {
  532. static const char* const sFallback = "urn:null";
  533. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, sFallback);
  534. CARLA_SAFE_ASSERT_RETURN(urid < fCustomURIDs.count(), sFallback);
  535. carla_debug("CarlaLv2Client::getCustomURIDString(%i)", urid);
  536. return fCustomURIDs.getAt(urid, sFallback);
  537. }
  538. // ---------------------------------------------------------------------
  539. void handleProgramChanged(const int32_t /*index*/)
  540. {
  541. if (isPipeRunning())
  542. writeConfigureMessage("reloadprograms", "");
  543. }
  544. uint32_t handleUiPortMap(const char* const symbol)
  545. {
  546. CARLA_SAFE_ASSERT_RETURN(symbol != nullptr && symbol[0] != '\0', LV2UI_INVALID_PORT_INDEX);
  547. carla_debug("CarlaLv2Client::handleUiPortMap(\"%s\")", symbol);
  548. for (uint32_t i=0; i < fRdfDescriptor->PortCount; ++i)
  549. {
  550. if (std::strcmp(fRdfDescriptor->Ports[i].Symbol, symbol) == 0)
  551. return i;
  552. }
  553. return LV2UI_INVALID_PORT_INDEX;
  554. }
  555. int handleUiResize(const int width, const int height)
  556. {
  557. CARLA_SAFE_ASSERT_RETURN(fToolkit != nullptr, 1);
  558. CARLA_SAFE_ASSERT_RETURN(width > 0, 1);
  559. CARLA_SAFE_ASSERT_RETURN(height > 0, 1);
  560. carla_debug("CarlaLv2Client::handleUiResize(%i, %i)", width, height);
  561. fToolkit->setSize(static_cast<uint>(width), static_cast<uint>(height));
  562. return 0;
  563. }
  564. void handleUiWrite(uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  565. {
  566. CARLA_SAFE_ASSERT_RETURN(buffer != nullptr,);
  567. CARLA_SAFE_ASSERT_RETURN(bufferSize > 0,);
  568. carla_debug("CarlaLv2Client::handleUiWrite(%i, %i, %i, %p)", portIndex, bufferSize, format, buffer);
  569. if (format == 0)
  570. {
  571. CARLA_SAFE_ASSERT_RETURN(bufferSize == sizeof(float),);
  572. const float value(*(const float*)buffer);
  573. if (isPipeRunning())
  574. writeControlMessage(portIndex, value);
  575. }
  576. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM || CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  577. {
  578. CARLA_SAFE_ASSERT_RETURN(bufferSize >= sizeof(LV2_Atom),);
  579. const LV2_Atom* const atom((const LV2_Atom*)buffer);
  580. if (isPipeRunning())
  581. writeLv2AtomMessage(portIndex, atom);
  582. }
  583. else
  584. {
  585. carla_stdout("CarlaLv2Client::handleUiWrite(%i, %i, %i:\"%s\", %p) - unknown format", portIndex, bufferSize, format, carla_lv2_urid_unmap(this, format), buffer);
  586. }
  587. }
  588. // ---------------------------------------------------------------------
  589. private:
  590. LV2UI_Handle fHandle;
  591. LV2UI_Widget fWidget;
  592. LV2_Feature* fFeatures[kFeatureCount+1];
  593. const LV2UI_Descriptor* fDescriptor;
  594. const LV2_RDF_Descriptor* fRdfDescriptor;
  595. const LV2_RDF_UI* fRdfUiDescriptor;
  596. Lv2PluginOptions fLv2Options;
  597. Options fUiOptions;
  598. LinkedList<const char*> fCustomURIDs;
  599. struct Extensions {
  600. const LV2_Options_Interface* options;
  601. const LV2_Programs_UI_Interface* programs;
  602. const LV2UI_Idle_Interface* idle;
  603. const LV2UI_Resize* resize;
  604. Extensions()
  605. : options(nullptr),
  606. programs(nullptr),
  607. idle(nullptr),
  608. resize(nullptr) {}
  609. } fExt;
  610. // -------------------------------------------------------------------
  611. // Logs Feature
  612. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  613. {
  614. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 0);
  615. CARLA_SAFE_ASSERT_RETURN(type != CARLA_URI_MAP_ID_NULL, 0);
  616. CARLA_SAFE_ASSERT_RETURN(fmt != nullptr, 0);
  617. #ifndef DEBUG
  618. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  619. return 0;
  620. #endif
  621. va_list args;
  622. va_start(args, fmt);
  623. const int ret(carla_lv2_log_vprintf(handle, type, fmt, args));
  624. va_end(args);
  625. return ret;
  626. }
  627. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  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. int ret = 0;
  637. switch (type)
  638. {
  639. case CARLA_URI_MAP_ID_LOG_ERROR:
  640. std::fprintf(stderr, "\x1b[31m");
  641. ret = std::vfprintf(stderr, fmt, ap);
  642. std::fprintf(stderr, "\x1b[0m");
  643. break;
  644. case CARLA_URI_MAP_ID_LOG_NOTE:
  645. ret = std::vfprintf(stdout, fmt, ap);
  646. break;
  647. case CARLA_URI_MAP_ID_LOG_TRACE:
  648. #ifdef DEBUG
  649. std::fprintf(stdout, "\x1b[30;1m");
  650. ret = std::vfprintf(stdout, fmt, ap);
  651. std::fprintf(stdout, "\x1b[0m");
  652. #endif
  653. break;
  654. case CARLA_URI_MAP_ID_LOG_WARNING:
  655. ret = std::vfprintf(stderr, fmt, ap);
  656. break;
  657. default:
  658. break;
  659. }
  660. return ret;
  661. }
  662. // -------------------------------------------------------------------
  663. // Programs Feature
  664. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  665. {
  666. CARLA_SAFE_ASSERT_RETURN(handle != nullptr,);
  667. carla_debug("carla_lv2_program_changed(%p, %i)", handle, index);
  668. ((CarlaLv2Client*)handle)->handleProgramChanged(index);
  669. }
  670. // -------------------------------------------------------------------
  671. // State Feature
  672. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  673. {
  674. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  675. CARLA_SAFE_ASSERT_RETURN(path != nullptr && path[0] != '\0', nullptr);
  676. carla_debug("carla_lv2_state_make_path(%p, \"%s\")", handle, path);
  677. File file;
  678. if (File::isAbsolutePath(path))
  679. file = File(path);
  680. else
  681. file = File::getCurrentWorkingDirectory().getChildFile(path);
  682. file.getParentDirectory().createDirectory();
  683. return strdup(file.getFullPathName().toRawUTF8());
  684. }
  685. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  686. {
  687. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  688. CARLA_SAFE_ASSERT_RETURN(absolute_path != nullptr && absolute_path[0] != '\0', nullptr);
  689. carla_debug("carla_lv2_state_map_abstract_path(%p, \"%s\")", handle, absolute_path);
  690. // may already be an abstract path
  691. if (! File::isAbsolutePath(absolute_path))
  692. return strdup(absolute_path);
  693. return strdup(File(absolute_path).getRelativePathFrom(File::getCurrentWorkingDirectory()).toRawUTF8());
  694. }
  695. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  696. {
  697. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  698. CARLA_SAFE_ASSERT_RETURN(abstract_path != nullptr && abstract_path[0] != '\0', nullptr);
  699. carla_debug("carla_lv2_state_map_absolute_path(%p, \"%s\")", handle, abstract_path);
  700. // may already be an absolute path
  701. if (File::isAbsolutePath(abstract_path))
  702. return strdup(abstract_path);
  703. return strdup(File::getCurrentWorkingDirectory().getChildFile(abstract_path).getFullPathName().toRawUTF8());
  704. }
  705. // -------------------------------------------------------------------
  706. // URI-Map Feature
  707. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  708. {
  709. carla_debug("carla_lv2_uri_to_id(%p, \"%s\", \"%s\")", data, map, uri);
  710. return carla_lv2_urid_map((LV2_URID_Map_Handle*)data, uri);
  711. // unused
  712. (void)map;
  713. }
  714. // -------------------------------------------------------------------
  715. // URID Feature
  716. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  717. {
  718. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, CARLA_URI_MAP_ID_NULL);
  719. CARLA_SAFE_ASSERT_RETURN(uri != nullptr && uri[0] != '\0', CARLA_URI_MAP_ID_NULL);
  720. carla_debug("carla_lv2_urid_map(%p, \"%s\")", handle, uri);
  721. // Atom types
  722. if (std::strcmp(uri, LV2_ATOM__Blank) == 0)
  723. return CARLA_URI_MAP_ID_ATOM_BLANK;
  724. if (std::strcmp(uri, LV2_ATOM__Bool) == 0)
  725. return CARLA_URI_MAP_ID_ATOM_BOOL;
  726. if (std::strcmp(uri, LV2_ATOM__Chunk) == 0)
  727. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  728. if (std::strcmp(uri, LV2_ATOM__Double) == 0)
  729. return CARLA_URI_MAP_ID_ATOM_DOUBLE;
  730. if (std::strcmp(uri, LV2_ATOM__Event) == 0)
  731. return CARLA_URI_MAP_ID_ATOM_EVENT;
  732. if (std::strcmp(uri, LV2_ATOM__Float) == 0)
  733. return CARLA_URI_MAP_ID_ATOM_FLOAT;
  734. if (std::strcmp(uri, LV2_ATOM__Int) == 0)
  735. return CARLA_URI_MAP_ID_ATOM_INT;
  736. if (std::strcmp(uri, LV2_ATOM__Literal) == 0)
  737. return CARLA_URI_MAP_ID_ATOM_LITERAL;
  738. if (std::strcmp(uri, LV2_ATOM__Long) == 0)
  739. return CARLA_URI_MAP_ID_ATOM_LONG;
  740. if (std::strcmp(uri, LV2_ATOM__Number) == 0)
  741. return CARLA_URI_MAP_ID_ATOM_NUMBER;
  742. if (std::strcmp(uri, LV2_ATOM__Object) == 0)
  743. return CARLA_URI_MAP_ID_ATOM_OBJECT;
  744. if (std::strcmp(uri, LV2_ATOM__Path) == 0)
  745. return CARLA_URI_MAP_ID_ATOM_PATH;
  746. if (std::strcmp(uri, LV2_ATOM__Property) == 0)
  747. return CARLA_URI_MAP_ID_ATOM_PROPERTY;
  748. if (std::strcmp(uri, LV2_ATOM__Resource) == 0)
  749. return CARLA_URI_MAP_ID_ATOM_RESOURCE;
  750. if (std::strcmp(uri, LV2_ATOM__Sequence) == 0)
  751. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  752. if (std::strcmp(uri, LV2_ATOM__Sound) == 0)
  753. return CARLA_URI_MAP_ID_ATOM_SOUND;
  754. if (std::strcmp(uri, LV2_ATOM__String) == 0)
  755. return CARLA_URI_MAP_ID_ATOM_STRING;
  756. if (std::strcmp(uri, LV2_ATOM__Tuple) == 0)
  757. return CARLA_URI_MAP_ID_ATOM_TUPLE;
  758. if (std::strcmp(uri, LV2_ATOM__URI) == 0)
  759. return CARLA_URI_MAP_ID_ATOM_URI;
  760. if (std::strcmp(uri, LV2_ATOM__URID) == 0)
  761. return CARLA_URI_MAP_ID_ATOM_URID;
  762. if (std::strcmp(uri, LV2_ATOM__Vector) == 0)
  763. return CARLA_URI_MAP_ID_ATOM_VECTOR;
  764. if (std::strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  765. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  766. if (std::strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  767. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  768. // BufSize types
  769. if (std::strcmp(uri, LV2_BUF_SIZE__maxBlockLength) == 0)
  770. return CARLA_URI_MAP_ID_BUF_MAX_LENGTH;
  771. if (std::strcmp(uri, LV2_BUF_SIZE__minBlockLength) == 0)
  772. return CARLA_URI_MAP_ID_BUF_MIN_LENGTH;
  773. if (std::strcmp(uri, LV2_BUF_SIZE__sequenceSize) == 0)
  774. return CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE;
  775. // Log types
  776. if (std::strcmp(uri, LV2_LOG__Error) == 0)
  777. return CARLA_URI_MAP_ID_LOG_ERROR;
  778. if (std::strcmp(uri, LV2_LOG__Note) == 0)
  779. return CARLA_URI_MAP_ID_LOG_NOTE;
  780. if (std::strcmp(uri, LV2_LOG__Trace) == 0)
  781. return CARLA_URI_MAP_ID_LOG_TRACE;
  782. if (std::strcmp(uri, LV2_LOG__Warning) == 0)
  783. return CARLA_URI_MAP_ID_LOG_WARNING;
  784. // Time types
  785. if (std::strcmp(uri, LV2_TIME__Position) == 0)
  786. return CARLA_URI_MAP_ID_TIME_POSITION;
  787. if (std::strcmp(uri, LV2_TIME__bar) == 0)
  788. return CARLA_URI_MAP_ID_TIME_BAR;
  789. if (std::strcmp(uri, LV2_TIME__barBeat) == 0)
  790. return CARLA_URI_MAP_ID_TIME_BAR_BEAT;
  791. if (std::strcmp(uri, LV2_TIME__beat) == 0)
  792. return CARLA_URI_MAP_ID_TIME_BEAT;
  793. if (std::strcmp(uri, LV2_TIME__beatUnit) == 0)
  794. return CARLA_URI_MAP_ID_TIME_BEAT_UNIT;
  795. if (std::strcmp(uri, LV2_TIME__beatsPerBar) == 0)
  796. return CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR;
  797. if (std::strcmp(uri, LV2_TIME__beatsPerMinute) == 0)
  798. return CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE;
  799. if (std::strcmp(uri, LV2_TIME__frame) == 0)
  800. return CARLA_URI_MAP_ID_TIME_FRAME;
  801. if (std::strcmp(uri, LV2_TIME__framesPerSecond) == 0)
  802. return CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND;
  803. if (std::strcmp(uri, LV2_TIME__speed) == 0)
  804. return CARLA_URI_MAP_ID_TIME_SPEED;
  805. if (std::strcmp(uri, LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat) == 0)
  806. return CARLA_URI_MAP_ID_TIME_TICKS_PER_BEAT;
  807. // Others
  808. if (std::strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  809. return CARLA_URI_MAP_ID_MIDI_EVENT;
  810. if (std::strcmp(uri, LV2_PARAMETERS__sampleRate) == 0)
  811. return CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE;
  812. if (std::strcmp(uri, LV2_UI__windowTitle) == 0)
  813. return CARLA_URI_MAP_ID_UI_WINDOW_TITLE;
  814. // Custom
  815. if (std::strcmp(uri, LV2_KXSTUDIO_PROPERTIES__TransientWindowId) == 0)
  816. return CARLA_URI_MAP_ID_CARLA_TRANSIENT_WIN_ID;
  817. if (std::strcmp(uri, URI_CARLA_ATOM_WORKER) == 0)
  818. return CARLA_URI_MAP_ID_CARLA_ATOM_WORKER;
  819. // Custom types
  820. return ((CarlaLv2Client*)handle)->getCustomURID(uri);
  821. }
  822. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  823. {
  824. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, nullptr);
  825. CARLA_SAFE_ASSERT_RETURN(urid != CARLA_URI_MAP_ID_NULL, nullptr);
  826. carla_debug("carla_lv2_urid_unmap(%p, %i)", handle, urid);
  827. // Atom types
  828. if (urid == CARLA_URI_MAP_ID_ATOM_BLANK)
  829. return LV2_ATOM__Blank;
  830. if (urid == CARLA_URI_MAP_ID_ATOM_BOOL)
  831. return LV2_ATOM__Bool;
  832. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  833. return LV2_ATOM__Chunk;
  834. if (urid == CARLA_URI_MAP_ID_ATOM_DOUBLE)
  835. return LV2_ATOM__Double;
  836. if (urid == CARLA_URI_MAP_ID_ATOM_EVENT)
  837. return LV2_ATOM__Event;
  838. if (urid == CARLA_URI_MAP_ID_ATOM_FLOAT)
  839. return LV2_ATOM__Float;
  840. if (urid == CARLA_URI_MAP_ID_ATOM_INT)
  841. return LV2_ATOM__Int;
  842. if (urid == CARLA_URI_MAP_ID_ATOM_LITERAL)
  843. return LV2_ATOM__Literal;
  844. if (urid == CARLA_URI_MAP_ID_ATOM_LONG)
  845. return LV2_ATOM__Long;
  846. if (urid == CARLA_URI_MAP_ID_ATOM_NUMBER)
  847. return LV2_ATOM__Number;
  848. if (urid == CARLA_URI_MAP_ID_ATOM_OBJECT)
  849. return LV2_ATOM__Object;
  850. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  851. return LV2_ATOM__Path;
  852. if (urid == CARLA_URI_MAP_ID_ATOM_PROPERTY)
  853. return LV2_ATOM__Property;
  854. if (urid == CARLA_URI_MAP_ID_ATOM_RESOURCE)
  855. return LV2_ATOM__Resource;
  856. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  857. return LV2_ATOM__Sequence;
  858. if (urid == CARLA_URI_MAP_ID_ATOM_SOUND)
  859. return LV2_ATOM__Sound;
  860. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  861. return LV2_ATOM__String;
  862. if (urid == CARLA_URI_MAP_ID_ATOM_TUPLE)
  863. return LV2_ATOM__Tuple;
  864. if (urid == CARLA_URI_MAP_ID_ATOM_URI)
  865. return LV2_ATOM__URI;
  866. if (urid == CARLA_URI_MAP_ID_ATOM_URID)
  867. return LV2_ATOM__URID;
  868. if (urid == CARLA_URI_MAP_ID_ATOM_VECTOR)
  869. return LV2_ATOM__Vector;
  870. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  871. return LV2_ATOM__atomTransfer;
  872. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  873. return LV2_ATOM__eventTransfer;
  874. // BufSize types
  875. if (urid == CARLA_URI_MAP_ID_BUF_MAX_LENGTH)
  876. return LV2_BUF_SIZE__maxBlockLength;
  877. if (urid == CARLA_URI_MAP_ID_BUF_MIN_LENGTH)
  878. return LV2_BUF_SIZE__minBlockLength;
  879. if (urid == CARLA_URI_MAP_ID_BUF_SEQUENCE_SIZE)
  880. return LV2_BUF_SIZE__sequenceSize;
  881. // Log types
  882. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  883. return LV2_LOG__Error;
  884. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  885. return LV2_LOG__Note;
  886. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  887. return LV2_LOG__Trace;
  888. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  889. return LV2_LOG__Warning;
  890. // Time types
  891. if (urid == CARLA_URI_MAP_ID_TIME_POSITION)
  892. return LV2_TIME__Position;
  893. if (urid == CARLA_URI_MAP_ID_TIME_BAR)
  894. return LV2_TIME__bar;
  895. if (urid == CARLA_URI_MAP_ID_TIME_BAR_BEAT)
  896. return LV2_TIME__barBeat;
  897. if (urid == CARLA_URI_MAP_ID_TIME_BEAT)
  898. return LV2_TIME__beat;
  899. if (urid == CARLA_URI_MAP_ID_TIME_BEAT_UNIT)
  900. return LV2_TIME__beatUnit;
  901. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_BAR)
  902. return LV2_TIME__beatsPerBar;
  903. if (urid == CARLA_URI_MAP_ID_TIME_BEATS_PER_MINUTE)
  904. return LV2_TIME__beatsPerMinute;
  905. if (urid == CARLA_URI_MAP_ID_TIME_FRAME)
  906. return LV2_TIME__frame;
  907. if (urid == CARLA_URI_MAP_ID_TIME_FRAMES_PER_SECOND)
  908. return LV2_TIME__framesPerSecond;
  909. if (urid == CARLA_URI_MAP_ID_TIME_SPEED)
  910. return LV2_TIME__speed;
  911. if (urid == CARLA_URI_MAP_ID_TIME_TICKS_PER_BEAT)
  912. return LV2_KXSTUDIO_PROPERTIES__TimePositionTicksPerBeat;
  913. // Others
  914. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  915. return LV2_MIDI__MidiEvent;
  916. if (urid == CARLA_URI_MAP_ID_PARAM_SAMPLE_RATE)
  917. return LV2_PARAMETERS__sampleRate;
  918. if (urid == CARLA_URI_MAP_ID_UI_WINDOW_TITLE)
  919. return LV2_UI__windowTitle;
  920. // Custom
  921. if (urid == CARLA_URI_MAP_ID_CARLA_ATOM_WORKER)
  922. return URI_CARLA_ATOM_WORKER;
  923. if (urid == CARLA_URI_MAP_ID_CARLA_TRANSIENT_WIN_ID)
  924. return LV2_KXSTUDIO_PROPERTIES__TransientWindowId;
  925. // Custom types
  926. return ((CarlaLv2Client*)handle)->getCustomURIDString(urid);
  927. }
  928. // -------------------------------------------------------------------
  929. // UI Port-Map Feature
  930. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  931. {
  932. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, LV2UI_INVALID_PORT_INDEX);
  933. carla_debug("carla_lv2_ui_port_map(%p, \"%s\")", handle, symbol);
  934. return ((CarlaLv2Client*)handle)->handleUiPortMap(symbol);
  935. }
  936. // -------------------------------------------------------------------
  937. // UI Resize Feature
  938. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  939. {
  940. CARLA_SAFE_ASSERT_RETURN(handle != nullptr, 1);
  941. carla_debug("carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  942. return ((CarlaLv2Client*)handle)->handleUiResize(width, height);
  943. }
  944. // -------------------------------------------------------------------
  945. // UI Extension
  946. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  947. {
  948. CARLA_SAFE_ASSERT_RETURN(controller != nullptr,);
  949. carla_debug("carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  950. ((CarlaLv2Client*)controller)->handleUiWrite(port_index, buffer_size, format, buffer);
  951. }
  952. CARLA_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(CarlaLv2Client)
  953. };
  954. // -----------------------------------------------------------------------
  955. CARLA_BRIDGE_END_NAMESPACE
  956. // -----------------------------------------------------------------------
  957. int main(int argc, const char* argv[])
  958. {
  959. CARLA_BRIDGE_USE_NAMESPACE
  960. if (argc < 3)
  961. {
  962. carla_stderr("usage: %s <plugin-uri> <ui-uri>", argv[0]);
  963. return 1;
  964. }
  965. const bool testingModeOnly = (argc != 7);
  966. // try to get sampleRate value
  967. if (const char* const sampleRateStr = std::getenv("CARLA_SAMPLE_RATE"))
  968. gSampleRate = std::atof(sampleRateStr);
  969. // Init LV2 client
  970. CarlaLv2Client client;
  971. // Load UI
  972. int ret;
  973. if (client.init(argc, argv))
  974. {
  975. client.exec(testingModeOnly);
  976. ret = 0;
  977. }
  978. else
  979. {
  980. ret = 1;
  981. }
  982. return ret;
  983. }
  984. // -----------------------------------------------------------------------