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.

314 lines
8.7KB

  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. #include <stdbool.h>
  16. #include <stdint.h>
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include <dbus/dbus.h>
  20. #include "controller_internal.h"
  21. void
  22. jack_controller_settings_set_bool_option(
  23. const char *value_str,
  24. int *value_ptr)
  25. {
  26. if (strcmp(value_str, "true") == 0)
  27. {
  28. *value_ptr = true;
  29. }
  30. else if (strcmp(value_str, "false") == 0)
  31. {
  32. *value_ptr = false;
  33. }
  34. else
  35. {
  36. jack_error("ignoring unknown bool value \"%s\"", value_str);
  37. }
  38. }
  39. void
  40. jack_controller_settings_set_sint_option(
  41. const char *value_str,
  42. int *value_ptr)
  43. {
  44. *value_ptr = atoi(value_str);
  45. }
  46. void
  47. jack_controller_settings_set_uint_option(
  48. const char *value_str,
  49. unsigned int *value_ptr)
  50. {
  51. *value_ptr = strtoul(value_str, NULL, 10);
  52. }
  53. void
  54. jack_controller_settings_set_char_option(
  55. const char *value_str,
  56. char *value_ptr)
  57. {
  58. if (value_str[0] == 0 || value_str[1] != 0)
  59. {
  60. jack_error("invalid char option value \"%s\"", value_str);
  61. return;
  62. }
  63. *value_ptr = *value_str;
  64. }
  65. void
  66. jack_controller_settings_set_string_option(
  67. const char *value_str,
  68. char *value_ptr,
  69. size_t max_size)
  70. {
  71. size_t size;
  72. size = strlen(value_str);
  73. if (size >= max_size)
  74. {
  75. jack_error("string option value \"%s\" is too long, max is %u chars (including terminating zero)", value_str, (unsigned int)max_size);
  76. return;
  77. }
  78. strcpy(value_ptr, value_str);
  79. }
  80. void
  81. jack_controller_settings_set_driver_option(
  82. jackctl_driver_t *driver,
  83. const char *option_name,
  84. const char *option_value)
  85. {
  86. jackctl_parameter_t *parameter;
  87. jackctl_param_type_t type;
  88. int value_int;
  89. unsigned int value_uint;
  90. union jackctl_parameter_value value;
  91. jack_info("setting driver option \"%s\" to value \"%s\"", option_name, option_value);
  92. parameter = jack_controller_find_parameter(jackctl_driver_get_parameters(driver), option_name);
  93. if (parameter == NULL)
  94. {
  95. jack_error(
  96. "Unknown parameter \"%s\" of driver \"%s\"",
  97. option_name,
  98. jackctl_driver_get_name(driver));
  99. return;
  100. }
  101. type = jackctl_parameter_get_type(parameter);
  102. switch (type)
  103. {
  104. case JackParamInt:
  105. jack_controller_settings_set_sint_option(option_value, &value_int);
  106. value.i = value_int;
  107. break;
  108. case JackParamUInt:
  109. jack_controller_settings_set_uint_option(option_value, &value_uint);
  110. value.ui = value_uint;
  111. break;
  112. case JackParamChar:
  113. jack_controller_settings_set_char_option(option_value, &value.c);
  114. break;
  115. case JackParamString:
  116. jack_controller_settings_set_string_option(option_value, value.str, sizeof(value.str));
  117. break;
  118. case JackParamBool:
  119. jack_controller_settings_set_bool_option(option_value, &value_int);
  120. value.i = value_int;
  121. break;
  122. default:
  123. jack_error("Parameter \"%s\" of driver \"%s\" is of unknown type %d",
  124. jackctl_parameter_get_name(parameter),
  125. jackctl_driver_get_name(driver),
  126. type);
  127. }
  128. jackctl_parameter_set_value(parameter, &value);
  129. }
  130. void
  131. jack_controller_settings_set_engine_option(
  132. struct jack_controller *controller_ptr,
  133. const char *option_name,
  134. const char *option_value)
  135. {
  136. jackctl_parameter_t *parameter;
  137. jackctl_param_type_t type;
  138. int value_int;
  139. unsigned int value_uint;
  140. union jackctl_parameter_value value;
  141. jack_info("setting engine option \"%s\" to value \"%s\"", option_name, option_value);
  142. if (strcmp(option_name, "driver") == 0)
  143. {
  144. if (!jack_controller_select_driver(controller_ptr, option_value))
  145. {
  146. jack_error("unknown driver '%s'", option_value);
  147. }
  148. return;
  149. }
  150. parameter = jack_controller_find_parameter(jackctl_server_get_parameters(controller_ptr->server), option_name);
  151. if (parameter == NULL)
  152. {
  153. jack_error(
  154. "Unknown engine parameter \"%s\"",
  155. option_name);
  156. return;
  157. }
  158. type = jackctl_parameter_get_type(parameter);
  159. switch (type)
  160. {
  161. case JackParamInt:
  162. jack_controller_settings_set_sint_option(option_value, &value_int);
  163. value.i = value_int;
  164. break;
  165. case JackParamUInt:
  166. jack_controller_settings_set_uint_option(option_value, &value_uint);
  167. value.ui = value_uint;
  168. break;
  169. case JackParamChar:
  170. jack_controller_settings_set_char_option(option_value, &value.c);
  171. break;
  172. case JackParamString:
  173. jack_controller_settings_set_string_option(option_value, value.str, sizeof(value.str));
  174. break;
  175. case JackParamBool:
  176. jack_controller_settings_set_bool_option(option_value, &value_int);
  177. value.i = value_int;
  178. break;
  179. default:
  180. jack_error("Engine parameter \"%s\" is of unknown type %d",
  181. jackctl_parameter_get_name(parameter),
  182. type);
  183. }
  184. jackctl_parameter_set_value(parameter, &value);
  185. }
  186. static
  187. bool
  188. jack_controller_settings_save_options(
  189. void *context,
  190. const JSList * parameters_list,
  191. void *dbus_call_context_ptr)
  192. {
  193. jackctl_parameter_t *parameter;
  194. jackctl_param_type_t type;
  195. union jackctl_parameter_value value;
  196. const char * name;
  197. char value_str[50];
  198. while (parameters_list != NULL)
  199. {
  200. parameter = (jackctl_parameter_t *)parameters_list->data;
  201. if (jackctl_parameter_is_set(parameter))
  202. {
  203. type = jackctl_parameter_get_type(parameter);
  204. value = jackctl_parameter_get_value(parameter);
  205. name = jackctl_parameter_get_name(parameter);
  206. switch (type)
  207. {
  208. case JackParamInt:
  209. sprintf(value_str, "%d", (int)value.i);
  210. if (!jack_controller_settings_write_option(context, name, value_str, dbus_call_context_ptr))
  211. {
  212. return false;
  213. }
  214. break;
  215. case JackParamUInt:
  216. sprintf(value_str, "%u", (unsigned int)value.ui);
  217. if (!jack_controller_settings_write_option(context, name, value_str, dbus_call_context_ptr))
  218. {
  219. return false;
  220. }
  221. break;
  222. case JackParamChar:
  223. sprintf(value_str, "%c", (char)value.c);
  224. if (!jack_controller_settings_write_option(context, name, value_str, dbus_call_context_ptr))
  225. {
  226. return false;
  227. }
  228. break;
  229. case JackParamString:
  230. if (!jack_controller_settings_write_option(context, name, value.str, dbus_call_context_ptr))
  231. {
  232. return false;
  233. }
  234. break;
  235. case JackParamBool:
  236. if (!jack_controller_settings_write_option(context, name, value.b ? "true" : "false", dbus_call_context_ptr))
  237. {
  238. return false;
  239. }
  240. break;
  241. default:
  242. jack_error("parameter of unknown type %d", type);
  243. }
  244. }
  245. parameters_list = jack_slist_next(parameters_list);
  246. }
  247. return true;
  248. }
  249. bool
  250. jack_controller_settings_save_engine_options(
  251. void *context,
  252. struct jack_controller *controller_ptr,
  253. void *dbus_call_context_ptr)
  254. {
  255. if (controller_ptr->driver != NULL)
  256. {
  257. if (!jack_controller_settings_write_option(
  258. context,
  259. "driver",
  260. jackctl_driver_get_name(controller_ptr->driver),
  261. dbus_call_context_ptr))
  262. {
  263. return false;
  264. }
  265. }
  266. return jack_controller_settings_save_options(context, jackctl_server_get_parameters(controller_ptr->server), dbus_call_context_ptr);
  267. }
  268. bool
  269. jack_controller_settings_save_driver_options(
  270. void *context,
  271. jackctl_driver_t *driver,
  272. void *dbus_call_context_ptr)
  273. {
  274. return jack_controller_settings_save_options(context, jackctl_driver_get_parameters(driver), dbus_call_context_ptr);
  275. }