1 /* -*- Mode: C ; c-basic-offset: 4 -*- */
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)
26 #include <sys/types.h>
32 #include <dbus/dbus.h>
35 #include "controller_internal.h"
39 jack_controller_settings_write_string(int fd
, const char * string
, void *dbus_call_context_ptr
)
45 if (write(fd
, string
, len
) != len
)
47 jack_dbus_error(dbus_call_context_ptr
, JACK_DBUS_ERROR_GENERIC
, "write() failed to write config file.");
59 jack_params_handle params
;
60 const char * address
[PARAM_ADDRESS_SIZE
];
64 #define ctx_ptr ((struct save_context *)context)
65 #define fd (ctx_ptr->fd)
67 static bool jack_controller_serialize_parameter(void * context
, const struct jack_parameter
* param_ptr
)
69 char value
[JACK_PARAM_STRING_MAX
+ 1];
71 if (!param_ptr
->vtable
.is_set(param_ptr
->obj
))
76 jack_controller_serialize_parameter_value(param_ptr
, value
);
79 jack_controller_settings_write_string(fd
, ctx_ptr
->indent
, ctx_ptr
->call
) &&
80 jack_controller_settings_write_string(fd
, "<option name=\"", ctx_ptr
->call
) &&
81 jack_controller_settings_write_string(fd
, param_ptr
->name
, ctx_ptr
->call
) &&
82 jack_controller_settings_write_string(fd
, "\">", ctx_ptr
->call
) &&
83 jack_controller_settings_write_string(fd
, value
, ctx_ptr
->call
) &&
84 jack_controller_settings_write_string(fd
, "</option>\n", ctx_ptr
->call
);
87 bool serialize_modules(void * context
, const char * name
)
89 ctx_ptr
->indent
= " ";
90 ctx_ptr
->address
[1] = name
;
91 ctx_ptr
->address
[2] = NULL
;
94 jack_controller_settings_write_string(fd
, " <", ctx_ptr
->call
) &&
95 jack_controller_settings_write_string(fd
, ctx_ptr
->str
, ctx_ptr
->call
) &&
96 jack_controller_settings_write_string(fd
, " name=\"", ctx_ptr
->call
) &&
97 jack_controller_settings_write_string(fd
, name
, ctx_ptr
->call
) &&
98 jack_controller_settings_write_string(fd
, "\">\n", ctx_ptr
->call
) &&
99 jack_params_iterate_params(ctx_ptr
->params
, ctx_ptr
->address
, jack_controller_serialize_parameter
, ctx_ptr
) &&
100 jack_controller_settings_write_string(fd
, " </", ctx_ptr
->call
) &&
101 jack_controller_settings_write_string(fd
, ctx_ptr
->str
, ctx_ptr
->call
) &&
102 jack_controller_settings_write_string(fd
, ">\n", ctx_ptr
->call
);
109 jack_controller_settings_save(
110 struct jack_controller
* controller_ptr
,
111 void *dbus_call_context_ptr
)
118 char timestamp_str
[26];
119 struct save_context context
;
120 const char * modules
[] = {"driver", "internal", NULL
};
125 ctime_r(×tamp
, timestamp_str
);
126 timestamp_str
[24] = 0;
130 conf_len
= strlen(JACKDBUS_CONF
);
132 filename
= malloc(g_jackdbus_config_dir_len
+ conf_len
+ 1);
133 if (filename
== NULL
)
135 jack_error("Out of memory.");
139 memcpy(filename
, g_jackdbus_config_dir
, g_jackdbus_config_dir_len
);
140 memcpy(filename
+ g_jackdbus_config_dir_len
, JACKDBUS_CONF
, conf_len
);
141 filename
[g_jackdbus_config_dir_len
+ conf_len
] = 0;
143 jack_info("Saving settings to \"%s\" ...", filename
);
145 fd
= open(filename
, O_WRONLY
| O_TRUNC
| O_CREAT
, S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
);
148 jack_error("open() failed to open conf filename. error is %d (%s)", errno
, strerror(errno
));
149 goto exit_free_filename
;
153 context
.call
= dbus_call_context_ptr
;
155 if (!jack_controller_settings_write_string(fd
, "<?xml version=\"1.0\"?>\n", dbus_call_context_ptr
))
160 if (!jack_controller_settings_write_string(fd
, "<!--\n", dbus_call_context_ptr
))
165 if (!jack_controller_settings_write_string(fd
, JACK_CONF_HEADER_TEXT
, dbus_call_context_ptr
))
170 if (!jack_controller_settings_write_string(fd
, "-->\n", dbus_call_context_ptr
))
175 if (!jack_controller_settings_write_string(fd
, "<!-- ", dbus_call_context_ptr
))
180 if (!jack_controller_settings_write_string(fd
, timestamp_str
, dbus_call_context_ptr
))
185 if (!jack_controller_settings_write_string(fd
, " -->\n", dbus_call_context_ptr
))
190 if (!jack_controller_settings_write_string(fd
, "<jack>\n", dbus_call_context_ptr
))
197 if (!jack_controller_settings_write_string(fd
, " <engine>\n", dbus_call_context_ptr
))
202 context
.indent
= " ";
203 context
.address
[0] = PTNODE_ENGINE
;
204 context
.address
[1] = NULL
;
205 if (!jack_params_iterate_params(controller_ptr
->params
, context
.address
, jack_controller_serialize_parameter
, &context
))
210 if (!jack_controller_settings_write_string(fd
, " </engine>\n", dbus_call_context_ptr
))
215 for (i
= 0; modules
[i
] != NULL
; i
++)
217 if (!jack_controller_settings_write_string(fd
, " <", dbus_call_context_ptr
))
222 if (!jack_controller_settings_write_string(fd
, modules
[i
], dbus_call_context_ptr
))
227 if (!jack_controller_settings_write_string(fd
, "s>\n", dbus_call_context_ptr
))
232 context
.indent
= " ";
233 context
.params
= controller_ptr
->params
;
234 context
.str
= modules
[i
];
235 strcpy(buffer
, modules
[i
]);
237 context
.address
[0] = buffer
;
238 context
.address
[1] = NULL
;
240 if (!jack_params_iterate_container(controller_ptr
->params
, context
.address
, serialize_modules
, &context
))
245 if (!jack_controller_settings_write_string(fd
, " </", dbus_call_context_ptr
))
250 if (!jack_controller_settings_write_string(fd
, modules
[i
], dbus_call_context_ptr
))
255 if (!jack_controller_settings_write_string(fd
, "s>\n", dbus_call_context_ptr
))
261 if (!jack_controller_settings_write_string(fd
, "</jack>\n", dbus_call_context_ptr
))
279 jack_controller_settings_save_auto(
280 struct jack_controller
* controller_ptr
)
282 jack_controller_settings_save(controller_ptr
, NULL
);