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.

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