Compiles on Windows again.
[jack2.git] / example-clients / wait.c
blob81870321bcd1764fd422f0904a95fb1743f1e85e
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, " -w, --wait Wait for server to become available\n");
21 fprintf (stderr, " -q, --quit Wait until server is quit\n");
22 fprintf (stderr, " -c, --check Check wether server is running\n");
23 fprintf (stderr, " -t, --timeout Wait timeout in seconds\n");
24 fprintf (stderr, " -h, --help Display this help message\n");
25 fprintf (stderr, "For more information see http://jackaudio.org/\n");
28 int
29 main (int argc, char *argv[])
31 jack_client_t *client;
32 jack_status_t status;
33 jack_options_t options = JackNoStartServer;
34 int c;
35 int option_index;
36 char *server_name = NULL;
37 int wait_for_start = 0;
38 int wait_for_quit = 0;
39 int just_check = 0;
40 int wait_timeout = 0;
41 time_t start_timestamp;
44 struct option long_options[] = {
45 { "server", 1, 0, 's' },
46 { "wait", 0, 0, 'w' },
47 { "quit", 0, 0, 'q' },
48 { "check", 0, 0, 'c' },
49 { "timeout", 1, 0, 't' },
50 { "help", 0, 0, 'h' },
51 { 0, 0, 0, 0 }
54 my_name = strrchr(argv[0], '/');
55 if (my_name == 0) {
56 my_name = argv[0];
57 } else {
58 my_name ++;
61 while ((c = getopt_long (argc, argv, "s:wqct:hv", long_options, &option_index)) >= 0) {
62 switch (c) {
63 case 's':
64 server_name = (char *) malloc (sizeof (char) * strlen(optarg));
65 strcpy (server_name, optarg);
66 options |= JackServerName;
67 break;
68 case 'w':
69 wait_for_start = 1;
70 break;
71 case 'q':
72 wait_for_quit = 1;
73 break;
74 case 'c':
75 just_check = 1;
76 break;
77 case 't':
78 wait_timeout = atoi(optarg);
79 break;
80 case 'h':
81 show_usage ();
82 return 1;
83 break;
84 default:
85 show_usage ();
86 return 1;
87 break;
91 /* try to open server in a loop. breaking under certein conditions */
93 start_timestamp = time( NULL );
95 while(1) {
96 client = jack_client_open ("wait", options, &status, server_name);
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;
104 if( client == NULL ) {
105 if( wait_for_quit ) {
106 fprintf( stdout, "server is gone\n" );
107 break;
109 if( just_check ) {
110 fprintf( stdout, "not running\n" );
111 break;
113 } else {
114 jack_client_close( client );
115 if( wait_for_start ) {
116 fprintf( stdout, "server is available\n" );
117 break;
119 if( just_check ) {
120 fprintf( stdout, "running\n" );
121 break;
124 if( wait_timeout ) {
125 if( (time( NULL ) - start_timestamp) > wait_timeout ) {
126 fprintf( stdout, "timeout\n" );
127 break;
131 // Wait a second, and repeat
132 sleep(1);
135 exit (0);