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.

1105 lines
30KB

  1. #include "../ports.h"
  2. #include <ostream>
  3. #include <cassert>
  4. #include <climits>
  5. #include <cstring>
  6. #include <string>
  7. using namespace rtosc;
  8. static inline void scat(char *dest, const char *src)
  9. {
  10. while(*dest) dest++;
  11. if(*dest) dest++;
  12. while(*src && *src!=':') *dest++ = *src++;
  13. *dest = 0;
  14. }
  15. RtData::RtData(void)
  16. :loc(NULL), loc_size(0), obj(NULL), matches(0), message(NULL)
  17. {}
  18. void RtData::reply(const char *path, const char *args, ...)
  19. {
  20. va_list va;
  21. va_start(va,args);
  22. char buffer[1024];
  23. rtosc_vmessage(buffer,1024,path,args,va);
  24. reply(buffer);
  25. va_end(va);
  26. };
  27. void RtData::reply(const char *msg)
  28. {(void)msg;};
  29. void RtData::broadcast(const char *path, const char *args, ...)
  30. {
  31. va_list va;
  32. va_start(va,args);
  33. char buffer[1024];
  34. rtosc_vmessage(buffer,1024,path,args,va);
  35. broadcast(buffer);
  36. va_end(va);
  37. }
  38. void RtData::broadcast(const char *msg)
  39. {reply(msg);};
  40. void RtData::forward(const char *rational)
  41. {}
  42. void metaiterator_advance(const char *&title, const char *&value)
  43. {
  44. if(!title || !*title) {
  45. value = NULL;
  46. return;
  47. }
  48. //Try to find "\0=" after title string
  49. value = title;
  50. while(*value)
  51. ++value;
  52. if(*++value != '=')
  53. value = NULL;
  54. else
  55. value++;
  56. }
  57. Port::MetaIterator::MetaIterator(const char *str)
  58. :title(str), value(NULL)
  59. {
  60. metaiterator_advance(title, value);
  61. }
  62. Port::MetaIterator& Port::MetaIterator::operator++(void)
  63. {
  64. if(!title || !*title) {
  65. title = NULL;
  66. return *this;
  67. }
  68. //search for next parameter start
  69. //aka "\0:" unless "\0\0" is seen
  70. char prev = 0;
  71. while(prev || (*title && *title != ':'))
  72. prev = *title++;
  73. if(!*title)
  74. title = NULL;
  75. else
  76. ++title;
  77. metaiterator_advance(title, value);
  78. return *this;
  79. }
  80. Port::MetaContainer::MetaContainer(const char *str_)
  81. :str_ptr(str_)
  82. {}
  83. Port::MetaIterator Port::MetaContainer::begin(void) const
  84. {
  85. if(str_ptr && *str_ptr == ':')
  86. return Port::MetaIterator(str_ptr+1);
  87. else
  88. return Port::MetaIterator(str_ptr);
  89. }
  90. Port::MetaIterator Port::MetaContainer::end(void) const
  91. {
  92. return MetaIterator(NULL);
  93. }
  94. Port::MetaIterator Port::MetaContainer::find(const char *str) const
  95. {
  96. for(const auto x : *this)
  97. if(!strcmp(x.title, str))
  98. return x;
  99. return NULL;
  100. }
  101. size_t Port::MetaContainer::length(void) const
  102. {
  103. if(!str_ptr || !*str_ptr)
  104. return 0;
  105. char prev = 0;
  106. const char *itr = str_ptr;
  107. while(prev || *itr)
  108. prev = *itr++;
  109. return 2+(itr-str_ptr);
  110. }
  111. const char *Port::MetaContainer::operator[](const char *str) const
  112. {
  113. for(const auto x : *this)
  114. if(!strcmp(x.title, str))
  115. return x.value;
  116. return NULL;
  117. }
  118. //Match the arg string or fail
  119. inline bool arg_matcher(const char *pattern, const char *args)
  120. {
  121. //match anything if now arg restriction is present (ie the ':')
  122. if(*pattern++ != ':')
  123. return true;
  124. const char *arg_str = args;
  125. bool arg_match = *pattern || *pattern == *arg_str;
  126. while(*pattern && *pattern != ':')
  127. arg_match &= (*pattern++==*arg_str++);
  128. if(*pattern==':') {
  129. if(arg_match && !*arg_str)
  130. return true;
  131. else
  132. return arg_matcher(pattern, args); //retry
  133. }
  134. return arg_match;
  135. }
  136. inline bool scmp(const char *a, const char *b)
  137. {
  138. while(*a && *a == *b) a++, b++;
  139. return a[0] == b[0];
  140. }
  141. typedef std::vector<std::string> words_t;
  142. typedef std::vector<std::string> svec_t;
  143. typedef std::vector<const char *> cvec_t;
  144. typedef std::vector<int> ivec_t;
  145. typedef std::vector<int> tuple_t;
  146. typedef std::vector<tuple_t> tvec_t;
  147. namespace rtosc{
  148. class Port_Matcher
  149. {
  150. public:
  151. bool *enump;
  152. svec_t fixed;
  153. cvec_t arg_spec;
  154. ivec_t pos;
  155. ivec_t assoc;
  156. ivec_t remap;
  157. bool rtosc_match_args(const char *pattern, const char *msg)
  158. {
  159. //match anything if now arg restriction is present
  160. //(ie the ':')
  161. if(*pattern++ != ':')
  162. return true;
  163. const char *arg_str = rtosc_argument_string(msg);
  164. bool arg_match = *pattern || *pattern == *arg_str;
  165. while(*pattern && *pattern != ':')
  166. arg_match &= (*pattern++==*arg_str++);
  167. if(*pattern==':') {
  168. if(arg_match && !*arg_str)
  169. return true;
  170. else
  171. return rtosc_match_args(pattern, msg); //retry
  172. }
  173. return arg_match;
  174. }
  175. bool hard_match(int i, const char *msg)
  176. {
  177. if(strncmp(msg, fixed[i].c_str(), fixed[i].length()))
  178. return false;
  179. if(arg_spec[i])
  180. return rtosc_match_args(arg_spec[i], msg);
  181. else
  182. return true;
  183. }
  184. };
  185. }
  186. tvec_t do_hash(const words_t &strs, const ivec_t &pos)
  187. {
  188. tvec_t tvec;
  189. for(auto &s:strs) {
  190. tuple_t tuple;
  191. tuple.push_back(s.length());
  192. for(const auto &p:pos)
  193. if(p < (int)s.size())
  194. tuple.push_back(s[p]);
  195. tvec.push_back(std::move(tuple));
  196. }
  197. return tvec;
  198. }
  199. template<class T>
  200. int count_dups(std::vector<T> &t)
  201. {
  202. int dups = 0;
  203. int N = t.size();
  204. bool mark[t.size()];
  205. memset(mark, 0, N);
  206. for(int i=0; i<N; ++i) {
  207. if(mark[i])
  208. continue;
  209. for(int j=i+1; j<N; ++j) {
  210. if(t[i] == t[j]) {
  211. dups++;
  212. mark[j] = true;
  213. }
  214. }
  215. }
  216. return dups;
  217. }
  218. template<class T, class Z>
  219. bool has(T &t, Z&z)
  220. {
  221. for(auto tt:t)
  222. if(tt==z)
  223. return true;
  224. return false;
  225. }
  226. static int int_max(int a, int b) { return a<b?b:a;}
  227. static ivec_t find_pos(words_t &strs)
  228. {
  229. ivec_t pos;
  230. int current_dups = strs.size();
  231. int N = 0;
  232. for(auto w:strs)
  233. N = int_max(N,w.length());
  234. int pos_best = -1;
  235. int pos_best_val = INT_MAX;
  236. while(true)
  237. {
  238. for(int i=0; i<N; ++i) {
  239. ivec_t npos = pos;
  240. if(has(pos, i))
  241. continue;
  242. npos.push_back(i);
  243. auto hashed = do_hash(strs, npos);
  244. int d = count_dups(hashed);
  245. if(d < pos_best_val) {
  246. pos_best_val = d;
  247. pos_best = i;
  248. }
  249. }
  250. if(pos_best_val >= current_dups)
  251. break;
  252. current_dups = pos_best_val;
  253. pos.push_back(pos_best);
  254. }
  255. auto hashed = do_hash(strs, pos);
  256. int d = count_dups(hashed);
  257. //printf("Total Dups: %d\n", d);
  258. if(d != 0)
  259. pos.clear();
  260. return pos;
  261. }
  262. static ivec_t do_hash(const words_t &strs, const ivec_t &pos, const ivec_t &assoc)
  263. {
  264. ivec_t ivec;
  265. ivec.reserve(strs.size());
  266. for(auto &s:strs) {
  267. int t = s.length();
  268. for(auto p:pos)
  269. if(p < (int)s.size())
  270. t += assoc[s[p]];
  271. ivec.push_back(t);
  272. }
  273. return ivec;
  274. }
  275. static ivec_t find_assoc(const words_t &strs, const ivec_t &pos)
  276. {
  277. ivec_t assoc;
  278. int current_dups = strs.size();
  279. int N = 127;
  280. std::vector<char> useful_chars;
  281. for(auto w:strs)
  282. for(auto c:w)
  283. if(!has(useful_chars, c))
  284. useful_chars.push_back(c);
  285. for(int i=0; i<N; ++i)
  286. assoc.push_back(0);
  287. int assoc_best = -1;
  288. int assoc_best_val = INT_MAX;
  289. for(int k=0; k<4; ++k)
  290. {
  291. for(int i:useful_chars) {
  292. assoc_best_val = INT_MAX;
  293. for(int j=0; j<100; ++j) {
  294. //printf(".");
  295. assoc[i] = j;
  296. auto hashed = do_hash(strs, pos, assoc);
  297. //for(int i=0; i<hashed.size(); ++i)
  298. // printf("%d ", hashed[i]);
  299. //printf("\n");
  300. int d = count_dups(hashed);
  301. //printf("dup %d\n",d);
  302. if(d < assoc_best_val) {
  303. assoc_best_val = d;
  304. assoc_best = j;
  305. }
  306. }
  307. assoc[i] = assoc_best;
  308. }
  309. if(assoc_best_val >= current_dups)
  310. break;
  311. current_dups = assoc_best_val;
  312. }
  313. auto hashed = do_hash(strs, pos, assoc);
  314. //int d = count_dups(hashed);
  315. //printf("Total Dups Assoc: %d\n", d);
  316. return assoc;
  317. }
  318. static ivec_t find_remap(words_t &strs, ivec_t &pos, ivec_t &assoc)
  319. {
  320. ivec_t remap;
  321. auto hashed = do_hash(strs, pos, assoc);
  322. //for(int i=0; i<strs.size(); ++i)
  323. // printf("%d) '%s'\n", hashed[i], strs[i].c_str());
  324. int N = 0;
  325. for(auto h:hashed)
  326. N = int_max(N,h+1);
  327. for(int i=0; i<N; ++i)
  328. remap.push_back(0);
  329. for(int i=0; i<(int)hashed.size(); ++i)
  330. remap[hashed[i]] = i;
  331. return remap;
  332. }
  333. static void generate_minimal_hash(std::vector<std::string> str, Port_Matcher &pm)
  334. {
  335. if(str.empty())
  336. return;
  337. pm.pos = find_pos(str);
  338. if(pm.pos.empty()) {
  339. fprintf(stderr, "rtosc: Failed to generate minimal hash\n");
  340. return;
  341. }
  342. pm.assoc = find_assoc(str, pm.pos);
  343. pm.remap = find_remap(str, pm.pos, pm.assoc);
  344. }
  345. static void generate_minimal_hash(Ports &p, Port_Matcher &pm)
  346. {
  347. svec_t keys;
  348. cvec_t args;
  349. bool enump = false;
  350. for(unsigned i=0; i<p.ports.size(); ++i)
  351. if(strchr(p.ports[i].name, '#'))
  352. enump = true;
  353. if(enump)
  354. return;
  355. for(unsigned i=0; i<p.ports.size(); ++i)
  356. {
  357. std::string tmp = p.ports[i].name;
  358. const char *arg = NULL;
  359. int idx = tmp.find(':');
  360. if(idx > 0) {
  361. arg = p.ports[i].name+idx;
  362. tmp = tmp.substr(0,idx);
  363. }
  364. keys.push_back(tmp);
  365. args.push_back(arg);
  366. }
  367. pm.fixed = keys;
  368. pm.arg_spec = args;
  369. generate_minimal_hash(keys, pm);
  370. }
  371. Ports::Ports(std::initializer_list<Port> l)
  372. :ports(l), impl(NULL)
  373. {
  374. refreshMagic();
  375. }
  376. Ports::~Ports()
  377. {
  378. delete []impl->enump;
  379. delete impl;
  380. }
  381. #if !defined(__GNUC__)
  382. #define __builtin_expect(a,b) a
  383. #endif
  384. void Ports::dispatch(const char *m, rtosc::RtData &d, bool base_dispatch) const
  385. {
  386. void *obj = d.obj;
  387. //handle the first dispatch layer
  388. if(base_dispatch) {
  389. d.matches = 0;
  390. d.message = m;
  391. if(m && *m == '/')
  392. m++;
  393. if(d.loc)
  394. d.loc[0] = 0;
  395. }
  396. //simple case
  397. if(!d.loc || !d.loc_size) {
  398. for(const Port &port: ports) {
  399. if(rtosc_match(port.name,m))
  400. d.port = &port, port.cb(m,d), d.obj = obj;
  401. }
  402. } else {
  403. //TODO this function is certainly buggy at the moment, some tests
  404. //are needed to make it clean
  405. //XXX buffer_size is not properly handled yet
  406. if(__builtin_expect(d.loc[0] == 0, 0)) {
  407. memset(d.loc, 0, d.loc_size);
  408. d.loc[0] = '/';
  409. }
  410. char *old_end = d.loc;
  411. while(*old_end) ++old_end;
  412. if(impl->pos.empty()) { //No perfect minimal hash function
  413. for(unsigned i=0; i<elms; ++i) {
  414. const Port &port = ports[i];
  415. if(!rtosc_match(port.name, m))
  416. continue;
  417. if(!port.ports)
  418. d.matches++;
  419. //Append the path
  420. if(strchr(port.name,'#')) {
  421. const char *msg = m;
  422. char *pos = old_end;
  423. while(*msg && *msg != '/')
  424. *pos++ = *msg++;
  425. if(strchr(port.name, '/'))
  426. *pos++ = '/';
  427. *pos = '\0';
  428. } else
  429. scat(d.loc, port.name);
  430. d.port = &port;
  431. //Apply callback
  432. port.cb(m,d), d.obj = obj;
  433. //Remove the rest of the path
  434. char *tmp = old_end;
  435. while(*tmp) *tmp++=0;
  436. }
  437. } else {
  438. //Define string to be hashed
  439. unsigned len=0;
  440. const char *tmp = m;
  441. while(*tmp && *tmp != '/')
  442. tmp++;
  443. if(*tmp == '/')
  444. tmp++;
  445. len = tmp-m;
  446. //Compute the hash
  447. int t = len;
  448. for(auto p:impl->pos)
  449. if(p < (int)len)
  450. t += impl->assoc[m[p]];
  451. if(t >= (int)impl->remap.size() && !default_handler)
  452. return;
  453. else if(t >= (int)impl->remap.size() && default_handler) {
  454. d.matches++;
  455. default_handler(m,d), d.obj = obj;
  456. return;
  457. }
  458. int port_num = impl->remap[t];
  459. //Verify the chosen port is correct
  460. if(__builtin_expect(impl->hard_match(port_num, m), 1)) {
  461. const Port &port = ports[impl->remap[t]];
  462. if(!port.ports)
  463. d.matches++;
  464. //Append the path
  465. if(impl->enump[port_num]) {
  466. const char *msg = m;
  467. char *pos = old_end;
  468. while(*msg && *msg != '/')
  469. *pos++ = *msg++;
  470. if(strchr(port.name, '/'))
  471. *pos++ = '/';
  472. *pos = '\0';
  473. } else
  474. memcpy(old_end, impl->fixed[port_num].c_str(),
  475. impl->fixed[port_num].length()+1);
  476. d.port = &port;
  477. //Apply callback
  478. port.cb(m,d), d.obj = obj;
  479. //Remove the rest of the path
  480. old_end[0] = '\0';
  481. } else if(default_handler) {
  482. d.matches++;
  483. default_handler(m,d), d.obj = obj;
  484. }
  485. }
  486. }
  487. }
  488. const Port *Ports::operator[](const char *name) const
  489. {
  490. for(const Port &port:ports) {
  491. const char *_needle = name,
  492. *_haystack = port.name;
  493. while(*_needle && *_needle==*_haystack)_needle++,_haystack++;
  494. if(*_needle == 0 && (*_haystack == ':' || *_haystack == '\0')) {
  495. return &port;
  496. }
  497. }
  498. return NULL;
  499. }
  500. static msg_t snip(msg_t m)
  501. {
  502. while(*m && *m != '/') ++m;
  503. return m+1;
  504. }
  505. const Port *Ports::apropos(const char *path) const
  506. {
  507. if(path && path[0] == '/')
  508. ++path;
  509. for(const Port &port: ports)
  510. if(strchr(port.name,'/') && rtosc_match_path(port.name,path))
  511. return (strchr(path,'/')[1]==0) ? &port :
  512. port.ports->apropos(snip(path));
  513. //This is the lowest level, now find the best port
  514. for(const Port &port: ports)
  515. if(*path && (strstr(port.name, path)==port.name ||
  516. rtosc_match_path(port.name, path)))
  517. return &port;
  518. return NULL;
  519. }
  520. static bool parent_path_p(char *read, char *start)
  521. {
  522. if(read-start<2)
  523. return false;
  524. return read[0]=='.' && read[-1]=='.' && read[-2]=='/';
  525. }
  526. static void read_path(char *&r, char *start)
  527. {
  528. while(1)
  529. {
  530. if(r<start)
  531. break;
  532. bool doBreak = *r=='/';
  533. r--;
  534. if(doBreak)
  535. break;
  536. }
  537. }
  538. static void move_path(char *&r, char *&w, char *start)
  539. {
  540. while(1)
  541. {
  542. if(r<start)
  543. break;
  544. bool doBreak = *r=='/';
  545. *w-- = *r--;
  546. if(doBreak)
  547. break;
  548. }
  549. }
  550. char *Ports::collapsePath(char *p)
  551. {
  552. //obtain the pointer to the last non-null char
  553. char *p_end = p;
  554. while(*p_end) p_end++;
  555. p_end--;
  556. //number of subpaths to consume
  557. int consuming = 0;
  558. char *write_pos = p_end;
  559. char *read_pos = p_end;
  560. while(read_pos >= p) {
  561. //per path chunk either
  562. //(1) find a parent ref and inc consuming
  563. //(2) find a normal ref and consume
  564. //(3) find a normal ref and write through
  565. bool ppath = parent_path_p(read_pos, p);
  566. if(ppath) {
  567. read_path(read_pos, p);
  568. consuming++;
  569. } else if(consuming) {
  570. read_path(read_pos, p);
  571. consuming--;
  572. } else
  573. move_path(read_pos, write_pos, p);
  574. }
  575. //return last written location, not next to write
  576. return write_pos+1;
  577. };
  578. void Ports::refreshMagic()
  579. {
  580. delete impl;
  581. impl = new Port_Matcher;
  582. generate_minimal_hash(*this, *impl);
  583. impl->enump = new bool[ports.size()];
  584. for(int i=0; i<(int)ports.size(); ++i)
  585. impl->enump[i] = strchr(ports[i].name, '#');
  586. elms = ports.size();
  587. }
  588. ClonePorts::ClonePorts(const Ports &ports_,
  589. std::initializer_list<ClonePort> c)
  590. :Ports({})
  591. {
  592. for(auto &to_clone:c) {
  593. const Port *clone_port = NULL;
  594. for(auto &p:ports_.ports)
  595. if(!strcmp(p.name, to_clone.name))
  596. clone_port = &p;
  597. if(!clone_port && strcmp("*", to_clone.name)) {
  598. fprintf(stderr, "Cannot find a clone port for '%s'\n",to_clone.name);
  599. assert(false);
  600. }
  601. if(clone_port) {
  602. ports.push_back({clone_port->name, clone_port->metadata,
  603. clone_port->ports, to_clone.cb});
  604. } else {
  605. default_handler = to_clone.cb;
  606. }
  607. }
  608. refreshMagic();
  609. }
  610. MergePorts::MergePorts(std::initializer_list<const rtosc::Ports*> c)
  611. :Ports({})
  612. {
  613. //XXX TODO remove duplicates in some sane and documented way
  614. //e.g. repeated ports override and remove older ones
  615. for(auto *to_clone:c) {
  616. assert(to_clone);
  617. for(auto &p:to_clone->ports) {
  618. bool already_there = false;
  619. for(auto &pp:ports)
  620. if(!strcmp(pp.name, p.name))
  621. already_there = true;
  622. if(!already_there)
  623. ports.push_back(p);
  624. }
  625. }
  626. refreshMagic();
  627. }
  628. void rtosc::walk_ports(const Ports *base,
  629. char *name_buffer,
  630. size_t buffer_size,
  631. void *data,
  632. port_walker_t walker)
  633. {
  634. assert(name_buffer);
  635. //XXX buffer_size is not properly handled yet
  636. if(name_buffer[0] == 0)
  637. name_buffer[0] = '/';
  638. char *old_end = name_buffer;
  639. while(*old_end) ++old_end;
  640. for(const Port &p: *base) {
  641. if(strchr(p.name, '/')) {//it is another tree
  642. if(strchr(p.name,'#')) {
  643. const char *name = p.name;
  644. char *pos = old_end;
  645. while(*name != '#') *pos++ = *name++;
  646. const unsigned max = atoi(name+1);
  647. for(unsigned i=0; i<max; ++i)
  648. {
  649. sprintf(pos,"%d",i);
  650. //Ensure the result is a path
  651. if(strrchr(name_buffer, '/')[1] != '/')
  652. strcat(name_buffer, "/");
  653. //Recurse
  654. rtosc::walk_ports(p.ports, name_buffer, buffer_size,
  655. data, walker);
  656. }
  657. } else {
  658. //Append the path
  659. scat(name_buffer, p.name);
  660. //Recurse
  661. rtosc::walk_ports(p.ports, name_buffer, buffer_size,
  662. data, walker);
  663. }
  664. } else {
  665. if(strchr(p.name,'#')) {
  666. const char *name = p.name;
  667. char *pos = old_end;
  668. while(*name != '#') *pos++ = *name++;
  669. const unsigned max = atoi(name+1);
  670. for(unsigned i=0; i<max; ++i)
  671. {
  672. sprintf(pos,"%d",i);
  673. //Apply walker function
  674. walker(&p, name_buffer, data);
  675. }
  676. } else {
  677. //Append the path
  678. scat(name_buffer, p.name);
  679. //Apply walker function
  680. walker(&p, name_buffer, data);
  681. }
  682. }
  683. //Remove the rest of the path
  684. char *tmp = old_end;
  685. while(*tmp) *tmp++=0;
  686. }
  687. }
  688. void walk_ports2(const rtosc::Ports *base,
  689. char *name_buffer,
  690. size_t buffer_size,
  691. void *data,
  692. rtosc::port_walker_t walker)
  693. {
  694. assert(name_buffer);
  695. //XXX buffer_size is not properly handled yet
  696. if(name_buffer[0] == 0)
  697. name_buffer[0] = '/';
  698. char *old_end = name_buffer;
  699. while(*old_end) ++old_end;
  700. for(const rtosc::Port &p: *base) {
  701. if(strchr(p.name, '/')) {//it is another tree
  702. if(strchr(p.name,'#')) {
  703. const char *name = p.name;
  704. char *pos = old_end;
  705. while(*name != '#') *pos++ = *name++;
  706. const unsigned max = atoi(name+1);
  707. //for(unsigned i=0; i<max; ++i)
  708. {
  709. sprintf(pos,"[0,%d]",max-1);
  710. //Ensure the result is a path
  711. if(strrchr(name_buffer, '/')[1] != '/')
  712. strcat(name_buffer, "/");
  713. //Recurse
  714. walk_ports2(p.ports, name_buffer, buffer_size,
  715. data, walker);
  716. }
  717. } else {
  718. //Append the path
  719. scat(name_buffer, p.name);
  720. //Recurse
  721. walk_ports2(p.ports, name_buffer, buffer_size,
  722. data, walker);
  723. }
  724. } else {
  725. if(strchr(p.name,'#')) {
  726. const char *name = p.name;
  727. char *pos = old_end;
  728. while(*name != '#') *pos++ = *name++;
  729. const unsigned max = atoi(name+1);
  730. //for(unsigned i=0; i<max; ++i)
  731. {
  732. sprintf(pos,"[0,%d]",max-1);
  733. //Apply walker function
  734. walker(&p, name_buffer, data);
  735. }
  736. } else {
  737. //Append the path
  738. scat(name_buffer, p.name);
  739. //Apply walker function
  740. walker(&p, name_buffer, data);
  741. }
  742. }
  743. //Remove the rest of the path
  744. char *tmp = old_end;
  745. while(*tmp) *tmp++=0;
  746. }
  747. }
  748. static void units(std::ostream &o, const char *u)
  749. {
  750. if(!u)
  751. return;
  752. o << " units=\"" << u << "\"";
  753. }
  754. using std::ostream;
  755. using std::string;
  756. static int enum_min(Port::MetaContainer meta)
  757. {
  758. int min = 0;
  759. for(auto m:meta)
  760. if(strstr(m.title, "map "))
  761. min = atoi(m.title+4);
  762. for(auto m:meta)
  763. if(strstr(m.title, "map "))
  764. min = min>atoi(m.title+4) ? atoi(m.title+4) : min;
  765. return min;
  766. }
  767. static int enum_max(Port::MetaContainer meta)
  768. {
  769. int max = 0;
  770. for(auto m:meta)
  771. if(strstr(m.title, "map "))
  772. max = atoi(m.title+4);
  773. for(auto m:meta)
  774. if(strstr(m.title, "map "))
  775. max = max<atoi(m.title+4) ? atoi(m.title+4) : max;
  776. return max;
  777. }
  778. static ostream &add_options(ostream &o, Port::MetaContainer meta)
  779. {
  780. string sym_names = "xyzabcdefghijklmnopqrstuvw";
  781. int sym_idx = 0;
  782. bool has_options = false;
  783. for(auto m:meta)
  784. if(strstr(m.title, "map "))
  785. has_options = true;
  786. for(auto m:meta)
  787. if(strcmp(m.title, "documentation") &&
  788. strcmp(m.title, "parameter") &&
  789. strcmp(m.title, "max") &&
  790. strcmp(m.title, "min"))
  791. printf("m.title = <%s>\n", m.title);
  792. if(!has_options)
  793. return o;
  794. o << " <hints>\n";
  795. for(auto m:meta) {
  796. if(strstr(m.title, "map ")) {
  797. o << " <point symbol=\"" << sym_names[sym_idx++] << "\" value=\"";
  798. o << m.title+4 << "\">" << m.value << "</point>\n";
  799. }
  800. }
  801. o << " </hints>\n";
  802. return o;
  803. }
  804. static ostream &dump_t_f_port(ostream &o, string name, string doc)
  805. {
  806. o << " <message_in pattern=\"" << name << "\" typetag=\"T\">\n";
  807. o << " <desc>Enable " << doc << "</desc>\n";
  808. o << " <param_T symbol=\"x\"/>\n";
  809. o << " </message_in>\n";
  810. o << " <message_in pattern=\"" << name << "\" typetag=\"F\">\n";
  811. o << " <desc>Disable " << doc << "</desc>\n";
  812. o << " <param_F symbol=\"x\"/>\n";
  813. o << " </message_in>\n";
  814. o << " <message_in pattern=\"" << name << "\" typetag=\"\">\n";
  815. o << " <desc>Get state of " << doc << "</desc>\n";
  816. o << " </message_in>\n";
  817. o << " <message_out pattern=\"" << name << "\" typetag=\"T\">\n";
  818. o << " <desc>Value of " << doc << "</desc>\n";
  819. o << " <param_T symbol=\"x\"/>";
  820. o << " </message_out>\n";
  821. o << " <message_out pattern=\"" << name << "\" typetag=\"F\">\n";
  822. o << " <desc>Value of " << doc << "</desc>\n";
  823. o << " <param_F symbol=\"x\"/>";
  824. o << " </message_out>\n";
  825. return o;
  826. }
  827. static ostream &dump_any_port(ostream &o, string name, string doc)
  828. {
  829. o << " <message_in pattern=\"" << name << "\" typetag=\"*\">\n";
  830. o << " <desc>" << doc << "</desc>\n";
  831. o << " </message_in>\n";
  832. return o;
  833. }
  834. static ostream &dump_generic_port(ostream &o, string name, string doc, string type)
  835. {
  836. const char *t = type.c_str();
  837. string arg_names = "xyzabcdefghijklmnopqrstuvw";
  838. //start out with argument separator
  839. if(*t++ != ':')
  840. return o;
  841. //now real arguments (assume [] don't exist)
  842. string args;
  843. while(*t && *t != ':')
  844. args += *t++;
  845. o << " <message_in pattern=\"" << name << "\" typetag=\"" << args << "\">\n";
  846. o << " <desc>" << doc << "</desc>\n";
  847. assert(args.length()<arg_names.length());
  848. for(unsigned i=0; i<args.length(); ++i)
  849. o << " <param_" << args[i] << " symbol=\"" << arg_names[i] << "\"/>\n";
  850. o << " </message_in>\n";
  851. if(*t == ':')
  852. return dump_generic_port(o, name, doc, t);
  853. else
  854. return o;
  855. }
  856. void dump_ports_cb(const rtosc::Port *p, const char *name, void *v)
  857. {
  858. std::ostream &o = *(std::ostream*)v;
  859. auto meta = p->meta();
  860. const char *args = strchr(p->name, ':');
  861. auto mparameter = meta.find("parameter");
  862. auto mdoc = meta.find("documentation");
  863. string doc;
  864. if(mdoc != p->meta().end())
  865. doc = mdoc.value;
  866. if(meta.find("internal") != meta.end()) {
  867. doc += "[INTERNAL]";
  868. }
  869. if(mparameter != p->meta().end()) {
  870. char type = 0;
  871. if(args) {
  872. if(strchr(args, 'f'))
  873. type = 'f';
  874. else if(strchr(args, 'i'))
  875. type = 'i';
  876. else if(strchr(args, 'c'))
  877. type = 'c';
  878. else if(strchr(args, 'T'))
  879. type = 't';
  880. else if(strchr(args, 's'))
  881. type = 's';
  882. }
  883. if(!type) {
  884. fprintf(stderr, "rtosc port dumper: Cannot handle '%s'\n", name);
  885. fprintf(stderr, " args = <%s>\n", args);
  886. return;
  887. }
  888. if(type == 't') {
  889. dump_t_f_port(o, name, doc);
  890. return;
  891. }
  892. o << " <message_in pattern=\"" << name << "\" typetag=\"" << type << "\">\n";
  893. o << " <desc>Set Value of " << doc << "</desc>\n";
  894. if(meta.find("min") != meta.end() && meta.find("max") != meta.end() && type != 'c') {
  895. o << " <param_" << type << " symbol=\"x\"";
  896. units(o, meta["unit"]);
  897. o << ">\n";
  898. o << " <range_min_max " << (type == 'f' ? "lmin=\"[\" lmax=\"]\"" : "");
  899. o << " min=\"" << meta["min"] << "\" max=\"" << meta["max"] << "\"/>\n";
  900. o << " </param_" << type << ">";
  901. } else if(meta.find("enumerated") != meta.end()) {
  902. o << " <param_" << type << " symbol=\"x\">\n";
  903. o << " <range_min_max min=\"" << enum_min(meta) << "\" max=\"";
  904. o << enum_max(meta) << "\">\n";
  905. add_options(o, meta);
  906. o << " </range_min_max>\n";
  907. o << " </param_" << type << ">\n";
  908. } else {
  909. o << " <param_" << type << " symbol=\"x\"";
  910. units(o, meta["unit"]);
  911. o << "/>\n";
  912. }
  913. o << " </message_in>\n";
  914. o << " <message_in pattern=\"" << name << "\" typetag=\"\">\n";
  915. o << " <desc>Get Value of " << doc << "</desc>\n";
  916. o << " </message_in>\n";
  917. o << " <message_out pattern=\"" << name << "\" typetag=\"" << type << "\">\n";
  918. o << " <desc>Value of " << doc << "</desc>\n";
  919. if(meta.find("min") != meta.end() && meta.find("max") != meta.end() && type != 'c') {
  920. o << " <param_" << type << " symbol=\"x\"";
  921. units(o, meta["unit"]);
  922. o << ">\n";
  923. o << " <range_min_max " << (type == 'f' ? "lmin=\"[\" lmax=\"]\"" : "");
  924. o << " min=\"" << meta["min"] << "\" max=\"" << meta["max"] << "\"/>\n";
  925. o << " </param_" << type << ">\n";
  926. } else if(meta.find("enumerated") != meta.end()) {
  927. o << " <param_" << type << " symbol=\"x\">\n";
  928. o << " <range_min_max min=\"" << enum_min(meta) << "\" max=\"";
  929. o << enum_max(meta) << "\">\n";
  930. add_options(o, meta);
  931. o << " </range_min_max>\n";
  932. o << " </param_" << type << ">\n";
  933. } else {
  934. o << " <param_" << type << " symbol=\"x\"";
  935. units(o, meta["unit"]);
  936. o << "/>\n";
  937. }
  938. o << " </message_out>\n";
  939. } else if(mdoc != meta.end() && (!args || args == std::string(""))) {
  940. dump_any_port(o, name, doc);
  941. } else if(mdoc != meta.end() && args) {
  942. dump_generic_port(o, name, doc, args);
  943. } else if(mdoc != meta.end()) {
  944. fprintf(stderr, "Skipping \"%s\"\n", name);
  945. if(args) {
  946. fprintf(stderr, " type = %s\n", args);
  947. }
  948. } else
  949. fprintf(stderr, "Skipping [UNDOCUMENTED] \"%s\"\n", name);
  950. }
  951. std::ostream &rtosc::operator<<(std::ostream &o, rtosc::OscDocFormatter &formatter)
  952. {
  953. o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
  954. o << "<osc_unit format_version=\"1.0\">\n";
  955. o << " <meta>\n";
  956. o << " <name>" << formatter.prog_name << "</name>\n";
  957. o << " <uri>" << formatter.uri << "</uri>\n";
  958. o << " <doc_origin>" << formatter.doc_origin << "</doc_origin>\n";
  959. o << " <author><firstname>" << formatter.author_first;
  960. o << "</firstname><lastname>" << formatter.author_last << "</lastname></author>\n";
  961. o << " </meta>\n";
  962. char buffer[1024];
  963. memset(buffer, 0, sizeof(buffer));
  964. walk_ports2(formatter.p, buffer, 1024, &o, dump_ports_cb);
  965. o << "</osc_unit>\n";
  966. return o;
  967. }