Collection of tools useful for audio production
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.

921 lines
30KB

  1. /*
  2. * Carla UI bridge code
  3. * Copyright (C) 2011-2012 Filipe Coelho <falktx@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * 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 COPYING file
  16. */
  17. #ifdef BRIDGE_LV2
  18. #include "carla_bridge_client.h"
  19. #include "carla_lv2.h"
  20. #include "carla_midi.h"
  21. #include <vector>
  22. #include <QtCore/QDir>
  23. #ifdef BRIDGE_LV2_X11
  24. # include <QtGui/QDialog>
  25. #endif
  26. CARLA_BRIDGE_START_NAMESPACE
  27. // -------------------------------------------------------------------------
  28. // fake values
  29. uint32_t bufferSize = 512;
  30. double sampleRate = 44100.0;
  31. // feature ids
  32. const uint32_t lv2_feature_id_event = 0;
  33. const uint32_t lv2_feature_id_logs = 1;
  34. const uint32_t lv2_feature_id_programs = 2;
  35. const uint32_t lv2_feature_id_state_make_path = 3;
  36. const uint32_t lv2_feature_id_state_map_path = 4;
  37. const uint32_t lv2_feature_id_strict_bounds = 5;
  38. const uint32_t lv2_feature_id_uri_map = 6;
  39. const uint32_t lv2_feature_id_urid_map = 7;
  40. const uint32_t lv2_feature_id_urid_unmap = 8;
  41. const uint32_t lv2_feature_id_ui_parent = 9;
  42. const uint32_t lv2_feature_id_ui_port_map = 10;
  43. const uint32_t lv2_feature_id_ui_resize = 11;
  44. const uint32_t lv2_feature_count = 12;
  45. // pre-set uri[d] map ids
  46. const uint32_t CARLA_URI_MAP_ID_NULL = 0;
  47. const uint32_t CARLA_URI_MAP_ID_ATOM_CHUNK = 1;
  48. const uint32_t CARLA_URI_MAP_ID_ATOM_PATH = 2;
  49. const uint32_t CARLA_URI_MAP_ID_ATOM_SEQUENCE = 3;
  50. const uint32_t CARLA_URI_MAP_ID_ATOM_STRING = 4;
  51. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM = 5;
  52. const uint32_t CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT = 6;
  53. const uint32_t CARLA_URI_MAP_ID_LOG_ERROR = 7;
  54. const uint32_t CARLA_URI_MAP_ID_LOG_NOTE = 8;
  55. const uint32_t CARLA_URI_MAP_ID_LOG_TRACE = 9;
  56. const uint32_t CARLA_URI_MAP_ID_LOG_WARNING = 10;
  57. const uint32_t CARLA_URI_MAP_ID_MIDI_EVENT = 11;
  58. const uint32_t CARLA_URI_MAP_ID_COUNT = 12;
  59. // -------------------------------------------------------------------------
  60. class CarlaLv2Client : public CarlaClient
  61. {
  62. public:
  63. CarlaLv2Client(CarlaToolkit* const toolkit)
  64. : CarlaClient(toolkit)
  65. {
  66. handle = nullptr;
  67. widget = nullptr;
  68. descriptor = nullptr;
  69. rdf_descriptor = nullptr;
  70. rdf_ui_descriptor = nullptr;
  71. programs = nullptr;
  72. #ifdef BRIDGE_LV2_X11
  73. m_resizable = false;
  74. x11_widget = new QDialog;
  75. #else
  76. m_resizable = true;
  77. #endif
  78. for (uint32_t i=0; i < CARLA_URI_MAP_ID_COUNT; i++)
  79. customURIDs.push_back(nullptr);
  80. for (uint32_t i=0; i < lv2_feature_count+1; i++)
  81. features[i] = nullptr;
  82. // -----------------------------------------------------------------
  83. // initialize features
  84. LV2_Event_Feature* Event_Feature = new LV2_Event_Feature;
  85. Event_Feature->callback_data = this;
  86. Event_Feature->lv2_event_ref = carla_lv2_event_ref;
  87. Event_Feature->lv2_event_unref = carla_lv2_event_unref;
  88. LV2_Log_Log* Log_Feature = new LV2_Log_Log;
  89. Log_Feature->handle = this;
  90. Log_Feature->printf = carla_lv2_log_printf;
  91. Log_Feature->vprintf = carla_lv2_log_vprintf;
  92. LV2_Programs_Host* Programs_Feature = new LV2_Programs_Host;
  93. Programs_Feature->handle = this;
  94. Programs_Feature->program_changed = carla_lv2_program_changed;
  95. LV2_State_Make_Path* State_MakePath_Feature = new LV2_State_Make_Path;
  96. State_MakePath_Feature->handle = this;
  97. State_MakePath_Feature->path = carla_lv2_state_make_path;
  98. LV2_State_Map_Path* State_MapPath_Feature = new LV2_State_Map_Path;
  99. State_MapPath_Feature->handle = this;
  100. State_MapPath_Feature->abstract_path = carla_lv2_state_map_abstract_path;
  101. State_MapPath_Feature->absolute_path = carla_lv2_state_map_absolute_path;
  102. LV2_URI_Map_Feature* URI_Map_Feature = new LV2_URI_Map_Feature;
  103. URI_Map_Feature->callback_data = this;
  104. URI_Map_Feature->uri_to_id = carla_lv2_uri_to_id;
  105. LV2_URID_Map* URID_Map_Feature = new LV2_URID_Map;
  106. URID_Map_Feature->handle = this;
  107. URID_Map_Feature->map = carla_lv2_urid_map;
  108. LV2_URID_Unmap* URID_Unmap_Feature = new LV2_URID_Unmap;
  109. URID_Unmap_Feature->handle = this;
  110. URID_Unmap_Feature->unmap = carla_lv2_urid_unmap;
  111. LV2UI_Port_Map* UI_PortMap_Feature = new LV2UI_Port_Map;
  112. UI_PortMap_Feature->handle = this;
  113. UI_PortMap_Feature->port_index = carla_lv2_ui_port_map;
  114. LV2UI_Resize* UI_Resize_Feature = new LV2UI_Resize;
  115. UI_Resize_Feature->handle = this;
  116. UI_Resize_Feature->ui_resize = carla_lv2_ui_resize;
  117. features[lv2_feature_id_event] = new LV2_Feature;
  118. features[lv2_feature_id_event]->URI = LV2_EVENT_URI;
  119. features[lv2_feature_id_event]->data = Event_Feature;
  120. features[lv2_feature_id_logs] = new LV2_Feature;
  121. features[lv2_feature_id_logs]->URI = LV2_LOG__log;
  122. features[lv2_feature_id_logs]->data = Log_Feature;
  123. features[lv2_feature_id_programs] = new LV2_Feature;
  124. features[lv2_feature_id_programs]->URI = LV2_PROGRAMS__Host;
  125. features[lv2_feature_id_programs]->data = Programs_Feature;
  126. features[lv2_feature_id_state_make_path] = new LV2_Feature;
  127. features[lv2_feature_id_state_make_path]->URI = LV2_STATE__makePath;
  128. features[lv2_feature_id_state_make_path]->data = State_MakePath_Feature;
  129. features[lv2_feature_id_state_map_path] = new LV2_Feature;
  130. features[lv2_feature_id_state_map_path]->URI = LV2_STATE__mapPath;
  131. features[lv2_feature_id_state_map_path]->data = State_MapPath_Feature;
  132. features[lv2_feature_id_strict_bounds] = new LV2_Feature;
  133. features[lv2_feature_id_strict_bounds]->URI = LV2_PORT_PROPS__supportsStrictBounds;
  134. features[lv2_feature_id_strict_bounds]->data = nullptr;
  135. features[lv2_feature_id_uri_map] = new LV2_Feature;
  136. features[lv2_feature_id_uri_map]->URI = LV2_URI_MAP_URI;
  137. features[lv2_feature_id_uri_map]->data = URI_Map_Feature;
  138. features[lv2_feature_id_urid_map] = new LV2_Feature;
  139. features[lv2_feature_id_urid_map]->URI = LV2_URID__map;
  140. features[lv2_feature_id_urid_map]->data = URID_Map_Feature;
  141. features[lv2_feature_id_urid_unmap] = new LV2_Feature;
  142. features[lv2_feature_id_urid_unmap]->URI = LV2_URID__unmap;
  143. features[lv2_feature_id_urid_unmap]->data = URID_Unmap_Feature;
  144. features[lv2_feature_id_ui_parent] = new LV2_Feature;
  145. features[lv2_feature_id_ui_parent]->URI = LV2_UI__parent;
  146. #ifdef BRIDGE_LV2_X11
  147. features[lv2_feature_id_ui_parent]->data = (void*)x11_widget->winId();
  148. #else
  149. features[lv2_feature_id_ui_parent]->data = nullptr;
  150. #endif
  151. features[lv2_feature_id_ui_port_map] = new LV2_Feature;
  152. features[lv2_feature_id_ui_port_map]->URI = LV2_UI__portMap;
  153. features[lv2_feature_id_ui_port_map]->data = UI_PortMap_Feature;
  154. features[lv2_feature_id_ui_resize] = new LV2_Feature;
  155. features[lv2_feature_id_ui_resize]->URI = LV2_UI__resize;
  156. features[lv2_feature_id_ui_resize]->data = UI_Resize_Feature;
  157. }
  158. ~CarlaLv2Client()
  159. {
  160. if (rdf_descriptor)
  161. lv2_rdf_free(rdf_descriptor);
  162. delete (LV2_Event_Feature*)features[lv2_feature_id_event]->data;
  163. delete (LV2_Log_Log*)features[lv2_feature_id_logs]->data;
  164. delete (LV2_Programs_Host*)features[lv2_feature_id_programs]->data;
  165. delete (LV2_State_Make_Path*)features[lv2_feature_id_state_make_path]->data;
  166. delete (LV2_State_Map_Path*)features[lv2_feature_id_state_map_path]->data;
  167. delete (LV2_URI_Map_Feature*)features[lv2_feature_id_uri_map]->data;
  168. delete (LV2_URID_Map*)features[lv2_feature_id_urid_map]->data;
  169. delete (LV2_URID_Unmap*)features[lv2_feature_id_urid_unmap]->data;
  170. delete (LV2UI_Port_Map*)features[lv2_feature_id_ui_port_map]->data;
  171. delete (LV2UI_Resize*)features[lv2_feature_id_ui_resize]->data;
  172. for (uint32_t i=0; i < lv2_feature_count; i++)
  173. {
  174. if (features[i])
  175. delete features[i];
  176. }
  177. for (size_t i=0; i < customURIDs.size(); i++)
  178. {
  179. if (customURIDs[i])
  180. free((void*)customURIDs[i]);
  181. }
  182. customURIDs.clear();
  183. }
  184. // ---------------------------------------------------------------------
  185. // ui initialization
  186. bool init(const char* plugin_uri, const char* ui_uri)
  187. {
  188. // -----------------------------------------------------------------
  189. // get plugin from lv2_rdf (lilv)
  190. Lv2World.init();
  191. rdf_descriptor = lv2_rdf_new(plugin_uri);
  192. if (! rdf_descriptor)
  193. return false;
  194. // -----------------------------------------------------------------
  195. // find requested UI
  196. for (uint32_t i=0; i < rdf_descriptor->UICount; i++)
  197. {
  198. if (strcmp(rdf_descriptor->UIs[i].URI, ui_uri) == 0)
  199. {
  200. rdf_ui_descriptor = &rdf_descriptor->UIs[i];
  201. break;
  202. }
  203. }
  204. if (! rdf_ui_descriptor)
  205. return false;
  206. // -----------------------------------------------------------------
  207. // open DLL
  208. if (! libOpen(rdf_ui_descriptor->Binary))
  209. return false;
  210. // -----------------------------------------------------------------
  211. // get DLL main entry
  212. LV2UI_DescriptorFunction const ui_descfn = (LV2UI_DescriptorFunction)libSymbol("lv2ui_descriptor");
  213. if (! ui_descfn)
  214. return false;
  215. // -----------------------------------------------------------
  216. // get descriptor that matches URI
  217. uint32_t i = 0;
  218. while ((descriptor = ui_descfn(i++)))
  219. {
  220. if (strcmp(descriptor->URI, ui_uri) == 0)
  221. break;
  222. }
  223. if (! descriptor)
  224. return false;
  225. // -----------------------------------------------------------
  226. // initialize UI
  227. handle = descriptor->instantiate(descriptor, plugin_uri, rdf_ui_descriptor->Bundle, carla_lv2_ui_write_function, this, &widget, features);
  228. if (! handle)
  229. return false;
  230. // -----------------------------------------------------------
  231. // check if not resizable
  232. #ifndef BRIDGE_LV2_X11
  233. for (uint32_t i=0; i < rdf_ui_descriptor->FeatureCount; i++)
  234. {
  235. if (strcmp(rdf_ui_descriptor->Features[i].URI, LV2_UI__fixedSize) == 0 || strcmp(rdf_ui_descriptor->Features[i].URI, LV2_UI__noUserResize) == 0)
  236. {
  237. m_resizable = false;
  238. break;
  239. }
  240. }
  241. #endif
  242. // -----------------------------------------------------------
  243. // check for known extensions
  244. for (uint32_t i=0; descriptor->extension_data && i < rdf_ui_descriptor->ExtensionCount; i++)
  245. {
  246. if (strcmp(rdf_ui_descriptor->Extensions[i], LV2_PROGRAMS__UIInterface) == 0)
  247. {
  248. programs = (LV2_Programs_UI_Interface*)descriptor->extension_data(LV2_PROGRAMS__UIInterface);
  249. break;
  250. }
  251. }
  252. return true;
  253. }
  254. void close()
  255. {
  256. if (handle && descriptor && descriptor->cleanup)
  257. descriptor->cleanup(handle);
  258. libClose();
  259. }
  260. // ---------------------------------------------------------------------
  261. // ui management
  262. void* getWidget() const
  263. {
  264. #ifdef BRIDGE_LV2_X11
  265. return x11_widget;
  266. #else
  267. return widget;
  268. #endif
  269. }
  270. bool isResizable() const
  271. {
  272. return m_resizable;
  273. }
  274. bool needsReparent() const
  275. {
  276. #ifdef BRIDGE_LV2_X11
  277. return true;
  278. #else
  279. return false;
  280. #endif
  281. }
  282. // ---------------------------------------------------------------------
  283. // processing
  284. void setParameter(const int32_t rindex, const double value)
  285. {
  286. Q_ASSERT(handle && descriptor);
  287. if (handle && descriptor && descriptor->port_event)
  288. {
  289. float fvalue = value;
  290. descriptor->port_event(handle, rindex, sizeof(float), 0, &fvalue);
  291. }
  292. }
  293. void setProgram(const uint32_t)
  294. {
  295. }
  296. void setMidiProgram(const uint32_t bank, const uint32_t program)
  297. {
  298. Q_ASSERT(handle);
  299. if (handle && programs)
  300. programs->select_program(handle, bank, program);
  301. }
  302. void noteOn(const uint8_t channel, const uint8_t note, const uint8_t velo)
  303. {
  304. Q_ASSERT(handle && descriptor);
  305. if (handle && descriptor && descriptor->port_event)
  306. {
  307. uint8_t buf[3] = { uint8_t(0x90 + channel), note, velo };
  308. descriptor->port_event(handle, 0, 3, CARLA_URI_MAP_ID_MIDI_EVENT, buf);
  309. }
  310. }
  311. void noteOff(const uint8_t channel, const uint8_t note)
  312. {
  313. Q_ASSERT(handle && descriptor);
  314. if (handle && descriptor && descriptor->port_event)
  315. {
  316. uint8_t buf[3] = { uint8_t(0x80 + channel), note, 0 };
  317. descriptor->port_event(handle, 0, 3, CARLA_URI_MAP_ID_MIDI_EVENT, buf);
  318. }
  319. }
  320. // ---------------------------------------------------------------------
  321. uint32_t getCustomURID(const char* const uri)
  322. {
  323. qDebug("CarlaLv2Client::getCustomURID(%s)", uri);
  324. Q_ASSERT(uri);
  325. for (size_t i=0; i < customURIDs.size(); i++)
  326. {
  327. if (customURIDs[i] && strcmp(customURIDs[i], uri) == 0)
  328. return i;
  329. }
  330. customURIDs.push_back(strdup(uri));
  331. return customURIDs.size()-1;
  332. }
  333. const char* getCustomURIString(LV2_URID urid)
  334. {
  335. qDebug("CarlaLv2Client::getCustomURIString(%i)", urid);
  336. Q_ASSERT(urid != 0);
  337. if (urid < customURIDs.size())
  338. return customURIDs[urid];
  339. return nullptr;
  340. }
  341. void handleTransferAtom(const char* const type, const char* const value)
  342. {
  343. qDebug("CarlaLv2Client::handleTransferEvent(%s, %s)", type, value);
  344. Q_ASSERT(type);
  345. Q_ASSERT(value);
  346. }
  347. void handleTransferEvent(const char* const type, const char* const value)
  348. {
  349. qDebug("CarlaLv2Client::handleTransferEvent(%s, %s)", type, value);
  350. Q_ASSERT(type);
  351. Q_ASSERT(value);
  352. if (handle && descriptor && descriptor->port_event)
  353. {
  354. LV2_URID_Map* const URID_Map = (LV2_URID_Map*)features[lv2_feature_id_urid_map]->data;
  355. const LV2_URID uridPatchSet = getCustomURID(LV2_PATCH__Set);
  356. const LV2_URID uridPatchBody = getCustomURID(LV2_PATCH__body);
  357. Sratom* sratom = sratom_new(URID_Map);
  358. SerdChunk chunk = { nullptr, 0 };
  359. LV2_Atom_Forge forge;
  360. lv2_atom_forge_init(&forge, URID_Map);
  361. lv2_atom_forge_set_sink(&forge, sratom_forge_sink, sratom_forge_deref, &chunk);
  362. LV2_Atom_Forge_Frame refFrame, bodyFrame;
  363. LV2_Atom_Forge_Ref ref = lv2_atom_forge_blank(&forge, &refFrame, 1, uridPatchSet);
  364. lv2_atom_forge_property_head(&forge, uridPatchBody, CARLA_URI_MAP_ID_NULL);
  365. lv2_atom_forge_blank(&forge, &bodyFrame, 2, CARLA_URI_MAP_ID_NULL);
  366. //lv2_atom_forge_property_head(&forge, getCustomURID(key), CARLA_URI_MAP_ID_NULL);
  367. if (strcmp(type, "string") == 0)
  368. lv2_atom_forge_string(&forge, value, strlen(value));
  369. else if (strcmp(type, "path") == 0)
  370. lv2_atom_forge_path(&forge, value, strlen(value));
  371. else if (strcmp(type, "chunk") == 0)
  372. lv2_atom_forge_literal(&forge, value, strlen(value), CARLA_URI_MAP_ID_ATOM_CHUNK, CARLA_URI_MAP_ID_NULL);
  373. //else
  374. // lv2_atom_forge_literal(&forge, value, strlen(value), getCustomURID(key), CARLA_URI_MAP_ID_NULL);
  375. lv2_atom_forge_pop(&forge, &bodyFrame);
  376. lv2_atom_forge_pop(&forge, &refFrame);
  377. const LV2_Atom* const atom = lv2_atom_forge_deref(&forge, ref);
  378. descriptor->port_event(handle, 0, atom->size, CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  379. free((void*)chunk.buf);
  380. sratom_free(sratom);
  381. }
  382. }
  383. void handleProgramChanged(int32_t /*index*/)
  384. {
  385. sendOscConfigure("reloadprograms", "");
  386. }
  387. uint32_t handleUiPortMap(const char* const symbol)
  388. {
  389. Q_ASSERT(symbol);
  390. for (uint32_t i=0; i < rdf_descriptor->PortCount; i++)
  391. {
  392. if (strcmp(rdf_descriptor->Ports[i].Symbol, symbol) == 0)
  393. return i;
  394. }
  395. return LV2UI_INVALID_PORT_INDEX;
  396. }
  397. int handleUiResize(int width, int height)
  398. {
  399. Q_ASSERT(width > 0);
  400. Q_ASSERT(height > 0);
  401. quequeMessage(MESSAGE_RESIZE_GUI, width, height, 0.0);
  402. return 0;
  403. }
  404. void handleUiWrite(uint32_t portIndex, uint32_t bufferSize, uint32_t format, const void* buffer)
  405. {
  406. if (format == 0)
  407. {
  408. Q_ASSERT(bufferSize == sizeof(float));
  409. if (bufferSize == sizeof(float))
  410. {
  411. float value = *(float*)buffer;
  412. sendOscControl(portIndex, value);
  413. }
  414. }
  415. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  416. {
  417. const LV2_Atom* const atom = (const LV2_Atom*)buffer;
  418. QByteArray chunk((const char*)buffer, bufferSize);
  419. sendOscLv2TransferAtom(getCustomURIString(atom->type), chunk.toBase64().constData());
  420. if (descriptor && descriptor->port_event)
  421. descriptor->port_event(handle, 0, atom->size, CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM, atom);
  422. }
  423. else if (format == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  424. {
  425. const LV2_Atom* const atom = (const LV2_Atom*)buffer;
  426. QByteArray chunk((const char*)buffer, bufferSize);
  427. sendOscLv2TransferEvent(getCustomURIString(atom->type), chunk.toBase64().constData());
  428. if (descriptor && descriptor->port_event)
  429. descriptor->port_event(handle, 0, atom->size, CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT, atom);
  430. }
  431. }
  432. // ----------------- Event Feature ---------------------------------------------------
  433. static uint32_t carla_lv2_event_ref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  434. {
  435. qDebug("CarlaLv2Client::carla_lv2_event_ref(%p, %p)", callback_data, event);
  436. Q_ASSERT(callback_data);
  437. Q_ASSERT(event);
  438. return 0;
  439. }
  440. static uint32_t carla_lv2_event_unref(LV2_Event_Callback_Data callback_data, LV2_Event* event)
  441. {
  442. qDebug("CarlaLv2Client::carla_lv2_event_unref(%p, %p)", callback_data, event);
  443. Q_ASSERT(callback_data);
  444. Q_ASSERT(event);
  445. return 0;
  446. }
  447. // ----------------- Logs Feature ----------------------------------------------------
  448. static int carla_lv2_log_printf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, ...)
  449. {
  450. qDebug("CarlaLv2Client::carla_lv2_log_printf(%p, %i, %s, ...)", handle, type, fmt);
  451. Q_ASSERT(handle);
  452. Q_ASSERT(type > 0);
  453. #ifndef DEBUG
  454. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  455. return 0;
  456. #endif
  457. va_list args;
  458. va_start(args, fmt);
  459. const int ret = carla_lv2_log_vprintf(handle, type, fmt, args);
  460. va_end(args);
  461. return ret;
  462. }
  463. static int carla_lv2_log_vprintf(LV2_Log_Handle handle, LV2_URID type, const char* fmt, va_list ap)
  464. {
  465. qDebug("CarlaLv2Client::carla_lv2_log_vprintf(%p, %i, %s, ...)", handle, type, fmt);
  466. Q_ASSERT(handle);
  467. Q_ASSERT(type > 0);
  468. #ifndef DEBUG
  469. if (type == CARLA_URI_MAP_ID_LOG_TRACE)
  470. return 0;
  471. #endif
  472. char buf[8196];
  473. vsprintf(buf, fmt, ap);
  474. if (*buf == 0)
  475. return 0;
  476. switch (type)
  477. {
  478. case CARLA_URI_MAP_ID_LOG_ERROR:
  479. qCritical("%s", buf);
  480. break;
  481. case CARLA_URI_MAP_ID_LOG_NOTE:
  482. printf("%s\n", buf);
  483. break;
  484. case CARLA_URI_MAP_ID_LOG_TRACE:
  485. qDebug("%s", buf);
  486. break;
  487. case CARLA_URI_MAP_ID_LOG_WARNING:
  488. qWarning("%s", buf);
  489. break;
  490. default:
  491. break;
  492. }
  493. return strlen(buf);
  494. }
  495. // ----------------- Programs Feature ------------------------------------------------
  496. static void carla_lv2_program_changed(LV2_Programs_Handle handle, int32_t index)
  497. {
  498. qDebug("CarlaLv2Client::carla_lv2_program_changed(%p, %i)", handle, index);
  499. Q_ASSERT(handle);
  500. CarlaLv2Client* const client = (CarlaLv2Client*)handle;
  501. client->handleProgramChanged(index);
  502. }
  503. // ----------------- State Feature ---------------------------------------------------
  504. static char* carla_lv2_state_make_path(LV2_State_Make_Path_Handle handle, const char* path)
  505. {
  506. qDebug("CarlaLv2Client::carla_lv2_state_make_path(%p, %p)", handle, path);
  507. Q_ASSERT(handle);
  508. Q_ASSERT(path);
  509. QDir dir;
  510. dir.mkpath(path);
  511. return strdup(path);
  512. }
  513. static char* carla_lv2_state_map_abstract_path(LV2_State_Map_Path_Handle handle, const char* absolute_path)
  514. {
  515. qDebug("CarlaLv2Client::carla_lv2_state_map_abstract_path(%p, %p)", handle, absolute_path);
  516. Q_ASSERT(handle);
  517. Q_ASSERT(absolute_path);
  518. QDir dir(absolute_path);
  519. return strdup(dir.canonicalPath().toUtf8().constData());
  520. }
  521. static char* carla_lv2_state_map_absolute_path(LV2_State_Map_Path_Handle handle, const char* abstract_path)
  522. {
  523. qDebug("CarlaLv2Client::carla_lv2_state_map_absolute_path(%p, %p)", handle, abstract_path);
  524. Q_ASSERT(handle);
  525. Q_ASSERT(abstract_path);
  526. QDir dir(abstract_path);
  527. return strdup(dir.absolutePath().toUtf8().constData());
  528. }
  529. // ----------------- URI-Map Feature ---------------------------------------
  530. static uint32_t carla_lv2_uri_to_id(LV2_URI_Map_Callback_Data data, const char* map, const char* uri)
  531. {
  532. qDebug("CarlaLv2Client::carla_lv2_uri_to_id(%p, %s, %s)", data, map, uri);
  533. return carla_lv2_urid_map(data, uri);
  534. }
  535. // ----------------- URID Feature ------------------------------------------
  536. static LV2_URID carla_lv2_urid_map(LV2_URID_Map_Handle handle, const char* uri)
  537. {
  538. qDebug("CarlaLv2Client::carla_lv2_urid_map(%p, %s)", handle, uri);
  539. Q_ASSERT(handle);
  540. Q_ASSERT(uri);
  541. // Atom types
  542. if (strcmp(uri, LV2_ATOM__Chunk) == 0)
  543. return CARLA_URI_MAP_ID_ATOM_CHUNK;
  544. if (strcmp(uri, LV2_ATOM__Path) == 0)
  545. return CARLA_URI_MAP_ID_ATOM_PATH;
  546. if (strcmp(uri, LV2_ATOM__Sequence) == 0)
  547. return CARLA_URI_MAP_ID_ATOM_SEQUENCE;
  548. if (strcmp(uri, LV2_ATOM__String) == 0)
  549. return CARLA_URI_MAP_ID_ATOM_STRING;
  550. if (strcmp(uri, LV2_ATOM__atomTransfer) == 0)
  551. return CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM;
  552. if (strcmp(uri, LV2_ATOM__eventTransfer) == 0)
  553. return CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT;
  554. // Log types
  555. if (strcmp(uri, LV2_LOG__Error) == 0)
  556. return CARLA_URI_MAP_ID_LOG_ERROR;
  557. if (strcmp(uri, LV2_LOG__Note) == 0)
  558. return CARLA_URI_MAP_ID_LOG_NOTE;
  559. if (strcmp(uri, LV2_LOG__Trace) == 0)
  560. return CARLA_URI_MAP_ID_LOG_TRACE;
  561. if (strcmp(uri, LV2_LOG__Warning) == 0)
  562. return CARLA_URI_MAP_ID_LOG_WARNING;
  563. // Others
  564. if (strcmp(uri, LV2_MIDI__MidiEvent) == 0)
  565. return CARLA_URI_MAP_ID_MIDI_EVENT;
  566. // Custom types
  567. CarlaLv2Client* const client = (CarlaLv2Client*)handle;
  568. return client->getCustomURID(uri);
  569. }
  570. static const char* carla_lv2_urid_unmap(LV2_URID_Map_Handle handle, LV2_URID urid)
  571. {
  572. qDebug("CarlaLv2Client::carla_lv2_urid_unmap(%p, %i)", handle, urid);
  573. Q_ASSERT(handle);
  574. Q_ASSERT(urid > 0);
  575. // Atom types
  576. if (urid == CARLA_URI_MAP_ID_ATOM_CHUNK)
  577. return LV2_ATOM__Chunk;
  578. if (urid == CARLA_URI_MAP_ID_ATOM_PATH)
  579. return LV2_ATOM__Path;
  580. if (urid == CARLA_URI_MAP_ID_ATOM_SEQUENCE)
  581. return LV2_ATOM__Sequence;
  582. if (urid == CARLA_URI_MAP_ID_ATOM_STRING)
  583. return LV2_ATOM__String;
  584. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_ATOM)
  585. return LV2_ATOM__atomTransfer;
  586. if (urid == CARLA_URI_MAP_ID_ATOM_TRANSFER_EVENT)
  587. return LV2_ATOM__eventTransfer;
  588. // Log types
  589. if (urid == CARLA_URI_MAP_ID_LOG_ERROR)
  590. return LV2_LOG__Error;
  591. if (urid == CARLA_URI_MAP_ID_LOG_NOTE)
  592. return LV2_LOG__Note;
  593. if (urid == CARLA_URI_MAP_ID_LOG_TRACE)
  594. return LV2_LOG__Trace;
  595. if (urid == CARLA_URI_MAP_ID_LOG_WARNING)
  596. return LV2_LOG__Warning;
  597. // Others
  598. if (urid == CARLA_URI_MAP_ID_MIDI_EVENT)
  599. return LV2_MIDI__MidiEvent;
  600. // Custom types
  601. CarlaLv2Client* const client = (CarlaLv2Client*)handle;
  602. return client->getCustomURIString(urid);
  603. }
  604. // ----------------- UI Port-Map Feature ---------------------------------------------
  605. static uint32_t carla_lv2_ui_port_map(LV2UI_Feature_Handle handle, const char* symbol)
  606. {
  607. qDebug("CarlaLv2Client::carla_lv2_ui_port_map(%p, %s)", handle, symbol);
  608. Q_ASSERT(handle);
  609. CarlaLv2Client* const client = (CarlaLv2Client*)handle;
  610. return client->handleUiPortMap(symbol);
  611. }
  612. // ----------------- UI Resize Feature -------------------------------------
  613. static int carla_lv2_ui_resize(LV2UI_Feature_Handle handle, int width, int height)
  614. {
  615. qDebug("CarlaLv2Client::carla_lv2_ui_resize(%p, %i, %i)", handle, width, height);
  616. Q_ASSERT(handle);
  617. CarlaLv2Client* const client = (CarlaLv2Client*)handle;
  618. return client->handleUiResize(width, height);
  619. }
  620. // ----------------- UI Extension ------------------------------------------
  621. static void carla_lv2_ui_write_function(LV2UI_Controller controller, uint32_t port_index, uint32_t buffer_size, uint32_t format, const void* buffer)
  622. {
  623. qDebug("CarlaLv2Client::carla_lv2_ui_write_function(%p, %i, %i, %i, %p)", controller, port_index, buffer_size, format, buffer);
  624. Q_ASSERT(controller);
  625. CarlaLv2Client* const client = (CarlaLv2Client*)controller;
  626. client->handleUiWrite(port_index, buffer_size, format, buffer);
  627. }
  628. private:
  629. LV2UI_Handle handle;
  630. LV2UI_Widget widget;
  631. const LV2UI_Descriptor* descriptor;
  632. LV2_Feature* features[lv2_feature_count+1];
  633. const LV2_RDF_Descriptor* rdf_descriptor;
  634. const LV2_RDF_UI* rdf_ui_descriptor;
  635. const LV2_Programs_UI_Interface* programs;
  636. #ifdef BRIDGE_LV2_X11
  637. QDialog* x11_widget;
  638. #endif
  639. bool m_resizable;
  640. std::vector<const char*> customURIDs;
  641. };
  642. int CarlaOsc::handleMsgLv2TransferAtom(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  643. {
  644. qDebug("CarlaOsc::handle_lv2_atom_transfer()");
  645. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ss");
  646. if (! client)
  647. return 1;
  648. const char* type = (const char*)&argv[0]->s;
  649. const char* value = (const char*)&argv[2]->s;
  650. CarlaLv2Client* const lv2client = (CarlaLv2Client*)client;
  651. lv2client->handleTransferAtom(type, value);
  652. return 0;
  653. }
  654. int CarlaOsc::handleMsgLv2TransferEvent(CARLA_BRIDGE_OSC_HANDLE_ARGS)
  655. {
  656. qDebug("CarlaOsc::handle_lv2_event_transfer()");
  657. CARLA_BRIDGE_OSC_CHECK_OSC_TYPES(2, "ss");
  658. if (! client)
  659. return 1;
  660. const char* type = (const char*)&argv[0]->s;
  661. const char* value = (const char*)&argv[2]->s;
  662. CarlaLv2Client* const lv2client = (CarlaLv2Client*)client;
  663. lv2client->handleTransferEvent(type, value);
  664. return 0;
  665. }
  666. CARLA_BRIDGE_END_NAMESPACE
  667. int main(int argc, char* argv[])
  668. {
  669. using namespace CarlaBridge;
  670. if (argc != 5)
  671. {
  672. qWarning("usage: %s <osc-url|\"null\"> <plugin-uri> <ui-uri> <ui-title>", argv[0]);
  673. return 1;
  674. }
  675. const char* oscUrl = argv[1];
  676. const char* pluginURI = argv[2];
  677. const char* uiURI = argv[3];
  678. const char* uiTitle = argv[4];
  679. const bool useOsc = strcmp(oscUrl, "null");
  680. // try to get sampleRate value
  681. const char* const sampleRateStr = getenv("CARLA_SAMPLE_RATE");
  682. if (sampleRateStr)
  683. sampleRate = atof(sampleRateStr);
  684. // Init toolkit
  685. CarlaToolkit* const toolkit = CarlaToolkit::createNew(uiTitle);
  686. toolkit->init();
  687. // Init LV2-UI
  688. CarlaLv2Client client(toolkit);
  689. // Init OSC
  690. if (useOsc && ! client.oscInit(oscUrl))
  691. {
  692. toolkit->quit();
  693. delete toolkit;
  694. return -1;
  695. }
  696. // Load UI
  697. int ret;
  698. if (client.init(pluginURI, uiURI))
  699. {
  700. toolkit->exec(&client, !useOsc);
  701. ret = 0;
  702. }
  703. else
  704. {
  705. qCritical("Failed to load LV2 UI");
  706. ret = 1;
  707. }
  708. // Close OSC
  709. if (useOsc)
  710. {
  711. client.sendOscExiting();
  712. client.oscClose();
  713. }
  714. // Close LV2-UI
  715. client.close();
  716. // Close toolkit
  717. toolkit->quit();
  718. delete toolkit;
  719. return ret;
  720. }
  721. #endif