2 Copyright (C) 2005 Grame
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "JackServerGlobals.h"
21 #include "JackTools.h"
26 static char* server_name
= NULL
;
31 JackServer
* JackServerGlobals::fInstance
;
32 unsigned int JackServerGlobals::fUserCount
;
33 bool (* JackServerGlobals::on_device_acquire
)(const char * device_name
) = NULL
;
34 void (* JackServerGlobals::on_device_release
)(const char * device_name
) = NULL
;
36 int JackServerGlobals::Start(const char* server_name
,
37 jack_driver_desc_t
* driver_desc
,
38 JSList
* driver_params
,
46 jack_timer_type_t clock
)
48 jack_log("Jackdmp: sync = %ld timeout = %ld rt = %ld priority = %ld verbose = %ld ", sync
, time_out_ms
, rt
, priority
, verbose
);
49 new JackServer(sync
, temporary
, time_out_ms
, rt
, priority
, port_max
, verbose
, clock
, server_name
); // Will setup fInstance and fUserCount globals
50 int res
= fInstance
->Open(driver_desc
, driver_params
);
51 return (res
< 0) ? res
: fInstance
->Start();
54 void JackServerGlobals::Stop()
56 jack_log("Jackdmp: server close");
61 void JackServerGlobals::Delete()
63 jack_log("Jackdmp: delete server");
68 bool JackServerGlobals::Init()
71 int client_timeout
= 0; /* msecs; if zero, use period size. */
72 int realtime_priority
= 10;
75 unsigned int port_max
= 128;
82 char *driver_name
= NULL
;
83 char **driver_args
= NULL
;
84 JSList
* driver_params
= NULL
;
86 JSList
* drivers
= NULL
;
97 jack_timer_type_t clock_source
= JACK_TIMER_SYSTEM_CLOCK
;
99 // First user starts the server
100 if (fUserCount
++ == 0) {
102 jack_log("JackServerGlobals Init");
104 jack_driver_desc_t
* driver_desc
;
105 const char *options
= "-ad:P:uvshVRL:STFl:t:mn:p:c:";
106 static struct option long_options
[] = {
107 { "clock-source", 1, 0, 'c' },
108 { "driver", 1, 0, 'd' },
109 { "verbose", 0, 0, 'v' },
110 { "help", 0, 0, 'h' },
111 { "port-max", 1, 0, 'p' },
112 { "no-mlock", 0, 0, 'm' },
113 { "name", 0, 0, 'n' },
114 { "unlock", 0, 0, 'u' },
115 { "realtime", 0, 0, 'R' },
116 { "realtime-priority", 1, 0, 'P' },
117 { "timeout", 1, 0, 't' },
118 { "temporary", 0, 0, 'T' },
119 { "version", 0, 0, 'V' },
120 { "silent", 0, 0, 's' },
121 { "sync", 0, 0, 'S' },
125 snprintf(filename
, 255, "%s/.jackdrc", getenv("HOME"));
126 fp
= fopen(filename
, "r");
129 fp
= fopen("/etc/jackdrc", "r");
131 // if still not found, check old config name for backwards compatability
133 fp
= fopen("/etc/jackd.conf", "r");
138 ret
= fscanf(fp
, "%s", buffer
);
139 while (ret
!= 0 && ret
!= EOF
) {
140 argv
[argc
] = (char*)malloc(64);
141 strcpy(argv
[argc
], buffer
);
142 ret
= fscanf(fp
, "%s", buffer
);
151 char* argv[] = {"jackdmp", "-R", "-v", "-d", "coreaudio", "-p", "512", "-d", "~:Aggregate:0", "-r", "48000", "-i", "2", "-o", "2" };
155 optind
= 1; // Important : to reset argv parsing
157 while (!seen_driver
&&
158 (opt
= getopt_long(argc
, argv
, options
, long_options
, &option_index
)) != EOF
) {
163 if (tolower (optarg
[0]) == 'h') {
164 clock_source
= JACK_TIMER_HPET
;
165 } else if (tolower (optarg
[0]) == 'c') {
166 clock_source
= JACK_TIMER_CYCLE_COUNTER
;
167 } else if (tolower (optarg
[0]) == 's') {
168 clock_source
= JACK_TIMER_SYSTEM_CLOCK
;
170 jack_error("unknown option character %c", optopt
);
176 driver_name
= optarg
;
188 server_name
= optarg
;
196 port_max
= (unsigned int)atol(optarg
);
200 realtime_priority
= atoi(optarg
);
212 client_timeout
= atoi(optarg
);
224 jack_error("unknown option character %c", optopt
);
229 drivers
= jack_drivers_load(drivers
);
231 jack_error("jackdmp: no drivers found; exiting");
235 driver_desc
= jack_find_driver_descriptor(drivers
, driver_name
);
237 jack_error("jackdmp: unknown driver '%s'", driver_name
);
242 driver_nargs
= 1 + argc
- optind
;
247 if (driver_nargs
== 0) {
248 jack_error("No driver specified ... hmm. JACK won't do"
249 " anything when run like this.");
253 driver_args
= (char**)malloc(sizeof(char*) * driver_nargs
);
254 driver_args
[0] = driver_name
;
256 for (i
= 1; i
< driver_nargs
; i
++) {
257 driver_args
[i
] = argv
[optind
++];
260 if (jack_parse_driver_params(driver_desc
, driver_nargs
, driver_args
, &driver_params
)) {
265 if (server_name
== NULL
)
266 server_name
= (char*)JackTools::DefaultServerName();
269 rc
= jack_register_server(server_name
, false);
272 jack_error("`%s' server already active", server_name
);
275 jack_error("too many servers already active");
278 jack_error("no access to shm registry");
281 jack_info("server `%s' registered", server_name
);
284 /* clean up shared memory and files from any previous instance of this server name */
286 JackTools::CleanupFiles(server_name
);
288 if (!realtime
&& client_timeout
== 0)
289 client_timeout
= 500; /* 0.5 sec; usable when non realtime. */
291 for (i
= 0; i
< argc
; i
++) {
295 int res
= Start(server_name
, driver_desc
, driver_params
, sync
, temporary
, client_timeout
, realtime
, realtime_priority
, port_max
, verbose_aux
, clock_source
);
297 jack_error("Cannot start server... exit");
300 JackTools::CleanupFiles(server_name
);
301 jack_unregister_server(server_name
);
307 jack_free_driver_params(driver_params
);
312 jack_free_driver_params(driver_params
);
317 void JackServerGlobals::Destroy()
319 if (--fUserCount
== 0) {
320 jack_log("JackServerGlobals Destroy");
324 JackTools::CleanupFiles(server_name
);
325 jack_unregister_server(server_name
);
329 } // end of namespace