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.
25 #include <sys/types.h>
35 #include "JackConstants.h"
36 #include "JackPlatformPlug.h"
38 #if defined(JACK_DBUS) && defined(__linux__)
39 #include <dbus/dbus.h>
40 #include "audio_reserve.h"
44 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
45 are "hard-coded" in the source. A much better approach would be to use the control API to:
46 - dynamically retrieve available server parameters and then prepare to parse them
47 - get available drivers and their possible parameters, then prepare to parse them.
51 #include <CoreFoundation/CFNotificationCenter.h>
52 #include <CoreFoundation/CoreFoundation.h>
54 static void notify_server_start(const char* server_name
)
56 // Send notification to be used in the JackRouter plugin
57 CFStringRef ref
= CFStringCreateWithCString(NULL
, server_name
, kCFStringEncodingMacRoman
);
58 CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDistributedCenter(),
59 CFSTR("com.grame.jackserver.start"),
62 kCFNotificationDeliverImmediately
| kCFNotificationPostToAllSessions
);
66 static void notify_server_stop(const char* server_name
)
68 // Send notification to be used in the JackRouter plugin
69 CFStringRef ref1
= CFStringCreateWithCString(NULL
, server_name
, kCFStringEncodingMacRoman
);
70 CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDistributedCenter(),
71 CFSTR("com.grame.jackserver.stop"),
74 kCFNotificationDeliverImmediately
| kCFNotificationPostToAllSessions
);
80 static void notify_server_start(const char* server_name
)
82 static void notify_server_stop(const char* server_name
)
87 static void copyright(FILE* file
)
89 fprintf(file
, "jackdmp " VERSION
"\n"
90 "Copyright 2001-2005 Paul Davis and others.\n"
91 "Copyright 2004-2012 Grame.\n"
92 "jackdmp comes with ABSOLUTELY NO WARRANTY\n"
93 "This is free software, and you are welcome to redistribute it\n"
94 "under certain conditions; see the file COPYING for details\n");
97 static void usage(FILE* file
)
100 "usage: jackdmp [ --no-realtime OR -r ]\n"
101 " [ --realtime OR -R [ --realtime-priority OR -P priority ] ]\n"
102 " (the two previous arguments are mutually exclusive. The default is --realtime)\n"
103 " [ --name OR -n server-name ]\n"
104 " [ --timeout OR -t client-timeout-in-msecs ]\n"
105 " [ --loopback OR -L loopback-port-number ]\n"
106 " [ --port-max OR -p maximum-number-of-ports]\n"
107 " [ --slave-backend OR -X slave-backend-name ]\n"
108 " [ --internal-client OR -I internal-client-name ]\n"
109 " [ --verbose OR -v ]\n"
111 " [ --clocksource OR -c [ c(ycle) | h(pet) | s(ystem) ]\n"
113 " [ --replace-registry ]\n"
114 " [ --silent OR -s ]\n"
115 " [ --sync OR -S ]\n"
116 " [ --temporary OR -T ]\n"
117 " [ --version OR -V ]\n"
118 " -d master-backend-name [ ... master-backend args ... ]\n"
120 " Available master backends may include: coreaudio, dummy, net or netone.\n\n"
123 " Available master backends may include: portaudio, dummy, net or netone.\n\n"
126 " Available master backends may include: alsa, dummy, freebob, firewire, net or netone.\n\n"
128 #if defined(__sun__) || defined(sun)
129 " Available master backends may include: boomer, oss, dummy or net.\n\n"
131 " jackdmp -d master-backend-name --help\n"
132 " to display options for each master backend\n\n");
135 // To put in the control.h interface ??
136 static jackctl_driver_t
* jackctl_server_get_driver(jackctl_server_t
*server
, const char *driver_name
)
138 const JSList
* node_ptr
= jackctl_server_get_drivers_list(server
);
141 if (strcmp(jackctl_driver_get_name((jackctl_driver_t
*)node_ptr
->data
), driver_name
) == 0) {
142 return (jackctl_driver_t
*)node_ptr
->data
;
144 node_ptr
= jack_slist_next(node_ptr
);
150 static jackctl_internal_t
* jackctl_server_get_internal(jackctl_server_t
*server
, const char *internal_name
)
152 const JSList
* node_ptr
= jackctl_server_get_internals_list(server
);
155 if (strcmp(jackctl_internal_get_name((jackctl_internal_t
*)node_ptr
->data
), internal_name
) == 0) {
156 return (jackctl_internal_t
*)node_ptr
->data
;
158 node_ptr
= jack_slist_next(node_ptr
);
164 static jackctl_parameter_t
* jackctl_get_parameter(const JSList
* parameters_list
, const char * parameter_name
)
166 while (parameters_list
) {
167 if (strcmp(jackctl_parameter_get_name((jackctl_parameter_t
*)parameters_list
->data
), parameter_name
) == 0) {
168 return (jackctl_parameter_t
*)parameters_list
->data
;
170 parameters_list
= jack_slist_next(parameters_list
);
176 // Prototype to be found in libjackserver
177 extern "C" void silent_jack_error_callback(const char *desc
);
179 int main(int argc
, char** argv
)
181 jackctl_server_t
* server_ctl
;
182 const JSList
* server_parameters
;
183 const char* server_name
= "default";
184 jackctl_driver_t
* master_driver_ctl
;
185 jackctl_driver_t
* loopback_driver_ctl
= NULL
;
186 int replace_registry
= 0;
188 const char *options
= "-d:X:I:P:uvshVrRL:STFl:t:mn:p:"
194 struct option long_options
[] = {
196 { "clock-source", 1, 0, 'c' },
198 { "loopback-driver", 1, 0, 'L' },
199 { "audio-driver", 1, 0, 'd' },
200 { "midi-driver", 1, 0, 'X' },
201 { "internal-client", 1, 0, 'I' },
202 { "verbose", 0, 0, 'v' },
203 { "help", 0, 0, 'h' },
204 { "port-max", 1, 0, 'p' },
205 { "no-mlock", 0, 0, 'm' },
206 { "name", 1, 0, 'n' },
207 { "unlock", 0, 0, 'u' },
208 { "realtime", 0, 0, 'R' },
209 { "no-realtime", 0, 0, 'r' },
210 { "replace-registry", 0, &replace_registry
, 0 },
211 { "loopback", 0, 0, 'L' },
212 { "realtime-priority", 1, 0, 'P' },
213 { "timeout", 1, 0, 't' },
214 { "temporary", 0, 0, 'T' },
215 { "version", 0, 0, 'V' },
216 { "silent", 0, 0, 's' },
217 { "sync", 0, 0, 'S' },
222 int option_index
= 0;
223 char* master_driver_name
= NULL
;
224 char** master_driver_args
= NULL
;
225 int master_driver_nargs
= 1;
229 bool show_version
= false;
230 jackctl_sigmask_t
* sigmask
;
231 jackctl_parameter_t
* param
;
232 union jackctl_parameter_value value
;
234 std::list
<char*> internals_list
;
235 std::list
<char*> slaves_list
;
236 std::list
<char*>::iterator it
;
238 // Assume that we fail.
239 int return_value
= -1;
240 bool notify_sent
= false;
243 #if defined(JACK_DBUS) && defined(__linux__)
244 server_ctl
= jackctl_server_create(audio_acquire
, audio_release
);
246 server_ctl
= jackctl_server_create(NULL
, NULL
);
248 if (server_ctl
== NULL
) {
249 fprintf(stderr
, "Failed to create server object\n");
253 server_parameters
= jackctl_server_get_parameters(server_ctl
);
256 while (!master_driver_name
&&
257 (opt
= getopt_long(argc
, argv
, options
,
258 long_options
, &option_index
)) != EOF
) {
263 param
= jackctl_get_parameter(server_parameters
, "clock-source");
265 if (tolower (optarg
[0]) == 'h') {
266 value
.ui
= JACK_TIMER_HPET
;
267 jackctl_parameter_set_value(param
, &value
);
268 } else if (tolower (optarg
[0]) == 'c') {
269 value
.ui
= JACK_TIMER_CYCLE_COUNTER
;
270 jackctl_parameter_set_value(param
, &value
);
271 } else if (tolower (optarg
[0]) == 's') {
272 value
.ui
= JACK_TIMER_SYSTEM_CLOCK
;
273 jackctl_parameter_set_value(param
, &value
);
283 master_driver_name
= optarg
;
287 loopback
= atoi(optarg
);
291 slaves_list
.push_back(optarg
);
295 internals_list
.push_back(optarg
);
299 param
= jackctl_get_parameter(server_parameters
, "port-max");
301 value
.ui
= atoi(optarg
);
302 jackctl_parameter_set_value(param
, &value
);
315 param
= jackctl_get_parameter(server_parameters
, "verbose");
318 jackctl_parameter_set_value(param
, &value
);
323 jack_set_error_function(silent_jack_error_callback
);
327 param
= jackctl_get_parameter(server_parameters
, "sync");
330 jackctl_parameter_set_value(param
, &value
);
335 server_name
= optarg
;
336 param
= jackctl_get_parameter(server_parameters
, "name");
338 strncpy(value
.str
, optarg
, JACK_PARAM_STRING_MAX
);
339 jackctl_parameter_set_value(param
, &value
);
344 param
= jackctl_get_parameter(server_parameters
, "realtime-priority");
346 value
.i
= atoi(optarg
);
347 jackctl_parameter_set_value(param
, &value
);
352 param
= jackctl_get_parameter(server_parameters
, "realtime");
355 jackctl_parameter_set_value(param
, &value
);
360 param
= jackctl_get_parameter(server_parameters
, "realtime");
363 jackctl_parameter_set_value(param
, &value
);
368 param
= jackctl_get_parameter(server_parameters
, "temporary");
371 jackctl_parameter_set_value(param
, &value
);
376 param
= jackctl_get_parameter(server_parameters
, "client-timeout");
378 value
.i
= atoi(optarg
);
379 jackctl_parameter_set_value(param
, &value
);
388 fprintf(stderr
, "unknown option character %c\n", optopt
);
397 // Long option with no letter so treated separately
398 param
= jackctl_get_parameter(server_parameters
, "replace-registry");
400 value
.b
= replace_registry
;
401 jackctl_parameter_set_value(param
, &value
);
405 printf( "jackdmp version " VERSION
406 " tmpdir " jack_server_dir
408 "\n", JACK_PROTOCOL_VERSION
);
412 if (!master_driver_name
) {
418 master_driver_ctl
= jackctl_server_get_driver(server_ctl
, master_driver_name
);
419 if (master_driver_ctl
== NULL
) {
420 fprintf(stderr
, "Unknown driver \"%s\"\n", master_driver_name
);
424 if (jackctl_driver_get_type(master_driver_ctl
) != JackMaster
) {
425 fprintf(stderr
, "Driver \"%s\" is not a master \n", master_driver_name
);
430 master_driver_nargs
= 1 + argc
- optind
;
432 master_driver_nargs
= 1;
435 if (master_driver_nargs
== 0) {
436 fprintf(stderr
, "No driver specified ... hmm. JACK won't do"
437 " anything when run like this.\n");
441 master_driver_args
= (char **) malloc(sizeof(char *) * master_driver_nargs
);
442 master_driver_args
[0] = master_driver_name
;
444 for (i
= 1; i
< master_driver_nargs
; i
++) {
445 master_driver_args
[i
] = argv
[optind
++];
448 if (jackctl_driver_params_parse(master_driver_ctl
, master_driver_nargs
, master_driver_args
)) {
453 sigmask
= jackctl_setup_signals(0);
456 if (! jackctl_server_open(server_ctl
, master_driver_ctl
)) {
457 fprintf(stderr
, "Failed to open server\n");
462 for (it
= slaves_list
.begin(); it
!= slaves_list
.end(); it
++) {
463 jackctl_driver_t
* slave_driver_ctl
= jackctl_server_get_driver(server_ctl
, *it
);
464 if (slave_driver_ctl
== NULL
) {
465 fprintf(stderr
, "Unknown driver \"%s\"\n", *it
);
468 if (jackctl_driver_get_type(slave_driver_ctl
) != JackSlave
) {
469 fprintf(stderr
, "Driver \"%s\" is not a slave \n", *it
);
472 if (!jackctl_server_add_slave(server_ctl
, slave_driver_ctl
)) {
473 fprintf(stderr
, "Driver \"%s\" cannot be loaded\n", *it
);
480 loopback_driver_ctl
= jackctl_server_get_driver(server_ctl
, "loopback");
482 if (loopback_driver_ctl
!= NULL
) {
483 const JSList
* loopback_parameters
= jackctl_driver_get_parameters(loopback_driver_ctl
);
484 param
= jackctl_get_parameter(loopback_parameters
, "channels");
487 jackctl_parameter_set_value(param
, &value
);
489 if (!jackctl_server_add_slave(server_ctl
, loopback_driver_ctl
)) {
490 fprintf(stderr
, "Driver \"loopback\" cannot be loaded\n");
494 fprintf(stderr
, "Driver \"loopback\" not found\n");
500 if (!jackctl_server_start(server_ctl
)) {
501 fprintf(stderr
, "Failed to start server\n");
506 for (it
= internals_list
.begin(); it
!= internals_list
.end(); it
++) {
507 jackctl_internal_t
* internal_driver_ctl
= jackctl_server_get_internal(server_ctl
, *it
);
508 if (internal_driver_ctl
== NULL
) {
509 fprintf(stderr
, "Unknown internal \"%s\"\n", *it
);
512 if (!jackctl_server_load_internal(server_ctl
, internal_driver_ctl
)) {
513 fprintf(stderr
, "Internal client \"%s\" cannot be loaded\n", *it
);
518 notify_server_start(server_name
);
523 jackctl_wait_signals(sigmask
);
526 if (!jackctl_server_stop(server_ctl
)) {
527 fprintf(stderr
, "Cannot stop server...\n");
531 if (loopback
> 0 && loopback_driver_ctl
) {
532 jackctl_server_remove_slave(server_ctl
, loopback_driver_ctl
);
535 for (it
= slaves_list
.begin(); it
!= slaves_list
.end(); it
++) {
536 jackctl_driver_t
* slave_driver_ctl
= jackctl_server_get_driver(server_ctl
, *it
);
537 if (slave_driver_ctl
)
538 jackctl_server_remove_slave(server_ctl
, slave_driver_ctl
);
542 for (it
= internals_list
.begin(); it
!= internals_list
.end(); it
++) {
543 jackctl_internal_t
* internal_driver_ctl
= jackctl_server_get_internal(server_ctl
, *it
);
544 if (internal_driver_ctl
)
545 jackctl_server_unload_internal(server_ctl
, internal_driver_ctl
);
547 jackctl_server_close(server_ctl
);
550 jackctl_server_destroy(server_ctl
);
552 notify_server_stop(server_name
);