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.

634 lines
19KB

  1. /*
  2. ZynAddSubFX - a software synthesizer
  3. guimain.cpp - Main file of synthesizer GUI
  4. Copyright (C) 2015 Mark McCurry
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. */
  10. #include <rtosc/thread-link.h>
  11. #include <lo/lo.h>
  12. #include <string>
  13. #include <thread>
  14. //GUI System
  15. #include "Connection.h"
  16. #include "NSM.H"
  17. #include <sys/stat.h>
  18. GUI::ui_handle_t gui = 0;
  19. const char *embedId = NULL;
  20. #if USE_NSM
  21. NSM_Client *nsm = NULL;
  22. #endif
  23. lo_server server;
  24. std::string sendtourl;
  25. /*
  26. * Program exit
  27. */
  28. void exitprogram()
  29. {
  30. GUI::destroyUi(gui);
  31. }
  32. bool fileexists(const char *filename)
  33. {
  34. struct stat tmp;
  35. int result = stat(filename, &tmp);
  36. if(result >= 0)
  37. return true;
  38. return false;
  39. }
  40. int Pexitprogram = 0;
  41. #include "Connection.h"
  42. #include "Fl_Osc_Interface.h"
  43. #include "../globals.h"
  44. #include <map>
  45. #include <cassert>
  46. #include <rtosc/rtosc.h>
  47. #include <rtosc/ports.h>
  48. #include <FL/Fl.H>
  49. #include "Fl_Osc_Tree.H"
  50. #include "common.H"
  51. #include "MasterUI.h"
  52. #ifdef NTK_GUI
  53. #include <FL/Fl_Shared_Image.H>
  54. #include <FL/Fl_Tiled_Image.H>
  55. #include <FL/Fl_Dial.H>
  56. #include <FL/x.H>
  57. #include <err.h>
  58. #endif // NTK_GUI
  59. #ifndef NO_UI
  60. #include "Fl_Osc_Widget.H"
  61. #endif
  62. using namespace GUI;
  63. class MasterUI *ui=0;
  64. bool isPlugin = false;
  65. #ifdef NTK_GUI
  66. static Fl_Tiled_Image *module_backdrop;
  67. #endif
  68. int kb_shortcut_handler(int)
  69. {
  70. const bool undo_ = Fl::event_ctrl() && Fl::event_key() == 'z';
  71. const bool redo = Fl::event_ctrl() && Fl::event_key() == 'r';
  72. const bool show = Fl::event_ctrl() && Fl::event_shift() &&
  73. Fl::event_key() == 's';
  74. const bool panel = Fl::event_ctrl() && Fl::event_shift() &&
  75. Fl::event_key() == 'p';
  76. if(undo_)
  77. ui->osc->write("/undo", "");
  78. else if(redo)
  79. ui->osc->write("/redo", "");
  80. else if (show) {
  81. ui->simplemasterwindow->hide();
  82. ui->masterwindow->show();
  83. }
  84. else if (panel)
  85. ui->panelwindow->show();
  86. return undo_ || redo || show;
  87. }
  88. void
  89. set_module_parameters ( Fl_Widget *o )
  90. {
  91. #ifdef NTK_GUI
  92. o->box( FL_DOWN_FRAME );
  93. o->align( o->align() | FL_ALIGN_IMAGE_BACKDROP );
  94. o->color( FL_BLACK );
  95. o->image( module_backdrop );
  96. o->labeltype( FL_SHADOW_LABEL );
  97. if(o->parent()) {
  98. o->parent()->labeltype(FL_NO_LABEL);
  99. o->parent()->box(FL_NO_BOX);
  100. }
  101. #else
  102. o->box( FL_PLASTIC_UP_BOX );
  103. o->color( FL_CYAN );
  104. o->labeltype( FL_EMBOSSED_LABEL );
  105. #endif
  106. }
  107. ui_handle_t GUI::createUi(Fl_Osc_Interface *osc, void *exit)
  108. {
  109. #ifdef NTK_GUI
  110. fl_register_images();
  111. Fl_Dial::default_style(Fl_Dial::PIXMAP_DIAL);
  112. #ifdef CARLA_VERSION_STRING
  113. if(Fl_Shared_Image *img = Fl_Shared_Image::get(gUiPixmapPath + "/knob.png"))
  114. Fl_Dial::default_image(img);
  115. #else
  116. if(Fl_Shared_Image *img = Fl_Shared_Image::get(PIXMAP_PATH "/knob.png"))
  117. Fl_Dial::default_image(img);
  118. #endif
  119. else if(Fl_Shared_Image *img = Fl_Shared_Image::get(SOURCE_DIR "/pixmaps/knob.png"))
  120. Fl_Dial::default_image(img);
  121. else
  122. errx(1, "ERROR: Cannot find pixmaps/knob.png");
  123. #ifdef CARLA_VERSION_STRING
  124. if(Fl_Shared_Image *img = Fl_Shared_Image::get(gUiPixmapPath + "/window_backdrop.png"))
  125. Fl::scheme_bg(new Fl_Tiled_Image(img));
  126. #else
  127. if(Fl_Shared_Image *img = Fl_Shared_Image::get(PIXMAP_PATH "/window_backdrop.png"))
  128. Fl::scheme_bg(new Fl_Tiled_Image(img));
  129. #endif
  130. else if(Fl_Shared_Image *img = Fl_Shared_Image::get(SOURCE_DIR "/pixmaps/window_backdrop.png"))
  131. Fl::scheme_bg(new Fl_Tiled_Image(img));
  132. else
  133. errx(1, "ERROR: Cannot find pixmaps/window_backdrop.png");
  134. #ifdef CARLA_VERSION_STRING
  135. if(Fl_Shared_Image *img = Fl_Shared_Image::get(gUiPixmapPath + "/module_backdrop.png"))
  136. module_backdrop = new Fl_Tiled_Image(img);
  137. #else
  138. if(Fl_Shared_Image *img = Fl_Shared_Image::get(PIXMAP_PATH "/module_backdrop.png"))
  139. module_backdrop = new Fl_Tiled_Image(img);
  140. #endif
  141. else if(Fl_Shared_Image *img = Fl_Shared_Image::get(SOURCE_DIR "/pixmaps/module_backdrop.png"))
  142. module_backdrop = new Fl_Tiled_Image(img);
  143. else
  144. errx(1, "ERROR: Cannot find pixmaps/module_backdrop");
  145. Fl::background(50, 50, 50);
  146. Fl::background2(70, 70, 70);
  147. Fl::foreground(255, 255, 255);
  148. #endif
  149. //Fl_Window *midi_win = new Fl_Double_Window(400, 400, "Midi connections");
  150. //Fl_Osc_Tree *tree = new Fl_Osc_Tree(0,0,400,400);
  151. //midi_win->resizable(tree);
  152. //tree->root_ports = &Master::ports;
  153. //tree->osc = osc;
  154. //midi_win->show();
  155. Fl::add_handler(kb_shortcut_handler);
  156. ui = new MasterUI((int*)exit, osc);
  157. if (embedId != NULL)
  158. {
  159. if (long long winId = atoll(embedId))
  160. {
  161. // running as plugin
  162. isPlugin = true;
  163. MasterUI::menu_mastermenu[11].hide(); // file -> nio settings
  164. MasterUI::menu_mastermenu[26].deactivate(); // misc -> switch interface mode
  165. #ifdef NTK_GUI
  166. if (winId != 1)
  167. {
  168. MasterUI::menu_mastermenu[13].hide(); // file -> exit
  169. fl_embed(ui->masterwindow, winId);
  170. }
  171. #endif
  172. ui->masterwindow->show();
  173. }
  174. }
  175. return (void*) ui;
  176. }
  177. void GUI::destroyUi(ui_handle_t ui)
  178. {
  179. delete static_cast<MasterUI*>(ui);
  180. }
  181. #define BEGIN(x) {x,":non-realtime\0",NULL,[](const char *m, rtosc::RtData d){ \
  182. MasterUI *ui = static_cast<MasterUI*>(d.obj); \
  183. rtosc_arg_t a0 = {0}, a1 = {0}; \
  184. if(rtosc_narguments(m) > 0) \
  185. a0 = rtosc_argument(m,0); \
  186. if(rtosc_narguments(m) > 1) \
  187. a1 = rtosc_argument(m,1); \
  188. (void)ui;(void)a1;(void)a0;
  189. #define END }},
  190. struct uiPorts {
  191. static rtosc::Ports ports;
  192. };
  193. //DSL based ports
  194. rtosc::Ports uiPorts::ports = {
  195. BEGIN("show:i") {
  196. ui->showUI(a0.i);
  197. } END
  198. BEGIN("alert:s") {
  199. fl_alert("%s",a0.s);
  200. } END
  201. BEGIN("session-type:s") {
  202. if(strcmp(a0.s,"LASH"))
  203. return;
  204. ui->sm_indicator1->value(1);
  205. ui->sm_indicator2->value(1);
  206. ui->sm_indicator1->tooltip("LASH");
  207. ui->sm_indicator2->tooltip("LASH");
  208. } END
  209. BEGIN("save-master:s") {
  210. ui->do_save_master(a0.s);
  211. } END
  212. BEGIN("load-master:s") {
  213. ui->do_load_master(a0.s);
  214. } END
  215. BEGIN("vu-meter:bb") {
  216. #ifdef DEBUG
  217. printf("Vu meter handler...\n");
  218. #endif
  219. if(a0.b.len == sizeof(vuData) &&
  220. a1.b.len == sizeof(float)*NUM_MIDI_PARTS) {
  221. #ifdef DEBUG
  222. printf("Normal behavior...\n");
  223. #endif
  224. //Refresh the primary VU meters
  225. ui->simplemastervu->update((vuData*)a0.b.data);
  226. ui->mastervu->update((vuData*)a0.b.data);
  227. float *partvu = (float*)a1.b.data;
  228. for(int i=0; i<NUM_MIDI_PARTS; ++i)
  229. ui->panellistitem[i]->partvu->update(partvu[i]);
  230. }
  231. } END
  232. BEGIN("close-ui") {
  233. ui->close();
  234. } END
  235. };
  236. void GUI::raiseUi(ui_handle_t gui, const char *message)
  237. {
  238. if(!gui)
  239. return;
  240. MasterUI *mui = (MasterUI*)gui;
  241. mui->osc->tryLink(message);
  242. #ifdef DEBUG
  243. printf("got message for UI '%s:%s'\n", message, rtosc_argument_string(message));
  244. #endif
  245. char buffer[1024];
  246. memset(buffer, 0, sizeof(buffer));
  247. rtosc::RtData d;
  248. d.loc = buffer;
  249. d.loc_size = 1024;
  250. d.obj = gui;
  251. uiPorts::ports.dispatch(message+1, d);
  252. }
  253. void GUI::raiseUi(ui_handle_t gui, const char *dest, const char *args, ...)
  254. {
  255. char buffer[1024];
  256. va_list va;
  257. va_start(va,args);
  258. if(rtosc_vmessage(buffer,1024,dest,args,va))
  259. raiseUi(gui, buffer);
  260. va_end(va);
  261. }
  262. void GUI::tickUi(ui_handle_t)
  263. {
  264. Fl::wait(0.02f);
  265. }
  266. /******************************************************************************
  267. * OSC Interface For User Interface *
  268. * *
  269. * This is a largely out of date section of code *
  270. * Most type specific write methods are no longer used *
  271. * See UI/Fl_Osc_* to see what is actually used in this interface *
  272. ******************************************************************************/
  273. class UI_Interface:public Fl_Osc_Interface
  274. {
  275. public:
  276. UI_Interface()
  277. {}
  278. void transmitMsg(const char *path, const char *args, ...)
  279. {
  280. char buffer[1024];
  281. va_list va;
  282. va_start(va,args);
  283. if(rtosc_vmessage(buffer,1024,path,args,va))
  284. transmitMsg(buffer);
  285. else
  286. fprintf(stderr, "Error in transmitMsg(...)\n");
  287. va_end(va);
  288. }
  289. void transmitMsg(const char *rtmsg)
  290. {
  291. //Send to known url
  292. if(!sendtourl.empty()) {
  293. lo_message msg = lo_message_deserialise((void*)rtmsg,
  294. rtosc_message_length(rtmsg, rtosc_message_length(rtmsg,-1)), NULL);
  295. lo_address addr = lo_address_new_from_url(sendtourl.c_str());
  296. lo_send_message(addr, rtmsg, msg);
  297. }
  298. }
  299. void requestValue(string s) override
  300. {
  301. //printf("Request Value '%s'\n", s.c_str());
  302. assert(s!="/Psysefxvol-1/part0");
  303. //Fl_Osc_Interface::requestValue(s);
  304. /*
  305. if(impl->activeUrl() != "GUI") {
  306. impl->transmitMsg("/echo", "ss", "OSC_URL", "GUI");
  307. impl->activeUrl("GUI");
  308. }*/
  309. transmitMsg(s.c_str(),"");
  310. }
  311. void write(string s, const char *args, ...) override
  312. {
  313. char buffer[4096];
  314. va_list va;
  315. va_start(va, args);
  316. rtosc_vmessage(buffer, sizeof(buffer), s.c_str(), args, va);
  317. //fprintf(stderr, "%c[%d;%d;%dm", 0x1B, 0, 4 + 30, 0 + 40);
  318. ////fprintf(stderr, ".");
  319. //fprintf(stderr, "write(%s:%s)\n", s.c_str(), args);
  320. //fprintf(stderr, "%c[%d;%d;%dm", 0x1B, 0, 7 + 30, 0 + 40);
  321. transmitMsg(buffer);
  322. va_end(va);
  323. }
  324. void writeRaw(const char *msg) override
  325. {
  326. //fprintf(stderr, "%c[%d;%d;%dm", 0x1B, 0, 4 + 30, 0 + 40);
  327. ////fprintf(stderr, ".");
  328. //fprintf(stderr, "rawWrite(%s:%s)\n", msg, rtosc_argument_string(msg));
  329. //fprintf(stderr, "%c[%d;%d;%dm", 0x1B, 0, 7 + 30, 0 + 40);
  330. transmitMsg(msg);
  331. }
  332. void writeValue(string s, string ss) override
  333. {
  334. //fprintf(stderr, "%c[%d;%d;%dm", 0x1B, 0, 4 + 30, 0 + 40);
  335. //fprintf(stderr, "writevalue<string>(%s,%s)\n", s.c_str(),ss.c_str());
  336. //fprintf(stderr, "%c[%d;%d;%dm", 0x1B, 0, 7 + 30, 0 + 40);
  337. transmitMsg(s.c_str(), "s", ss.c_str());
  338. }
  339. void writeValue(string s, char c) override
  340. {
  341. //fprintf(stderr, "%c[%d;%d;%dm", 0x1B, 0, 4 + 30, 0 + 40);
  342. //fprintf(stderr, "writevalue<char>(%s,%d)\n", s.c_str(),c);
  343. //fprintf(stderr, "%c[%d;%d;%dm", 0x1B, 0, 7 + 30, 0 + 40);
  344. transmitMsg(s.c_str(), "c", c);
  345. }
  346. void writeValue(string s, float f) override
  347. {
  348. //fprintf(stderr, "%c[%d;%d;%dm", 0x1B, 0, 4 + 30, 0 + 40);
  349. //fprintf(stderr, "writevalue<float>(%s,%f)\n", s.c_str(),f);
  350. //fprintf(stderr, "%c[%d;%d;%dm", 0x1B, 0, 7 + 30, 0 + 40);
  351. transmitMsg(s.c_str(), "f", f);
  352. }
  353. void createLink(string s, class Fl_Osc_Widget*w) override
  354. {
  355. assert(s.length() != 0);
  356. Fl_Osc_Interface::createLink(s,w);
  357. assert(!strstr(s.c_str(), "/part0/kit-1"));
  358. map.insert(std::pair<string,Fl_Osc_Widget*>(s,w));
  359. }
  360. void renameLink(string old, string newer, Fl_Osc_Widget *w) override
  361. {
  362. fprintf(stdout, "renameLink('%s','%s',%p)\n",
  363. old.c_str(), newer.c_str(), w);
  364. removeLink(old, w);
  365. createLink(newer, w);
  366. }
  367. void removeLink(string s, class Fl_Osc_Widget*w) override
  368. {
  369. for(auto i = map.begin(); i != map.end(); ++i) {
  370. if(i->first == s && i->second == w) {
  371. map.erase(i);
  372. break;
  373. }
  374. }
  375. //printf("[%d] removing '%s' (%p)...\n", map.size(), s.c_str(), w);
  376. }
  377. virtual void removeLink(class Fl_Osc_Widget *w) override
  378. {
  379. bool processing = true;
  380. while(processing)
  381. {
  382. //Verify Iterator invalidation sillyness
  383. processing = false;//Exit if no new elements are found
  384. for(auto i = map.begin(); i != map.end(); ++i) {
  385. if(i->second == w) {
  386. //printf("[%d] removing '%s' (%p)...\n", map.size()-1,
  387. // i->first.c_str(), w);
  388. map.erase(i);
  389. processing = true;
  390. break;
  391. }
  392. }
  393. }
  394. }
  395. //A very simplistic implementation of a UI agnostic refresh method
  396. virtual void damage(const char *path) override
  397. {
  398. //printf("\n\nDamage(\"%s\")\n", path);
  399. for(auto pair:map) {
  400. if(strstr(pair.first.c_str(), path)) {
  401. auto *tmp = dynamic_cast<Fl_Widget*>(pair.second);
  402. //if(tmp)
  403. // printf("%x, %d %d [%s]\n", (int)pair.second, tmp->visible_r(), tmp->visible(), pair.first.c_str());
  404. //else
  405. // printf("%x, (NULL)[%s]\n", (int)pair.second,pair.first.c_str());
  406. if(!tmp || tmp->visible_r())
  407. pair.second->update();
  408. }
  409. }
  410. }
  411. void tryLink(const char *msg) override
  412. {
  413. //DEBUG
  414. //if(strcmp(msg, "/vu-meter"))//Ignore repeated message
  415. // printf("trying the link for a '%s'<%s>\n", msg, rtosc_argument_string(msg));
  416. const char *handle = strrchr(msg,'/');
  417. if(handle)
  418. ++handle;
  419. int found_count = 0;
  420. auto range = map.equal_range(msg);
  421. for(auto itr = range.first; itr != range.second; ++itr) {
  422. auto widget = itr->second;
  423. found_count++;
  424. const char *arg_str = rtosc_argument_string(msg);
  425. //Always provide the raw message
  426. widget->OSC_raw(msg);
  427. if(!strcmp(arg_str, "b")) {
  428. widget->OSC_value(rtosc_argument(msg,0).b.len,
  429. rtosc_argument(msg,0).b.data,
  430. handle);
  431. } else if(!strcmp(arg_str, "c")) {
  432. widget->OSC_value((char)rtosc_argument(msg,0).i,
  433. handle);
  434. } else if(!strcmp(arg_str, "s")) {
  435. widget->OSC_value((const char*)rtosc_argument(msg,0).s,
  436. handle);
  437. } else if(!strcmp(arg_str, "i")) {
  438. widget->OSC_value((int)rtosc_argument(msg,0).i,
  439. handle);
  440. } else if(!strcmp(arg_str, "f")) {
  441. widget->OSC_value((float)rtosc_argument(msg,0).f,
  442. handle);
  443. } else if(!strcmp(arg_str, "T") || !strcmp(arg_str, "F")) {
  444. widget->OSC_value((bool)rtosc_argument(msg,0).T, handle);
  445. }
  446. }
  447. if(found_count == 0
  448. && strcmp(msg, "/vu-meter")
  449. && strcmp(msg, "undo_change")
  450. && !strstr(msg, "parameter")
  451. && !strstr(msg, "Prespoint")) {
  452. //fprintf(stderr, "%c[%d;%d;%dm", 0x1B, 1, 7 + 30, 0 + 40);
  453. //fprintf(stderr, "Unknown widget '%s'\n", msg);
  454. //fprintf(stderr, "%c[%d;%d;%dm", 0x1B, 0, 7 + 30, 0 + 40);
  455. }
  456. };
  457. void dumpLookupTable(void)
  458. {
  459. if(!map.empty()) {
  460. printf("Leaked controls:\n");
  461. for(auto i = map.begin(); i != map.end(); ++i) {
  462. printf("Known control '%s' (%p)...\n", i->first.c_str(), i->second);
  463. }
  464. }
  465. }
  466. private:
  467. std::multimap<string,Fl_Osc_Widget*> map;
  468. };
  469. Fl_Osc_Interface *GUI::genOscInterface(MiddleWare *)
  470. {
  471. return new UI_Interface();
  472. }
  473. rtosc::ThreadLink lo_buffer(4096*2, 1000);
  474. static void liblo_error_cb(int i, const char *m, const char *loc)
  475. {
  476. fprintf(stderr, "liblo :-( %d-%s@%s\n",i,m,loc);
  477. }
  478. static int handler_function(const char *path, const char *types, lo_arg **argv,
  479. int argc, lo_message msg, void *user_data)
  480. {
  481. (void) types;
  482. (void) argv;
  483. (void) argc;
  484. (void) user_data;
  485. char buffer[8192];
  486. memset(buffer, 0, sizeof(buffer));
  487. size_t size = sizeof(buffer);
  488. assert(lo_message_length(msg, path) <= sizeof(buffer));
  489. lo_message_serialise(msg, path, buffer, &size);
  490. assert(size <= sizeof(buffer));
  491. lo_buffer.raw_write(buffer);
  492. return 0;
  493. }
  494. void watch_lo(void)
  495. {
  496. while(server && Pexitprogram == 0)
  497. lo_server_recv_noblock(server, 100);
  498. }
  499. const char *help_message =
  500. "zynaddsubfx-ext-gui [options] uri - Connect to remote ZynAddSubFX\n"
  501. " --help print this help message\n"
  502. " --no-uri run without a remote ZynAddSubFX\n"
  503. " --embed window ID [Internal Flag For Embedding Windows]\n"
  504. "\n"
  505. " example: zynaddsubfx-ext-gui osc.udp://localhost:1234/\n"
  506. " This will connect to a running zynaddsubfx instance on the same\n"
  507. " machine on port 1234.\n";
  508. #ifndef CARLA_VERSION_STRING
  509. int main(int argc, char *argv[])
  510. {
  511. const char *uri = NULL;
  512. const char *title = NULL;
  513. bool help = false;
  514. bool no_uri = false;
  515. for(int i=1; i<argc; ++i) {
  516. if(!strcmp("--help", argv[i]))
  517. help = true;
  518. else if(!strcmp("--no-uri", argv[i]))
  519. no_uri = true;
  520. else if(!strcmp("--embed", argv[i]))
  521. embedId = argv[++i];
  522. else if(!strcmp("--title", argv[i]))
  523. title = argv[++i];
  524. else
  525. uri = argv[i];
  526. }
  527. if(uri == NULL && no_uri == false)
  528. help = true;
  529. if(help) {
  530. puts(help_message);
  531. return 1;
  532. }
  533. //Startup Liblo Link
  534. if(uri) {
  535. server = lo_server_new_with_proto(NULL, LO_UDP, liblo_error_cb);
  536. lo_server_add_method(server, NULL, NULL, handler_function, 0);
  537. sendtourl = uri;
  538. }
  539. fprintf(stderr, "ext client running on %d\n", lo_server_get_port(server));
  540. std::thread lo_watch(watch_lo);
  541. gui = GUI::createUi(new UI_Interface(), &Pexitprogram);
  542. if (title != NULL)
  543. GUI::raiseUi(gui, "/ui/title", "s", title);
  544. GUI::raiseUi(gui, "/show", "i", 1);
  545. while(Pexitprogram == 0) {
  546. GUI::tickUi(gui);
  547. while(lo_buffer.hasNext())
  548. raiseUi(gui, lo_buffer.read());
  549. }
  550. exitprogram();
  551. lo_watch.join();
  552. return 0;
  553. }
  554. #endif