Jackdmp.cpp 11.6 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

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.

*/

21
22
23
24
#if defined(HAVE_CONFIG_H)
#include "config.h"
#endif

sletz's avatar
sletz committed
25
26
27
#include <iostream>
#include <assert.h>
#include <signal.h>
sletz's avatar
sletz committed
28

sletz's avatar
sletz committed
29
30
#include <sys/types.h>
#include <getopt.h>
sletz's avatar
sletz committed
31
#include <string.h>
32
#include "types.h"
sletz's avatar
sletz committed
33
#include "jack.h"
sletz's avatar
sletz committed
34
35
#include "JackConstants.h"
#include "JackDriverLoader.h"
sletz's avatar
sletz committed
36
37
38
39
40
41
42

/*
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 
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
43

44
45
46
#ifdef __APPLE_
#include <CoreFoundation/CFNotificationCenter.h>

sletz's avatar
sletz committed
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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
    CFStringRef ref1 = CFStringCreateWithCString(NULL, server_name, kCFStringEncodingMacRoman);    
    CFNotificationCenterPostNotificationWithOptions(CFNotificationCenterGetDistributedCenter(),
            CFSTR("com.grame.jackserver.stop"),
            ref1,
            NULL,
            kCFNotificationDeliverImmediately | kCFNotificationPostToAllSessions);
    CFRelease(ref1);
}
sletz's avatar
sletz committed
70

sletz's avatar
sletz committed
71
72
73
74
75
76
77
78
#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
79

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

static void copyright(FILE* file)
{
sletz's avatar
sletz committed
85
    fprintf(file, "jackdmp " VERSION "\n"
sletz's avatar
sletz committed
86
87
88
89
90
            "Copyright 2001-2005 Paul Davis and others.\n"
            "Copyright 2004-2008 Grame.\n"
            "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
91
92
}

sletz's avatar
sletz committed
93
static void usage(FILE* file)
sletz's avatar
sletz committed
94
{
sletz's avatar
sletz committed
95
    fprintf(file, "\n"
sletz's avatar
sletz committed
96
97
98
99
100
101
102
103
104
105
106
107
108
109
            "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"
            "               [ --loopback OR -L loopback-port-number ]\n"
            "               [ --verbose OR -v ]\n"
            "               [ --replace-registry OR -r ]\n"
            "               [ --silent OR -s ]\n"
            "               [ --sync OR -S ]\n"
            "               [ --temporary OR -T ]\n"
            "               [ --version OR -V ]\n"
            "         -d driver [ ... driver args ... ]\n"
            "             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
110
111
}

sletz's avatar
sletz committed
112
113
114
115
116
// 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
117
{
sletz's avatar
sletz committed
118
    const JSList * node_ptr;
sletz's avatar
sletz committed
119

sletz's avatar
sletz committed
120
    node_ptr = jackctl_server_get_drivers_list(server);
sletz's avatar
sletz committed
121

sletz's avatar
sletz committed
122
123
124
125
126
127
    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
128

sletz's avatar
sletz committed
129
130
131
132
        node_ptr = jack_slist_next(node_ptr);
    }

    return NULL;
sletz's avatar
sletz committed
133
134
}

sletz's avatar
sletz committed
135
136
137
138
static jackctl_parameter_t *
jackctl_get_parameter(
    const JSList * parameters_list,
    const char * parameter_name)
sletz's avatar
sletz committed
139
{
sletz's avatar
sletz committed
140
141
142
143
144
145
146
147
148
149
150
    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
151
152
153
154
}

int main(int argc, char* argv[])
{
sletz's avatar
sletz committed
155
156
157
158
    jackctl_server_t * server_ctl;
    const JSList * server_parameters;
    const char* server_name = "default";
    jackctl_driver_t * driver_ctl;
159
    const char *options = "-ad:P:uvrshVRL:STFl:t:mn:p:";
sletz's avatar
sletz committed
160
    struct option long_options[] = {
sletz's avatar
sletz committed
161
                                       { "driver", 1, 0, 'd' },
sletz's avatar
sletz committed
162
163
164
165
166
167
168
                                       { "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
169
                                       { "replace-registry", 0, 0, 'r' },
sletz's avatar
sletz committed
170
171
172
173
174
175
176
177
178
                                       { "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' },
                                       { 0, 0, 0, 0 }
                                   };
sletz's avatar
sletz committed
179
    int i,opt = 0;
sletz's avatar
sletz committed
180
    int option_index = 0;
sletz's avatar
sletz committed
181
    bool seen_driver = false;
sletz's avatar
sletz committed
182
183
184
    char *driver_name = NULL;
    char **driver_args = NULL;
    int driver_nargs = 1;
185
    int port_max;
sletz's avatar
sletz committed
186
187
188
189
    bool show_version = false;
    sigset_t signals;
    jackctl_parameter_t* param;
    union jackctl_parameter_value value;
sletz's avatar
sletz committed
190
191

	copyright(stdout);
sletz's avatar
sletz committed
192
193
194
    
    server_ctl = jackctl_server_create();
    if (server_ctl == NULL) {
sletz's avatar
Cleanup    
sletz committed
195
        fprintf(stderr, "Failed to create server object\n");
sletz's avatar
sletz committed
196
197
198
199
        return -1;
    }
    
    server_parameters = jackctl_server_get_parameters(server_ctl);
sletz's avatar
sletz committed
200
201
202
203
204
205
206
207

    opterr = 0;
    while (!seen_driver &&
            (opt = getopt_long(argc, argv, options,
                               long_options, &option_index)) != EOF) {
        switch (opt) {

            case 'd':
sletz's avatar
sletz committed
208
                seen_driver = true;
sletz's avatar
sletz committed
209
210
                driver_name = optarg;
                break;
211
212
213
214
                
            case 'p':
                port_max = (unsigned int)atol(optarg);
                break;
sletz's avatar
sletz committed
215
216

            case 'v':
sletz's avatar
sletz committed
217
218
219
220
221
                param = jackctl_get_parameter(server_parameters, "verbose");
                if (param != NULL) {
                    value.b = true;
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
222
223
224
225
226
227
228
                break;

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

            case 'S':
sletz's avatar
sletz committed
229
230
231
232
233
                param = jackctl_get_parameter(server_parameters, "sync");
                if (param != NULL) {
                    value.b = true;
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
234
235
236
237
                break;

            case 'n':
                server_name = optarg;
sletz's avatar
sletz committed
238
239
240
241
242
                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
243
244
245
                break;

            case 'P':
sletz's avatar
sletz committed
246
247
248
249
250
                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
251
                break;
sletz's avatar
sletz committed
252
253

            case 'r':
sletz's avatar
sletz committed
254
255
256
257
258
                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
259
260
                break;

sletz's avatar
sletz committed
261
            case 'R':
sletz's avatar
sletz committed
262
263
264
265
266
                param = jackctl_get_parameter(server_parameters, "realtime");
                if (param != NULL) {
                    value.b = true;
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
267
268
269
                break;

            case 'L':
sletz's avatar
sletz committed
270
                param = jackctl_get_parameter(server_parameters, "loopback-ports");
sletz's avatar
sletz committed
271
272
273
274
                if (param != NULL) {
                    value.ui = atoi(optarg);
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
275
276
277
                break;

            case 'T':
sletz's avatar
sletz committed
278
279
280
281
282
                param = jackctl_get_parameter(server_parameters, "temporary");
                if (param != NULL) {
                    value.b = true;
                    jackctl_parameter_set_value(param, &value);
                }
sletz's avatar
sletz committed
283
284
285
                break;

            case 't':
sletz's avatar
sletz committed
286
287
288
289
290
                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
291
292
293
                break;

            case 'V':
sletz's avatar
sletz committed
294
                show_version = true;
sletz's avatar
sletz committed
295
296
297
                break;

            default:
sletz's avatar
sletz committed
298
                fprintf(stderr, "unknown option character %c\n", optopt);
sletz's avatar
sletz committed
299
300
301
                /*fallthru*/
            case 'h':
                usage(stdout);
sletz's avatar
sletz committed
302
                goto fail_free;
sletz's avatar
sletz committed
303
304
305
        }
    }

sletz's avatar
sletz committed
306
307
    if (show_version) {
    	printf("jackdmp version" VERSION 
308
               "\n");
sletz's avatar
sletz committed
309
310
    	return -1;
    }
311
 
sletz's avatar
sletz committed
312
    if (!seen_driver) {
sletz's avatar
sletz committed
313
        usage(stderr);
sletz's avatar
sletz committed
314
        goto fail_free;
sletz's avatar
sletz committed
315
316
    }

sletz's avatar
sletz committed
317
318
319
320
    driver_ctl = jackctl_server_get_driver(server_ctl, driver_name);
    if (driver_ctl == NULL) {
	fprintf(stderr, "Unkown driver \"%s\"\n", driver_name);
        goto fail_free;
sletz's avatar
sletz committed
321
322
323
324
325
326
327
328
329
    }

    if (optind < argc) {
        driver_nargs = 1 + argc - optind;
    } else {
        driver_nargs = 1;
    }

    if (driver_nargs == 0) {
sletz's avatar
sletz committed
330
        fprintf(stderr, "No driver specified ... hmm. JACK won't do"
sletz's avatar
sletz committed
331
                " anything when run like this.\n");
sletz's avatar
sletz committed
332
        goto fail_free;
sletz's avatar
sletz committed
333
334
    }

sletz's avatar
sletz committed
335
    driver_args = (char **) malloc(sizeof(char *) * driver_nargs);
sletz's avatar
sletz committed
336
337
338
339
340
341
    driver_args[0] = driver_name;

    for (i = 1; i < driver_nargs; i++) {
        driver_args[i] = argv[optind++];
    }

sletz's avatar
sletz committed
342
343
    if (jackctl_parse_driver_params(driver_ctl, driver_nargs, driver_args)) {
        goto fail_free;
sletz's avatar
sletz committed
344
    }
sletz's avatar
sletz committed
345
346
    
    if (!jackctl_server_start(server_ctl, driver_ctl)) {
sletz's avatar
Cleanup    
sletz committed
347
        fprintf(stderr, "Failed to start server\n");
sletz's avatar
sletz committed
348
        goto fail_free;
sletz's avatar
sletz committed
349
    }
sletz's avatar
sletz committed
350
351
352
353
354
355
356
357
    
    notify_server_start(server_name);

    // Waits for signal
    signals = jackctl_setup_signals(0);
    jackctl_wait_signals(signals);
       
    if (!jackctl_server_stop(server_ctl))
sletz's avatar
Cleanup    
sletz committed
358
        fprintf(stderr, "Cannot stop server...\n");
sletz's avatar
sletz committed
359
360
361
362
363
        
fail_free:
        
    jackctl_server_destroy(server_ctl);
    notify_server_stop(server_name);
sletz's avatar
sletz committed
364
365
    return 1;
}