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
28 #include <pulse/xmalloc.h>
30 #include <pulsecore/core.h>
31 #include <pulsecore/sink-input.h>
32 #include <pulsecore/source-output.h>
33 #include <pulsecore/modargs.h>
34 #include <pulsecore/log.h>
35 #include <pulsecore/namereg.h>
37 #include "module-rescue-streams-symdef.h"
39 PA_MODULE_AUTHOR("Lennart Poettering")
40 PA_MODULE_DESCRIPTION("When a sink/source is removed, try to move their streams to the default sink/source")
41 PA_MODULE_VERSION(PACKAGE_VERSION
)
43 static const char* const valid_modargs
[] = {
48 pa_hook_slot
*sink_slot
, *source_slot
;
51 static pa_hook_result_t
sink_hook_callback(pa_core
*c
, pa_sink
*sink
, void* userdata
) {
58 if (!pa_idxset_size(sink
->inputs
)) {
59 pa_log_debug("No sink inputs to move away.");
63 if (!(target
= pa_namereg_get(c
, NULL
, PA_NAMEREG_SINK
, 0)) || target
== sink
) {
66 for (target
= pa_idxset_first(c
->sinks
, &idx
); target
; target
= pa_idxset_next(c
->sinks
, &idx
))
71 pa_log_info("No evacuation sink found.");
76 while ((i
= pa_idxset_first(sink
->inputs
, NULL
))) {
77 if (pa_sink_input_move_to(i
, target
, 1) < 0) {
78 pa_log_warn("Failed to move sink input %u \"%s\" to %s.", i
->index
, i
->name
, target
->name
);
82 pa_log_info("Sucessfully moved sink input %u \"%s\" to %s.", i
->index
, i
->name
, target
->name
);
89 static pa_hook_result_t
source_hook_callback(pa_core
*c
, pa_source
*source
, void* userdata
) {
96 if (!pa_idxset_size(source
->outputs
)) {
97 pa_log_debug("No source outputs to move away.");
101 if (!(target
= pa_namereg_get(c
, NULL
, PA_NAMEREG_SOURCE
, 0)) || target
== source
) {
104 for (target
= pa_idxset_first(c
->sources
, &idx
); target
; target
= pa_idxset_next(c
->sources
, &idx
))
105 if (target
!= source
&& !target
->monitor_of
== !source
->monitor_of
)
109 pa_log_info("No evacuation source found.");
114 pa_assert(target
!= source
);
116 while ((o
= pa_idxset_first(source
->outputs
, NULL
))) {
117 if (pa_source_output_move_to(o
, target
) < 0) {
118 pa_log_warn("Failed to move source output %u \"%s\" to %s.", o
->index
, o
->name
, target
->name
);
122 pa_log_info("Sucessfully moved source output %u \"%s\" to %s.", o
->index
, o
->name
, target
->name
);
129 int pa__init(pa_module
*m
) {
130 pa_modargs
*ma
= NULL
;
135 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
136 pa_log("Failed to parse module arguments");
140 m
->userdata
= u
= pa_xnew(struct userdata
, 1);
141 u
->sink_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_UNLINK
], (pa_hook_cb_t
) sink_hook_callback
, NULL
);
142 u
->source_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_UNLINK
], (pa_hook_cb_t
) source_hook_callback
, NULL
);
148 void pa__done(pa_module
*m
) {
158 pa_hook_slot_free(u
->sink_slot
);
160 pa_hook_slot_free(u
->source_slot
);