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.

206 lines
4.8KB

  1. /*
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation; either version 2 of the License, or
  5. (at your option) any later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program; if not, write to the Free Software
  12. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  13. */
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <signal.h>
  18. #ifndef WIN32
  19. #include <unistd.h>
  20. #endif
  21. #include <getopt.h>
  22. #include <jack/jack.h>
  23. #include <jack/intclient.h>
  24. jack_client_t *client;
  25. jack_intclient_t intclient;
  26. char *client_name;
  27. char *intclient_name;
  28. char *load_name;
  29. char *load_init = "";
  30. char *server_name = NULL;
  31. int wait_opt = 0;
  32. static void
  33. signal_handler (int sig)
  34. {
  35. jack_status_t status;
  36. fprintf (stderr, "signal received, unloading...");
  37. status = jack_internal_client_unload (client, intclient);
  38. if (status & JackFailure)
  39. fprintf (stderr, "(failed), status = 0x%2.0x\n", status);
  40. else
  41. fprintf (stderr, "(succeeded)\n");
  42. jack_client_close (client);
  43. exit (0);
  44. }
  45. static void
  46. show_usage ()
  47. {
  48. fprintf (stderr, "usage: %s [ options ] client-name [ load-name "
  49. "[ init-string]]\n\noptions:\n", client_name);
  50. fprintf (stderr,
  51. "\t-h, --help \t\t print help message\n"
  52. "\t-i, --init string\t initialize string\n"
  53. "\t-s, --server name\t select JACK server\n"
  54. "\t-w, --wait \t\t wait for signal, then unload\n"
  55. "\n"
  56. );
  57. }
  58. static int
  59. parse_args (int argc, char *argv[])
  60. {
  61. int c;
  62. int option_index = 0;
  63. char *short_options = "hi:s:w";
  64. struct option long_options[] = {
  65. { "help", 0, 0, 'h' },
  66. { "init", required_argument, 0, 'i' },
  67. { "server", required_argument, 0, 's' },
  68. { "wait", 0, 0, 'w' },
  69. { 0, 0, 0, 0 }
  70. };
  71. client_name = strrchr(argv[0], '/');
  72. if (client_name == NULL) {
  73. client_name = argv[0];
  74. } else {
  75. client_name++;
  76. }
  77. while ((c = getopt_long (argc, argv, short_options, long_options,
  78. &option_index)) >= 0) {
  79. switch (c) {
  80. case 'i':
  81. load_init = optarg;
  82. break;
  83. case 's':
  84. server_name = optarg;
  85. break;
  86. case 'w':
  87. wait_opt = 1;
  88. break;
  89. case 'h':
  90. default:
  91. show_usage ();
  92. return 1;
  93. }
  94. }
  95. if (optind == argc) { /* no positional args? */
  96. show_usage ();
  97. return 1;
  98. }
  99. if (optind < argc)
  100. load_name = intclient_name = argv[optind++];
  101. if (optind < argc)
  102. load_name = argv[optind++];
  103. if (optind < argc)
  104. load_init = argv[optind++];
  105. //fprintf (stderr, "client-name = `%s', load-name = `%s', "
  106. // "load-init = `%s', wait = %d\n",
  107. // intclient_name, load_name, load_init, wait_opt);
  108. return 0; /* args OK */
  109. }
  110. int
  111. main (int argc, char *argv[])
  112. {
  113. jack_status_t status;
  114. char* name;
  115. /* parse and validate command arguments */
  116. if (parse_args (argc, argv))
  117. exit (1); /* invalid command line */
  118. /* first, become a JACK client */
  119. client = jack_client_open (client_name, JackServerName,
  120. &status, server_name);
  121. if (client == NULL) {
  122. fprintf (stderr, "jack_client_open() failed, "
  123. "status = 0x%2.0x\n", status);
  124. if (status & JackServerFailed) {
  125. fprintf (stderr, "Unable to connect to JACK server\n");
  126. }
  127. exit (1);
  128. }
  129. if (status & JackServerStarted) {
  130. fprintf (stderr, "JACK server started\n");
  131. }
  132. if (status & JackNameNotUnique) {
  133. client_name = jack_get_client_name(client);
  134. fprintf (stderr, "unique name `%s' assigned\n", client_name);
  135. }
  136. /* then, load the internal client */
  137. intclient = jack_internal_client_load (client, intclient_name,
  138. (JackLoadName|JackLoadInit),
  139. &status, load_name, load_init);
  140. if (status & JackFailure) {
  141. fprintf (stderr, "could not load %s, status = 0x%2.0x\n",
  142. load_name, status);
  143. return 2;
  144. }
  145. if (status & JackNameNotUnique) {
  146. intclient_name =
  147. jack_get_internal_client_name (client, intclient);
  148. fprintf (stderr, "unique internal client name `%s' assigned\n",
  149. intclient_name);
  150. }
  151. fprintf (stdout, "%s is running.\n", load_name);
  152. name = jack_get_internal_client_name(client, intclient);
  153. if (name) {
  154. printf("client name = %s\n", name);
  155. free(name);
  156. }
  157. if (wait_opt) {
  158. /* define a signal handler to unload the client, then
  159. * wait for it to exit */
  160. #ifdef WIN32
  161. signal(SIGINT, signal_handler);
  162. signal(SIGABRT, signal_handler);
  163. signal(SIGTERM, signal_handler);
  164. #else
  165. signal(SIGQUIT, signal_handler);
  166. signal(SIGTERM, signal_handler);
  167. signal(SIGHUP, signal_handler);
  168. signal(SIGINT, signal_handler);
  169. #endif
  170. while (1) {
  171. #ifdef WIN32
  172. Sleep(1000);
  173. #else
  174. sleep (1);
  175. #endif
  176. }
  177. }
  178. jack_client_close(client);
  179. return 0;
  180. }