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.

171 lines
3.7KB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <signal.h>
  5. #include <unistd.h>
  6. #include <getopt.h>
  7. #include <jack/jack.h>
  8. #include <jack/intclient.h>
  9. jack_client_t *client;
  10. jack_intclient_t intclient;
  11. char *client_name;
  12. char *intclient_name;
  13. char *load_name;
  14. char *load_init = NULL;
  15. char *server_name = NULL;
  16. int wait_opt = 0;
  17. void
  18. signal_handler (int sig)
  19. {
  20. jack_status_t status;
  21. fprintf (stderr, "signal received, unloading...");
  22. status = jack_internal_client_unload (client, intclient);
  23. if (status & JackFailure)
  24. fprintf (stderr, "(failed), status = 0x%2.0x\n", status);
  25. else
  26. fprintf (stderr, "(succeeded)\n");
  27. jack_client_close (client);
  28. exit (0);
  29. }
  30. void
  31. show_usage ()
  32. {
  33. fprintf (stderr, "usage: %s [ options ] client-name [ load-name "
  34. "[ init-string]]\n\noptions:\n", client_name);
  35. fprintf (stderr,
  36. "\t-h, --help \t\t print help message\n"
  37. "\t-i, --init string\t initialize string\n"
  38. "\t-s, --server name\t select JACK server\n"
  39. "\t-w, --wait \t\t wait for signal, then unload\n"
  40. "\n"
  41. );
  42. }
  43. int
  44. parse_args (int argc, char *argv[])
  45. {
  46. int c;
  47. int option_index = 0;
  48. char *short_options = "hi:s:w";
  49. struct option long_options[] = {
  50. { "help", 0, 0, 'h' },
  51. { "init", required_argument, 0, 'i' },
  52. { "server", required_argument, 0, 's' },
  53. { "wait", 0, 0, 'w' },
  54. { 0, 0, 0, 0 }
  55. };
  56. client_name = strrchr(argv[0], '/');
  57. if (client_name == NULL) {
  58. client_name = argv[0];
  59. } else {
  60. client_name++;
  61. }
  62. while ((c = getopt_long (argc, argv, short_options, long_options,
  63. &option_index)) >= 0) {
  64. switch (c) {
  65. case 'i':
  66. load_init = optarg;
  67. break;
  68. case 's':
  69. server_name = optarg;
  70. break;
  71. case 'w':
  72. wait_opt = 1;
  73. break;
  74. case 'h':
  75. default:
  76. show_usage ();
  77. return 1;
  78. }
  79. }
  80. if (optind == argc) { /* no positional args? */
  81. show_usage ();
  82. return 1;
  83. }
  84. if (optind < argc)
  85. load_name = intclient_name = argv[optind++];
  86. if (optind < argc)
  87. load_name = argv[optind++];
  88. if (optind < argc)
  89. load_init = argv[optind++];
  90. //fprintf (stderr, "client-name = `%s', load-name = `%s', "
  91. // "load-init = `%s', wait = %d\n",
  92. // intclient_name, load_name, load_init, wait_opt);
  93. return 0; /* args OK */
  94. }
  95. int
  96. main (int argc, char *argv[])
  97. {
  98. jack_status_t status;
  99. /* parse and validate command arguments */
  100. if (parse_args (argc, argv))
  101. exit (1); /* invalid command line */
  102. /* first, become a JACK client */
  103. client = jack_client_open (client_name, JackServerName,
  104. &status, server_name);
  105. if (client == NULL) {
  106. fprintf (stderr, "jack_client_open() failed, "
  107. "status = 0x%2.0x\n", status);
  108. if (status & JackServerFailed) {
  109. fprintf (stderr, "Unable to connect to JACK server\n");
  110. }
  111. exit (1);
  112. }
  113. if (status & JackServerStarted) {
  114. fprintf (stderr, "JACK server started\n");
  115. }
  116. if (status & JackNameNotUnique) {
  117. client_name = jack_get_client_name(client);
  118. fprintf (stderr, "unique name `%s' assigned\n", client_name);
  119. }
  120. /* then, load the internal client */
  121. intclient = jack_internal_client_load (client, intclient_name,
  122. (JackLoadName|JackLoadInit),
  123. &status, load_name, load_init);
  124. if (status & JackFailure) {
  125. fprintf (stderr, "could not load %s, status = 0x%2.0x\n",
  126. load_name, status);
  127. return 2;
  128. }
  129. if (status & JackNameNotUnique) {
  130. intclient_name =
  131. jack_get_internal_client_name (client, intclient);
  132. fprintf (stderr, "unique internal client name `%s' assigned\n",
  133. intclient_name);
  134. }
  135. fprintf (stdout, "%s is running.\n", load_name);
  136. if (wait_opt) {
  137. /* define a signal handler to unload the client, then
  138. * wait for it to exit */
  139. signal (SIGQUIT, signal_handler);
  140. signal (SIGTERM, signal_handler);
  141. signal (SIGHUP, signal_handler);
  142. signal (SIGINT, signal_handler);
  143. while (1) {
  144. sleep (1);
  145. }
  146. }
  147. return 0;
  148. }