Jackdmp.cpp 15.4 KB
Newer Older
sletz's avatar
sletz committed
1
/*
sletz's avatar
sletz committed
2
Copyright (C) 2001 Paul Davis
sletz's avatar
sletz committed
3
Copyright (C) 2004-2008 Grame
sletz's avatar
sletz committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

*/

#include <iostream>
#include <assert.h>
23
24
#include <cassert>
#include <csignal>
sletz's avatar
sletz committed
25
26
#include <sys/types.h>
#include <getopt.h>
27
28
29
#include <cstring>
#include <cstdio>

30
#include "types.h"
sletz's avatar
sletz committed
31
#include "jack.h"
sletz's avatar
sletz committed
32
33
#include "JackConstants.h"
#include "JackDriverLoader.h"
sletz's avatar
sletz committed
34
35

/*
sletz's avatar
sletz committed
36
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
sletz's avatar
sletz committed
37
38
39
40
are "hard-coded" in the source. A much better approach would be to use the control API to:
- dynamically retrieve available server parameters and then prepare to parse them
- get available drivers and their possible parameters, then prepare to parse them.
*/
sletz's avatar
sletz committed
41

42
#ifdef __APPLE__
43
#include <CoreFoundation/CFNotificationCenter.h>
44
#include <CoreFoundation/CoreFoundation.h>
45

sletz's avatar
sletz committed
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
static void notify_server_start(const char* server_name)
{
    // Send notification to be used in the JackRouter plugin
    CFStringRef ref = CFStringCreateWithCString(NULL, server_name, kCFStringEncodingMacRoman);
    CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDistributedCenter(),
            CFSTR("com.grame.jackserver.start"),
            ref,
            NULL,
            kCFNotificationDeliverImmediately | kCFNotificationPostToAllSessions);
    CFRelease(ref);
}

static void notify_server_stop(const char* server_name)
{
    // Send notification to be used in the JackRouter plugin
sletz's avatar
sletz committed
61
    CFStringRef ref1 = CFStringCreateWithCString(NULL, server_name, kCFStringEncodingMacRoman);
sletz's avatar
sletz committed
62
63
64
65
66
67
68
    CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDistributedCenter(),
            CFSTR("com.grame.jackserver.stop"),
            ref1,
            NULL,
            kCFNotificationDeliverImmediately | kCFNotificationPostToAllSessions);
    CFRelease(ref1);
}
sletz's avatar
sletz committed
69

sletz's avatar
sletz committed
70
71
72
73
74
75
76
77
#else

static void notify_server_start(const char* server_name)
{}
static void notify_server_stop(const char* server_name)
{}

#endif
sletz's avatar
sletz committed
78

sletz's avatar
sletz committed
79
static void silent_jack_error_callback(const char *desc)
sletz's avatar
sletz committed
80
81
82
83
{}

static void copyright(FILE* file)
{
sletz's avatar
sletz committed
84
    fprintf(file, "jackdmp " VERSION "\n"
sletz's avatar
sletz committed
85
            "Copyright 2001-2005 Paul Davis and others.\n"
sletz's avatar
sletz committed
86
            "Copyright 2004-2009 Grame.\n"
sletz's avatar
sletz committed
87
88
89
            "jackdmp comes with ABSOLUTELY NO WARRANTY\n"
            "This is free software, and you are welcome to redistribute it\n"
            "under certain conditions; see the file COPYING for details\n");
sletz's avatar
sletz committed
90
91
}

sletz's avatar
sletz committed
92
static void usage(FILE* file)
sletz's avatar
sletz committed
93
{
sletz's avatar
sletz committed
94
    fprintf(file, "\n"
sletz's avatar
sletz committed
95
96
97
            "usage: jackdmp [ --realtime OR -R [ --realtime-priority OR -P priority ] ]\n"
            "               [ --name OR -n server-name ]\n"
            "               [ --timeout OR -t client-timeout-in-msecs ]\n"
sletz's avatar
sletz committed
98
            "               [ --midi OR -X midi-driver ]\n"
sletz's avatar
sletz committed
99
            "               [ --verbose OR -v ]\n"
100
101
102
#ifdef __linux__
            "               [ --clocksource OR -c [ c(ycle) | h(pet) | s(ystem) ]\n"
#endif
sletz's avatar
sletz committed
103
104
105
106
107
            "               [ --replace-registry OR -r ]\n"
            "               [ --silent OR -s ]\n"
            "               [ --sync OR -S ]\n"
            "               [ --temporary OR -T ]\n"
            "               [ --version OR -V ]\n"
108
            "         -d audio-driver [ ... driver args ... ]\n"
sletz's avatar
sletz committed
109
110
111
            "             where driver can be `alsa', `coreaudio', 'portaudio' or `dummy'\n"
            "       jackdmp -d driver --help\n"
            "             to display options for each driver\n\n");
sletz's avatar
sletz committed
112
113
}

sletz's avatar
sletz committed
114
115
116
117
118
// To put in the control.h interface??
static jackctl_driver_t *
jackctl_server_get_driver(
    jackctl_server_t *server,
    const char *driver_name)
sletz's avatar
sletz committed
119
{
sletz's avatar
sletz committed
120
    const JSList * node_ptr;
sletz's avatar
sletz committed
121

sletz's avatar
sletz committed
122
    node_ptr = jackctl_server_get_drivers_list(server);
sletz's avatar
sletz committed
123

sletz's avatar
sletz committed
124
125
126
127
128
129
    while (node_ptr)
    {
        if (strcmp(jackctl_driver_get_name((jackctl_driver_t *)node_ptr->data), driver_name) == 0)
        {
            return (jackctl_driver_t *)node_ptr->data;
        }
sletz's avatar
sletz committed
130

sletz's avatar
sletz committed
131
132
133
134
        node_ptr = jack_slist_next(node_ptr);
    }

    return NULL;
sletz's avatar
sletz committed
135
136
}

sletz's avatar
sletz committed
137
138
139
140
static jackctl_parameter_t *
jackctl_get_parameter(
    const JSList * parameters_list,
    const char * parameter_name)
sletz's avatar
sletz committed
141
{
sletz's avatar
sletz committed
142
143
144
145
146
147
148
149
150
151
152
    while (parameters_list)
    {
        if (strcmp(jackctl_parameter_get_name((jackctl_parameter_t *)parameters_list->data), parameter_name) == 0)
        {
            return (jackctl_parameter_t *)parameters_list->data;
        }

        parameters_list = jack_slist_next(parameters_list);
    }

    return NULL;
sletz's avatar
sletz committed
153
154
155
156
}

int main(int argc, char* argv[])
{
sletz's avatar
sletz committed
157
158
159
    jackctl_server_t * server_ctl;
    const JSList * server_parameters;
    const char* server_name = "default";
160
161
    jackctl_driver_t * audio_driver_ctl;
    jackctl_driver_t * midi_driver_ctl;
162
163
164
165
    
#ifdef __linux__
    const char *options = "-ad:X:P:uvrshVRL:STFl:t:mn:p:c:";
#else
166
    const char *options = "-ad:X:P:uvrshVRL:STFl:t:mn:p:";
167
168
#endif
    
sletz's avatar
sletz committed
169
    struct option long_options[] = {
170
171
172
#ifdef __linux__
                                       { "clock-source", 1, 0, 'c' },
#endif
173
174
                                       { "audio-driver", 1, 0, 'd' },
                                       { "midi-driver", 1, 0, 'X' },
sletz's avatar
sletz committed
175
176
177
178
179
180
181
                                       { "verbose", 0, 0, 'v' },
                                       { "help", 0, 0, 'h' },
                                       { "port-max", 1, 0, 'p' },
                                       { "no-mlock", 0, 0, 'm' },
                                       { "name", 0, 0, 'n' },
                                       { "unlock", 0, 0, 'u' },
                                       { "realtime", 0, 0, 'R' },
sletz's avatar
sletz committed
182
                                       { "replace-registry", 0, 0, 'r' },
sletz's avatar
sletz committed
183
184
185
186
187
188
189
                                       { "loopback", 0, 0, 'L' },
                                       { "realtime-priority", 1, 0, 'P' },
                                       { "timeout", 1, 0, 't' },
                                       { "temporary", 0, 0, 'T' },
                                       { "version", 0, 0, 'V' },
                                       { "silent", 0, 0, 's' },
                                       { "sync", 0, 0, 'S' },
190
                                       { "autoconnect", 1, 0, 'a' },
sletz's avatar
sletz committed
191
192
                                       { 0, 0, 0, 0 }
                                   };
193

sletz's avatar
sletz committed
194
    int i,opt = 0;
sletz's avatar
sletz committed
195
    int option_index = 0;
196
197
198
199
200
201
202
203
    bool seen_audio_driver = false;
    bool seen_midi_driver = false;
    char *audio_driver_name = NULL;
    char **audio_driver_args = NULL;
    int audio_driver_nargs = 1;
    char *midi_driver_name = NULL;
    char **midi_driver_args = NULL;
    int midi_driver_nargs = 1;
204
205
    int port_max = 512;
    int do_mlock = 1;
206
    int do_unlock = 0;
sletz's avatar
sletz committed
207
208
209
210
    bool show_version = false;
    sigset_t signals;
    jackctl_parameter_t* param;
    union jackctl_parameter_value value;
sletz's avatar
sletz committed
211
212

	copyright(stdout);
sletz's avatar
sletz committed
213

sletz's avatar
sletz committed
214
215
    server_ctl = jackctl_server_create();
    if (server_ctl == NULL) {
sletz's avatar
Cleanup    
sletz committed
216
        fprintf(stderr, "Failed to create server object\n");
sletz's avatar
sletz committed
217
218
        return -1;
    }
sletz's avatar
sletz committed
219

sletz's avatar
sletz committed
220
    server_parameters = jackctl_server_get_parameters(server_ctl);
sletz's avatar
sletz committed
221
    opterr = 0;
222
    while (!seen_audio_driver &&
sletz's avatar
sletz committed
223
224
225
226
            (opt = getopt_long(argc, argv, options,
                               long_options, &option_index)) != EOF) {
        switch (opt) {

227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
        #ifdef __linux__        
            case 'c':
                param = jackctl_get_parameter(server_parameters, "clock-source");
                if (param != NULL) {
                    if (tolower (optarg[0]) == 'h') {
                        value.ui = JACK_TIMER_HPET;
                        jackctl_parameter_set_value(param, &value);
                    } else if (tolower (optarg[0]) == 'c') {
                        value.ui = JACK_TIMER_CYCLE_COUNTER;
                        jackctl_parameter_set_value(param, &value);
                    } else if (tolower (optarg[0]) == 's') {
                        value.ui = JACK_TIMER_SYSTEM_CLOCK;
                        jackctl_parameter_set_value(param, &value);
                    } else {
                        usage(stdout);
                        goto fail_free;
                    }
                }
                break;
        #endif

248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
            case 'a':
                param = jackctl_get_parameter(server_parameters, "self-connect-mode");
                if (param != NULL) {
                    if (optarg[0] == SELF_CONNECT_MODE_ALLOW_CHAR) {
                        value.ui = JackSelfConnectAllow;
                        jackctl_parameter_set_value(param, &value);
                    } else if (optarg[0] == SELF_CONNECT_MODE_FAIL_EXTERNAL_ONLY_CHAR) {
                        value.ui = JackSelfConnectFailExternalOnly;
                        jackctl_parameter_set_value(param, &value);
                    } else if (optarg[0] == SELF_CONNECT_MODE_IGNORE_EXTERNAL_ONLY_CHAR) {
                        value.ui = JackSelfConnectFailExternalOnly;
                        jackctl_parameter_set_value(param, &value);
                    } else if (optarg[0] == SELF_CONNECT_MODE_FAIL_ALL_CHAR) {
                        value.ui = JackSelfConnectFailExternalOnly;
                        jackctl_parameter_set_value(param, &value);
                    } else if (optarg[0] == SELF_CONNECT_MODE_IGNORE_ALL_CHAR) {
                        value.ui = JackSelfConnectFailExternalOnly;
                        jackctl_parameter_set_value(param, &value);
                    } else {
                        usage(stdout);
                        goto fail_free;
                    }
                }
                break;
sletz's avatar
sletz committed
272
            case 'd':
273
274
275
                seen_audio_driver = true;
                audio_driver_name = optarg;
                break;
sletz's avatar
sletz committed
276

277
278
279
            case 'X':
                seen_midi_driver = true;
                midi_driver_name = optarg;
sletz's avatar
sletz committed
280
                break;
sletz's avatar
sletz committed
281

282
283
284
            case 'p':
                port_max = (unsigned int)atol(optarg);
                break;
sletz's avatar
sletz committed
285

286
287
288
            case 'm':
                do_mlock = 0;
                break;
sletz's avatar
sletz committed
289

290
291
292
            case 'u':
                do_unlock = 1;
                break;
sletz's avatar
sletz committed
293
294

            case 'v':
sletz's avatar
sletz committed
295
296
297
298
299
                param = jackctl_get_parameter(server_parameters, "verbose");
                if (param != NULL) {
                    value.b = true;
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
300
301
302
303
304
305
306
                break;

            case 's':
                jack_set_error_function(silent_jack_error_callback);
                break;

            case 'S':
sletz's avatar
sletz committed
307
308
309
310
311
                param = jackctl_get_parameter(server_parameters, "sync");
                if (param != NULL) {
                    value.b = true;
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
312
313
314
315
                break;

            case 'n':
                server_name = optarg;
sletz's avatar
sletz committed
316
317
318
319
320
                param = jackctl_get_parameter(server_parameters, "name");
                if (param != NULL) {
                    strncpy(value.str, optarg, JACK_PARAM_STRING_MAX);
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
321
322
323
                break;

            case 'P':
sletz's avatar
sletz committed
324
325
326
327
328
                param = jackctl_get_parameter(server_parameters, "realtime-priority");
                if (param != NULL) {
                    value.i = atoi(optarg);
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
329
                break;
sletz's avatar
sletz committed
330
331

            case 'r':
sletz's avatar
sletz committed
332
333
334
335
336
                param = jackctl_get_parameter(server_parameters, "replace-registry");
                if (param != NULL) {
                    value.b = true;
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
337
338
                break;

sletz's avatar
sletz committed
339
            case 'R':
sletz's avatar
sletz committed
340
341
342
343
344
                param = jackctl_get_parameter(server_parameters, "realtime");
                if (param != NULL) {
                    value.b = true;
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
345
346
347
                break;

            case 'L':
sletz's avatar
sletz committed
348
                param = jackctl_get_parameter(server_parameters, "loopback-ports");
sletz's avatar
sletz committed
349
350
351
352
                if (param != NULL) {
                    value.ui = atoi(optarg);
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
353
354
355
                break;

            case 'T':
sletz's avatar
sletz committed
356
357
358
359
360
                param = jackctl_get_parameter(server_parameters, "temporary");
                if (param != NULL) {
                    value.b = true;
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
361
362
363
                break;

            case 't':
sletz's avatar
sletz committed
364
365
366
367
368
                param = jackctl_get_parameter(server_parameters, "client-timeout");
                if (param != NULL) {
                    value.i = atoi(optarg);
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
369
370
371
                break;

            case 'V':
sletz's avatar
sletz committed
372
                show_version = true;
sletz's avatar
sletz committed
373
374
375
                break;

            default:
sletz's avatar
sletz committed
376
                fprintf(stderr, "unknown option character %c\n", optopt);
sletz's avatar
sletz committed
377
                /*fallthru*/
sletz's avatar
sletz committed
378

sletz's avatar
sletz committed
379
380
            case 'h':
                usage(stdout);
sletz's avatar
sletz committed
381
                goto fail_free;
sletz's avatar
sletz committed
382
383
384
        }
    }

sletz's avatar
sletz committed
385
    if (show_version) {
sletz's avatar
sletz committed
386
387
        printf( "jackdmp version " VERSION
				" tmpdir " jack_server_dir
sletz's avatar
sletz committed
388
389
				" protocol %d"
				"\n", JACK_PROTOCOL_VERSION);
sletz's avatar
sletz committed
390
391
    	return -1;
    }
sletz's avatar
sletz committed
392

393
    if (!seen_audio_driver) {
sletz's avatar
sletz committed
394
        usage(stderr);
sletz's avatar
sletz committed
395
        goto fail_free;
sletz's avatar
sletz committed
396
    }
sletz's avatar
sletz committed
397

398
399
400
401
    // Audio driver
    audio_driver_ctl = jackctl_server_get_driver(server_ctl, audio_driver_name);
    if (audio_driver_ctl == NULL) {
        fprintf(stderr, "Unkown driver \"%s\"\n", audio_driver_name);
sletz's avatar
sletz committed
402
        goto fail_free;
sletz's avatar
sletz committed
403
404
405
    }

    if (optind < argc) {
406
        audio_driver_nargs = 1 + argc - optind;
sletz's avatar
sletz committed
407
    } else {
408
        audio_driver_nargs = 1;
sletz's avatar
sletz committed
409
410
    }

411
    if (audio_driver_nargs == 0) {
sletz's avatar
sletz committed
412
        fprintf(stderr, "No driver specified ... hmm. JACK won't do"
sletz's avatar
sletz committed
413
                " anything when run like this.\n");
sletz's avatar
sletz committed
414
        goto fail_free;
sletz's avatar
sletz committed
415
416
    }

417
418
    audio_driver_args = (char **) malloc(sizeof(char *) * audio_driver_nargs);
    audio_driver_args[0] = audio_driver_name;
sletz's avatar
sletz committed
419

420
421
    for (i = 1; i < audio_driver_nargs; i++) {
        audio_driver_args[i] = argv[optind++];
sletz's avatar
sletz committed
422
423
    }

424
    if (jackctl_parse_driver_params(audio_driver_ctl, audio_driver_nargs, audio_driver_args)) {
sletz's avatar
sletz committed
425
        goto fail_free;
sletz's avatar
sletz committed
426
    }
sletz's avatar
sletz committed
427

428
429
    // Start server
    if (!jackctl_server_start(server_ctl, audio_driver_ctl)) {
sletz's avatar
Cleanup    
sletz committed
430
        fprintf(stderr, "Failed to start server\n");
sletz's avatar
sletz committed
431
        goto fail_free;
sletz's avatar
sletz committed
432
    }
sletz's avatar
sletz committed
433

434
435
    // MIDI driver
    if (seen_midi_driver) {
sletz's avatar
sletz committed
436

437
438
439
440
441
        midi_driver_ctl = jackctl_server_get_driver(server_ctl, midi_driver_name);
        if (midi_driver_ctl == NULL) {
            fprintf(stderr, "Unkown driver \"%s\"\n", midi_driver_name);
            goto fail_free;
        }
sletz's avatar
sletz committed
442

443
        jackctl_server_add_slave(server_ctl, midi_driver_ctl);
444
445
    }

sletz's avatar
sletz committed
446
447
448
449
450
    notify_server_start(server_name);

    // Waits for signal
    signals = jackctl_setup_signals(0);
    jackctl_wait_signals(signals);
sletz's avatar
sletz committed
451

sletz's avatar
sletz committed
452
    if (!jackctl_server_stop(server_ctl))
sletz's avatar
Cleanup    
sletz committed
453
        fprintf(stderr, "Cannot stop server...\n");
sletz's avatar
sletz committed
454

sletz's avatar
sletz committed
455
fail_free:
sletz's avatar
sletz committed
456

sletz's avatar
sletz committed
457
458
    jackctl_server_destroy(server_ctl);
    notify_server_stop(server_name);
sletz's avatar
sletz committed
459
460
    return 1;
}