In combined --dbus and --classic compilation ode, use PulseAudio acquire/release...
[jack2.git] / common / Jackdmp.cpp
blob7b7859b14543b6d21d9191dbd775d378b3a10c3b
1 /*
2 Copyright (C) 2001 Paul Davis
3 Copyright (C) 2004-2008 Grame
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <iostream>
22 #include <assert.h>
23 #include <cassert>
24 #include <csignal>
25 #include <sys/types.h>
26 #include <getopt.h>
27 #include <cstring>
28 #include <cstdio>
30 #include "types.h"
31 #include "jack.h"
32 #include "JackConstants.h"
33 #include "JackDriverLoader.h"
35 #if defined(JACK_DBUS) && defined(__linux__)
36 #include <dbus/dbus.h>
37 #include "audio_reserve.h"
38 #endif
41 This is a simple port of the old jackdmp.cpp file to use the new Jack 2.0 control API. Available options for the server
42 are "hard-coded" in the source. A much better approach would be to use the control API to:
43 - dynamically retrieve available server parameters and then prepare to parse them
44 - get available drivers and their possible parameters, then prepare to parse them.
47 #ifdef __APPLE__
48 #include <CoreFoundation/CFNotificationCenter.h>
49 #include <CoreFoundation/CoreFoundation.h>
51 static void notify_server_start(const char* server_name)
53 // Send notification to be used in the JackRouter plugin
54 CFStringRef ref = CFStringCreateWithCString(NULL, server_name, kCFStringEncodingMacRoman);
55 CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDistributedCenter(),
56 CFSTR("com.grame.jackserver.start"),
57 ref,
58 NULL,
59 kCFNotificationDeliverImmediately | kCFNotificationPostToAllSessions);
60 CFRelease(ref);
63 static void notify_server_stop(const char* server_name)
65 // Send notification to be used in the JackRouter plugin
66 CFStringRef ref1 = CFStringCreateWithCString(NULL, server_name, kCFStringEncodingMacRoman);
67 CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDistributedCenter(),
68 CFSTR("com.grame.jackserver.stop"),
69 ref1,
70 NULL,
71 kCFNotificationDeliverImmediately | kCFNotificationPostToAllSessions);
72 CFRelease(ref1);
75 #else
77 static void notify_server_start(const char* server_name)
79 static void notify_server_stop(const char* server_name)
82 #endif
84 static void copyright(FILE* file)
86 fprintf(file, "jackdmp " VERSION "\n"
87 "Copyright 2001-2005 Paul Davis and others.\n"
88 "Copyright 2004-2009 Grame.\n"
89 "jackdmp comes with ABSOLUTELY NO WARRANTY\n"
90 "This is free software, and you are welcome to redistribute it\n"
91 "under certain conditions; see the file COPYING for details\n");
94 static void usage(FILE* file)
96 fprintf(file, "\n"
97 "usage: jackdmp [ --realtime OR -R [ --realtime-priority OR -P priority ] ]\n"
98 " [ --name OR -n server-name ]\n"
99 " [ --timeout OR -t client-timeout-in-msecs ]\n"
100 " [ --midi OR -X midi-driver ]\n"
101 " [ --verbose OR -v ]\n"
102 #ifdef __linux__
103 " [ --clocksource OR -c [ c(ycle) | h(pet) | s(ystem) ]\n"
104 #endif
105 " [ --replace-registry OR -r ]\n"
106 " [ --silent OR -s ]\n"
107 " [ --sync OR -S ]\n"
108 " [ --temporary OR -T ]\n"
109 " [ --version OR -V ]\n"
110 " -d audio-driver [ ... driver args ... ]\n"
111 " where driver can be `alsa', `coreaudio', 'portaudio' or `dummy'\n"
112 " jackdmp -d driver --help\n"
113 " to display options for each driver\n\n");
116 // To put in the control.h interface??
117 static jackctl_driver_t *
118 jackctl_server_get_driver(
119 jackctl_server_t *server,
120 const char *driver_name)
122 const JSList * node_ptr;
124 node_ptr = jackctl_server_get_drivers_list(server);
126 while (node_ptr)
128 if (strcmp(jackctl_driver_get_name((jackctl_driver_t *)node_ptr->data), driver_name) == 0)
130 return (jackctl_driver_t *)node_ptr->data;
133 node_ptr = jack_slist_next(node_ptr);
136 return NULL;
139 static jackctl_parameter_t *
140 jackctl_get_parameter(
141 const JSList * parameters_list,
142 const char * parameter_name)
144 while (parameters_list)
146 if (strcmp(jackctl_parameter_get_name((jackctl_parameter_t *)parameters_list->data), parameter_name) == 0)
148 return (jackctl_parameter_t *)parameters_list->data;
151 parameters_list = jack_slist_next(parameters_list);
154 return NULL;
157 int main(int argc, char* argv[])
159 jackctl_server_t * server_ctl;
160 const JSList * server_parameters;
161 const char* server_name = "default";
162 jackctl_driver_t * audio_driver_ctl;
163 jackctl_driver_t * midi_driver_ctl;
165 #ifdef __linux__
166 const char *options = "-ad:X:P:uvrshVRL:STFl:t:mn:p:c:";
167 #else
168 const char *options = "-ad:X:P:uvrshVRL:STFl:t:mn:p:";
169 #endif
171 struct option long_options[] = {
172 #ifdef __linux__
173 { "clock-source", 1, 0, 'c' },
174 #endif
175 { "audio-driver", 1, 0, 'd' },
176 { "midi-driver", 1, 0, 'X' },
177 { "verbose", 0, 0, 'v' },
178 { "help", 0, 0, 'h' },
179 { "port-max", 1, 0, 'p' },
180 { "no-mlock", 0, 0, 'm' },
181 { "name", 0, 0, 'n' },
182 { "unlock", 0, 0, 'u' },
183 { "realtime", 0, 0, 'R' },
184 { "replace-registry", 0, 0, 'r' },
185 { "loopback", 0, 0, 'L' },
186 { "realtime-priority", 1, 0, 'P' },
187 { "timeout", 1, 0, 't' },
188 { "temporary", 0, 0, 'T' },
189 { "version", 0, 0, 'V' },
190 { "silent", 0, 0, 's' },
191 { "sync", 0, 0, 'S' },
192 { 0, 0, 0, 0 }
195 int i,opt = 0;
196 int option_index = 0;
197 bool seen_audio_driver = false;
198 bool seen_midi_driver = false;
199 char *audio_driver_name = NULL;
200 char **audio_driver_args = NULL;
201 int audio_driver_nargs = 1;
202 char *midi_driver_name = NULL;
203 char **midi_driver_args = NULL;
204 int midi_driver_nargs = 1;
205 int port_max = 512;
206 int do_mlock = 1;
207 int do_unlock = 0;
208 bool show_version = false;
209 sigset_t signals;
210 jackctl_parameter_t* param;
211 union jackctl_parameter_value value;
213 copyright(stdout);
214 #if defined(JACK_DBUS) && defined(__linux__)
215 server_ctl = jackctl_server_create(audio_acquire, audio_release);
216 #else
217 server_ctl = jackctl_server_create(NULL, NULL);
218 #endif
219 if (server_ctl == NULL) {
220 fprintf(stderr, "Failed to create server object\n");
221 return -1;
224 server_parameters = jackctl_server_get_parameters(server_ctl);
225 opterr = 0;
226 while (!seen_audio_driver &&
227 (opt = getopt_long(argc, argv, options,
228 long_options, &option_index)) != EOF) {
229 switch (opt) {
231 #ifdef __linux__
232 case 'c':
233 param = jackctl_get_parameter(server_parameters, "clock-source");
234 if (param != NULL) {
235 if (tolower (optarg[0]) == 'h') {
236 value.ui = JACK_TIMER_HPET;
237 jackctl_parameter_set_value(param, &value);
238 } else if (tolower (optarg[0]) == 'c') {
239 value.ui = JACK_TIMER_CYCLE_COUNTER;
240 jackctl_parameter_set_value(param, &value);
241 } else if (tolower (optarg[0]) == 's') {
242 value.ui = JACK_TIMER_SYSTEM_CLOCK;
243 jackctl_parameter_set_value(param, &value);
244 } else {
245 usage(stdout);
246 goto fail_free;
249 break;
250 #endif
252 case 'd':
253 seen_audio_driver = true;
254 audio_driver_name = optarg;
255 break;
257 case 'X':
258 seen_midi_driver = true;
259 midi_driver_name = optarg;
260 break;
262 case 'p':
263 port_max = (unsigned int)atol(optarg);
264 break;
266 case 'm':
267 do_mlock = 0;
268 break;
270 case 'u':
271 do_unlock = 1;
272 break;
274 case 'v':
275 param = jackctl_get_parameter(server_parameters, "verbose");
276 if (param != NULL) {
277 value.b = true;
278 jackctl_parameter_set_value(param, &value);
280 break;
282 case 's':
283 jack_set_error_function(silent_jack_error_callback);
284 break;
286 case 'S':
287 param = jackctl_get_parameter(server_parameters, "sync");
288 if (param != NULL) {
289 value.b = true;
290 jackctl_parameter_set_value(param, &value);
292 break;
294 case 'n':
295 server_name = optarg;
296 param = jackctl_get_parameter(server_parameters, "name");
297 if (param != NULL) {
298 strncpy(value.str, optarg, JACK_PARAM_STRING_MAX);
299 jackctl_parameter_set_value(param, &value);
301 break;
303 case 'P':
304 param = jackctl_get_parameter(server_parameters, "realtime-priority");
305 if (param != NULL) {
306 value.i = atoi(optarg);
307 jackctl_parameter_set_value(param, &value);
309 break;
311 case 'r':
312 param = jackctl_get_parameter(server_parameters, "replace-registry");
313 if (param != NULL) {
314 value.b = true;
315 jackctl_parameter_set_value(param, &value);
317 break;
319 case 'R':
320 param = jackctl_get_parameter(server_parameters, "realtime");
321 if (param != NULL) {
322 value.b = true;
323 jackctl_parameter_set_value(param, &value);
325 break;
327 case 'L':
328 param = jackctl_get_parameter(server_parameters, "loopback-ports");
329 if (param != NULL) {
330 value.ui = atoi(optarg);
331 jackctl_parameter_set_value(param, &value);
333 break;
335 case 'T':
336 param = jackctl_get_parameter(server_parameters, "temporary");
337 if (param != NULL) {
338 value.b = true;
339 jackctl_parameter_set_value(param, &value);
341 break;
343 case 't':
344 param = jackctl_get_parameter(server_parameters, "client-timeout");
345 if (param != NULL) {
346 value.i = atoi(optarg);
347 jackctl_parameter_set_value(param, &value);
349 break;
351 case 'V':
352 show_version = true;
353 break;
355 default:
356 fprintf(stderr, "unknown option character %c\n", optopt);
357 /*fallthru*/
359 case 'h':
360 usage(stdout);
361 goto fail_free;
365 if (show_version) {
366 printf( "jackdmp version " VERSION
367 " tmpdir " jack_server_dir
368 " protocol %d"
369 "\n", JACK_PROTOCOL_VERSION);
370 return -1;
373 if (!seen_audio_driver) {
374 usage(stderr);
375 goto fail_free;
378 // Audio driver
379 audio_driver_ctl = jackctl_server_get_driver(server_ctl, audio_driver_name);
380 if (audio_driver_ctl == NULL) {
381 fprintf(stderr, "Unkown driver \"%s\"\n", audio_driver_name);
382 goto fail_free;
385 if (optind < argc) {
386 audio_driver_nargs = 1 + argc - optind;
387 } else {
388 audio_driver_nargs = 1;
391 if (audio_driver_nargs == 0) {
392 fprintf(stderr, "No driver specified ... hmm. JACK won't do"
393 " anything when run like this.\n");
394 goto fail_free;
397 audio_driver_args = (char **) malloc(sizeof(char *) * audio_driver_nargs);
398 audio_driver_args[0] = audio_driver_name;
400 for (i = 1; i < audio_driver_nargs; i++) {
401 audio_driver_args[i] = argv[optind++];
404 if (jackctl_parse_driver_params(audio_driver_ctl, audio_driver_nargs, audio_driver_args)) {
405 goto fail_free;
408 // Start server
409 if (!jackctl_server_start(server_ctl, audio_driver_ctl)) {
410 fprintf(stderr, "Failed to start server\n");
411 goto fail_free;
414 // MIDI driver
415 if (seen_midi_driver) {
417 midi_driver_ctl = jackctl_server_get_driver(server_ctl, midi_driver_name);
418 if (midi_driver_ctl == NULL) {
419 fprintf(stderr, "Unkown driver \"%s\"\n", midi_driver_name);
420 goto fail_free;
423 jackctl_server_add_slave(server_ctl, midi_driver_ctl);
426 notify_server_start(server_name);
428 // Waits for signal
429 signals = jackctl_setup_signals(0);
430 jackctl_wait_signals(signals);
432 if (!jackctl_server_stop(server_ctl))
433 fprintf(stderr, "Cannot stop server...\n");
435 fail_free:
437 jackctl_server_destroy(server_ctl);
438 notify_server_stop(server_name);
439 return 1;