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.

318 lines
8.8KB

  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;
  92. unsigned int value_uint;
  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_engine_option(
  135. struct jack_controller *controller_ptr,
  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;
  142. unsigned int value_uint;
  143. union jackctl_parameter_value value;
  144. jack_info("setting engine option \"%s\" to value \"%s\"", option_name, option_value);
  145. if (strcmp(option_name, "driver") == 0)
  146. {
  147. if (!jack_controller_select_driver(controller_ptr, option_value))
  148. {
  149. jack_error("unknown driver '%s'", option_value);
  150. }
  151. return;
  152. }
  153. parameter = jack_controller_find_parameter(jackctl_server_get_parameters(controller_ptr->server), option_name);
  154. if (parameter == NULL)
  155. {
  156. jack_error(
  157. "Unknown engine parameter \"%s\"",
  158. option_name);
  159. return;
  160. }
  161. type = jackctl_parameter_get_type(parameter);
  162. switch (type)
  163. {
  164. case JackParamInt:
  165. jack_controller_settings_set_sint_option(option_value, &value_int);
  166. value.i = value_int;
  167. break;
  168. case JackParamUInt:
  169. jack_controller_settings_set_uint_option(option_value, &value_uint);
  170. value.ui = value_uint;
  171. break;
  172. case JackParamChar:
  173. jack_controller_settings_set_char_option(option_value, &value.c);
  174. break;
  175. case JackParamString:
  176. jack_controller_settings_set_string_option(option_value, value.str, sizeof(value.str));
  177. break;
  178. case JackParamBool:
  179. jack_controller_settings_set_bool_option(option_value, &value_int);
  180. value.i = value_int;
  181. break;
  182. default:
  183. jack_error("Engine parameter \"%s\" is of unknown type %d",
  184. jackctl_parameter_get_name(parameter),
  185. type);
  186. }
  187. jackctl_parameter_set_value(parameter, &value);
  188. }
  189. static
  190. bool
  191. jack_controller_settings_save_options(
  192. void *context,
  193. const JSList * parameters_list,
  194. void *dbus_call_context_ptr)
  195. {
  196. jackctl_parameter_t *parameter;
  197. jackctl_param_type_t type;
  198. union jackctl_parameter_value value;
  199. const char * name;
  200. char value_str[50];
  201. while (parameters_list != NULL)
  202. {
  203. parameter = (jackctl_parameter_t *)parameters_list->data;
  204. if (jackctl_parameter_is_set(parameter))
  205. {
  206. type = jackctl_parameter_get_type(parameter);
  207. value = jackctl_parameter_get_value(parameter);
  208. name = jackctl_parameter_get_name(parameter);
  209. switch (type)
  210. {
  211. case JackParamInt:
  212. sprintf(value_str, "%d", (int)value.i);
  213. if (!jack_controller_settings_write_option(context, name, value_str, dbus_call_context_ptr))
  214. {
  215. return false;
  216. }
  217. break;
  218. case JackParamUInt:
  219. sprintf(value_str, "%u", (unsigned int)value.ui);
  220. if (!jack_controller_settings_write_option(context, name, value_str, dbus_call_context_ptr))
  221. {
  222. return false;
  223. }
  224. break;
  225. case JackParamChar:
  226. sprintf(value_str, "%c", (char)value.c);
  227. if (!jack_controller_settings_write_option(context, name, value_str, dbus_call_context_ptr))
  228. {
  229. return false;
  230. }
  231. break;
  232. case JackParamString:
  233. if (!jack_controller_settings_write_option(context, name, value.str, dbus_call_context_ptr))
  234. {
  235. return false;
  236. }
  237. break;
  238. case JackParamBool:
  239. if (!jack_controller_settings_write_option(context, name, value.b ? "true" : "false", dbus_call_context_ptr))
  240. {
  241. return false;
  242. }
  243. break;
  244. default:
  245. jack_error("parameter of unknown type %d", type);
  246. }
  247. }
  248. parameters_list = jack_slist_next(parameters_list);
  249. }
  250. return true;
  251. }
  252. bool
  253. jack_controller_settings_save_engine_options(
  254. void *context,
  255. struct jack_controller *controller_ptr,
  256. void *dbus_call_context_ptr)
  257. {
  258. if (controller_ptr->driver != NULL)
  259. {
  260. if (!jack_controller_settings_write_option(
  261. context,
  262. "driver",
  263. jackctl_driver_get_name(controller_ptr->driver),
  264. dbus_call_context_ptr))
  265. {
  266. return false;
  267. }
  268. }
  269. return jack_controller_settings_save_options(context, jackctl_server_get_parameters(controller_ptr->server), dbus_call_context_ptr);
  270. }
  271. bool
  272. jack_controller_settings_save_driver_options(
  273. void *context,
  274. jackctl_driver_t *driver,
  275. void *dbus_call_context_ptr)
  276. {
  277. return jack_controller_settings_save_options(context, jackctl_driver_get_parameters(driver), dbus_call_context_ptr);
  278. }