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.

659 lines
20KB

  1. /*
  2. Copyright (C) 2001-2005 Paul Davis
  3. Copyright (C) 2004-2008 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. #if defined(HAVE_CONFIG_H)
  17. #include "config.h"
  18. #endif
  19. #ifdef WIN32
  20. #pragma warning (disable : 4786)
  21. #endif
  22. #ifndef WIN32
  23. #ifndef ADDON_DIR
  24. #include "config.h"
  25. #endif
  26. #endif
  27. #include "JackDriverLoader.h"
  28. #include "JackConstants.h"
  29. #include "JackError.h"
  30. #include <getopt.h>
  31. #include <stdio.h>
  32. #ifndef WIN32
  33. #include <dirent.h>
  34. #endif
  35. #include <errno.h>
  36. jack_driver_desc_t * jackctl_driver_get_desc(jackctl_driver_t * driver);
  37. static void
  38. jack_print_driver_options (jack_driver_desc_t * desc, FILE *file)
  39. {
  40. unsigned long i;
  41. char arg_default[JACK_DRIVER_PARAM_STRING_MAX + 1];
  42. for (i = 0; i < desc->nparams; i++) {
  43. switch (desc->params[i].type) {
  44. case JackDriverParamInt:
  45. sprintf (arg_default, "%" "i", desc->params[i].value.i);
  46. break;
  47. case JackDriverParamUInt:
  48. sprintf (arg_default, "%" "u", desc->params[i].value.ui);
  49. break;
  50. case JackDriverParamChar:
  51. sprintf (arg_default, "%c", desc->params[i].value.c);
  52. break;
  53. case JackDriverParamString:
  54. if (desc->params[i].value.str && strcmp (desc->params[i].value.str, "") != 0)
  55. sprintf (arg_default, "%s", desc->params[i].value.str);
  56. else
  57. sprintf (arg_default, "none");
  58. break;
  59. case JackDriverParamBool:
  60. sprintf (arg_default, "%s", desc->params[i].value.i ? "true" : "false");
  61. break;
  62. }
  63. fprintf (file, "\t-%c, --%s \t%s (default: %s)\n",
  64. desc->params[i].character,
  65. desc->params[i].name,
  66. desc->params[i].short_desc,
  67. arg_default);
  68. }
  69. }
  70. static void
  71. jack_print_driver_param_usage (jack_driver_desc_t * desc, unsigned long param, FILE *file)
  72. {
  73. fprintf (file, "Usage information for the '%s' parameter for driver '%s':\n",
  74. desc->params[param].name, desc->name);
  75. fprintf (file, "%s\n", desc->params[param].long_desc);
  76. }
  77. int
  78. jack_parse_driver_params (jack_driver_desc_t * desc, int argc, char* argv[], JSList ** param_ptr)
  79. {
  80. struct option * long_options;
  81. char * options, * options_ptr;
  82. unsigned long i;
  83. int opt;
  84. unsigned int param_index;
  85. JSList * params = NULL;
  86. jack_driver_param_t * driver_param;
  87. if (argc <= 1) {
  88. *param_ptr = NULL;
  89. return 0;
  90. }
  91. /* check for help */
  92. if (strcmp (argv[1], "-h") == 0 || strcmp (argv[1], "--help") == 0) {
  93. if (argc > 2) {
  94. for (i = 0; i < desc->nparams; i++) {
  95. if (strcmp (desc->params[i].name, argv[2]) == 0) {
  96. jack_print_driver_param_usage (desc, i, stdout);
  97. return 1;
  98. }
  99. }
  100. fprintf (stderr, "jackd: unknown option '%s' "
  101. "for driver '%s'\n", argv[2],
  102. desc->name);
  103. }
  104. printf ("Parameters for driver '%s' (all parameters are optional):\n", desc->name);
  105. jack_print_driver_options (desc, stdout);
  106. return 1;
  107. }
  108. /* set up the stuff for getopt */
  109. options = (char*)calloc (desc->nparams * 3 + 1, sizeof (char));
  110. long_options = (option*)calloc (desc->nparams + 1, sizeof (struct option));
  111. options_ptr = options;
  112. for (i = 0; i < desc->nparams; i++) {
  113. sprintf (options_ptr, "%c::", desc->params[i].character);
  114. options_ptr += 3;
  115. long_options[i].name = desc->params[i].name;
  116. long_options[i].flag = NULL;
  117. long_options[i].val = desc->params[i].character;
  118. long_options[i].has_arg = optional_argument;
  119. }
  120. /* create the params */
  121. optind = 0;
  122. opterr = 0;
  123. while ((opt = getopt_long(argc, argv, options, long_options, NULL)) != -1) {
  124. if (opt == ':' || opt == '?') {
  125. if (opt == ':') {
  126. fprintf (stderr, "Missing option to argument '%c'\n", optopt);
  127. } else {
  128. fprintf (stderr, "Unknownage with option '%c'\n", optopt);
  129. }
  130. fprintf (stderr, "Options for driver '%s':\n", desc->name);
  131. jack_print_driver_options (desc, stderr);
  132. exit (1);
  133. }
  134. for (param_index = 0; param_index < desc->nparams; param_index++) {
  135. if (opt == desc->params[param_index].character) {
  136. break;
  137. }
  138. }
  139. driver_param = (jack_driver_param_t*)calloc (1, sizeof (jack_driver_param_t));
  140. driver_param->character = desc->params[param_index].character;
  141. if (!optarg && optind < argc &&
  142. strlen(argv[optind]) &&
  143. argv[optind][0] != '-') {
  144. optarg = argv[optind];
  145. }
  146. if (optarg) {
  147. switch (desc->params[param_index].type) {
  148. case JackDriverParamInt:
  149. driver_param->value.i = atoi (optarg);
  150. break;
  151. case JackDriverParamUInt:
  152. driver_param->value.ui = strtoul (optarg, NULL, 10);
  153. break;
  154. case JackDriverParamChar:
  155. driver_param->value.c = optarg[0];
  156. break;
  157. case JackDriverParamString:
  158. strncpy (driver_param->value.str, optarg, JACK_DRIVER_PARAM_STRING_MAX);
  159. break;
  160. case JackDriverParamBool:
  161. /*
  162. if (strcasecmp ("false", optarg) == 0 ||
  163. strcasecmp ("off", optarg) == 0 ||
  164. strcasecmp ("no", optarg) == 0 ||
  165. strcasecmp ("0", optarg) == 0 ||
  166. strcasecmp ("(null)", optarg) == 0 ) {
  167. */
  168. // steph
  169. if (strcmp ("false", optarg) == 0 ||
  170. strcmp ("off", optarg) == 0 ||
  171. strcmp ("no", optarg) == 0 ||
  172. strcmp ("0", optarg) == 0 ||
  173. strcmp ("(null)", optarg) == 0 ) {
  174. driver_param->value.i = false;
  175. } else {
  176. driver_param->value.i = true;
  177. }
  178. break;
  179. }
  180. } else {
  181. if (desc->params[param_index].type == JackDriverParamBool) {
  182. driver_param->value.i = true;
  183. } else {
  184. driver_param->value = desc->params[param_index].value;
  185. }
  186. }
  187. params = jack_slist_append (params, driver_param);
  188. }
  189. free (options);
  190. free (long_options);
  191. if (param_ptr)
  192. *param_ptr = params;
  193. return 0;
  194. }
  195. EXPORT int
  196. jackctl_parse_driver_params (jackctl_driver *driver_ptr, int argc, char* argv[])
  197. {
  198. struct option * long_options;
  199. char * options, * options_ptr;
  200. unsigned long i;
  201. int opt;
  202. JSList * node_ptr;
  203. jackctl_parameter_t * param = NULL;
  204. union jackctl_parameter_value value;
  205. if (argc <= 1)
  206. return 0;
  207. const JSList * driver_params = jackctl_driver_get_parameters(driver_ptr);
  208. if (driver_params == NULL)
  209. return 1;
  210. jack_driver_desc_t * desc = jackctl_driver_get_desc(driver_ptr);
  211. /* check for help */
  212. if (strcmp (argv[1], "-h") == 0 || strcmp (argv[1], "--help") == 0) {
  213. if (argc > 2) {
  214. for (i = 0; i < desc->nparams; i++) {
  215. if (strcmp (desc->params[i].name, argv[2]) == 0) {
  216. jack_print_driver_param_usage (desc, i, stdout);
  217. return 1;
  218. }
  219. }
  220. fprintf (stderr, "jackd: unknown option '%s' "
  221. "for driver '%s'\n", argv[2],
  222. desc->name);
  223. }
  224. printf ("Parameters for driver '%s' (all parameters are optional):\n", desc->name);
  225. jack_print_driver_options (desc, stdout);
  226. return 1;
  227. }
  228. /* set up the stuff for getopt */
  229. options = (char*)calloc (desc->nparams * 3 + 1, sizeof (char));
  230. long_options = (option*)calloc (desc->nparams + 1, sizeof (struct option));
  231. options_ptr = options;
  232. for (i = 0; i < desc->nparams; i++) {
  233. sprintf (options_ptr, "%c::", desc->params[i].character);
  234. options_ptr += 3;
  235. long_options[i].name = desc->params[i].name;
  236. long_options[i].flag = NULL;
  237. long_options[i].val = desc->params[i].character;
  238. long_options[i].has_arg = optional_argument;
  239. }
  240. /* create the params */
  241. optind = 0;
  242. opterr = 0;
  243. while ((opt = getopt_long(argc, argv, options, long_options, NULL)) != -1) {
  244. if (opt == ':' || opt == '?') {
  245. if (opt == ':') {
  246. fprintf (stderr, "Missing option to argument '%c'\n", optopt);
  247. } else {
  248. fprintf (stderr, "Unknownage with option '%c'\n", optopt);
  249. }
  250. fprintf (stderr, "Options for driver '%s':\n", desc->name);
  251. jack_print_driver_options(desc, stderr);
  252. return 1;
  253. }
  254. node_ptr = (JSList *)driver_params;
  255. while (node_ptr) {
  256. param = (jackctl_parameter_t*)node_ptr->data;
  257. if (opt == jackctl_parameter_get_id(param)) {
  258. break;
  259. }
  260. node_ptr = node_ptr->next;
  261. }
  262. if (!optarg && optind < argc &&
  263. strlen(argv[optind]) &&
  264. argv[optind][0] != '-') {
  265. optarg = argv[optind];
  266. }
  267. if (optarg) {
  268. switch (jackctl_parameter_get_type(param)) {
  269. case JackDriverParamInt:
  270. value.i = atoi (optarg);
  271. jackctl_parameter_set_value(param, &value);
  272. break;
  273. case JackDriverParamUInt:
  274. value.ui = strtoul (optarg, NULL, 10);
  275. jackctl_parameter_set_value(param, &value);
  276. break;
  277. case JackDriverParamChar:
  278. value.c = optarg[0];
  279. jackctl_parameter_set_value(param, &value);
  280. break;
  281. case JackDriverParamString:
  282. strncpy (value.str, optarg, JACK_DRIVER_PARAM_STRING_MAX);
  283. jackctl_parameter_set_value(param, &value);
  284. break;
  285. case JackDriverParamBool:
  286. /*
  287. if (strcasecmp ("false", optarg) == 0 ||
  288. strcasecmp ("off", optarg) == 0 ||
  289. strcasecmp ("no", optarg) == 0 ||
  290. strcasecmp ("0", optarg) == 0 ||
  291. strcasecmp ("(null)", optarg) == 0 ) {
  292. */
  293. // steph
  294. if (strcmp ("false", optarg) == 0 ||
  295. strcmp ("off", optarg) == 0 ||
  296. strcmp ("no", optarg) == 0 ||
  297. strcmp ("0", optarg) == 0 ||
  298. strcmp ("(null)", optarg) == 0 ) {
  299. value.i = false;
  300. } else {
  301. value.i = true;
  302. }
  303. jackctl_parameter_set_value(param, &value);
  304. break;
  305. }
  306. } else {
  307. if (jackctl_parameter_get_type(param) == JackParamBool) {
  308. value.i = true;
  309. } else {
  310. value = jackctl_parameter_get_default_value(param);
  311. }
  312. jackctl_parameter_set_value(param, &value);
  313. }
  314. }
  315. free(options);
  316. free(long_options);
  317. return 0;
  318. }
  319. jack_driver_desc_t *
  320. jack_find_driver_descriptor (JSList * drivers, const char * name)
  321. {
  322. jack_driver_desc_t * desc = 0;
  323. JSList * node;
  324. for (node = drivers; node; node = jack_slist_next (node)) {
  325. desc = (jack_driver_desc_t *) node->data;
  326. if (strcmp (desc->name, name) != 0) {
  327. desc = NULL;
  328. } else {
  329. break;
  330. }
  331. }
  332. return desc;
  333. }
  334. jack_driver_desc_t *
  335. jack_drivers_get_descriptor (JSList * drivers, const char * sofile)
  336. {
  337. jack_driver_desc_t * descriptor, * other_descriptor;
  338. JackDriverDescFunction so_get_descriptor = NULL;
  339. JSList * node;
  340. void * dlhandle;
  341. char * filename;
  342. #ifdef WIN32
  343. int dlerr;
  344. #else
  345. const char * dlerr;
  346. #endif
  347. int err;
  348. const char* driver_dir;
  349. if ((driver_dir = getenv("JACK_DRIVER_DIR")) == 0) {
  350. // for WIN32 ADDON_DIR is defined in JackConstants.h as relative path
  351. // for posix systems, it is absolute path of default driver dir
  352. #ifdef WIN32
  353. char temp_driver_dir1[512];
  354. char temp_driver_dir2[512];
  355. GetCurrentDirectory(512, temp_driver_dir1);
  356. sprintf(temp_driver_dir2, "%s/%s", temp_driver_dir1, ADDON_DIR);
  357. driver_dir = temp_driver_dir2;
  358. #else
  359. driver_dir = ADDON_DIR;
  360. #endif
  361. }
  362. filename = (char *)malloc(strlen (driver_dir) + 1 + strlen(sofile) + 1);
  363. sprintf (filename, "%s/%s", driver_dir, sofile);
  364. if ((dlhandle = LoadDriverModule(filename)) == NULL) {
  365. #ifdef WIN32
  366. jack_error ("could not open driver .dll '%s': %ld\n", filename, GetLastError());
  367. #else
  368. jack_error ("could not open driver .so '%s': %s\n", filename, dlerror());
  369. #endif
  370. free(filename);
  371. return NULL;
  372. }
  373. so_get_descriptor = (JackDriverDescFunction)
  374. GetProc(dlhandle, "driver_get_descriptor");
  375. #ifdef WIN32
  376. if ((so_get_descriptor == NULL) && (dlerr = GetLastError()) != 0) {
  377. jack_log("Jack::jack_drivers_get_descriptor : dll is not a driver, error ld", dlerr);
  378. #else
  379. if ((so_get_descriptor == NULL) && (dlerr = dlerror ()) != NULL) {
  380. jack_error("%s", dlerr);
  381. #endif
  382. UnloadDriverModule(dlhandle);
  383. free(filename);
  384. return NULL;
  385. }
  386. if ((descriptor = so_get_descriptor ()) == NULL) {
  387. jack_error("driver from '%s' returned NULL descriptor\n", filename);
  388. UnloadDriverModule(dlhandle);
  389. free(filename);
  390. return NULL;
  391. }
  392. #ifdef WIN32
  393. if ((err = UnloadDriverModule(dlhandle)) == 0) {
  394. jack_error ("error closing driver .so '%s': %ld\n", filename, GetLastError ());
  395. }
  396. #else
  397. if ((err = UnloadDriverModule(dlhandle)) != 0) {
  398. jack_error ("error closing driver .so '%s': %s\n", filename, dlerror ());
  399. }
  400. #endif
  401. /* check it doesn't exist already */
  402. for (node = drivers; node; node = jack_slist_next (node)) {
  403. other_descriptor = (jack_driver_desc_t *) node->data;
  404. if (strcmp(descriptor->name, other_descriptor->name) == 0) {
  405. jack_error("the drivers in '%s' and '%s' both have the name '%s'; using the first\n",
  406. other_descriptor->file, filename, other_descriptor->name);
  407. /* FIXME: delete the descriptor */
  408. free(filename);
  409. return NULL;
  410. }
  411. }
  412. strncpy(descriptor->file, filename, PATH_MAX);
  413. free(filename);
  414. return descriptor;
  415. }
  416. #ifdef WIN32
  417. JSList *
  418. jack_drivers_load (JSList * drivers) {
  419. char * driver_dir;
  420. char driver_dir_storage[512];
  421. char dll_filename[512];
  422. WIN32_FIND_DATA filedata;
  423. HANDLE file;
  424. const char * ptr = NULL;
  425. JSList * driver_list = NULL;
  426. jack_driver_desc_t * desc;
  427. if ((driver_dir = getenv("JACK_DRIVER_DIR")) == 0) {
  428. // for WIN32 ADDON_DIR is defined in JackConstants.h as relative path
  429. GetCurrentDirectory(512, driver_dir_storage);
  430. strcat(driver_dir_storage, "/");
  431. strcat(driver_dir_storage, ADDON_DIR);
  432. driver_dir = driver_dir_storage;
  433. }
  434. sprintf(dll_filename, "%s/*.dll", driver_dir);
  435. file = (HANDLE )FindFirstFile(dll_filename, &filedata);
  436. if (file == INVALID_HANDLE_VALUE) {
  437. jack_error("error");
  438. return NULL;
  439. }
  440. do {
  441. ptr = strrchr (filedata.cFileName, '.');
  442. if (!ptr) {
  443. continue;
  444. }
  445. ptr++;
  446. if (strncmp ("dll", ptr, 3) != 0) {
  447. continue;
  448. }
  449. desc = jack_drivers_get_descriptor (drivers, filedata.cFileName);
  450. if (desc) {
  451. driver_list = jack_slist_append (driver_list, desc);
  452. }
  453. } while (FindNextFile(file, &filedata));
  454. if (!driver_list) {
  455. jack_error ("could not find any drivers in %s!\n", driver_dir);
  456. return NULL;
  457. }
  458. return driver_list;
  459. }
  460. #else
  461. JSList *
  462. jack_drivers_load (JSList * drivers) {
  463. struct dirent * dir_entry;
  464. DIR * dir_stream;
  465. const char * ptr;
  466. int err;
  467. JSList * driver_list = NULL;
  468. jack_driver_desc_t * desc;
  469. const char* driver_dir;
  470. if ((driver_dir = getenv("JACK_DRIVER_DIR")) == 0) {
  471. driver_dir = ADDON_DIR;
  472. }
  473. /* search through the driver_dir and add get descriptors
  474. from the .so files in it */
  475. dir_stream = opendir (driver_dir);
  476. if (!dir_stream) {
  477. jack_error ("could not open driver directory %s: %s\n",
  478. driver_dir, strerror (errno));
  479. return NULL;
  480. }
  481. while ((dir_entry = readdir(dir_stream))) {
  482. /* check the filename is of the right format */
  483. if (strncmp ("jack_", dir_entry->d_name, 5) != 0) {
  484. continue;
  485. }
  486. ptr = strrchr (dir_entry->d_name, '.');
  487. if (!ptr) {
  488. continue;
  489. }
  490. ptr++;
  491. if (strncmp ("so", ptr, 2) != 0) {
  492. continue;
  493. }
  494. desc = jack_drivers_get_descriptor (drivers, dir_entry->d_name);
  495. if (desc) {
  496. driver_list = jack_slist_append (driver_list, desc);
  497. }
  498. }
  499. err = closedir (dir_stream);
  500. if (err) {
  501. jack_error ("error closing driver directory %s: %s\n",
  502. driver_dir, strerror (errno));
  503. }
  504. if (!driver_list) {
  505. jack_error ("could not find any drivers in %s!\n", driver_dir);
  506. return NULL;
  507. }
  508. return driver_list;
  509. }
  510. #endif
  511. jack_driver_info_t *
  512. jack_load_driver (jack_driver_desc_t * driver_desc) {
  513. #ifdef WIN32
  514. int errstr;
  515. #else
  516. const char * errstr;
  517. #endif
  518. jack_driver_info_t *info;
  519. info = (jack_driver_info_t *) calloc (1, sizeof (*info));
  520. info->handle = LoadDriverModule (driver_desc->file);
  521. if (info->handle == NULL) {
  522. #ifdef WIN32
  523. if ((errstr = GetLastError ()) != 0) {
  524. jack_error ("can't load \"%s\": %ld", driver_desc->file,
  525. errstr);
  526. #else
  527. if ((errstr = dlerror ()) != 0) {
  528. jack_error ("can't load \"%s\": %s", driver_desc->file,
  529. errstr);
  530. #endif
  531. } else {
  532. jack_error ("bizarre error loading driver shared "
  533. "object %s", driver_desc->file);
  534. }
  535. goto fail;
  536. }
  537. info->initialize = (initialize)GetProc(info->handle, "driver_initialize");
  538. #ifdef WIN32
  539. if ((info->initialize == NULL) && (errstr = GetLastError ()) != 0) {
  540. #else
  541. if ((info->initialize == NULL) && (errstr = dlerror ()) != 0) {
  542. #endif
  543. jack_error ("no initialize function in shared object %s\n",
  544. driver_desc->file);
  545. goto fail;
  546. }
  547. return info;
  548. fail:
  549. if (info->handle) {
  550. UnloadDriverModule(info->handle);
  551. }
  552. free (info);
  553. return NULL;
  554. }