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.

265 lines
6.6KB

  1. /*
  2. * Carla patchbay utils
  3. * Copyright (C) 2011-2014 Filipe Coelho <falktx@falktx.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or 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 doc/GPL.txt file.
  16. */
  17. #ifndef CARLA_PATCHBAY_UTILS_HPP_INCLUDED
  18. #define CARLA_PATCHBAY_UTILS_HPP_INCLUDED
  19. #include "LinkedList.hpp"
  20. #define STR_MAX 0xFF
  21. // -----------------------------------------------------------------------
  22. struct GroupNameToId {
  23. uint group;
  24. char name[STR_MAX+1]; // globally unique
  25. void clear() noexcept
  26. {
  27. group = 0;
  28. name[0] = '\0';
  29. }
  30. void setData(const uint g, const char n[]) noexcept
  31. {
  32. group = g;
  33. rename(n);
  34. }
  35. void rename(const char n[]) noexcept
  36. {
  37. std::strncpy(name, n, STR_MAX);
  38. name[STR_MAX] = '\0';
  39. }
  40. bool operator==(const GroupNameToId& groupNameToId) const noexcept
  41. {
  42. if (groupNameToId.group != group)
  43. return false;
  44. if (std::strcmp(groupNameToId.name, name) != 0)
  45. return false;
  46. return true;
  47. }
  48. bool operator!=(const GroupNameToId& groupNameToId) const noexcept
  49. {
  50. return !operator==(groupNameToId);
  51. }
  52. };
  53. struct PatchbayGroupList {
  54. uint lastId;
  55. LinkedList<GroupNameToId> list;
  56. PatchbayGroupList() noexcept
  57. : lastId(0),
  58. list() {}
  59. void clear() noexcept
  60. {
  61. lastId = 0;
  62. list.clear();
  63. }
  64. uint getGroupId(const char* const groupName) const noexcept
  65. {
  66. CARLA_SAFE_ASSERT_RETURN(groupName != nullptr && groupName[0] != '\0', 0);
  67. for (LinkedList<GroupNameToId>::Itenerator it = list.begin(); it.valid(); it.next())
  68. {
  69. const GroupNameToId& groupNameToId(it.getValue());
  70. if (std::strcmp(groupNameToId.name, groupName) == 0)
  71. return groupNameToId.group;
  72. }
  73. return 0;
  74. }
  75. const char* getGroupName(const uint groupId) const noexcept
  76. {
  77. static const char fallback[] = { '\0' };
  78. for (LinkedList<GroupNameToId>::Itenerator it = list.begin(); it.valid(); it.next())
  79. {
  80. const GroupNameToId& groupNameToId(it.getValue());
  81. if (groupNameToId.group == groupId)
  82. return groupNameToId.name;
  83. }
  84. return fallback;
  85. }
  86. };
  87. // -----------------------------------------------------------------------
  88. struct PortNameToId {
  89. uint group;
  90. uint port;
  91. char name[STR_MAX+1]; // locally unique
  92. char fullName[STR_MAX+1]; // globally unique
  93. void clear() noexcept
  94. {
  95. group = 0;
  96. port = 0;
  97. name[0] = '\0';
  98. fullName[0] = '\0';
  99. }
  100. void setData(const uint g, const uint p, const char n[], const char fn[]) noexcept
  101. {
  102. group = g;
  103. port = p;
  104. rename(n, fn);
  105. }
  106. void rename(const char n[], const char fn[]) noexcept
  107. {
  108. std::strncpy(name, n, STR_MAX);
  109. name[STR_MAX] = '\0';
  110. std::strncpy(fullName, fn, STR_MAX);
  111. fullName[STR_MAX] = '\0';
  112. }
  113. bool operator==(const PortNameToId& portNameToId) noexcept
  114. {
  115. if (portNameToId.group != group || portNameToId.port != port)
  116. return false;
  117. if (std::strcmp(portNameToId.name, name) != 0)
  118. return false;
  119. if (std::strcmp(portNameToId.fullName, fullName) != 0)
  120. return false;
  121. return true;
  122. }
  123. bool operator!=(const PortNameToId& portNameToId) noexcept
  124. {
  125. return !operator==(portNameToId);
  126. }
  127. };
  128. struct PatchbayPortList {
  129. uint lastId;
  130. LinkedList<PortNameToId> list;
  131. PatchbayPortList() noexcept
  132. : lastId(0),
  133. list() {}
  134. void clear() noexcept
  135. {
  136. lastId = 0;
  137. list.clear();
  138. }
  139. const char* getFullPortName(const uint groupId, const uint portId) const noexcept
  140. {
  141. static const char fallback[] = { '\0' };
  142. for (LinkedList<PortNameToId>::Itenerator it = list.begin(); it.valid(); it.next())
  143. {
  144. const PortNameToId& portNameToId(it.getValue());
  145. if (portNameToId.group == groupId && portNameToId.port == portId)
  146. return portNameToId.fullName;
  147. }
  148. return fallback;
  149. }
  150. const PortNameToId& getPortNameToId(const char* const fullPortName) const noexcept
  151. {
  152. static const PortNameToId fallback = { 0, 0, { '\0' }, { '\0' } };
  153. CARLA_SAFE_ASSERT_RETURN(fullPortName != nullptr && fullPortName[0] != '\0', fallback);
  154. for (LinkedList<PortNameToId>::Itenerator it = list.begin(); it.valid(); it.next())
  155. {
  156. const PortNameToId& portNameToId(it.getValue());
  157. if (std::strcmp(portNameToId.fullName, fullPortName) == 0)
  158. return portNameToId;
  159. }
  160. return fallback;
  161. }
  162. };
  163. // -----------------------------------------------------------------------
  164. struct ConnectionToId {
  165. uint id;
  166. uint groupA, portA;
  167. uint groupB, portB;
  168. void clear() noexcept
  169. {
  170. id = 0;
  171. groupA = 0;
  172. portA = 0;
  173. groupB = 0;
  174. portB = 0;
  175. }
  176. void setData(const uint i, const uint gA, const uint pA, const uint gB, const uint pB) noexcept
  177. {
  178. id = i;
  179. groupA = gA;
  180. portA = pA;
  181. groupB = gB;
  182. portB = pB;
  183. }
  184. bool operator==(const ConnectionToId& connectionToId) const noexcept
  185. {
  186. if (connectionToId.id != id)
  187. return false;
  188. if (connectionToId.groupA != groupA || connectionToId.portA != portA)
  189. return false;
  190. if (connectionToId.groupB != groupB || connectionToId.portB != portB)
  191. return false;
  192. return true;
  193. }
  194. bool operator!=(const ConnectionToId& connectionToId) const noexcept
  195. {
  196. return !operator==(connectionToId);
  197. }
  198. };
  199. struct PatchbayConnectionList {
  200. uint lastId;
  201. LinkedList<ConnectionToId> list;
  202. PatchbayConnectionList() noexcept
  203. : lastId(0),
  204. list() {}
  205. void clear() noexcept
  206. {
  207. lastId = 0;
  208. list.clear();
  209. }
  210. };
  211. // -----------------------------------------------------------------------
  212. #endif // CARLA_PATCHBAY_UTILS_HPP_INCLUDED