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.

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