4 This file is part of PulseAudio.
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <sys/types.h>
35 #include <pulse/xmalloc.h>
37 #include <pulsecore/core-error.h>
38 #include <pulsecore/module.h>
39 #include <pulsecore/core-util.h>
40 #include <pulsecore/modargs.h>
41 #include <pulsecore/log.h>
42 #include <pulsecore/core-subscribe.h>
43 #include <pulsecore/sink-input.h>
44 #include <pulsecore/core-util.h>
45 #include <pulse/volume.h>
47 #include "module-volume-restore-symdef.h"
49 PA_MODULE_AUTHOR("Lennart Poettering")
50 PA_MODULE_DESCRIPTION("Playback stream automatic volume restore module")
51 PA_MODULE_USAGE("table=<filename>")
52 PA_MODULE_VERSION(PACKAGE_VERSION
)
54 #define WHITESPACE "\n\r \t"
56 #define DEFAULT_VOLUME_TABLE_FILE "volume.table"
58 static const char* const valid_modargs
[] = {
70 pa_subscription
*subscription
;
71 pa_hook_slot
*hook_slot
;
76 static pa_cvolume
* parse_volume(const char *s
, pa_cvolume
*v
) {
88 if (k
<= 0 || k
> PA_CHANNELS_MAX
)
91 v
->channels
= (unsigned) k
;
93 for (i
= 0; i
< v
->channels
; i
++) {
94 p
+= strspn(p
, WHITESPACE
);
101 if (k
< PA_VOLUME_MUTED
)
104 v
->values
[i
] = (pa_volume_t
) k
;
113 static int load_rules(struct userdata
*u
) {
117 char buf_name
[256], buf_volume
[256];
121 fopen(u
->table_file
, "r") :
122 pa_open_config_file(NULL
, DEFAULT_VOLUME_TABLE_FILE
, NULL
, &u
->table_file
, "r");
125 if (errno
== ENOENT
) {
126 pa_log_info(__FILE__
": starting with empty ruleset.");
129 pa_log(__FILE__
": failed to open file '%s': %s", u
->table_file
, pa_cstrerror(errno
));
134 pa_lock_fd(fileno(f
), 1);
140 if (!fgets(ln
, sizeof(buf_name
), f
))
147 if (ln
[0] == '#' || !*ln
)
150 if (ln
== buf_name
) {
155 assert(ln
== buf_volume
);
157 if (!parse_volume(buf_volume
, &v
)) {
158 pa_log(__FILE__
": parse failure in %s:%u, stopping parsing", u
->table_file
, n
);
164 if (pa_hashmap_get(u
->hashmap
, buf_name
)) {
165 pa_log(__FILE__
": double entry in %s:%u, ignoring", u
->table_file
, n
);
169 rule
= pa_xnew(struct rule
, 1);
170 rule
->name
= pa_xstrdup(buf_name
);
173 pa_hashmap_put(u
->hashmap
, rule
->name
, rule
);
176 if (ln
== buf_volume
) {
177 pa_log(__FILE__
": invalid number of lines in %s.", u
->table_file
);
185 pa_lock_fd(fileno(f
), 0);
192 static int save_rules(struct userdata
*u
) {
199 fopen(u
->table_file
, "w") :
200 pa_open_config_file(NULL
, DEFAULT_VOLUME_TABLE_FILE
, NULL
, &u
->table_file
, "w");
203 pa_log(__FILE__
": failed to open file '%s': %s", u
->table_file
, pa_cstrerror(errno
));
207 pa_lock_fd(fileno(f
), 1);
209 while ((rule
= pa_hashmap_iterate(u
->hashmap
, &state
, NULL
))) {
212 fprintf(f
, "%s\n%u", rule
->name
, rule
->volume
.channels
);
214 for (i
= 0; i
< rule
->volume
.channels
; i
++)
215 fprintf(f
, " %u", rule
->volume
.values
[i
]);
224 pa_lock_fd(fileno(f
), 0);
231 static char* client_name(pa_client
*c
) {
234 if (!c
->name
|| !c
->driver
)
237 t
= pa_sprintf_malloc("%s$%s", c
->driver
, c
->name
);
238 t
[strcspn(t
, "\n\r#")] = 0;
243 if ((e
= strrchr(t
, '('))) {
244 char *k
= e
+ 1 + strspn(e
+ 1, "0123456789-");
246 /* Dirty trick: truncate all trailing parens with numbers in
247 * between, since they are usually used to identify multiple
248 * sessions of the same application, which is something we
249 * explicitly don't want. Besides other stuff this makes xmms
250 * with esound work properly for us. */
252 if (*k
== ')' && *(k
+1) == 0)
259 static void subscribe_callback(pa_core
*c
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
260 struct userdata
*u
= userdata
;
268 if (t
!= (PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_NEW
) &&
269 t
!= (PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
))
272 if (!(si
= pa_idxset_get_by_index(c
->sink_inputs
, idx
)))
275 if (!si
->client
|| !(name
= client_name(si
->client
)))
278 if ((r
= pa_hashmap_get(u
->hashmap
, name
))) {
281 if (!pa_cvolume_equal(pa_sink_input_get_volume(si
), &r
->volume
)) {
282 pa_log_info(__FILE__
": Saving volume for <%s>", r
->name
);
283 r
->volume
= *pa_sink_input_get_volume(si
);
287 pa_log_info(__FILE__
": Creating new entry for <%s>", name
);
289 r
= pa_xnew(struct rule
, 1);
291 r
->volume
= *pa_sink_input_get_volume(si
);
292 pa_hashmap_put(u
->hashmap
, r
->name
, r
);
298 static pa_hook_result_t
hook_callback(pa_core
*c
, pa_sink_input_new_data
*data
, struct userdata
*u
) {
304 if (!data
->client
|| !(name
= client_name(data
->client
)))
307 if ((r
= pa_hashmap_get(u
->hashmap
, name
))) {
309 if (data
->sample_spec_is_set
&& data
->sample_spec
.channels
== r
->volume
.channels
) {
310 pa_log_info(__FILE__
": Restoring volume for <%s>", r
->name
);
311 pa_sink_input_new_data_set_volume(data
, &r
->volume
);
318 int pa__init(pa_core
*c
, pa_module
*m
) {
319 pa_modargs
*ma
= NULL
;
325 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
326 pa_log(__FILE__
": Failed to parse module arguments");
330 u
= pa_xnew(struct userdata
, 1);
331 u
->hashmap
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
332 u
->subscription
= NULL
;
333 u
->table_file
= pa_xstrdup(pa_modargs_get_value(ma
, "table", NULL
));
338 if (load_rules(u
) < 0)
341 u
->subscription
= pa_subscription_new(c
, PA_SUBSCRIPTION_MASK_SINK_INPUT
, subscribe_callback
, u
);
342 u
->hook_slot
= pa_hook_connect(&c
->hook_sink_input_new
, (pa_hook_cb_t
) hook_callback
, u
);
356 static void free_func(void *p
, void *userdata
) {
364 void pa__done(pa_core
*c
, pa_module
*m
) {
370 if (!(u
= m
->userdata
))
374 pa_subscription_free(u
->subscription
);
377 pa_hook_slot_free(u
->hook_slot
);
384 pa_hashmap_free(u
->hashmap
, free_func
, NULL
);
387 pa_xfree(u
->table_file
);