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.

332 lines
11KB

  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 Interpret UUID as a port name, not a UUID\n");
  26. fprintf (stderr, "Display options:\n");
  27. fprintf (stderr, " -l Show all properties\n");
  28. fprintf (stderr, " -l, --list UUID Show value all properties of UUID\n");
  29. fprintf (stderr, " -l, --list UUID key Show value for key of UUID\n");
  30. fprintf (stderr, "For 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. }
  41. if (jack_uuid_parse (ustr, uuid)) {
  42. fprintf (stderr, "cannot parse client UUID as UUID\n");
  43. return -1;
  44. }
  45. subject = cstr;
  46. } else if (subject_is_port) {
  47. char* pstr = argv[(*optind)++];
  48. jack_port_t* port;
  49. if ((port = jack_port_by_name (client, pstr)) == NULL) {
  50. fprintf (stderr, "cannot find port name %s\n", pstr);
  51. return -1;
  52. }
  53. jack_port_uuid (port, uuid);
  54. subject = pstr;
  55. } else {
  56. char* str = argv[(*optind)++];
  57. if (jack_uuid_parse (str, uuid)) {
  58. fprintf (stderr, "cannot parse subject as UUID\n");
  59. return -1;
  60. }
  61. subject = str;
  62. }
  63. return 0;
  64. }
  65. int main (int argc, char* argv[])
  66. {
  67. jack_client_t* client = NULL;
  68. jack_options_t options = JackNullOption;
  69. char* key = NULL;
  70. char* value = NULL;
  71. char* type = NULL;
  72. int set = 1;
  73. int delete = 0;
  74. int delete_all = 0;
  75. int list_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. while ((c = getopt_long (argc, argv, "sdDlaApc", long_options, &option_index)) >= 0) {
  90. switch (c) {
  91. case 's':
  92. if (argc < 5) {
  93. show_usage ();
  94. exit (1);
  95. }
  96. set = 1;
  97. break;
  98. case 'd':
  99. if (argc < 3) {
  100. show_usage ();
  101. return 1;
  102. }
  103. set = 0;
  104. delete = 1;
  105. break;
  106. case 'D':
  107. delete = 0;
  108. set = 0;
  109. delete_all = 1;
  110. break;
  111. case 'l':
  112. set = 0;
  113. delete = 0;
  114. delete_all = 0;
  115. break;
  116. case 'a':
  117. list_all = 1;
  118. set = 0;
  119. delete = 0;
  120. delete_all = 0;
  121. break;
  122. case 'p':
  123. subject_is_port = 1;
  124. break;
  125. case 'c':
  126. subject_is_client = 1;
  127. break;
  128. case '?':
  129. default:
  130. show_usage ();
  131. exit (1);
  132. }
  133. }
  134. if ((client = jack_client_open ("jack-property", options, NULL)) == 0) {
  135. fprintf (stderr, "Cannot connect to JACK server\n");
  136. exit (1);
  137. }
  138. if (delete_all) {
  139. if (jack_remove_all_properties (client) == 0) {
  140. printf ("JACK metadata successfully delete\n");
  141. exit (0);
  142. }
  143. exit (1);
  144. }
  145. if (delete) {
  146. int args_left = argc - optind;
  147. if (args_left < 1) {
  148. show_usage ();
  149. exit (1);
  150. }
  151. /* argc == 3: delete all properties for a subject
  152. argc == 4: delete value of key for subject
  153. */
  154. if (args_left >= 2) {
  155. if (get_subject (client, argv, &optind)) {
  156. return 1;
  157. }
  158. key = argv[optind++];
  159. if (jack_remove_property (client, uuid, key)) {
  160. fprintf (stderr, "\"%s\" property not removed for %s\n", key, subject);
  161. exit (1);
  162. }
  163. } else {
  164. if (get_subject (client, argv, &optind)) {
  165. return 1;
  166. }
  167. if (jack_remove_properties (client, uuid) < 0) {
  168. fprintf (stderr, "cannot remove properties for UUID %s\n", subject);
  169. exit (1);
  170. }
  171. }
  172. } else if (set) {
  173. int args_left = argc - optind;
  174. if (get_subject (client, argv, &optind)) {
  175. return -1;
  176. }
  177. key = argv[optind++];
  178. value = argv[optind++];
  179. if (args_left >= 3) {
  180. type = argv[optind++];
  181. } else {
  182. type = "";
  183. }
  184. if (jack_set_property (client, uuid, key, value, type)) {
  185. fprintf (stderr, "cannot set value for key %s of %s\n", value, subject);
  186. exit (1);
  187. }
  188. } else {
  189. /* list properties */
  190. int args_left = argc - optind;
  191. if (args_left >= 2) {
  192. /* list properties for a UUID/key pair */
  193. if (get_subject (client, argv, &optind)) {
  194. return -1;
  195. }
  196. key = argv[optind++];
  197. if (jack_get_property (uuid, key, &value, &type) == 0) {
  198. printf ("%s\n", value);
  199. free (value);
  200. if (type) {
  201. free (type);
  202. }
  203. } else {
  204. fprintf (stderr, "Value not found for %s of %s\n", key, subject);
  205. exit (1);
  206. }
  207. } else if (args_left == 1) {
  208. /* list all properties for a given UUID */
  209. jack_description_t description;
  210. size_t cnt, n;
  211. if (get_subject (client, argv, &optind)) {
  212. return -1;
  213. }
  214. if ((cnt = jack_get_properties (uuid, &description)) < 0) {
  215. fprintf (stderr, "could not retrieve properties for %s\n", subject);
  216. exit (1);
  217. }
  218. for (n = 0; n < cnt; ++n) {
  219. if (description.properties[n].type) {
  220. printf ("key: %s value: %s type: %s\n",
  221. description.properties[n].key,
  222. description.properties[n].data,
  223. description.properties[n].type);
  224. } else {
  225. printf ("key: %s value: %s\n",
  226. description.properties[n].key,
  227. description.properties[n].data);
  228. }
  229. }
  230. jack_free_description (&description, 0);
  231. } else {
  232. /* list all properties */
  233. jack_description_t* description;
  234. size_t cnt;
  235. size_t p;
  236. size_t n;
  237. char buf[JACK_UUID_STRING_SIZE];
  238. if ((cnt = jack_get_all_properties (&description)) < 0) {
  239. fprintf (stderr, "could not retrieve properties for %s\n", subject);
  240. exit (1);
  241. }
  242. for (n = 0; n < cnt; ++n) {
  243. jack_uuid_unparse (description[n].subject, buf);
  244. printf ("%s\n", buf);
  245. for (p = 0; p < description[n].property_cnt; ++p) {
  246. if (description[n].properties[p].type) {
  247. printf ("key: %s value: %s type: %s\n",
  248. description[n].properties[n].key,
  249. description[n].properties[n].data,
  250. description[n].properties[n].type);
  251. } else {
  252. printf ("key: %s value: %s\n",
  253. description[n].properties[p].key,
  254. description[n].properties[p].data);
  255. }
  256. }
  257. jack_free_description (&description[n], 0);
  258. }
  259. free (description);
  260. }
  261. }
  262. (void) jack_client_close (client);
  263. return 0;
  264. }