21 #include "JackSystemDeps.h"
22 #include "JackDriverLoader.h"
23 #include "JackDriverInfo.h"
24 #include "JackConstants.h"
25 #include "JackError.h"
37 static char* locate_dll_driver_dir()
40 HMODULE libjack_handle = LoadLibrary(
"libjackserver64.dll");
42 HMODULE libjack_handle = LoadLibrary(
"libjackserver.dll");
46 char driver_dir_storage[512];
47 if (3 < GetModuleFileName(libjack_handle, driver_dir_storage, 512)) {
48 char *p = strrchr(driver_dir_storage,
'\\');
49 if (p && (p != driver_dir_storage)) {
52 jack_info(
"Drivers/internals found in : %s", driver_dir_storage);
53 strcat(driver_dir_storage,
"/");
54 strcat(driver_dir_storage, ADDON_DIR);
55 FreeLibrary(libjack_handle);
56 return strdup(driver_dir_storage);
58 jack_error(
"Cannot get JACK dll directory : %d", GetLastError());
59 FreeLibrary(libjack_handle);
64 static char* locate_driver_dir(HANDLE& file, WIN32_FIND_DATA& filedata)
67 char* driver_dir = locate_dll_driver_dir();
68 char dll_filename[512];
69 snprintf(dll_filename,
sizeof(dll_filename),
"%s/*.dll", driver_dir);
70 file = (HANDLE)FindFirstFile(dll_filename, &filedata);
72 if (file == INVALID_HANDLE_VALUE) {
88 char arg_default[JACK_DRIVER_PARAM_STRING_MAX + 1];
90 for (i = 0; i < desc->
nparams; i++) {
92 case JackDriverParamInt:
93 sprintf (arg_default,
"%" "i", desc->
params[i].
value.i);
95 case JackDriverParamUInt:
96 sprintf (arg_default,
"%" "u", desc->
params[i].
value.ui);
98 case JackDriverParamChar:
99 sprintf (arg_default,
"%c", desc->
params[i].
value.c);
101 case JackDriverParamString:
103 sprintf (arg_default,
"%s", desc->
params[i].
value.str);
105 sprintf (arg_default,
"none");
108 case JackDriverParamBool:
109 sprintf (arg_default,
"%s", desc->
params[i].
value.i ?
"true" :
"false");
113 fprintf(file,
"\t-%c, --%s \t%s (default: %s)\n",
121 static void jack_print_driver_param_usage (
jack_driver_desc_t* desc,
unsigned long param, FILE *file)
123 fprintf (file,
"Usage information for the '%s' parameter for driver '%s':\n",
128 void jack_free_driver_params(
JSList * driver_params)
130 JSList*node_ptr = driver_params;
134 next_node_ptr = node_ptr->next;
135 free(node_ptr->data);
137 node_ptr = next_node_ptr;
143 struct option * long_options;
144 char* options, * options_ptr;
147 unsigned int param_index;
157 if (strcmp (argv[1],
"-h") == 0 || strcmp (argv[1],
"--help") == 0) {
159 for (i = 0; i < desc->
nparams; i++) {
160 if (strcmp (desc->
params[i].
name, argv[2]) == 0) {
161 jack_print_driver_param_usage (desc, i, stdout);
166 fprintf (stderr,
"Jackd: unknown option '%s' "
167 "for driver '%s'\n", argv[2],
171 jack_log(
"Parameters for driver '%s' (all parameters are optional):", desc->
name);
172 jack_print_driver_options (desc, stdout);
177 options = (
char*)calloc (desc->
nparams * 3 + 1, sizeof (
char));
180 options_ptr = options;
181 for (i = 0; i < desc->
nparams; i++) {
185 long_options[i].flag = NULL;
187 long_options[i].has_arg = optional_argument;
193 while ((opt = getopt_long(argc, argv, options, long_options, NULL)) != -1) {
195 if (opt ==
':' || opt ==
'?') {
197 fprintf (stderr,
"Missing option to argument '%c'\n", optopt);
199 fprintf (stderr,
"Unknownage with option '%c'\n", optopt);
202 fprintf (stderr,
"Options for driver '%s':\n", desc->
name);
203 jack_print_driver_options (desc, stderr);
207 for (param_index = 0; param_index < desc->
nparams; param_index++) {
216 if (!optarg && optind < argc &&
217 strlen(argv[optind]) &&
218 argv[optind][0] !=
'-') {
219 optarg = argv[optind];
224 case JackDriverParamInt:
225 driver_param->value.i = atoi(optarg);
227 case JackDriverParamUInt:
228 driver_param->value.ui = strtoul(optarg, NULL, 10);
230 case JackDriverParamChar:
231 driver_param->value.c = optarg[0];
233 case JackDriverParamString:
234 strncpy (driver_param->value.str, optarg, JACK_DRIVER_PARAM_STRING_MAX);
236 case JackDriverParamBool:
237 if (strcasecmp(
"false", optarg) == 0 ||
238 strcasecmp(
"off", optarg) == 0 ||
239 strcasecmp(
"no", optarg) == 0 ||
240 strcasecmp(
"0", optarg) == 0 ||
241 strcasecmp(
"(null)", optarg) == 0 ) {
242 driver_param->value.i =
false;
244 driver_param->value.i =
true;
249 if (desc->
params[param_index].
type == JackDriverParamBool) {
250 driver_param->value.i =
true;
252 driver_param->value = desc->
params[param_index].
value;
256 params = jack_slist_append (params, driver_param);
270 struct option* long_options;
271 char* options, * options_ptr;
283 if (driver_params == NULL) {
290 if (strcmp (argv[1],
"-h") == 0 || strcmp (argv[1],
"--help") == 0) {
292 for (i = 0; i < desc->
nparams; i++) {
293 if (strcmp (desc->
params[i].
name, argv[2]) == 0) {
294 jack_print_driver_param_usage (desc, i, stdout);
299 fprintf (stderr,
"Jackd: unknown option '%s' "
300 "for driver '%s'\n", argv[2],
304 jack_log(
"Parameters for driver '%s' (all parameters are optional):", desc->
name);
305 jack_print_driver_options (desc, stdout);
310 options = (
char*)calloc (desc->
nparams * 3 + 1, sizeof (
char));
313 options_ptr = options;
314 for (i = 0; i < desc->
nparams; i++) {
318 long_options[
i].flag = NULL;
320 long_options[
i].has_arg = optional_argument;
326 while ((opt = getopt_long(argc, argv, options, long_options, NULL)) != -1) {
328 if (opt ==
':' || opt ==
'?') {
330 fprintf (stderr,
"Missing option to argument '%c'\n", optopt);
332 fprintf (stderr,
"Unknownage with option '%c'\n", optopt);
335 fprintf (stderr,
"Options for driver '%s':\n", desc->
name);
336 jack_print_driver_options(desc, stderr);
340 node_ptr = (
JSList *)driver_params;
346 node_ptr = node_ptr->next;
349 if (!optarg && optind < argc &&
350 strlen(argv[optind]) &&
351 argv[optind][0] !=
'-') {
352 optarg = argv[optind];
357 case JackDriverParamInt:
358 value.
i = atoi(optarg);
361 case JackDriverParamUInt:
362 value.
ui = strtoul(optarg, NULL, 10);
365 case JackDriverParamChar:
369 case JackDriverParamString:
370 strncpy(value.
str, optarg, JACK_DRIVER_PARAM_STRING_MAX);
373 case JackDriverParamBool:
374 if (strcasecmp(
"false", optarg) == 0 ||
375 strcasecmp(
"off", optarg) == 0 ||
376 strcasecmp(
"no", optarg) == 0 ||
377 strcasecmp(
"0", optarg) == 0 ||
378 strcasecmp(
"(null)", optarg) == 0 ) {
406 for (node = drivers; node; node = jack_slist_next (node)) {
409 if (strcmp (desc->
name, name) != 0) {
419 static void* check_symbol(
const char* sofile,
const char* symbol,
const char* driver_dir,
void** res_dllhandle = NULL)
424 sprintf(filename,
"%s/%s", driver_dir, sofile);
426 if ((dlhandle = LoadDriverModule(filename)) == NULL) {
428 jack_error (
"Could not open component .dll '%s': %ld", filename, GetLastError());
430 jack_error (
"Could not open component .so '%s': %s", filename, dlerror());
433 res = (
void*)GetDriverProc(dlhandle, symbol);
435 *res_dllhandle = dlhandle;
437 UnloadDriverModule(dlhandle);
444 static jack_driver_desc_t* jack_get_descriptor (
JSList* drivers,
const char* sofile,
const char* symbol,
const char* driver_dir)
448 JackDriverDescFunction so_get_descriptor = NULL;
451 void* dlhandle = NULL;
453 sprintf(filename,
"%s/%s", driver_dir, sofile);
454 so_get_descriptor = (JackDriverDescFunction)check_symbol(sofile, symbol, driver_dir, &dlhandle);
456 if (so_get_descriptor == NULL) {
457 jack_error(
"jack_get_descriptor : dll %s is not a driver", sofile);
461 if ((descriptor = so_get_descriptor ()) == NULL) {
462 jack_error(
"Driver from '%s' returned NULL descriptor", filename);
467 for (node = drivers; node; node = jack_slist_next (node)) {
469 if (strcmp(descriptor->
name, other_descriptor->
name) == 0) {
470 jack_error(
"The drivers in '%s' and '%s' both have the name '%s'; using the first",
471 other_descriptor->
file, filename, other_descriptor->
name);
477 strncpy(descriptor->
file, filename, JACK_PATH_MAX);
481 UnloadDriverModule(dlhandle);
491 WIN32_FIND_DATA filedata;
493 const char* ptr = NULL;
494 JSList* driver_list = NULL;
497 char* driver_dir = locate_driver_dir(file, filedata);
505 if (strncmp (
"jack_", filedata.cFileName, 5) != 0) {
509 ptr = strrchr (filedata.cFileName,
'.');
515 if (strncmp (
"dll", ptr, 3) != 0) {
520 if (check_symbol(filedata.cFileName,
"jack_internal_initialize", driver_dir) != NULL) {
524 desc = jack_get_descriptor (drivers, filedata.cFileName,
"driver_get_descriptor", driver_dir);
526 driver_list = jack_slist_append (driver_list, desc);
528 jack_error (
"jack_get_descriptor returns null for \'%s\'", filedata.cFileName);
531 }
while (FindNextFile(file, &filedata));
534 jack_error (
"Could not find any drivers in %s!", driver_dir);
549 struct dirent * dir_entry;
553 JSList* driver_list = NULL;
556 const char* driver_dir;
557 if ((driver_dir = getenv(
"JACK_DRIVER_DIR")) == 0) {
558 driver_dir = ADDON_DIR;
563 dir_stream = opendir (driver_dir);
565 jack_error (
"Could not open driver directory %s: %s",
566 driver_dir, strerror (errno));
570 while ((dir_entry = readdir(dir_stream))) {
573 if (strncmp (
"jack_", dir_entry->d_name, 5) != 0) {
577 ptr = strrchr (dir_entry->d_name,
'.');
582 if (strncmp (
"so", ptr, 2) != 0) {
587 if (check_symbol(dir_entry->d_name,
"jack_internal_initialize", driver_dir) != NULL) {
591 desc = jack_get_descriptor (drivers, dir_entry->d_name,
"driver_get_descriptor", driver_dir);
593 driver_list = jack_slist_append (driver_list, desc);
595 jack_error (
"jack_get_descriptor returns null for \'%s\'", dir_entry->d_name);
599 err = closedir (dir_stream);
601 jack_error (
"Error closing driver directory %s: %s",
602 driver_dir, strerror (errno));
606 jack_error (
"Could not find any drivers in %s!", driver_dir);
620 WIN32_FIND_DATA filedata;
622 const char* ptr = NULL;
623 JSList* driver_list = NULL;
626 char* driver_dir = locate_driver_dir(file, filedata);
634 ptr = strrchr (filedata.cFileName,
'.');
640 if (strncmp (
"dll", ptr, 3) != 0) {
645 if (check_symbol(filedata.cFileName,
"jack_internal_initialize", driver_dir) == NULL) {
649 desc = jack_get_descriptor (internals, filedata.cFileName,
"jack_get_descriptor", driver_dir);
651 driver_list = jack_slist_append (driver_list, desc);
653 jack_error (
"jack_get_descriptor returns null for \'%s\'", filedata.cFileName);
656 }
while (FindNextFile(file, &filedata));
659 jack_error (
"Could not find any internals in %s!", driver_dir);
674 struct dirent * dir_entry;
678 JSList* driver_list = NULL;
681 const char* driver_dir;
682 if ((driver_dir = getenv(
"JACK_DRIVER_DIR")) == 0) {
683 driver_dir = ADDON_DIR;
688 dir_stream = opendir (driver_dir);
690 jack_error (
"Could not open driver directory %s: %s\n",
691 driver_dir, strerror (errno));
695 while ((dir_entry = readdir(dir_stream))) {
697 ptr = strrchr (dir_entry->d_name,
'.');
703 if (strncmp (
"so", ptr, 2) != 0) {
708 if (check_symbol(dir_entry->d_name,
"jack_internal_initialize", driver_dir) == NULL) {
712 desc = jack_get_descriptor (internals, dir_entry->d_name,
"jack_get_descriptor", driver_dir);
714 driver_list = jack_slist_append (driver_list, desc);
716 jack_error (
"jack_get_descriptor returns null for \'%s\'", dir_entry->d_name);
720 err = closedir (dir_stream);
722 jack_error (
"Error closing internal directory %s: %s\n",
723 driver_dir, strerror (errno));
727 jack_error (
"Could not find any internals in %s!", driver_dir);
747 fHandle = LoadDriverModule (driver_desc->
file);
749 if (fHandle == NULL) {
751 if ((errstr = GetLastError ()) != 0) {
754 if ((errstr = dlerror ()) != 0) {
759 jack_error (
"Error loading driver shared object %s", driver_desc->
file);
764 fInitialize = (driverInitialize)GetDriverProc(fHandle,
"driver_initialize");
767 if ((fInitialize == NULL) && (errstr = GetLastError ()) != 0) {
769 if ((fInitialize == NULL) && (errstr = dlerror ()) != 0) {
771 jack_error(
"No initialize function in shared object %s\n", driver_desc->
file);
775 fBackend = fInitialize(engine, synchro, params);
779 JackDriverInfo::~JackDriverInfo()
783 UnloadDriverModule(fHandle);
789 jack_driver_type_t type,
790 const char * description,
794 size_t description_len;
797 name_len = strlen(name);
798 description_len = strlen(description);
800 if (name_len >
sizeof(desc_ptr->
name) - 1 ||
801 description_len >
sizeof(desc_ptr->
desc) - 1) {
807 if (desc_ptr == NULL) {
808 jack_error(
"Error calloc() failed to allocate memory for driver descriptor struct");
812 memcpy(desc_ptr->
name, name, name_len + 1);
813 memcpy(desc_ptr->
desc, description, description_len + 1);
816 desc_ptr->
type = type;
818 if (filler_ptr != NULL) {
819 filler_ptr->size = 0;
825 SERVER_EXPORT
int jack_driver_descriptor_add_parameter(
830 jack_driver_param_type_t type,
833 const char* short_desc,
834 const char* long_desc)
837 size_t short_desc_len;
838 size_t long_desc_len;
842 name_len = strlen(name);
843 short_desc_len = strlen(short_desc);
845 if (long_desc != NULL) {
846 long_desc_len = strlen(long_desc);
848 long_desc = short_desc;
849 long_desc_len = short_desc_len;
852 if (name_len >
sizeof(param_ptr->
name) - 1 ||
853 short_desc_len >
sizeof(param_ptr->
short_desc) - 1 ||
854 long_desc_len >
sizeof(param_ptr->
long_desc) - 1) {
859 if (desc_ptr->
nparams == filler_ptr->size) {
860 newsize = filler_ptr->size + 20;
862 if (param_ptr == NULL) {
863 jack_error(
"Error realloc() failed for parameter array of %zu elements", newsize);
866 filler_ptr->size = newsize;
867 desc_ptr->
params = param_ptr;
870 assert(desc_ptr->
nparams < filler_ptr->size);
873 memcpy(param_ptr->
name, name, name_len + 1);
875 param_ptr->
type = type;
876 param_ptr->
value = *value_ptr;
878 memcpy(param_ptr->
short_desc, short_desc, short_desc_len + 1);
879 memcpy(param_ptr->
long_desc, long_desc, long_desc_len + 1);
887 jack_constraint_add_enum(
889 uint32_t * array_size_ptr,
891 const char * short_desc)
898 len = strlen(short_desc) + 1;
899 if (len >
sizeof(possible_value_ptr->
short_desc))
905 constraint_ptr = *constraint_ptr_ptr;
906 if (constraint_ptr == NULL)
909 if (constraint_ptr == NULL)
911 jack_error(
"calloc() failed to allocate memory for param constraint struct");
919 array_size = *array_size_ptr;
922 if (constraint_ptr->constraint.
enumeration.count == array_size)
927 constraint_ptr->constraint.
enumeration.possible_values_array,
929 if (possible_value_ptr == NULL)
931 jack_error(
"calloc() failed to (re)allocate memory for possible values array");
934 constraint_ptr->constraint.
enumeration.possible_values_array = possible_value_ptr;
938 possible_value_ptr = constraint_ptr->constraint.
enumeration.possible_values_array;
941 possible_value_ptr += constraint_ptr->constraint.
enumeration.count;
944 possible_value_ptr->value = *value_ptr;
945 memcpy(possible_value_ptr->
short_desc, short_desc, len);
947 *constraint_ptr_ptr = constraint_ptr;
948 *array_size_ptr = array_size;
955 jack_constraint_free(
958 if (constraint_ptr != NULL)
960 if ((constraint_ptr->
flags & JACK_CONSTRAINT_FLAG_RANGE) == 0)
962 free(constraint_ptr->constraint.
enumeration.possible_values_array);
965 free(constraint_ptr);
969 #define JACK_CONSTRAINT_COMPOSE_ENUM_IMPL(type, copy) \
970 JACK_CONSTRAINT_COMPOSE_ENUM(type) \
972 jack_driver_param_constraint_desc_t * constraint_ptr; \
973 uint32_t array_size; \
974 jack_driver_param_value_t value; \
975 struct jack_constraint_enum_ ## type ## _descriptor * descr_ptr; \
977 constraint_ptr = NULL; \
978 for (descr_ptr = descr_array_ptr; \
983 if (!jack_constraint_add_enum( \
987 descr_ptr->short_desc)) \
989 jack_constraint_free(constraint_ptr); \
994 constraint_ptr->flags = flags; \
996 return constraint_ptr; \
999 JACK_CONSTRAINT_COMPOSE_ENUM_IMPL(uint32, value.c = descr_ptr->value);
1000 JACK_CONSTRAINT_COMPOSE_ENUM_IMPL(sint32, value.c = descr_ptr->value);
1001 JACK_CONSTRAINT_COMPOSE_ENUM_IMPL(
char, value.c = descr_ptr->value);
1002 JACK_CONSTRAINT_COMPOSE_ENUM_IMPL(str, strcpy(value.str, descr_ptr->value));