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.

383 lines
11KB

  1. /* -*- Mode: C ; c-basic-offset: 4 -*- */
  2. /*
  3. Copyright (C) 2007,2008 Nedko Arnaudov
  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.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  14. */
  15. #if defined(HAVE_CONFIG_H)
  16. #include "config.h"
  17. #endif
  18. #include <stdbool.h>
  19. #include <stdint.h>
  20. #include <string.h>
  21. #include <stdio.h>
  22. #include <dbus/dbus.h>
  23. #include "controller_internal.h"
  24. void
  25. jack_controller_settings_set_bool_option(
  26. const char *value_str,
  27. int *value_ptr)
  28. {
  29. if (strcmp(value_str, "true") == 0)
  30. {
  31. *value_ptr = true;
  32. }
  33. else if (strcmp(value_str, "false") == 0)
  34. {
  35. *value_ptr = false;
  36. }
  37. else
  38. {
  39. jack_error("ignoring unknown bool value \"%s\"", value_str);
  40. }
  41. }
  42. void
  43. jack_controller_settings_set_sint_option(
  44. const char *value_str,
  45. int *value_ptr)
  46. {
  47. *value_ptr = atoi(value_str);
  48. }
  49. void
  50. jack_controller_settings_set_uint_option(
  51. const char *value_str,
  52. unsigned int *value_ptr)
  53. {
  54. *value_ptr = strtoul(value_str, NULL, 10);
  55. }
  56. void
  57. jack_controller_settings_set_char_option(
  58. const char *value_str,
  59. char *value_ptr)
  60. {
  61. if (value_str[0] == 0 || value_str[1] != 0)
  62. {
  63. jack_error("invalid char option value \"%s\"", value_str);
  64. return;
  65. }
  66. *value_ptr = *value_str;
  67. }
  68. void
  69. jack_controller_settings_set_string_option(
  70. const char *value_str,
  71. char *value_ptr,
  72. size_t max_size)
  73. {
  74. size_t size;
  75. size = strlen(value_str);
  76. if (size >= max_size)
  77. {
  78. jack_error("string option value \"%s\" is too long, max is %u chars (including terminating zero)", value_str, (unsigned int)max_size);
  79. return;
  80. }
  81. strcpy(value_ptr, value_str);
  82. }
  83. void
  84. jack_controller_settings_set_driver_option(
  85. jackctl_driver_t *driver,
  86. const char *option_name,
  87. const char *option_value)
  88. {
  89. jackctl_parameter_t *parameter;
  90. jackctl_param_type_t type;
  91. int value_int = 0;
  92. unsigned int value_uint = 0;
  93. union jackctl_parameter_value value;
  94. jack_info("setting driver option \"%s\" to value \"%s\"", option_name, option_value);
  95. parameter = jack_controller_find_parameter(jackctl_driver_get_parameters(driver), option_name);
  96. if (parameter == NULL)
  97. {
  98. jack_error(
  99. "Unknown parameter \"%s\" of driver \"%s\"",
  100. option_name,
  101. jackctl_driver_get_name(driver));
  102. return;
  103. }
  104. type = jackctl_parameter_get_type(parameter);
  105. switch (type)
  106. {
  107. case JackParamInt:
  108. jack_controller_settings_set_sint_option(option_value, &value_int);
  109. value.i = value_int;
  110. break;
  111. case JackParamUInt:
  112. jack_controller_settings_set_uint_option(option_value, &value_uint);
  113. value.ui = value_uint;
  114. break;
  115. case JackParamChar:
  116. jack_controller_settings_set_char_option(option_value, &value.c);
  117. break;
  118. case JackParamString:
  119. jack_controller_settings_set_string_option(option_value, value.str, sizeof(value.str));
  120. break;
  121. case JackParamBool:
  122. jack_controller_settings_set_bool_option(option_value, &value_int);
  123. value.i = value_int;
  124. break;
  125. default:
  126. jack_error("Parameter \"%s\" of driver \"%s\" is of unknown type %d",
  127. jackctl_parameter_get_name(parameter),
  128. jackctl_driver_get_name(driver),
  129. type);
  130. }
  131. jackctl_parameter_set_value(parameter, &value);
  132. }
  133. void
  134. jack_controller_settings_set_internal_option(
  135. jackctl_internal_t *internal,
  136. const char *option_name,
  137. const char *option_value)
  138. {
  139. jackctl_parameter_t *parameter;
  140. jackctl_param_type_t type;
  141. int value_int = 0;
  142. unsigned int value_uint = 0;
  143. union jackctl_parameter_value value;
  144. jack_info("setting internal option \"%s\" to value \"%s\"", option_name, option_value);
  145. parameter = jack_controller_find_parameter(jackctl_internal_get_parameters(internal), option_name);
  146. if (parameter == NULL)
  147. {
  148. jack_error(
  149. "Unknown parameter \"%s\" of internal \"%s\"",
  150. option_name,
  151. jackctl_internal_get_name(internal));
  152. return;
  153. }
  154. type = jackctl_parameter_get_type(parameter);
  155. switch (type)
  156. {
  157. case JackParamInt:
  158. jack_controller_settings_set_sint_option(option_value, &value_int);
  159. value.i = value_int;
  160. break;
  161. case JackParamUInt:
  162. jack_controller_settings_set_uint_option(option_value, &value_uint);
  163. value.ui = value_uint;
  164. break;
  165. case JackParamChar:
  166. jack_controller_settings_set_char_option(option_value, &value.c);
  167. break;
  168. case JackParamString:
  169. jack_controller_settings_set_string_option(option_value, value.str, sizeof(value.str));
  170. break;
  171. case JackParamBool:
  172. jack_controller_settings_set_bool_option(option_value, &value_int);
  173. value.i = value_int;
  174. break;
  175. default:
  176. jack_error("Parameter \"%s\" of internal \"%s\" is of unknown type %d",
  177. jackctl_parameter_get_name(parameter),
  178. jackctl_internal_get_name(internal),
  179. type);
  180. }
  181. jackctl_parameter_set_value(parameter, &value);
  182. }
  183. void
  184. jack_controller_settings_set_engine_option(
  185. struct jack_controller *controller_ptr,
  186. const char *option_name,
  187. const char *option_value)
  188. {
  189. jackctl_parameter_t *parameter;
  190. jackctl_param_type_t type;
  191. int value_int = 0;
  192. unsigned int value_uint = 0;
  193. union jackctl_parameter_value value;
  194. jack_info("setting engine option \"%s\" to value \"%s\"", option_name, option_value);
  195. if (strcmp(option_name, "driver") == 0)
  196. {
  197. if (!jack_controller_select_driver(controller_ptr, option_value))
  198. {
  199. jack_error("unknown driver '%s'", option_value);
  200. }
  201. return;
  202. }
  203. parameter = jack_controller_find_parameter(jackctl_server_get_parameters(controller_ptr->server), option_name);
  204. if (parameter == NULL)
  205. {
  206. jack_error(
  207. "Unknown engine parameter \"%s\"",
  208. option_name);
  209. return;
  210. }
  211. type = jackctl_parameter_get_type(parameter);
  212. switch (type)
  213. {
  214. case JackParamInt:
  215. jack_controller_settings_set_sint_option(option_value, &value_int);
  216. value.i = value_int;
  217. break;
  218. case JackParamUInt:
  219. jack_controller_settings_set_uint_option(option_value, &value_uint);
  220. value.ui = value_uint;
  221. break;
  222. case JackParamChar:
  223. jack_controller_settings_set_char_option(option_value, &value.c);
  224. break;
  225. case JackParamString:
  226. jack_controller_settings_set_string_option(option_value, value.str, sizeof(value.str));
  227. break;
  228. case JackParamBool:
  229. jack_controller_settings_set_bool_option(option_value, &value_int);
  230. value.i = value_int;
  231. break;
  232. default:
  233. jack_error("Engine parameter \"%s\" is of unknown type %d",
  234. jackctl_parameter_get_name(parameter),
  235. type);
  236. }
  237. jackctl_parameter_set_value(parameter, &value);
  238. }
  239. static
  240. bool
  241. jack_controller_settings_save_options(
  242. void *context,
  243. const JSList * parameters_list,
  244. void *dbus_call_context_ptr)
  245. {
  246. jackctl_parameter_t *parameter;
  247. jackctl_param_type_t type;
  248. union jackctl_parameter_value value;
  249. const char * name;
  250. char value_str[50];
  251. while (parameters_list != NULL)
  252. {
  253. parameter = (jackctl_parameter_t *)parameters_list->data;
  254. if (jackctl_parameter_is_set(parameter))
  255. {
  256. type = jackctl_parameter_get_type(parameter);
  257. value = jackctl_parameter_get_value(parameter);
  258. name = jackctl_parameter_get_name(parameter);
  259. switch (type)
  260. {
  261. case JackParamInt:
  262. sprintf(value_str, "%d", (int)value.i);
  263. if (!jack_controller_settings_write_option(context, name, value_str, dbus_call_context_ptr))
  264. {
  265. return false;
  266. }
  267. break;
  268. case JackParamUInt:
  269. sprintf(value_str, "%u", (unsigned int)value.ui);
  270. if (!jack_controller_settings_write_option(context, name, value_str, dbus_call_context_ptr))
  271. {
  272. return false;
  273. }
  274. break;
  275. case JackParamChar:
  276. sprintf(value_str, "%c", (char)value.c);
  277. if (!jack_controller_settings_write_option(context, name, value_str, dbus_call_context_ptr))
  278. {
  279. return false;
  280. }
  281. break;
  282. case JackParamString:
  283. if (!jack_controller_settings_write_option(context, name, value.str, dbus_call_context_ptr))
  284. {
  285. return false;
  286. }
  287. break;
  288. case JackParamBool:
  289. if (!jack_controller_settings_write_option(context, name, value.b ? "true" : "false", dbus_call_context_ptr))
  290. {
  291. return false;
  292. }
  293. break;
  294. default:
  295. jack_error("parameter of unknown type %d", type);
  296. }
  297. }
  298. parameters_list = jack_slist_next(parameters_list);
  299. }
  300. return true;
  301. }
  302. bool
  303. jack_controller_settings_save_engine_options(
  304. void *context,
  305. struct jack_controller *controller_ptr,
  306. void *dbus_call_context_ptr)
  307. {
  308. if (controller_ptr->driver != NULL)
  309. {
  310. if (!jack_controller_settings_write_option(
  311. context,
  312. "driver",
  313. jackctl_driver_get_name(controller_ptr->driver),
  314. dbus_call_context_ptr))
  315. {
  316. return false;
  317. }
  318. }
  319. return jack_controller_settings_save_options(context, jackctl_server_get_parameters(controller_ptr->server), dbus_call_context_ptr);
  320. }
  321. bool
  322. jack_controller_settings_save_driver_options(
  323. void *context,
  324. jackctl_driver_t *driver,
  325. void *dbus_call_context_ptr)
  326. {
  327. return jack_controller_settings_save_options(context, jackctl_driver_get_parameters(driver), dbus_call_context_ptr);
  328. }
  329. bool
  330. jack_controller_settings_save_internal_options(
  331. void *context,
  332. jackctl_internal_t *internal,
  333. void *dbus_call_context_ptr)
  334. {
  335. return jack_controller_settings_save_options(context, jackctl_internal_get_parameters(internal), dbus_call_context_ptr);
  336. }