jack1 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.

170 lines
3.7KB

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