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.

158 lines
3.5KB

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <getopt.h>
  6. #include <time.h>
  7. #include <config.h>
  8. #include <jack/jack.h>
  9. char * my_name;
  10. void
  11. show_version (void)
  12. {
  13. fprintf (stderr, "%s: JACK Audio Connection Kit version " VERSION "\n",
  14. my_name);
  15. }
  16. void
  17. show_usage (void)
  18. {
  19. show_version ();
  20. fprintf (stderr, "\nUsage: %s [options]\n", my_name);
  21. fprintf (stderr, "Check for jack existence, or wait, until it either quits, or gets started\n");
  22. fprintf (stderr, "options:\n");
  23. fprintf (stderr, " -s, --server <name> Connect to the jack server named <name>\n");
  24. fprintf (stderr, " -w, --wait Wait for server to become available\n");
  25. fprintf (stderr, " -q, --quit Wait until server is quit\n");
  26. fprintf (stderr, " -c, --check Check wether server is running\n");
  27. fprintf (stderr, " -t, --timeout Wait timeout in seconds\n");
  28. fprintf (stderr, " -h, --help Display this help message\n");
  29. fprintf (stderr, " --version Output version information and exit\n\n");
  30. fprintf (stderr, "For more information see http://jackaudio.org/\n");
  31. }
  32. void silent_function( const char *ignore )
  33. {
  34. }
  35. int
  36. main (int argc, char *argv[])
  37. {
  38. jack_client_t *client;
  39. jack_status_t status;
  40. jack_options_t options = JackNoStartServer;
  41. int c;
  42. int option_index;
  43. char *server_name = NULL;
  44. int wait_for_start = 0;
  45. int wait_for_quit = 0;
  46. int just_check = 0;
  47. int wait_timeout = 0;
  48. time_t start_timestamp;
  49. struct option long_options[] = {
  50. { "server", 1, 0, 's' },
  51. { "wait", 0, 0, 'w' },
  52. { "quit", 0, 0, 'q' },
  53. { "check", 0, 0, 'c' },
  54. { "timeout", 1, 0, 't' },
  55. { "help", 0, 0, 'h' },
  56. { "version", 0, 0, 'v' },
  57. { 0, 0, 0, 0 }
  58. };
  59. my_name = strrchr(argv[0], '/');
  60. if (my_name == 0) {
  61. my_name = argv[0];
  62. } else {
  63. my_name ++;
  64. }
  65. while ((c = getopt_long (argc, argv, "s:wqct:hv", long_options, &option_index)) >= 0) {
  66. switch (c) {
  67. case 's':
  68. server_name = (char *) malloc (sizeof (char) * strlen(optarg));
  69. strcpy (server_name, optarg);
  70. options |= JackServerName;
  71. break;
  72. case 'w':
  73. wait_for_start = 1;
  74. break;
  75. case 'q':
  76. wait_for_quit = 1;
  77. break;
  78. case 'c':
  79. just_check = 1;
  80. break;
  81. case 't':
  82. wait_timeout = atoi(optarg);
  83. break;
  84. case 'h':
  85. show_usage ();
  86. return 1;
  87. break;
  88. case 'v':
  89. show_version ();
  90. return 1;
  91. break;
  92. default:
  93. show_usage ();
  94. return 1;
  95. break;
  96. }
  97. }
  98. /* try to open server in a loop. breaking under certein conditions */
  99. start_timestamp = time( NULL );
  100. jack_set_info_function( silent_function );
  101. while(1) {
  102. client = jack_client_open ("wait", options, &status, server_name);
  103. /* check for some real error and bail out */
  104. if( (client == NULL) && !(status & JackServerFailed) ) {
  105. fprintf (stderr, "jack_client_open() failed, "
  106. "status = 0x%2.0x\n", status);
  107. return 1;
  108. }
  109. if( client == NULL ) {
  110. if( wait_for_quit ) {
  111. fprintf( stdout, "server is gone\n" );
  112. break;
  113. }
  114. if( just_check ) {
  115. fprintf( stdout, "not running\n" );
  116. break;
  117. }
  118. } else {
  119. jack_client_close( client );
  120. if( wait_for_start ) {
  121. fprintf( stdout, "server is available\n" );
  122. break;
  123. }
  124. if( just_check ) {
  125. fprintf( stdout, "running\n" );
  126. break;
  127. }
  128. }
  129. if( wait_timeout ) {
  130. if( (time( NULL ) - start_timestamp) > wait_timeout ) {
  131. fprintf( stdout, "timeout\n" );
  132. break;
  133. }
  134. }
  135. // Wait a second, and repeat
  136. sleep(1);
  137. }
  138. exit (0);
  139. }