jack2 codebase
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

495 lines
13KB

  1. /*
  2. Copyright (C) 2001 Paul Davis
  3. Copyright (C) 2004-2006 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.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  15. */
  16. #include <iostream>
  17. #include <assert.h>
  18. #include <process.h>
  19. #include <getopt.h>
  20. #include <signal.h>
  21. #include "JackServer.h"
  22. #include "JackConstants.h"
  23. #include "driver_interface.h"
  24. #include "JackDriverLoader.h"
  25. #include "shm.h"
  26. using namespace Jack;
  27. static JackServer* fServer;
  28. static char *server_name = "default";
  29. static int realtime_priority = 10;
  30. static int do_mlock = 1;
  31. static unsigned int port_max = 128;
  32. static int realtime = 0;
  33. static int loopback = 0;
  34. static int temporary = 0;
  35. static int client_timeout = 0; /* msecs; if zero, use period size. */
  36. static int do_unlock = 0;
  37. static JSList* drivers = NULL;
  38. static int sync = 0;
  39. static int xverbose = 0;
  40. #define DEFAULT_TMP_DIR "/tmp"
  41. char *jack_tmpdir = DEFAULT_TMP_DIR;
  42. HANDLE waitEvent;
  43. void jack_print_driver_options (jack_driver_desc_t * desc, FILE *file);
  44. void jack_print_driver_param_usage (jack_driver_desc_t * desc, unsigned long param, FILE *file);
  45. int jack_parse_driver_params (jack_driver_desc_t * desc, int argc, char* argv[], JSList ** param_ptr);
  46. static void silent_jack_error_callback (const char *desc)
  47. {}
  48. static void copyright(FILE* file)
  49. {
  50. fprintf (file, "jackdmp " VERSION "\n"
  51. "Copyright 2001-2005 Paul Davis and others.\n"
  52. "Copyright 2004-2008 Grame.\n"
  53. "jackdmp comes with ABSOLUTELY NO WARRANTY\n"
  54. "This is free software, and you are welcome to redistribute it\n"
  55. "under certain conditions; see the file COPYING for details\n");
  56. }
  57. static void usage (FILE *file)
  58. {
  59. copyright (file);
  60. fprintf (file, "\n"
  61. "usage: jackdmp [ --realtime OR -R [ --realtime-priority OR -P priority ] ]\n"
  62. " [ --name OR -n server-name ]\n"
  63. // " [ --no-mlock OR -m ]\n"
  64. // " [ --unlock OR -u ]\n"
  65. " [ --timeout OR -t client-timeout-in-msecs ]\n"
  66. " [ --loopback OR -L loopback-port-number ]\n"
  67. // " [ --port-max OR -p maximum-number-of-ports]\n"
  68. " [ --verbose OR -v ]\n"
  69. " [ --replace-registry OR -r ]\n"
  70. " [ --silent OR -s ]\n"
  71. " [ --sync OR -S ]\n"
  72. " [ --version OR -V ]\n"
  73. " -d driver [ ... driver args ... ]\n"
  74. " where driver can be `alsa', `coreaudio', 'portaudio' or `dummy'\n"
  75. " jackdmp -d driver --help\n"
  76. " to display options for each driver\n\n");
  77. }
  78. static int JackStart(jack_driver_desc_t* driver_desc, JSList* driver_params, int sync, int time_out_ms, int rt, int priority, int loopback, int verbose, const char* server_name)
  79. {
  80. printf("Jackdmp: sync = %ld timeout = %ld rt = %ld priority = %ld verbose = %ld \n", sync, time_out_ms, rt, priority, verbose);
  81. fServer = new JackServer(sync, temporary, time_out_ms, rt, priority, loopback, verbose, server_name);
  82. int res = fServer->Open(driver_desc, driver_params);
  83. return (res < 0) ? res : fServer->Start();
  84. }
  85. static int JackStop()
  86. {
  87. fServer->Stop();
  88. fServer->Close();
  89. printf("Jackdmp: server close\n");
  90. delete fServer;
  91. printf("Jackdmp: delete server\n");
  92. return 0;
  93. }
  94. static int JackDelete()
  95. {
  96. delete fServer;
  97. printf("Jackdmp: delete server\n");
  98. return 0;
  99. }
  100. static void intrpt(int signum)
  101. {
  102. printf("jack main caught signal %d\n", signum);
  103. (void) signal(SIGINT, SIG_DFL);
  104. SetEvent(waitEvent);
  105. }
  106. /*
  107. static char* jack_default_server_name(void)
  108. {
  109. char *server_name;
  110. if ((server_name = getenv("JACK_DEFAULT_SERVER")) == NULL)
  111. server_name = "default";
  112. return server_name;
  113. }
  114. // returns the name of the per-user subdirectory of jack_tmpdir
  115. static char* jack_user_dir(void)
  116. {
  117. static char user_dir[PATH_MAX] = "";
  118. // format the path name on the first call
  119. if (user_dir[0] == '\0') {
  120. snprintf (user_dir, sizeof (user_dir), "%s/jack-%d",
  121. jack_tmpdir, _getuid ());
  122. }
  123. return user_dir;
  124. }
  125. // returns the name of the per-server subdirectory of jack_user_dir()
  126. static char* get_jack_server_dir(const char * toto)
  127. {
  128. static char server_dir[PATH_MAX] = "";
  129. // format the path name on the first call
  130. if (server_dir[0] == '\0') {
  131. snprintf (server_dir, sizeof (server_dir), "%s/%s",
  132. jack_user_dir (), server_name);
  133. }
  134. return server_dir;
  135. }
  136. static void jack_cleanup_files (const char *server_name)
  137. {
  138. DIR *dir;
  139. struct dirent *dirent;
  140. char *dir_name = get_jack_server_dir (server_name);
  141. // nothing to do if the server directory does not exist
  142. if ((dir = opendir (dir_name)) == NULL) {
  143. return;
  144. }
  145. // unlink all the files in this directory, they are mine
  146. while ((dirent = readdir (dir)) != NULL) {
  147. char fullpath[PATH_MAX];
  148. if ((strcmp (dirent->d_name, ".") == 0)
  149. || (strcmp (dirent->d_name, "..") == 0)) {
  150. continue;
  151. }
  152. snprintf (fullpath, sizeof (fullpath), "%s/%s",
  153. dir_name, dirent->d_name);
  154. if (unlink (fullpath)) {
  155. jack_error ("cannot unlink `%s' (%s)", fullpath,
  156. strerror (errno));
  157. }
  158. }
  159. closedir (dir);
  160. // now, delete the per-server subdirectory, itself
  161. if (rmdir (dir_name)) {
  162. jack_error ("cannot remove `%s' (%s)", dir_name,
  163. strerror (errno));
  164. }
  165. // finally, delete the per-user subdirectory, if empty
  166. if (rmdir (jack_user_dir ())) {
  167. if (errno != ENOTEMPTY) {
  168. jack_error ("cannot remove `%s' (%s)",
  169. jack_user_dir (), strerror (errno));
  170. }
  171. }
  172. }
  173. */
  174. /*
  175. BOOL CtrlHandler( DWORD fdwCtrlType )
  176. {
  177. switch( fdwCtrlType )
  178. {
  179. // Handle the CTRL-C signal.
  180. case CTRL_C_EVENT:
  181. printf( "Ctrl-C event\n\n" );
  182. Beep( 750, 300 );
  183. SetEvent(waitEvent);
  184. return( TRUE );
  185. // CTRL-CLOSE: confirm that the user wants to exit.
  186. case CTRL_CLOSE_EVENT:
  187. Beep( 600, 200 );
  188. printf( "Ctrl-Close event\n\n" );
  189. SetEvent(waitEvent);
  190. return( TRUE );
  191. // Pass other signals to the next handler.
  192. case CTRL_BREAK_EVENT:
  193. Beep( 900, 200 );
  194. printf( "Ctrl-Break event\n\n" );
  195. return FALSE;
  196. case CTRL_LOGOFF_EVENT:
  197. Beep( 1000, 200 );
  198. printf( "Ctrl-Logoff event\n\n" );
  199. return FALSE;
  200. case CTRL_SHUTDOWN_EVENT:
  201. Beep( 750, 500 );
  202. printf( "Ctrl-Shutdown event\n\n" );
  203. return FALSE;
  204. default:
  205. return FALSE;
  206. }
  207. }
  208. */
  209. int main(int argc, char* argv[])
  210. {
  211. jack_driver_desc_t * driver_desc;
  212. const char *options = "-ad:P:uvshVRL:STFl:t:mn:p:";
  213. struct option long_options[] = {
  214. { "driver", 1, 0, 'd'
  215. },
  216. { "verbose", 0, 0, 'v' },
  217. { "help", 0, 0, 'h' },
  218. { "port-max", 1, 0, 'p' },
  219. { "no-mlock", 0, 0, 'm' },
  220. { "name", 0, 0, 'n' },
  221. { "unlock", 0, 0, 'u' },
  222. { "realtime", 0, 0, 'R' },
  223. { "replace-registry", 0, 0, 'r' },
  224. { "loopback", 0, 0, 'L' },
  225. { "realtime-priority", 1, 0, 'P' },
  226. { "timeout", 1, 0, 't' },
  227. { "temporary", 0, 0, 'T' },
  228. { "version", 0, 0, 'V' },
  229. { "silent", 0, 0, 's' },
  230. { "sync", 0, 0, 'S' },
  231. { 0, 0, 0, 0 }
  232. };
  233. int opt = 0;
  234. int option_index = 0;
  235. int seen_driver = 0;
  236. char *driver_name = NULL;
  237. char **driver_args = NULL;
  238. JSList * driver_params;
  239. int driver_nargs = 1;
  240. int show_version = 0;
  241. int replace_registry = 0;
  242. int sync = 0;
  243. int i;
  244. int rc;
  245. char c;
  246. // Creates wait event
  247. if ((waitEvent = CreateEvent(NULL, FALSE, FALSE, NULL)) == NULL) {
  248. printf("CreateEvent fails err = %ld\n", GetLastError());
  249. return 0;
  250. }
  251. opterr = 0;
  252. while (!seen_driver &&
  253. (opt = getopt_long(argc, argv, options,
  254. long_options, &option_index)) != EOF) {
  255. switch (opt) {
  256. case 'd':
  257. seen_driver = 1;
  258. driver_name = optarg;
  259. break;
  260. case 'v':
  261. xverbose = 1;
  262. break;
  263. case 's':
  264. // steph
  265. //jack_set_error_function(silent_jack_error_callback);
  266. break;
  267. case 'S':
  268. sync = 1;
  269. break;
  270. case 'n':
  271. server_name = optarg;
  272. break;
  273. case 'm':
  274. do_mlock = 0;
  275. break;
  276. case 'p':
  277. port_max = (unsigned int)atol(optarg);
  278. break;
  279. case 'P':
  280. realtime_priority = atoi(optarg);
  281. break;
  282. case 'r':
  283. replace_registry = 1;
  284. break;
  285. case 'R':
  286. realtime = 1;
  287. break;
  288. case 'L':
  289. loopback = atoi(optarg);
  290. break;
  291. case 'T':
  292. temporary = 1;
  293. break;
  294. case 't':
  295. client_timeout = atoi(optarg);
  296. break;
  297. case 'u':
  298. do_unlock = 1;
  299. break;
  300. case 'V':
  301. show_version = 1;
  302. break;
  303. default:
  304. fprintf(stderr, "unknown option character %c\n",
  305. optopt);
  306. /*fallthru*/
  307. case 'h':
  308. usage(stdout);
  309. return -1;
  310. }
  311. }
  312. if (!seen_driver) {
  313. usage (stderr);
  314. //exit (1);
  315. return 0;
  316. }
  317. drivers = jack_drivers_load (drivers);
  318. if (!drivers) {
  319. fprintf (stderr, "jackdmp: no drivers found; exiting\n");
  320. //exit (1);
  321. return 0;
  322. }
  323. driver_desc = jack_find_driver_descriptor (drivers, driver_name);
  324. if (!driver_desc) {
  325. fprintf (stderr, "jackdmp: unknown driver '%s'\n", driver_name);
  326. //exit (1);
  327. return 0;
  328. }
  329. if (optind < argc) {
  330. driver_nargs = 1 + argc - optind;
  331. } else {
  332. driver_nargs = 1;
  333. }
  334. if (driver_nargs == 0) {
  335. fprintf (stderr, "No driver specified ... hmm. JACK won't do"
  336. " anything when run like this.\n");
  337. return -1;
  338. }
  339. driver_args = (char **) malloc (sizeof (char *) * driver_nargs);
  340. driver_args[0] = driver_name;
  341. for (i = 1; i < driver_nargs; i++) {
  342. driver_args[i] = argv[optind++];
  343. }
  344. if (jack_parse_driver_params (driver_desc, driver_nargs,
  345. driver_args, &driver_params)) {
  346. // exit (0);
  347. return 0;
  348. }
  349. //if (server_name == NULL)
  350. // server_name = jack_default_server_name ();
  351. copyright (stdout);
  352. rc = jack_register_server (server_name, replace_registry);
  353. switch (rc) {
  354. case EEXIST:
  355. fprintf (stderr, "`%s' server already active\n", server_name);
  356. //exit (1);
  357. return 0;
  358. case ENOSPC:
  359. fprintf (stderr, "too many servers already active\n");
  360. //exit (2);
  361. return 0;
  362. case ENOMEM:
  363. fprintf (stderr, "no access to shm registry\n");
  364. //exit (3);
  365. return 0;
  366. default:
  367. if (xverbose)
  368. fprintf (stderr, "server `%s' registered\n",
  369. server_name);
  370. }
  371. /* clean up shared memory and files from any previous
  372. * instance of this server name */
  373. jack_cleanup_shm();
  374. // jack_cleanup_files(server_name);
  375. if (!realtime && client_timeout == 0)
  376. client_timeout = 500; /* 0.5 sec; usable when non realtime. */
  377. int res = JackStart(driver_desc, driver_params, sync, client_timeout, realtime, realtime_priority, loopback, xverbose, server_name);
  378. if (res < 0) {
  379. printf("Cannot start server... exit\n");
  380. JackDelete();
  381. return 0;
  382. }
  383. /*
  384. if( SetConsoleCtrlHandler( (PHANDLER_ROUTINE) CtrlHandler, TRUE ) )
  385. {
  386. printf( "\nThe Control Handler is installed.\n" );
  387. } else {
  388. printf( "\nERROR: Could not set control handler");
  389. }
  390. */
  391. (void) signal(SIGINT, intrpt);
  392. (void) signal(SIGABRT, intrpt);
  393. (void) signal(SIGTERM, intrpt);
  394. if ((res = WaitForSingleObject(waitEvent, INFINITE)) != WAIT_OBJECT_0) {
  395. printf("WaitForSingleObject fails err = %ld\n", GetLastError());
  396. }
  397. /*
  398. printf("Type 'q' to quit\n");
  399. while ((c = getchar()) != 'q') {}
  400. */
  401. JackStop();
  402. jack_cleanup_shm();
  403. // jack_cleanup_files(server_name);
  404. jack_unregister_server(server_name);
  405. CloseHandle(waitEvent);
  406. return 1;
  407. }