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.

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