4 This file is part of PulseAudio.
6 Copyright 2006 Lennart Poettering
8 PulseAudio is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Lesser General Public License as published
10 by the Free Software Foundation; either version 2 of the License,
11 or (at your option) any later version.
13 PulseAudio is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with PulseAudio; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <sys/types.h>
36 #include <pulse/xmalloc.h>
37 #include <pulse/volume.h>
38 #include <pulse/timeval.h>
40 #include <pulsecore/core-error.h>
41 #include <pulsecore/module.h>
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/modargs.h>
44 #include <pulsecore/log.h>
45 #include <pulsecore/core-subscribe.h>
46 #include <pulsecore/sink-input.h>
47 #include <pulsecore/source-output.h>
48 #include <pulsecore/namereg.h>
50 #include "module-volume-restore-symdef.h"
52 PA_MODULE_AUTHOR("Lennart Poettering");
53 PA_MODULE_DESCRIPTION("Automatically restore the volume and the devices of streams");
54 PA_MODULE_VERSION(PACKAGE_VERSION
);
55 PA_MODULE_LOAD_ONCE(TRUE
);
58 "restore_device=<Restore the device for each stream?> "
59 "restore_volume=<Restore the volume for each stream?>"
62 #define WHITESPACE "\n\r \t"
63 #define DEFAULT_VOLUME_TABLE_FILE "volume-restore.table"
64 #define SAVE_INTERVAL 10
66 static const char* const valid_modargs
[] = {
75 pa_bool_t volume_is_set
;
83 pa_subscription
*subscription
;
85 *sink_input_new_hook_slot
,
86 *sink_input_fixate_hook_slot
,
87 *source_output_new_hook_slot
;
90 pa_time_event
*save_time_event
;
93 static pa_cvolume
* parse_volume(const char *s
, pa_cvolume
*v
) {
104 k
= strtol(s
, &p
, 0);
105 if (k
<= 0 || k
> PA_CHANNELS_MAX
)
108 v
->channels
= (unsigned) k
;
110 for (i
= 0; i
< v
->channels
; i
++) {
111 p
+= strspn(p
, WHITESPACE
);
116 k
= strtol(p
, &p
, 0);
118 if (k
< (long) PA_VOLUME_MUTED
)
121 v
->values
[i
] = (pa_volume_t
) k
;
130 static int load_rules(struct userdata
*u
) {
134 char buf_name
[256], buf_volume
[256], buf_sink
[256], buf_source
[256];
137 if (!(f
= fopen(u
->table_file
, "r"))) {
138 if (errno
== ENOENT
) {
139 pa_log_info("Starting with empty ruleset.");
142 pa_log("Failed to open file '%s': %s", u
->table_file
, pa_cstrerror(errno
));
147 pa_lock_fd(fileno(f
), 1);
154 if (!fgets(ln
, sizeof(buf_name
), f
))
164 if (ln
== buf_name
) {
169 if (ln
== buf_volume
) {
174 if (ln
== buf_sink
) {
179 pa_assert(ln
== buf_source
);
182 if (!parse_volume(buf_volume
, &v
)) {
183 pa_log("parse failure in %s:%u, stopping parsing", u
->table_file
, n
);
193 if (pa_hashmap_get(u
->hashmap
, buf_name
)) {
194 pa_log("double entry in %s:%u, ignoring", u
->table_file
, n
);
198 rule
= pa_xnew(struct rule
, 1);
199 rule
->name
= pa_xstrdup(buf_name
);
200 if ((rule
->volume_is_set
= v_is_set
))
202 rule
->sink
= buf_sink
[0] ? pa_xstrdup(buf_sink
) : NULL
;
203 rule
->source
= buf_source
[0] ? pa_xstrdup(buf_source
) : NULL
;
205 pa_hashmap_put(u
->hashmap
, rule
->name
, rule
);
208 if (ln
!= buf_name
) {
209 pa_log("invalid number of lines in %s.", u
->table_file
);
217 pa_lock_fd(fileno(f
), 0);
224 static int save_rules(struct userdata
*u
) {
233 pa_log_info("Saving rules...");
235 if (!(f
= fopen(u
->table_file
, "w"))) {
236 pa_log("Failed to open file '%s': %s", u
->table_file
, pa_cstrerror(errno
));
240 pa_lock_fd(fileno(f
), 1);
242 while ((rule
= pa_hashmap_iterate(u
->hashmap
, &state
, NULL
))) {
245 fprintf(f
, "%s\n", rule
->name
);
247 if (rule
->volume_is_set
) {
248 fprintf(f
, "%u", rule
->volume
.channels
);
250 for (i
= 0; i
< rule
->volume
.channels
; i
++)
251 fprintf(f
, " %u", rule
->volume
.values
[i
]);
254 fprintf(f
, "\n%s\n%s\n",
255 rule
->sink
? rule
->sink
: "",
256 rule
->source
? rule
->source
: "");
261 pa_log_debug("Successfully saved rules...");
265 pa_lock_fd(fileno(f
), 0);
272 static char* client_name(pa_client
*c
) {
275 if (!pa_proplist_gets(c
->proplist
, PA_PROP_APPLICATION_NAME
) || !c
->driver
)
278 t
= pa_sprintf_malloc("%s$%s", c
->driver
, pa_proplist_gets(c
->proplist
, PA_PROP_APPLICATION_NAME
));
279 t
[strcspn(t
, "\n\r#")] = 0;
286 if ((e
= strrchr(t
, '('))) {
287 char *k
= e
+ 1 + strspn(e
+ 1, "0123456789-");
289 /* Dirty trick: truncate all trailing parens with numbers in
290 * between, since they are usually used to identify multiple
291 * sessions of the same application, which is something we
292 * explicitly don't want. Besides other stuff this makes xmms
293 * with esound work properly for us. */
295 if (*k
== ')' && *(k
+1) == 0)
302 static void save_time_callback(pa_mainloop_api
*a
, pa_time_event
* e
, const struct timeval
*tv
, void *userdata
) {
303 struct userdata
*u
= userdata
;
310 pa_assert(e
== u
->save_time_event
);
311 u
->core
->mainloop
->time_free(u
->save_time_event
);
312 u
->save_time_event
= NULL
;
317 static void subscribe_callback(pa_core
*c
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
318 struct userdata
*u
= userdata
;
319 pa_sink_input
*si
= NULL
;
320 pa_source_output
*so
= NULL
;
327 if (t
!= (PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_NEW
) &&
328 t
!= (PA_SUBSCRIPTION_EVENT_SINK_INPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
) &&
329 t
!= (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_NEW
) &&
330 t
!= (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
|PA_SUBSCRIPTION_EVENT_CHANGE
))
333 if ((t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
) == PA_SUBSCRIPTION_EVENT_SINK_INPUT
) {
334 if (!(si
= pa_idxset_get_by_index(c
->sink_inputs
, idx
)))
337 if (!si
->client
|| !(name
= client_name(si
->client
)))
340 pa_assert((t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
) == PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT
);
342 if (!(so
= pa_idxset_get_by_index(c
->source_outputs
, idx
)))
345 if (!so
->client
|| !(name
= client_name(so
->client
)))
349 if ((r
= pa_hashmap_get(u
->hashmap
, name
))) {
354 if (!r
->volume_is_set
|| !pa_cvolume_equal(pa_sink_input_get_volume(si
), &r
->volume
)) {
355 pa_log_info("Saving volume for <%s>", r
->name
);
356 r
->volume
= *pa_sink_input_get_volume(si
);
357 r
->volume_is_set
= TRUE
;
361 if (!r
->sink
|| strcmp(si
->sink
->name
, r
->sink
) != 0) {
362 pa_log_info("Saving sink for <%s>", r
->name
);
364 r
->sink
= pa_xstrdup(si
->sink
->name
);
370 if (!r
->source
|| strcmp(so
->source
->name
, r
->source
) != 0) {
371 pa_log_info("Saving source for <%s>", r
->name
);
373 r
->source
= pa_xstrdup(so
->source
->name
);
379 pa_log_info("Creating new entry for <%s>", name
);
381 r
= pa_xnew(struct rule
, 1);
385 r
->volume
= *pa_sink_input_get_volume(si
);
386 r
->volume_is_set
= TRUE
;
387 r
->sink
= pa_xstrdup(si
->sink
->name
);
391 r
->volume_is_set
= FALSE
;
393 r
->source
= pa_xstrdup(so
->source
->name
);
396 pa_hashmap_put(u
->hashmap
, r
->name
, r
);
400 if (u
->modified
&& !u
->save_time_event
) {
402 pa_gettimeofday(&tv
);
403 tv
.tv_sec
+= SAVE_INTERVAL
;
404 u
->save_time_event
= u
->core
->mainloop
->time_new(u
->core
->mainloop
, &tv
, save_time_callback
, u
);
408 static pa_hook_result_t
sink_input_new_hook_callback(pa_core
*c
, pa_sink_input_new_data
*data
, struct userdata
*u
) {
414 /* In the NEW hook we only adjust the device. Adjusting the volume
415 * is left for the FIXATE hook */
417 if (!data
->client
|| !(name
= client_name(data
->client
)))
420 if ((r
= pa_hashmap_get(u
->hashmap
, name
))) {
421 if (!data
->sink
&& r
->sink
) {
422 if ((data
->sink
= pa_namereg_get(c
, r
->sink
, PA_NAMEREG_SINK
, 1)))
423 pa_log_info("Restoring sink for <%s>", r
->name
);
432 static pa_hook_result_t
sink_input_fixate_hook_callback(pa_core
*c
, pa_sink_input_new_data
*data
, struct userdata
*u
) {
438 /* In the FIXATE hook we only adjust the volum. Adjusting the device
439 * is left for the NEW hook */
441 if (!data
->client
|| !(name
= client_name(data
->client
)))
444 if ((r
= pa_hashmap_get(u
->hashmap
, name
))) {
446 if (r
->volume_is_set
&& data
->sample_spec
.channels
== r
->volume
.channels
) {
447 pa_log_info("Restoring volume for <%s>", r
->name
);
448 pa_sink_input_new_data_set_volume(data
, &r
->volume
);
457 static pa_hook_result_t
source_output_new_hook_callback(pa_core
*c
, pa_source_output_new_data
*data
, struct userdata
*u
) {
463 if (!data
->client
|| !(name
= client_name(data
->client
)))
466 if ((r
= pa_hashmap_get(u
->hashmap
, name
))) {
467 if (!data
->source
&& r
->source
) {
468 if ((data
->source
= pa_namereg_get(c
, r
->source
, PA_NAMEREG_SOURCE
, 1)))
469 pa_log_info("Restoring source for <%s>", r
->name
);
476 int pa__init(pa_module
*m
) {
477 pa_modargs
*ma
= NULL
;
479 pa_bool_t restore_device
= TRUE
, restore_volume
= TRUE
;
483 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
484 pa_log("Failed to parse module arguments");
488 u
= pa_xnew(struct userdata
, 1);
490 u
->hashmap
= pa_hashmap_new(pa_idxset_string_hash_func
, pa_idxset_string_compare_func
);
491 u
->table_file
= pa_runtime_path(pa_modargs_get_value(ma
, "table", DEFAULT_VOLUME_TABLE_FILE
));
493 u
->subscription
= NULL
;
494 u
->sink_input_new_hook_slot
= u
->sink_input_fixate_hook_slot
= u
->source_output_new_hook_slot
= NULL
;
495 u
->save_time_event
= NULL
;
499 if (pa_modargs_get_value_boolean(ma
, "restore_device", &restore_device
) < 0 ||
500 pa_modargs_get_value_boolean(ma
, "restore_volume", &restore_volume
) < 0) {
501 pa_log("restore_volume= and restore_device= expect boolean arguments");
505 if (!(restore_device
|| restore_volume
)) {
506 pa_log("Both restrong the volume and restoring the device are disabled. There's no point in using this module at all then, failing.");
510 if (load_rules(u
) < 0)
513 u
->subscription
= pa_subscription_new(m
->core
, PA_SUBSCRIPTION_MASK_SINK_INPUT
|PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT
, subscribe_callback
, u
);
515 if (restore_device
) {
516 u
->sink_input_new_hook_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_NEW
], (pa_hook_cb_t
) sink_input_new_hook_callback
, u
);
517 u
->source_output_new_hook_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_OUTPUT_NEW
], (pa_hook_cb_t
) source_output_new_hook_callback
, u
);
521 u
->sink_input_fixate_hook_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_INPUT_FIXATE
], (pa_hook_cb_t
) sink_input_fixate_hook_callback
, u
);
534 static void free_func(void *p
, void *userdata
) {
544 void pa__done(pa_module
*m
) {
549 if (!(u
= m
->userdata
))
553 pa_subscription_free(u
->subscription
);
555 if (u
->sink_input_new_hook_slot
)
556 pa_hook_slot_free(u
->sink_input_new_hook_slot
);
557 if (u
->sink_input_fixate_hook_slot
)
558 pa_hook_slot_free(u
->sink_input_fixate_hook_slot
);
559 if (u
->source_output_new_hook_slot
)
560 pa_hook_slot_free(u
->source_output_new_hook_slot
);
564 pa_hashmap_free(u
->hashmap
, free_func
, NULL
);
567 if (u
->save_time_event
)
568 u
->core
->mainloop
->time_free(u
->save_time_event
);
570 pa_xfree(u
->table_file
);