]> code.delx.au - pulseaudio/blob - src/modules/module-rescue-streams.c
tag modules that may only be loaded once at most especially, and enforce that in...
[pulseaudio] / src / modules / module-rescue-streams.c
1 /* $Id$ */
2
3 /***
4 This file is part of PulseAudio.
5
6 Copyright 2006 Lennart Poettering
7
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.
12
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.
17
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
21 USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <pulse/xmalloc.h>
29
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>
36
37 #include "module-rescue-streams-symdef.h"
38
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);
42 PA_MODULE_LOAD_ONCE(TRUE);
43
44 static const char* const valid_modargs[] = {
45 NULL,
46 };
47
48 struct userdata {
49 pa_hook_slot *sink_slot, *source_slot;
50 };
51
52 static pa_hook_result_t sink_hook_callback(pa_core *c, pa_sink *sink, void* userdata) {
53 pa_sink_input *i;
54 pa_sink *target;
55
56 pa_assert(c);
57 pa_assert(sink);
58
59 if (!pa_idxset_size(sink->inputs)) {
60 pa_log_debug("No sink inputs to move away.");
61 return PA_HOOK_OK;
62 }
63
64 if (!(target = pa_namereg_get(c, NULL, PA_NAMEREG_SINK, 0)) || target == sink) {
65 uint32_t idx;
66
67 for (target = pa_idxset_first(c->sinks, &idx); target; target = pa_idxset_next(c->sinks, &idx))
68 if (target != sink)
69 break;
70
71 if (!target) {
72 pa_log_info("No evacuation sink found.");
73 return PA_HOOK_OK;
74 }
75 }
76
77 while ((i = pa_idxset_first(sink->inputs, NULL))) {
78 if (pa_sink_input_move_to(i, target, 1) < 0) {
79 pa_log_warn("Failed to move sink input %u \"%s\" to %s.", i->index, i->name, target->name);
80 return PA_HOOK_OK;
81 }
82
83 pa_log_info("Sucessfully moved sink input %u \"%s\" to %s.", i->index, i->name, target->name);
84 }
85
86
87 return PA_HOOK_OK;
88 }
89
90 static pa_hook_result_t source_hook_callback(pa_core *c, pa_source *source, void* userdata) {
91 pa_source_output *o;
92 pa_source *target;
93
94 pa_assert(c);
95 pa_assert(source);
96
97 if (!pa_idxset_size(source->outputs)) {
98 pa_log_debug("No source outputs to move away.");
99 return PA_HOOK_OK;
100 }
101
102 if (!(target = pa_namereg_get(c, NULL, PA_NAMEREG_SOURCE, 0)) || target == source) {
103 uint32_t idx;
104
105 for (target = pa_idxset_first(c->sources, &idx); target; target = pa_idxset_next(c->sources, &idx))
106 if (target != source && !target->monitor_of == !source->monitor_of)
107 break;
108
109 if (!target) {
110 pa_log_info("No evacuation source found.");
111 return PA_HOOK_OK;
112 }
113 }
114
115 pa_assert(target != source);
116
117 while ((o = pa_idxset_first(source->outputs, NULL))) {
118 if (pa_source_output_move_to(o, target) < 0) {
119 pa_log_warn("Failed to move source output %u \"%s\" to %s.", o->index, o->name, target->name);
120 return PA_HOOK_OK;
121 }
122
123 pa_log_info("Sucessfully moved source output %u \"%s\" to %s.", o->index, o->name, target->name);
124 }
125
126
127 return PA_HOOK_OK;
128 }
129
130 int pa__init(pa_module*m) {
131 pa_modargs *ma = NULL;
132 struct userdata *u;
133
134 pa_assert(m);
135
136 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
137 pa_log("Failed to parse module arguments");
138 return -1;
139 }
140
141 m->userdata = u = pa_xnew(struct userdata, 1);
142 u->sink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SINK_UNLINK], (pa_hook_cb_t) sink_hook_callback, NULL);
143 u->source_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], (pa_hook_cb_t) source_hook_callback, NULL);
144
145 pa_modargs_free(ma);
146 return 0;
147 }
148
149 void pa__done(pa_module*m) {
150 struct userdata *u;
151
152 pa_assert(m);
153
154 if (!m->userdata)
155 return;
156
157 u = m->userdata;
158 if (u->sink_slot)
159 pa_hook_slot_free(u->sink_slot);
160 if (u->source_slot)
161 pa_hook_slot_free(u->source_slot);
162
163 pa_xfree(u);
164 }