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