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.

898 lines
29KB

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