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.

665 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. int
  320. jack_parse_internal_client_params(jack_driver_desc_t* desc, int argc, char* argv[], JSList** param_ptr)
  321. {
  322. return 0;
  323. }
  324. jack_driver_desc_t *
  325. jack_find_driver_descriptor (JSList * drivers, const char * name)
  326. {
  327. jack_driver_desc_t * desc = 0;
  328. JSList * node;
  329. for (node = drivers; node; node = jack_slist_next (node)) {
  330. desc = (jack_driver_desc_t *) node->data;
  331. if (strcmp (desc->name, name) != 0) {
  332. desc = NULL;
  333. } else {
  334. break;
  335. }
  336. }
  337. return desc;
  338. }
  339. jack_driver_desc_t *
  340. jack_drivers_get_descriptor (JSList * drivers, const char * sofile)
  341. {
  342. jack_driver_desc_t * descriptor, * other_descriptor;
  343. JackDriverDescFunction so_get_descriptor = NULL;
  344. JSList * node;
  345. void * dlhandle;
  346. char * filename;
  347. #ifdef WIN32
  348. int dlerr;
  349. #else
  350. const char * dlerr;
  351. #endif
  352. int err;
  353. const char* driver_dir;
  354. if ((driver_dir = getenv("JACK_DRIVER_DIR")) == 0) {
  355. // for WIN32 ADDON_DIR is defined in JackConstants.h as relative path
  356. // for posix systems, it is absolute path of default driver dir
  357. #ifdef WIN32
  358. char temp_driver_dir1[512];
  359. char temp_driver_dir2[512];
  360. GetCurrentDirectory(512, temp_driver_dir1);
  361. sprintf(temp_driver_dir2, "%s/%s", temp_driver_dir1, ADDON_DIR);
  362. driver_dir = temp_driver_dir2;
  363. #else
  364. driver_dir = ADDON_DIR;
  365. #endif
  366. }
  367. filename = (char *)malloc(strlen (driver_dir) + 1 + strlen(sofile) + 1);
  368. sprintf (filename, "%s/%s", driver_dir, sofile);
  369. if ((dlhandle = LoadDriverModule(filename)) == NULL) {
  370. #ifdef WIN32
  371. jack_error ("could not open driver .dll '%s': %ld\n", filename, GetLastError());
  372. #else
  373. jack_error ("could not open driver .so '%s': %s\n", filename, dlerror());
  374. #endif
  375. free(filename);
  376. return NULL;
  377. }
  378. so_get_descriptor = (JackDriverDescFunction)
  379. GetProc(dlhandle, "driver_get_descriptor");
  380. #ifdef WIN32
  381. if ((so_get_descriptor == NULL) && (dlerr = GetLastError()) != 0) {
  382. jack_log("Jack::jack_drivers_get_descriptor : dll is not a driver, error ld", dlerr);
  383. #else
  384. if ((so_get_descriptor == NULL) && (dlerr = dlerror ()) != NULL) {
  385. jack_error("%s", dlerr);
  386. #endif
  387. UnloadDriverModule(dlhandle);
  388. free(filename);
  389. return NULL;
  390. }
  391. if ((descriptor = so_get_descriptor ()) == NULL) {
  392. jack_error("driver from '%s' returned NULL descriptor\n", filename);
  393. UnloadDriverModule(dlhandle);
  394. free(filename);
  395. return NULL;
  396. }
  397. #ifdef WIN32
  398. if ((err = UnloadDriverModule(dlhandle)) == 0) {
  399. jack_error ("error closing driver .so '%s': %ld\n", filename, GetLastError ());
  400. }
  401. #else
  402. if ((err = UnloadDriverModule(dlhandle)) != 0) {
  403. jack_error ("error closing driver .so '%s': %s\n", filename, dlerror ());
  404. }
  405. #endif
  406. /* check it doesn't exist already */
  407. for (node = drivers; node; node = jack_slist_next (node)) {
  408. other_descriptor = (jack_driver_desc_t *) node->data;
  409. if (strcmp(descriptor->name, other_descriptor->name) == 0) {
  410. jack_error("the drivers in '%s' and '%s' both have the name '%s'; using the first\n",
  411. other_descriptor->file, filename, other_descriptor->name);
  412. /* FIXME: delete the descriptor */
  413. free(filename);
  414. return NULL;
  415. }
  416. }
  417. strncpy(descriptor->file, filename, PATH_MAX);
  418. free(filename);
  419. return descriptor;
  420. }
  421. #ifdef WIN32
  422. JSList *
  423. jack_drivers_load (JSList * drivers) {
  424. char * driver_dir;
  425. char driver_dir_storage[512];
  426. char dll_filename[512];
  427. WIN32_FIND_DATA filedata;
  428. HANDLE file;
  429. const char * ptr = NULL;
  430. JSList * driver_list = NULL;
  431. jack_driver_desc_t * desc;
  432. if ((driver_dir = getenv("JACK_DRIVER_DIR")) == 0) {
  433. // for WIN32 ADDON_DIR is defined in JackConstants.h as relative path
  434. GetCurrentDirectory(512, driver_dir_storage);
  435. strcat(driver_dir_storage, "/");
  436. strcat(driver_dir_storage, ADDON_DIR);
  437. driver_dir = driver_dir_storage;
  438. }
  439. sprintf(dll_filename, "%s/*.dll", driver_dir);
  440. file = (HANDLE )FindFirstFile(dll_filename, &filedata);
  441. if (file == INVALID_HANDLE_VALUE) {
  442. jack_error("error");
  443. return NULL;
  444. }
  445. do {
  446. ptr = strrchr (filedata.cFileName, '.');
  447. if (!ptr) {
  448. continue;
  449. }
  450. ptr++;
  451. if (strncmp ("dll", ptr, 3) != 0) {
  452. continue;
  453. }
  454. desc = jack_drivers_get_descriptor (drivers, filedata.cFileName);
  455. if (desc) {
  456. driver_list = jack_slist_append (driver_list, desc);
  457. }
  458. } while (FindNextFile(file, &filedata));
  459. if (!driver_list) {
  460. jack_error ("could not find any drivers in %s!\n", driver_dir);
  461. return NULL;
  462. }
  463. return driver_list;
  464. }
  465. #else
  466. JSList *
  467. jack_drivers_load (JSList * drivers) {
  468. struct dirent * dir_entry;
  469. DIR * dir_stream;
  470. const char * ptr;
  471. int err;
  472. JSList * driver_list = NULL;
  473. jack_driver_desc_t * desc;
  474. const char* driver_dir;
  475. if ((driver_dir = getenv("JACK_DRIVER_DIR")) == 0) {
  476. driver_dir = ADDON_DIR;
  477. }
  478. /* search through the driver_dir and add get descriptors
  479. from the .so files in it */
  480. dir_stream = opendir (driver_dir);
  481. if (!dir_stream) {
  482. jack_error ("could not open driver directory %s: %s\n",
  483. driver_dir, strerror (errno));
  484. return NULL;
  485. }
  486. while ((dir_entry = readdir(dir_stream))) {
  487. /* check the filename is of the right format */
  488. if (strncmp ("jack_", dir_entry->d_name, 5) != 0) {
  489. continue;
  490. }
  491. ptr = strrchr (dir_entry->d_name, '.');
  492. if (!ptr) {
  493. continue;
  494. }
  495. ptr++;
  496. if (strncmp ("so", ptr, 2) != 0) {
  497. continue;
  498. }
  499. desc = jack_drivers_get_descriptor (drivers, dir_entry->d_name);
  500. if (desc) {
  501. driver_list = jack_slist_append (driver_list, desc);
  502. }
  503. }
  504. err = closedir (dir_stream);
  505. if (err) {
  506. jack_error ("error closing driver directory %s: %s\n",
  507. driver_dir, strerror (errno));
  508. }
  509. if (!driver_list) {
  510. jack_error ("could not find any drivers in %s!\n", driver_dir);
  511. return NULL;
  512. }
  513. return driver_list;
  514. }
  515. #endif
  516. jack_driver_info_t *
  517. jack_load_driver (jack_driver_desc_t * driver_desc) {
  518. #ifdef WIN32
  519. int errstr;
  520. #else
  521. const char * errstr;
  522. #endif
  523. jack_driver_info_t *info;
  524. info = (jack_driver_info_t *) calloc (1, sizeof (*info));
  525. info->handle = LoadDriverModule (driver_desc->file);
  526. if (info->handle == NULL) {
  527. #ifdef WIN32
  528. if ((errstr = GetLastError ()) != 0) {
  529. jack_error ("can't load \"%s\": %ld", driver_desc->file,
  530. errstr);
  531. #else
  532. if ((errstr = dlerror ()) != 0) {
  533. jack_error ("can't load \"%s\": %s", driver_desc->file,
  534. errstr);
  535. #endif
  536. } else {
  537. jack_error ("bizarre error loading driver shared "
  538. "object %s", driver_desc->file);
  539. }
  540. goto fail;
  541. }
  542. info->initialize = (initialize)GetProc(info->handle, "driver_initialize");
  543. #ifdef WIN32
  544. if ((info->initialize == NULL) && (errstr = GetLastError ()) != 0) {
  545. #else
  546. if ((info->initialize == NULL) && (errstr = dlerror ()) != 0) {
  547. #endif
  548. jack_error ("no initialize function in shared object %s\n",
  549. driver_desc->file);
  550. goto fail;
  551. }
  552. return info;
  553. fail:
  554. if (info->handle) {
  555. UnloadDriverModule(info->handle);
  556. }
  557. free (info);
  558. return NULL;
  559. }