JACK tools
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.

335 lines
7.5KB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <getopt.h>
  6. #include <jack/jack.h>
  7. #include <jack/metadata.h>
  8. #include <jack/uuid.h>
  9. #include <jack/session.h>
  10. static int subject_is_client = 0;
  11. static int subject_is_port = 0;
  12. static jack_uuid_t uuid;
  13. static char* subject;
  14. static void
  15. show_usage (void)
  16. {
  17. fprintf (stderr, "\nUsage: jack_property [options] UUID [ key [ value [ type ] ] ]\n");
  18. fprintf (stderr, "Set/Display JACK properties (metadata).\n\n");
  19. fprintf (stderr, "Set options:\n");
  20. fprintf (stderr, " -s, --set Set property \"key\" to \"value\" for \"UUID\" with optional MIME type \"type\"\n");
  21. fprintf (stderr, " -d, --delete Remove/delete property \"key\" for \"UUID\"\n");
  22. fprintf (stderr, " -d, --delete UUID Remove/delete all properties for \"UUID\"\n");
  23. fprintf (stderr, " -D, --delete-all Remove/delete all properties\n");
  24. fprintf (stderr, " --client Interpret UUID as a client name, not a UUID\n");
  25. fprintf (stderr, " --port \tInterpret UUID as a port name, not a UUID\n");
  26. fprintf (stderr, "\nDisplay options:\n");
  27. fprintf (stderr, " -l Show all properties\n");
  28. fprintf (stderr, " -l, --list UUID \tShow value all properties of UUID\n");
  29. fprintf (stderr, " -l, --list UUID key Show value for key of UUID\n");
  30. fprintf (stderr, "\nFor more information see http://jackaudio.org/\n");
  31. }
  32. static int
  33. get_subject (jack_client_t* client, char* argv[], int* optind)
  34. {
  35. if (subject_is_client) {
  36. char* cstr = argv[(*optind)++];
  37. char* ustr;
  38. if ((ustr = jack_get_uuid_for_client_name (client, cstr)) == NULL) {
  39. fprintf (stderr, "cannot get UUID for client named %s\n", cstr);
  40. return -1;
  41. }
  42. if (jack_uuid_parse (ustr, &uuid)) {
  43. fprintf (stderr, "cannot parse client UUID as UUID\n");
  44. return -1;
  45. }
  46. subject = cstr;
  47. } else if (subject_is_port) {
  48. char* pstr = argv[(*optind)++];
  49. jack_port_t* port;
  50. if ((port = jack_port_by_name (client, pstr)) == NULL) {
  51. fprintf (stderr, "cannot find port name %s\n", pstr);
  52. return -1;
  53. }
  54. uuid = jack_port_uuid (port);
  55. subject = pstr;
  56. } else {
  57. char* str = argv[(*optind)++];
  58. if (jack_uuid_parse (str, &uuid)) {
  59. fprintf (stderr, "cannot parse subject as UUID\n");
  60. return -1;
  61. }
  62. subject = str;
  63. }
  64. return 0;
  65. }
  66. int main (int argc, char* argv[])
  67. {
  68. jack_client_t* client = NULL;
  69. jack_options_t options = JackNoStartServer;
  70. char* key = NULL;
  71. char* value = NULL;
  72. char* type = NULL;
  73. int set = 1;
  74. int delete = 0;
  75. int delete_all = 0;
  76. int c;
  77. int option_index;
  78. extern int optind;
  79. struct option long_options[] = {
  80. { "set", 0, 0, 's' },
  81. { "delete", 0, 0, 'd' },
  82. { "delete-all", 0, 0, 'D' },
  83. { "list", 0, 0, 'l' },
  84. { "all", 0, 0, 'a' },
  85. { "client", 0, 0, 'c' },
  86. { "port", 0, 0, 'p' },
  87. { 0, 0, 0, 0 }
  88. };
  89. if (argc < 2) {
  90. show_usage ();
  91. exit (1);
  92. }
  93. while ((c = getopt_long (argc, argv, "sdDlaApc", long_options, &option_index)) >= 0) {
  94. switch (c) {
  95. case 's':
  96. if (argc < 5) {
  97. show_usage ();
  98. exit (1);
  99. }
  100. set = 1;
  101. break;
  102. case 'd':
  103. if (argc < 3) {
  104. show_usage ();
  105. return 1;
  106. }
  107. set = 0;
  108. delete = 1;
  109. break;
  110. case 'D':
  111. delete = 0;
  112. set = 0;
  113. delete_all = 1;
  114. break;
  115. case 'l':
  116. set = 0;
  117. delete = 0;
  118. delete_all = 0;
  119. break;
  120. case 'a':
  121. set = 0;
  122. delete = 0;
  123. delete_all = 0;
  124. break;
  125. case 'p':
  126. subject_is_port = 1;
  127. break;
  128. case 'c':
  129. subject_is_client = 1;
  130. break;
  131. case '?':
  132. default:
  133. show_usage ();
  134. exit (1);
  135. }
  136. }
  137. if ((client = jack_client_open ("jack-property", options, NULL)) == 0) {
  138. fprintf (stderr, "Cannot connect to JACK server\n");
  139. exit (1);
  140. }
  141. if (delete_all) {
  142. if (jack_remove_all_properties (client) == 0) {
  143. printf ("JACK metadata successfully delete\n");
  144. exit (0);
  145. }
  146. exit (1);
  147. }
  148. if (delete) {
  149. int args_left = argc - optind;
  150. if (args_left < 1) {
  151. show_usage ();
  152. exit (1);
  153. }
  154. /* argc == 3: delete all properties for a subject
  155. argc == 4: delete value of key for subject
  156. */
  157. if (args_left >= 2) {
  158. if (get_subject (client, argv, &optind)) {
  159. return 1;
  160. }
  161. key = argv[optind++];
  162. if (jack_remove_property (client, uuid, key)) {
  163. fprintf (stderr, "\"%s\" property not removed for %s\n", key, subject);
  164. exit (1);
  165. }
  166. } else {
  167. if (get_subject (client, argv, &optind)) {
  168. return 1;
  169. }
  170. if (jack_remove_properties (client, uuid) < 0) {
  171. fprintf (stderr, "cannot remove properties for UUID %s\n", subject);
  172. exit (1);
  173. }
  174. }
  175. } else if (set) {
  176. int args_left = argc - optind;
  177. if (get_subject (client, argv, &optind)) {
  178. return -1;
  179. }
  180. key = argv[optind++];
  181. value = argv[optind++];
  182. if (args_left >= 3) {
  183. type = argv[optind++];
  184. } else {
  185. type = "";
  186. }
  187. if (jack_set_property (client, uuid, key, value, type)) {
  188. fprintf (stderr, "cannot set value for key %s of %s\n", value, subject);
  189. exit (1);
  190. }
  191. } else {
  192. /* list properties */
  193. int args_left = argc - optind;
  194. if (args_left >= 2) {
  195. /* list properties for a UUID/key pair */
  196. if (get_subject (client, argv, &optind)) {
  197. return -1;
  198. }
  199. key = argv[optind++];
  200. if (jack_get_property (uuid, key, &value, &type) == 0) {
  201. printf ("%s\n", value);
  202. free (value);
  203. if (type) {
  204. free (type);
  205. }
  206. } else {
  207. fprintf (stderr, "Value not found for %s of %s\n", key, subject);
  208. exit (1);
  209. }
  210. } else if (args_left == 1) {
  211. /* list all properties for a given UUID */
  212. jack_description_t description;
  213. size_t cnt, n;
  214. if (get_subject (client, argv, &optind)) {
  215. return -1;
  216. }
  217. if ((cnt = jack_get_properties (uuid, &description)) < 0) {
  218. fprintf (stderr, "could not retrieve properties for %s\n", subject);
  219. exit (1);
  220. }
  221. for (n = 0; n < cnt; ++n) {
  222. if (description.properties[n].type) {
  223. printf ("key: %s value: %s type: %s\n",
  224. description.properties[n].key,
  225. description.properties[n].data,
  226. description.properties[n].type);
  227. } else {
  228. printf ("key: %s value: %s\n",
  229. description.properties[n].key,
  230. description.properties[n].data);
  231. }
  232. }
  233. jack_free_description (&description, 0);
  234. } else {
  235. /* list all properties */
  236. jack_description_t* description;
  237. size_t cnt;
  238. size_t p;
  239. size_t n;
  240. char buf[JACK_UUID_STRING_SIZE];
  241. if ((cnt = jack_get_all_properties (&description)) < 0) {
  242. fprintf (stderr, "could not retrieve properties for %s\n", subject);
  243. exit (1);
  244. }
  245. for (n = 0; n < cnt; ++n) {
  246. jack_uuid_unparse (description[n].subject, buf);
  247. printf ("%s\n", buf);
  248. for (p = 0; p < description[n].property_cnt; ++p) {
  249. if (description[n].properties[p].type) {
  250. printf ("key: %s value: %s type: %s\n",
  251. description[n].properties[p].key,
  252. description[n].properties[p].data,
  253. description[n].properties[p].type);
  254. } else {
  255. printf ("key: %s value: %s\n",
  256. description[n].properties[p].key,
  257. description[n].properties[p].data);
  258. }
  259. }
  260. jack_free_description (&description[n], 0);
  261. }
  262. free (description);
  263. }
  264. }
  265. (void) jack_client_close (client);
  266. return 0;
  267. }