Fix merge conflicts.
[jack2.git] / dbus / xml.c
blobb506c5d5c1ff4c1339fd01cb6e696c1f9b5e1b45
1 /* -*- Mode: C ; c-basic-offset: 4 -*- */
2 /*
3 Copyright (C) 2007,2008 Nedko Arnaudov
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #if defined(HAVE_CONFIG_H)
21 #include "config.h"
22 #endif
24 #include <stdbool.h>
25 #include <stdint.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <dbus/dbus.h>
30 #include "controller_internal.h"
32 void
33 jack_controller_settings_set_bool_option(
34 const char *value_str,
35 int *value_ptr)
37 if (strcmp(value_str, "true") == 0)
39 *value_ptr = true;
41 else if (strcmp(value_str, "false") == 0)
43 *value_ptr = false;
45 else
47 jack_error("ignoring unknown bool value \"%s\"", value_str);
51 void
52 jack_controller_settings_set_sint_option(
53 const char *value_str,
54 int *value_ptr)
56 *value_ptr = atoi(value_str);
59 void
60 jack_controller_settings_set_uint_option(
61 const char *value_str,
62 unsigned int *value_ptr)
64 *value_ptr = strtoul(value_str, NULL, 10);
67 void
68 jack_controller_settings_set_char_option(
69 const char *value_str,
70 char *value_ptr)
72 if (value_str[0] == 0 || value_str[1] != 0)
74 jack_error("invalid char option value \"%s\"", value_str);
75 return;
78 *value_ptr = *value_str;
81 void
82 jack_controller_settings_set_string_option(
83 const char *value_str,
84 char *value_ptr,
85 size_t max_size)
87 size_t size;
89 size = strlen(value_str);
91 if (size >= max_size)
93 jack_error("string option value \"%s\" is too long, max is %u chars (including terminating zero)", value_str, (unsigned int)max_size);
94 return;
97 strcpy(value_ptr, value_str);
100 void
101 jack_controller_settings_set_driver_option(
102 jackctl_driver_t *driver,
103 const char *option_name,
104 const char *option_value)
106 jackctl_parameter_t *parameter;
107 jackctl_param_type_t type;
108 int value_int = 0;
109 unsigned int value_uint = 0;
110 union jackctl_parameter_value value;
112 jack_info("setting driver option \"%s\" to value \"%s\"", option_name, option_value);
114 parameter = jack_controller_find_parameter(jackctl_driver_get_parameters(driver), option_name);
115 if (parameter == NULL)
117 jack_error(
118 "Unknown parameter \"%s\" of driver \"%s\"",
119 option_name,
120 jackctl_driver_get_name(driver));
121 return;
124 type = jackctl_parameter_get_type(parameter);
126 switch (type)
128 case JackParamInt:
129 jack_controller_settings_set_sint_option(option_value, &value_int);
130 value.i = value_int;
131 break;
132 case JackParamUInt:
133 jack_controller_settings_set_uint_option(option_value, &value_uint);
134 value.ui = value_uint;
135 break;
136 case JackParamChar:
137 jack_controller_settings_set_char_option(option_value, &value.c);
138 break;
139 case JackParamString:
140 jack_controller_settings_set_string_option(option_value, value.str, sizeof(value.str));
141 break;
142 case JackParamBool:
143 jack_controller_settings_set_bool_option(option_value, &value_int);
144 value.i = value_int;
145 break;
146 default:
147 jack_error("Parameter \"%s\" of driver \"%s\" is of unknown type %d",
148 jackctl_parameter_get_name(parameter),
149 jackctl_driver_get_name(driver),
150 type);
153 jackctl_parameter_set_value(parameter, &value);
156 void
157 jack_controller_settings_set_internal_option(
158 jackctl_internal_t *internal,
159 const char *option_name,
160 const char *option_value)
162 jackctl_parameter_t *parameter;
163 jackctl_param_type_t type;
164 int value_int = 0;
165 unsigned int value_uint = 0;
166 union jackctl_parameter_value value;
168 jack_info("setting internal option \"%s\" to value \"%s\"", option_name, option_value);
170 parameter = jack_controller_find_parameter(jackctl_internal_get_parameters(internal), option_name);
171 if (parameter == NULL)
173 jack_error(
174 "Unknown parameter \"%s\" of internal \"%s\"",
175 option_name,
176 jackctl_internal_get_name(internal));
177 return;
180 type = jackctl_parameter_get_type(parameter);
182 switch (type)
184 case JackParamInt:
185 jack_controller_settings_set_sint_option(option_value, &value_int);
186 value.i = value_int;
187 break;
188 case JackParamUInt:
189 jack_controller_settings_set_uint_option(option_value, &value_uint);
190 value.ui = value_uint;
191 break;
192 case JackParamChar:
193 jack_controller_settings_set_char_option(option_value, &value.c);
194 break;
195 case JackParamString:
196 jack_controller_settings_set_string_option(option_value, value.str, sizeof(value.str));
197 break;
198 case JackParamBool:
199 jack_controller_settings_set_bool_option(option_value, &value_int);
200 value.i = value_int;
201 break;
202 default:
203 jack_error("Parameter \"%s\" of internal \"%s\" is of unknown type %d",
204 jackctl_parameter_get_name(parameter),
205 jackctl_internal_get_name(internal),
206 type);
209 jackctl_parameter_set_value(parameter, &value);
212 void
213 jack_controller_settings_set_engine_option(
214 struct jack_controller *controller_ptr,
215 const char *option_name,
216 const char *option_value)
218 jackctl_parameter_t *parameter;
219 jackctl_param_type_t type;
220 int value_int = 0;
221 unsigned int value_uint = 0;
222 union jackctl_parameter_value value;
224 jack_info("setting engine option \"%s\" to value \"%s\"", option_name, option_value);
226 if (strcmp(option_name, "driver") == 0)
228 if (!jack_controller_select_driver(controller_ptr, option_value))
230 jack_error("unknown driver '%s'", option_value);
233 return;
236 parameter = jack_controller_find_parameter(jackctl_server_get_parameters(controller_ptr->server), option_name);
237 if (parameter == NULL)
239 jack_error(
240 "Unknown engine parameter \"%s\"",
241 option_name);
242 return;
245 type = jackctl_parameter_get_type(parameter);
247 switch (type)
249 case JackParamInt:
250 jack_controller_settings_set_sint_option(option_value, &value_int);
251 value.i = value_int;
252 break;
253 case JackParamUInt:
254 jack_controller_settings_set_uint_option(option_value, &value_uint);
255 value.ui = value_uint;
256 break;
257 case JackParamChar:
258 jack_controller_settings_set_char_option(option_value, &value.c);
259 break;
260 case JackParamString:
261 jack_controller_settings_set_string_option(option_value, value.str, sizeof(value.str));
262 break;
263 case JackParamBool:
264 jack_controller_settings_set_bool_option(option_value, &value_int);
265 value.i = value_int;
266 break;
267 default:
268 jack_error("Engine parameter \"%s\" is of unknown type %d",
269 jackctl_parameter_get_name(parameter),
270 type);
273 jackctl_parameter_set_value(parameter, &value);
276 static
277 bool
278 jack_controller_settings_save_options(
279 void *context,
280 const JSList * parameters_list,
281 void *dbus_call_context_ptr)
283 jackctl_parameter_t *parameter;
284 jackctl_param_type_t type;
285 union jackctl_parameter_value value;
286 const char * name;
287 char value_str[50];
289 while (parameters_list != NULL)
291 parameter = (jackctl_parameter_t *)parameters_list->data;
293 if (jackctl_parameter_is_set(parameter))
295 type = jackctl_parameter_get_type(parameter);
296 value = jackctl_parameter_get_value(parameter);
297 name = jackctl_parameter_get_name(parameter);
299 switch (type)
301 case JackParamInt:
302 sprintf(value_str, "%d", (int)value.i);
303 if (!jack_controller_settings_write_option(context, name, value_str, dbus_call_context_ptr))
305 return false;
307 break;
308 case JackParamUInt:
309 sprintf(value_str, "%u", (unsigned int)value.ui);
310 if (!jack_controller_settings_write_option(context, name, value_str, dbus_call_context_ptr))
312 return false;
314 break;
315 case JackParamChar:
316 sprintf(value_str, "%c", (char)value.c);
317 if (!jack_controller_settings_write_option(context, name, value_str, dbus_call_context_ptr))
319 return false;
321 break;
322 case JackParamString:
323 if (!jack_controller_settings_write_option(context, name, value.str, dbus_call_context_ptr))
325 return false;
327 break;
328 case JackParamBool:
329 if (!jack_controller_settings_write_option(context, name, value.b ? "true" : "false", dbus_call_context_ptr))
331 return false;
333 break;
334 default:
335 jack_error("parameter of unknown type %d", type);
339 parameters_list = jack_slist_next(parameters_list);
342 return true;
345 bool
346 jack_controller_settings_save_engine_options(
347 void *context,
348 struct jack_controller *controller_ptr,
349 void *dbus_call_context_ptr)
351 if (controller_ptr->driver != NULL)
353 if (!jack_controller_settings_write_option(
354 context,
355 "driver",
356 jackctl_driver_get_name(controller_ptr->driver),
357 dbus_call_context_ptr))
359 return false;
363 return jack_controller_settings_save_options(context, jackctl_server_get_parameters(controller_ptr->server), dbus_call_context_ptr);
366 bool
367 jack_controller_settings_save_driver_options(
368 void *context,
369 jackctl_driver_t *driver,
370 void *dbus_call_context_ptr)
372 return jack_controller_settings_save_options(context, jackctl_driver_get_parameters(driver), dbus_call_context_ptr);
375 bool
376 jack_controller_settings_save_internal_options(
377 void *context,
378 jackctl_internal_t *internal,
379 void *dbus_call_context_ptr)
381 return jack_controller_settings_save_options(context, jackctl_internal_get_parameters(internal), dbus_call_context_ptr);