jack2 codebase
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.

337 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 \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 list_all = 0;
  77. int c;
  78. int option_index;
  79. extern int optind;
  80. struct option long_options[] = {
  81. { "set", 0, 0, 's' },
  82. { "delete", 0, 0, 'd' },
  83. { "delete-all", 0, 0, 'D' },
  84. { "list", 0, 0, 'l' },
  85. { "all", 0, 0, 'a' },
  86. { "client", 0, 0, 'c' },
  87. { "port", 0, 0, 'p' },
  88. { 0, 0, 0, 0 }
  89. };
  90. if (argc < 2) {
  91. show_usage ();
  92. exit (1);
  93. }
  94. while ((c = getopt_long (argc, argv, "sdDlaApc", long_options, &option_index)) >= 0) {
  95. switch (c) {
  96. case 's':
  97. if (argc < 5) {
  98. show_usage ();
  99. exit (1);
  100. }
  101. set = 1;
  102. break;
  103. case 'd':
  104. if (argc < 3) {
  105. show_usage ();
  106. return 1;
  107. }
  108. set = 0;
  109. delete = 1;
  110. break;
  111. case 'D':
  112. delete = 0;
  113. set = 0;
  114. delete_all = 1;
  115. break;
  116. case 'l':
  117. set = 0;
  118. delete = 0;
  119. delete_all = 0;
  120. break;
  121. case 'a':
  122. list_all = 1;
  123. set = 0;
  124. delete = 0;
  125. delete_all = 0;
  126. break;
  127. case 'p':
  128. subject_is_port = 1;
  129. break;
  130. case 'c':
  131. subject_is_client = 1;
  132. break;
  133. case '?':
  134. default:
  135. show_usage ();
  136. exit (1);
  137. }
  138. }
  139. if ((client = jack_client_open ("jack-property", options, NULL)) == 0) {
  140. fprintf (stderr, "Cannot connect to JACK server\n");
  141. exit (1);
  142. }
  143. if (delete_all) {
  144. if (jack_remove_all_properties (client) == 0) {
  145. printf ("JACK metadata successfully delete\n");
  146. exit (0);
  147. }
  148. exit (1);
  149. }
  150. if (delete) {
  151. int args_left = argc - optind;
  152. if (args_left < 1) {
  153. show_usage ();
  154. exit (1);
  155. }
  156. /* argc == 3: delete all properties for a subject
  157. argc == 4: delete value of key for subject
  158. */
  159. if (args_left >= 2) {
  160. if (get_subject (client, argv, &optind)) {
  161. return 1;
  162. }
  163. key = argv[optind++];
  164. if (jack_remove_property (client, uuid, key)) {
  165. fprintf (stderr, "\"%s\" property not removed for %s\n", key, subject);
  166. exit (1);
  167. }
  168. } else {
  169. if (get_subject (client, argv, &optind)) {
  170. return 1;
  171. }
  172. if (jack_remove_properties (client, uuid) < 0) {
  173. fprintf (stderr, "cannot remove properties for UUID %s\n", subject);
  174. exit (1);
  175. }
  176. }
  177. } else if (set) {
  178. int args_left = argc - optind;
  179. if (get_subject (client, argv, &optind)) {
  180. return -1;
  181. }
  182. key = argv[optind++];
  183. value = argv[optind++];
  184. if (args_left >= 3) {
  185. type = argv[optind++];
  186. } else {
  187. type = "";
  188. }
  189. if (jack_set_property (client, uuid, key, value, type)) {
  190. fprintf (stderr, "cannot set value for key %s of %s\n", value, subject);
  191. exit (1);
  192. }
  193. } else {
  194. /* list properties */
  195. int args_left = argc - optind;
  196. if (args_left >= 2) {
  197. /* list properties for a UUID/key pair */
  198. if (get_subject (client, argv, &optind)) {
  199. return -1;
  200. }
  201. key = argv[optind++];
  202. if (jack_get_property (uuid, key, &value, &type) == 0) {
  203. printf ("%s\n", value);
  204. free (value);
  205. if (type) {
  206. free (type);
  207. }
  208. } else {
  209. fprintf (stderr, "Value not found for %s of %s\n", key, subject);
  210. exit (1);
  211. }
  212. } else if (args_left == 1) {
  213. /* list all properties for a given UUID */
  214. jack_description_t description;
  215. size_t cnt, n;
  216. if (get_subject (client, argv, &optind)) {
  217. return -1;
  218. }
  219. if ((cnt = jack_get_properties (uuid, &description)) < 0) {
  220. fprintf (stderr, "could not retrieve properties for %s\n", subject);
  221. exit (1);
  222. }
  223. for (n = 0; n < cnt; ++n) {
  224. if (description.properties[n].type) {
  225. printf ("key: %s value: %s type: %s\n",
  226. description.properties[n].key,
  227. description.properties[n].data,
  228. description.properties[n].type);
  229. } else {
  230. printf ("key: %s value: %s\n",
  231. description.properties[n].key,
  232. description.properties[n].data);
  233. }
  234. }
  235. jack_free_description (&description, 0);
  236. } else {
  237. /* list all properties */
  238. jack_description_t* description;
  239. size_t cnt;
  240. size_t p;
  241. size_t n;
  242. char buf[JACK_UUID_STRING_SIZE];
  243. if ((cnt = jack_get_all_properties (&description)) < 0) {
  244. fprintf (stderr, "could not retrieve properties for %s\n", subject);
  245. exit (1);
  246. }
  247. for (n = 0; n < cnt; ++n) {
  248. jack_uuid_unparse (description[n].subject, buf);
  249. printf ("%s\n", buf);
  250. for (p = 0; p < description[n].property_cnt; ++p) {
  251. if (description[n].properties[p].type) {
  252. printf ("key: %s value: %s type: %s\n",
  253. description[n].properties[p].key,
  254. description[n].properties[p].data,
  255. description[n].properties[p].type);
  256. } else {
  257. printf ("key: %s value: %s\n",
  258. description[n].properties[p].key,
  259. description[n].properties[p].data);
  260. }
  261. }
  262. jack_free_description (&description[n], 0);
  263. }
  264. free (description);
  265. }
  266. }
  267. (void) jack_client_close (client);
  268. return 0;
  269. }