Correct JackPortAudioDriver.
[jack2.git] / example-clients / ipload.c
blob4868de3b29f813f5bb79427b65ef22dd3cce88ab
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 wait_opt = 0;
37 static void
38 signal_handler (int sig)
40 jack_status_t status;
42 fprintf (stderr, "signal received, unloading...");
43 status = jack_internal_client_unload (client, intclient);
44 if (status & JackFailure)
45 fprintf (stderr, "(failed), status = 0x%2.0x\n", status);
46 else
47 fprintf (stderr, "(succeeded)\n");
48 jack_client_close (client);
49 exit (0);
52 static void
53 show_usage ()
55 fprintf (stderr, "usage: %s [ options ] client-name [ load-name "
56 "[ init-string]]\n\noptions:\n", client_name);
57 fprintf (stderr,
58 "\t-h, --help \t\t print help message\n"
59 "\t-i, --init string\t initialize string\n"
60 "\t-s, --server name\t select JACK server\n"
61 "\t-w, --wait \t\t wait for signal, then unload\n"
62 "\n"
66 static int
67 parse_args (int argc, char *argv[])
69 int c;
70 int option_index = 0;
71 char *short_options = "hi:s:w";
72 struct option long_options[] = {
73 { "help", 0, 0, 'h' },
74 { "init", required_argument, 0, 'i' },
75 { "server", required_argument, 0, 's' },
76 { "wait", 0, 0, 'w' },
77 { 0, 0, 0, 0 }
80 client_name = strrchr(argv[0], '/');
81 if (client_name == NULL) {
82 client_name = argv[0];
83 } else {
84 client_name++;
87 while ((c = getopt_long (argc, argv, short_options, long_options,
88 &option_index)) >= 0) {
89 switch (c) {
90 case 'i':
91 load_init = optarg;
92 break;
93 case 's':
94 server_name = optarg;
95 break;
96 case 'w':
97 wait_opt = 1;
98 break;
99 case 'h':
100 default:
101 show_usage ();
102 return 1;
106 if (optind == argc) { /* no positional args? */
107 show_usage ();
108 return 1;
110 if (optind < argc)
111 load_name = intclient_name = argv[optind++];
113 if (optind < argc)
114 load_name = argv[optind++];
116 if (optind < argc)
117 load_init = argv[optind++];
119 //fprintf (stderr, "client-name = `%s', load-name = `%s', "
120 // "load-init = `%s', wait = %d\n",
121 // intclient_name, load_name, load_init, wait_opt);
123 return 0; /* args OK */
127 main (int argc, char *argv[])
129 jack_status_t status;
130 char* name;
132 /* parse and validate command arguments */
133 if (parse_args (argc, argv))
134 exit (1); /* invalid command line */
136 /* first, become a JACK client */
137 client = jack_client_open (client_name, JackServerName,
138 &status, server_name);
139 if (client == NULL) {
140 fprintf (stderr, "jack_client_open() failed, "
141 "status = 0x%2.0x\n", status);
142 if (status & JackServerFailed) {
143 fprintf (stderr, "Unable to connect to JACK server\n");
145 exit (1);
147 if (status & JackServerStarted) {
148 fprintf (stderr, "JACK server started\n");
150 if (status & JackNameNotUnique) {
151 client_name = jack_get_client_name(client);
152 fprintf (stderr, "unique name `%s' assigned\n", client_name);
155 /* then, load the internal client */
156 intclient = jack_internal_client_load (client, intclient_name,
157 (JackLoadName|JackLoadInit),
158 &status, load_name, load_init);
159 if (status & JackFailure) {
160 fprintf (stderr, "could not load %s, intclient = %d status = 0x%2.0x\n",
161 load_name, (int)intclient, status);
162 return 2;
164 if (status & JackNameNotUnique) {
165 intclient_name =
166 jack_get_internal_client_name (client, intclient);
167 fprintf (stderr, "unique internal client name `%s' assigned\n",
168 intclient_name);
171 fprintf (stdout, "%s is running.\n", load_name);
173 name = jack_get_internal_client_name(client, intclient);
174 if (name) {
175 printf("client name = %s\n", name);
176 free(name);
179 if (wait_opt) {
180 /* define a signal handler to unload the client, then
181 * wait for it to exit */
182 #ifdef WIN32
183 signal(SIGINT, signal_handler);
184 signal(SIGABRT, signal_handler);
185 signal(SIGTERM, signal_handler);
186 #else
187 signal(SIGQUIT, signal_handler);
188 signal(SIGTERM, signal_handler);
189 signal(SIGHUP, signal_handler);
190 signal(SIGINT, signal_handler);
191 #endif
193 while (1) {
194 #ifdef WIN32
195 Sleep(1000);
196 #else
197 sleep (1);
198 #endif
202 jack_client_close(client);
203 return 0;