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.

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