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.

ports.h 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (c) 2012 Mark McCurry
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice (including the next
  12. * paragraph) shall be included in all copies or substantial portions of the
  13. * Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. */
  24. #ifndef RTOSC_PORTS
  25. #define RTOSC_PORTS
  26. #include <vector>
  27. #include <functional>
  28. #include <initializer_list>
  29. #include <rtosc/rtosc.h>
  30. #include <cstring>
  31. #include <cctype>
  32. #include <cstdlib>
  33. #include <cstdio>
  34. #include <string>
  35. namespace rtosc {
  36. //First define all types
  37. typedef const char *msg_t;
  38. struct Port;
  39. struct Ports;
  40. struct RtData
  41. {
  42. RtData(void);
  43. char *loc;
  44. size_t loc_size;
  45. void *obj;
  46. int matches;
  47. const Port *port;
  48. const char *message;
  49. virtual void reply(const char *path, const char *args, ...);
  50. virtual void reply(const char *msg);
  51. virtual void chain(const char *path, const char *args, ...){};
  52. virtual void chain(const char *msg){};
  53. virtual void broadcast(const char *path, const char *args, ...);
  54. virtual void broadcast(const char *msg);
  55. virtual void forward(const char *rational=NULL);
  56. };
  57. /**
  58. * Port in rtosc dispatching hierarchy
  59. */
  60. struct Port {
  61. const char *name; //< Pattern for messages to match
  62. const char *metadata;//< Statically accessable data about port
  63. const Ports *ports; //< Pointer to further ports
  64. std::function<void(msg_t, RtData&)> cb;//< Callback for matching functions
  65. class MetaIterator
  66. {
  67. public:
  68. MetaIterator(const char *str);
  69. //A bit odd to return yourself, but it seems to work for this
  70. //context
  71. const MetaIterator& operator*(void) const {return *this;}
  72. const MetaIterator* operator->(void) const {return this;}
  73. bool operator==(MetaIterator a) {return title == a.title;}
  74. bool operator!=(MetaIterator a) {return title != a.title;}
  75. MetaIterator& operator++(void);
  76. const char *title;
  77. const char *value;
  78. };
  79. class MetaContainer
  80. {
  81. public:
  82. MetaContainer(const char *str_);
  83. MetaIterator begin(void) const;
  84. MetaIterator end(void) const;
  85. MetaIterator find(const char *str) const;
  86. size_t length(void) const;
  87. const char *operator[](const char *str) const;
  88. const char *str_ptr;
  89. };
  90. MetaContainer meta(void) const
  91. {
  92. if(metadata && *metadata == ':')
  93. return MetaContainer(metadata+1);
  94. else
  95. return MetaContainer(metadata);
  96. }
  97. };
  98. /**
  99. * Ports - a dispatchable collection of Port entries
  100. *
  101. * This structure makes it somewhat easier to perform actions on collections of
  102. * port entries and it is responsible for the dispatching of OSC messages to
  103. * their respective ports.
  104. * That said, it is a very simple structure, which uses a stl container to store
  105. * all data in a simple dispatch table.
  106. * All methods post-initialization are RT safe (assuming callbacks are RT safe)
  107. */
  108. struct Ports
  109. {
  110. std::vector<Port> ports;
  111. std::function<void(msg_t, RtData&)> default_handler;
  112. typedef std::vector<Port>::const_iterator itr_t;
  113. /**Forwards to builtin container*/
  114. itr_t begin() const {return ports.begin();}
  115. /**Forwards to builtin container*/
  116. itr_t end() const {return ports.end();}
  117. /**Forwards to builtin container*/
  118. size_t size() const {return ports.size();}
  119. /**Forwards to builtin container*/
  120. const Port &operator[](unsigned i) const {return ports[i];}
  121. Ports(std::initializer_list<Port> l);
  122. ~Ports(void);
  123. Ports(const Ports&) = delete;
  124. /**
  125. * Dispatches message to all matching ports.
  126. * This uses simple pattern matching available in rtosc::match.
  127. *
  128. * @param m a valid OSC message
  129. * @param d The RtData object shall contain a path buffer (or null), the length of
  130. * the buffer, a pointer to data.
  131. */
  132. void dispatch(const char *m, RtData &d, bool base_dispatch=false) const;
  133. /**
  134. * Retrieve local port by name
  135. * TODO implement full matching
  136. */
  137. const Port *operator[](const char *name) const;
  138. /**
  139. * Find the best match for a given path
  140. *
  141. * @parameter path partial OSC path
  142. * @returns first path prefixed by the argument
  143. *
  144. * Example usage:
  145. * @code
  146. * Ports p = {{"foo",0,0,dummy_method},
  147. * {"flam",0,0,dummy_method},
  148. * {"bar",0,0,dummy_method}};
  149. * p.apropos("/b")->name;//bar
  150. * p.apropos("/f")->name;//foo
  151. * p.apropos("/fl")->name;//flam
  152. * p.apropos("/gg");//NULL
  153. * @endcode
  154. */
  155. const Port *apropos(const char *path) const;
  156. /**
  157. * Collapse path with parent path identifiers "/.."
  158. *
  159. * e.g. /foo/bar/../baz => /foo/baz
  160. */
  161. static char *collapsePath(char *p);
  162. protected:
  163. void refreshMagic(void);
  164. private:
  165. //Performance hacks
  166. class Port_Matcher *impl;
  167. unsigned elms;
  168. };
  169. struct ClonePort
  170. {
  171. const char *name;
  172. std::function<void(msg_t, RtData&)> cb;
  173. };
  174. struct ClonePorts:public Ports
  175. {
  176. ClonePorts(const Ports &p,
  177. std::initializer_list<ClonePort> c);
  178. };
  179. struct MergePorts:public Ports
  180. {
  181. MergePorts(std::initializer_list<const Ports*> c);
  182. };
  183. /*********************
  184. * Port walking code *
  185. *********************/
  186. //typedef std::function<void(const Port*,const char*)> port_walker_t;
  187. typedef void(*port_walker_t)(const Port*,const char*,void*);
  188. void walk_ports(const Ports *base,
  189. char *name_buffer,
  190. size_t buffer_size,
  191. void *data,
  192. port_walker_t walker);
  193. /*********************
  194. * Port Dumping code *
  195. *********************/
  196. struct OscDocFormatter
  197. {
  198. const Ports *p;
  199. std::string prog_name;
  200. std::string uri;
  201. std::string doc_origin;
  202. std::string author_first;
  203. std::string author_last;
  204. //TODO extend this some more
  205. };
  206. std::ostream &operator<<(std::ostream &o, OscDocFormatter &formatter);
  207. };
  208. #endif