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.

194 lines
4.6KB

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