FreeBSD: Add missing OSS option --excl to man page.
[jack2.git] / tools / ipload.c
bloba5a47134e5599b78139957176bfe28b09994c97c
1 /*
2 This program is free software; you can redistribute it and/or modify
3 it under the terms of the GNU General Public License as published by
4 the Free Software Foundation; either version 2 of the License, or
5 (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
12 You should have received a copy of the GNU General Public License
13 along with this program; if not, write to the Free Software
14 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <signal.h>
21 #ifndef WIN32
22 #include <unistd.h>
23 #endif
24 #include <getopt.h>
25 #include <jack/jack.h>
26 #include <jack/intclient.h>
28 jack_client_t *client;
29 jack_intclient_t intclient;
30 char *client_name;
31 char *intclient_name;
32 char *load_name;
33 char *load_init = "";
34 char *server_name = NULL;
35 int autoclose_opt = 0;
36 int wait_opt = 0;
37 volatile int idling = 1;
39 static void
40 signal_handler (int sig)
42 /* do nothing if internal client closed itself */
43 if (idling == 0)
44 return;
46 jack_status_t status;
48 fprintf (stderr, "signal received, unloading...");
49 status = jack_internal_client_unload (client, intclient);
50 if (status & JackFailure)
51 fprintf (stderr, "(failed), status = 0x%2.0x\n", status);
52 else
53 fprintf (stderr, "(succeeded)\n");
54 if (autoclose_opt)
55 jack_deactivate(client);
56 jack_client_close (client);
57 exit (0);
60 static void
61 registration_callback (const char *name, int reg, void *arg)
63 if (reg || strcmp(intclient_name, name))
64 return;
66 /* this will stop the wait loop and thus close this application. */
67 idling = 0;
68 return;
70 /* unused */
71 (void)arg;
74 static void
75 show_usage ()
77 fprintf (stderr, "usage: %s [ options ] client-name [ load-name "
78 "[ init-string]]\n\noptions:\n", client_name);
79 fprintf (stderr,
80 "\t-h, --help \t\t print help message\n"
81 "\t-a, --autoclose\t automatically close when intclient is unloaded\n"
82 "\t-i, --init string\t initialize string\n"
83 "\t-s, --server name\t select JACK server\n"
84 "\t-w, --wait \t\t wait for signal, then unload\n"
85 "\n"
89 static int
90 parse_args (int argc, char *argv[])
92 int c;
93 int option_index = 0;
94 char *short_options = "hai:s:w";
95 struct option long_options[] = {
96 { "help", 0, 0, 'h' },
97 { "autoclose", 0, 0, 'a' },
98 { "init", required_argument, 0, 'i' },
99 { "server", required_argument, 0, 's' },
100 { "wait", 0, 0, 'w' },
101 { 0, 0, 0, 0 }
104 client_name = strrchr(argv[0], '/');
105 if (client_name == NULL) {
106 client_name = argv[0];
107 } else {
108 client_name++;
111 while ((c = getopt_long (argc, argv, short_options, long_options,
112 &option_index)) >= 0) {
113 switch (c) {
114 case 'a':
115 autoclose_opt = 1;
116 break;
117 case 'i':
118 load_init = optarg;
119 break;
120 case 's':
121 server_name = optarg;
122 break;
123 case 'w':
124 wait_opt = 1;
125 break;
126 case 'h':
127 default:
128 show_usage ();
129 return 1;
133 /* autoclose makes no sense without wait */
134 if (autoclose_opt && ! wait_opt)
135 autoclose_opt = 0;
137 if (optind == argc) { /* no positional args? */
138 show_usage ();
139 return 1;
141 if (optind < argc)
142 load_name = intclient_name = argv[optind++];
144 if (optind < argc)
145 load_name = argv[optind++];
147 if (optind < argc)
148 load_init = argv[optind++];
150 //fprintf (stderr, "client-name = `%s', load-name = `%s', "
151 // "load-init = `%s', wait = %d\n",
152 // intclient_name, load_name, load_init, wait_opt);
154 return 0; /* args OK */
158 main (int argc, char *argv[])
160 jack_status_t status;
161 char* name;
163 /* parse and validate command arguments */
164 if (parse_args (argc, argv))
165 exit (1); /* invalid command line */
167 /* first, become a JACK client */
168 client = jack_client_open (client_name, JackServerName,
169 &status, server_name);
170 if (client == NULL) {
171 fprintf (stderr, "jack_client_open() failed, "
172 "status = 0x%2.0x\n", status);
173 if (status & JackServerFailed) {
174 fprintf (stderr, "Unable to connect to JACK server\n");
176 exit (1);
178 if (status & JackServerStarted) {
179 fprintf (stderr, "JACK server started\n");
181 if (status & JackNameNotUnique) {
182 client_name = jack_get_client_name(client);
183 fprintf (stderr, "unique name `%s' assigned\n", client_name);
186 /* then, load the internal client */
187 intclient = jack_internal_client_load (client, intclient_name,
188 (JackLoadName|JackLoadInit),
189 &status, load_name, load_init);
190 if (status & JackFailure) {
191 fprintf (stderr, "could not load %s, intclient = %d status = 0x%2.0x\n",
192 load_name, (int)intclient, status);
193 return 2;
195 if (status & JackNameNotUnique) {
196 intclient_name =
197 jack_get_internal_client_name (client, intclient);
198 fprintf (stderr, "unique internal client name `%s' assigned\n",
199 intclient_name);
202 fprintf (stdout, "%s is running.\n", load_name);
204 name = jack_get_internal_client_name(client, intclient);
205 if (name) {
206 printf("client name = %s\n", name);
207 free(name);
209 fflush(stdout);
211 if (autoclose_opt) {
212 jack_set_client_registration_callback(client, registration_callback, NULL);
213 jack_activate(client);
216 if (wait_opt) {
217 /* define a signal handler to unload the client, then
218 * wait for it to exit */
219 #ifdef WIN32
220 signal(SIGINT, signal_handler);
221 signal(SIGABRT, signal_handler);
222 signal(SIGTERM, signal_handler);
223 #else
224 signal(SIGQUIT, signal_handler);
225 signal(SIGTERM, signal_handler);
226 signal(SIGHUP, signal_handler);
227 signal(SIGINT, signal_handler);
228 #endif
230 while (idling) {
231 #ifdef WIN32
232 Sleep(1000);
233 #else
234 sleep (1);
235 #endif
239 if (autoclose_opt) {
240 jack_deactivate(client);
243 jack_client_close(client);
244 return 0;