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.

153 lines
3.4KB

  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. int
  33. main (int argc, char *argv[])
  34. {
  35. jack_client_t *client;
  36. jack_status_t status;
  37. jack_options_t options = JackNoStartServer;
  38. int c;
  39. int option_index;
  40. char *server_name = NULL;
  41. int wait_for_start = 0;
  42. int wait_for_quit = 0;
  43. int just_check = 0;
  44. int wait_timeout = 0;
  45. time_t start_timestamp;
  46. struct option long_options[] = {
  47. { "server", 1, 0, 's' },
  48. { "wait", 0, 0, 'w' },
  49. { "quit", 0, 0, 'q' },
  50. { "check", 0, 0, 'c' },
  51. { "timeout", 1, 0, 't' },
  52. { "help", 0, 0, 'h' },
  53. { "version", 0, 0, 'v' },
  54. { 0, 0, 0, 0 }
  55. };
  56. my_name = strrchr(argv[0], '/');
  57. if (my_name == 0) {
  58. my_name = argv[0];
  59. } else {
  60. my_name ++;
  61. }
  62. while ((c = getopt_long (argc, argv, "s:wqct:hv", long_options, &option_index)) >= 0) {
  63. switch (c) {
  64. case 's':
  65. server_name = (char *) malloc (sizeof (char) * strlen(optarg));
  66. strcpy (server_name, optarg);
  67. options |= JackServerName;
  68. break;
  69. case 'w':
  70. wait_for_start = 1;
  71. break;
  72. case 'q':
  73. wait_for_quit = 1;
  74. break;
  75. case 'c':
  76. just_check = 1;
  77. break;
  78. case 't':
  79. wait_timeout = atoi(optarg);
  80. break;
  81. case 'h':
  82. show_usage ();
  83. return 1;
  84. break;
  85. case 'v':
  86. show_version ();
  87. return 1;
  88. break;
  89. default:
  90. show_usage ();
  91. return 1;
  92. break;
  93. }
  94. }
  95. /* try to open server in a loop. breaking under certein conditions */
  96. start_timestamp = time( NULL );
  97. while(1) {
  98. client = jack_client_open ("wait", options, &status, server_name);
  99. /* check for some real error and bail out */
  100. if( (client == NULL) && !(status & JackServerFailed) ) {
  101. fprintf (stderr, "jack_client_open() failed, "
  102. "status = 0x%2.0x\n", status);
  103. return 1;
  104. }
  105. if( client == NULL ) {
  106. if( wait_for_quit ) {
  107. fprintf( stdout, "server is gone\n" );
  108. break;
  109. }
  110. if( just_check ) {
  111. fprintf( stdout, "not running\n" );
  112. break;
  113. }
  114. } else {
  115. jack_client_close( client );
  116. if( wait_for_start ) {
  117. fprintf( stdout, "server is available\n" );
  118. break;
  119. }
  120. if( just_check ) {
  121. fprintf( stdout, "running\n" );
  122. break;
  123. }
  124. }
  125. if( wait_timeout ) {
  126. if( (time( NULL ) - start_timestamp) > wait_timeout ) {
  127. fprintf( stdout, "timeout\n" );
  128. break;
  129. }
  130. }
  131. // Wait a second, and repeat
  132. sleep(1);
  133. }
  134. exit (0);
  135. }