]> code.delx.au - pulseaudio/blob - src/modules/module-rescue-streams.c
25005f250377fdf5e73363fc39eeed53082d8f6b
[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
43 static const char* const valid_modargs[] = {
44 NULL,
45 };
46
47 struct userdata {
48 pa_hook_slot *sink_slot, *source_slot;
49 };
50
51 static pa_hook_result_t sink_hook_callback(pa_core *c, pa_sink *sink, void* userdata) {
52 pa_sink_input *i;
53 pa_sink *target;
54
55 assert(c);
56 assert(sink);
57
58 if (!pa_idxset_size(sink->inputs)) {
59 pa_log_debug("No sink inputs to move away.");
60 return PA_HOOK_OK;
61 }
62
63 if (!(target = pa_namereg_get(c, NULL, PA_NAMEREG_SINK, 0))) {
64 pa_log_info("No evacuation sink found.");
65 return PA_HOOK_OK;
66 }
67
68 assert(target != sink);
69
70 while ((i = pa_idxset_first(sink->inputs, NULL))) {
71 if (pa_sink_input_move_to(i, target, 1) < 0) {
72 pa_log_warn("Failed to move sink input %u \"%s\" to %s.", i->index, i->name, target->name);
73 return PA_HOOK_OK;
74 }
75
76 pa_log_info("Sucessfully moved sink input %u \"%s\" to %s.", i->index, i->name, target->name);
77 }
78
79
80 return PA_HOOK_OK;
81 }
82
83 static pa_hook_result_t source_hook_callback(pa_core *c, pa_source *source, void* userdata) {
84 pa_source_output *o;
85 pa_source *target;
86
87 assert(c);
88 assert(source);
89
90 if (!pa_idxset_size(source->outputs)) {
91 pa_log_debug("No source outputs to move away.");
92 return PA_HOOK_OK;
93 }
94
95 if (!(target = pa_namereg_get(c, NULL, PA_NAMEREG_SOURCE, 0))) {
96 pa_log_info("No evacuation source found.");
97 return PA_HOOK_OK;
98 }
99
100 assert(target != source);
101
102 while ((o = pa_idxset_first(source->outputs, NULL))) {
103 if (pa_source_output_move_to(o, target) < 0) {
104 pa_log_warn("Failed to move source output %u \"%s\" to %s.", o->index, o->name, target->name);
105 return PA_HOOK_OK;
106 }
107
108 pa_log_info("Sucessfully moved source output %u \"%s\" to %s.", o->index, o->name, target->name);
109 }
110
111
112 return PA_HOOK_OK;
113 }
114
115 int pa__init(pa_core *c, pa_module*m) {
116 pa_modargs *ma = NULL;
117 struct userdata *u;
118
119 assert(c);
120 assert(m);
121
122 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
123 pa_log("Failed to parse module arguments");
124 return -1;
125 }
126
127 m->userdata = u = pa_xnew(struct userdata, 1);
128 u->sink_slot = pa_hook_connect(&c->hook_sink_disconnect, (pa_hook_cb_t) sink_hook_callback, NULL);
129 u->source_slot = pa_hook_connect(&c->hook_source_disconnect, (pa_hook_cb_t) source_hook_callback, NULL);
130
131 pa_modargs_free(ma);
132 return 0;
133 }
134
135 void pa__done(pa_core *c, pa_module*m) {
136 struct userdata *u;
137
138 assert(c);
139 assert(m);
140
141 if (!m->userdata)
142 return;
143
144 u = m->userdata;
145 if (u->sink_slot)
146 pa_hook_slot_free(u->sink_slot);
147 if (u->source_slot)
148 pa_hook_slot_free(u->source_slot);
149
150 pa_xfree(u);
151 }