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.

143 lines
3.1KB

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