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.

137 lines
3.0KB

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