~ubuntu-branches/ubuntu/precise/pulseaudio/precise

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
From 1f4e7121bbc577f275faf008800da820e12b7004 Mon Sep 17 00:00:00 2001
From: David Henningsson <david.henningsson@canonical.com>
Date: Mon, 5 Sep 2011 14:20:48 +0200
Subject: [PATCH 2/6] Add a new module switch-on-port-available that acts on
 port changes

---
 src/Makefile.am                               |    6 +
 src/modules/module-switch-on-port-available.c |  217 +++++++++++++++++++++++++
 2 files changed, 223 insertions(+), 0 deletions(-)
 create mode 100644 src/modules/module-switch-on-port-available.c

diff --git a/src/Makefile.am b/src/Makefile.am
index 4260060..9fa7ea8 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -1002,6 +1002,7 @@ modlibexec_LTLIBRARIES += \
 		module-virtual-sink.la \
 		module-virtual-source.la \
 		module-switch-on-connect.la \
+		module-switch-on-port-available.la \
 		module-filter-apply.la \
 		module-filter-heuristics.la
 
@@ -1301,6 +1302,7 @@ SYMDEF_FILES = \
 		module-virtual-sink-symdef.h \
 		module-virtual-source-symdef.h \
 		module-switch-on-connect-symdef.h \
+		module-switch-on-port-available-symdef.h \
 		module-filter-apply-symdef.h \
 		module-filter-heuristics-symdef.h
 
@@ -1456,6 +1458,10 @@ module_switch_on_connect_la_SOURCES = modules/module-switch-on-connect.c
 module_switch_on_connect_la_LDFLAGS = $(MODULE_LDFLAGS)
 module_switch_on_connect_la_LIBADD = $(MODULE_LIBADD)
 
+module_switch_on_port_available_la_SOURCES = modules/module-switch-on-port-available.c
+module_switch_on_port_available_la_LDFLAGS = $(MODULE_LDFLAGS)
+module_switch_on_port_available_la_LIBADD = $(MODULE_LIBADD)
+
 module_filter_apply_la_SOURCES = modules/module-filter-apply.c
 module_filter_apply_la_LDFLAGS = $(MODULE_LDFLAGS)
 module_filter_apply_la_LIBADD = $(MODULE_LIBADD)
diff --git a/src/modules/module-switch-on-port-available.c b/src/modules/module-switch-on-port-available.c
new file mode 100644
index 0000000..8ee3192
--- /dev/null
+++ b/src/modules/module-switch-on-port-available.c
@@ -0,0 +1,217 @@
+/***
+  This file is part of PulseAudio.
+
+  Copyright 2006 Lennart Poettering
+  Copyright 2011 Canonical Ltd
+
+  PulseAudio is free software; you can redistribute it and/or modify
+  it under the terms of the GNU Lesser General Public License as published
+  by the Free Software Foundation; either version 2.1 of the License,
+  or (at your option) any later version.
+
+  PulseAudio is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU Lesser General Public License
+  along with PulseAudio; if not, write to the Free Software
+  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
+  USA.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <pulsecore/core.h>
+#include <pulsecore/device-port.h>
+#include <pulsecore/hashmap.h>
+
+#include "module-switch-on-port-available-symdef.h"
+
+struct userdata {
+     pa_hook_slot *callback_slot;
+ };
+
+static pa_device_port* find_best_port(pa_hashmap *ports) {
+    void *state;
+    pa_device_port* port, *result = NULL;
+
+    PA_HASHMAP_FOREACH(port, ports, state) {
+        if (result == NULL ||
+            result->available == PA_PORT_AVAILABLE_NO ||
+            (port->available != PA_PORT_AVAILABLE_NO && port->priority > result->priority)) {
+            result = port;
+        }
+    }
+    return result;
+}
+
+static pa_bool_t try_to_switch_profile(pa_card *card, pa_device_port *port) {
+    pa_card_profile *best_profile = NULL, *profile;
+    void *state;
+
+    pa_log_debug("Finding best profile");
+    if (port->profiles)
+        PA_HASHMAP_FOREACH(profile, port->profiles, state) {
+            if (best_profile && best_profile->priority >= profile->priority)
+                continue;
+            /* We make a best effort to keep other direction unchanged */
+            if (card->active_profile && !port->is_input) {
+                if (card->active_profile->n_sources != profile->n_sources)
+                    continue;
+                if (card->active_profile->max_source_channels != profile->max_source_channels)
+                    continue;
+            }
+            if (card->active_profile && !port->is_output) {
+                if (card->active_profile->n_sinks != profile->n_sinks)
+                    continue;
+                if (card->active_profile->max_sink_channels != profile->max_sink_channels)
+                    continue;
+            }
+            best_profile = profile;
+        }
+
+    if (!best_profile) {
+        pa_log_debug("No suitable profile found");
+        return FALSE;
+    }
+    if (pa_card_set_profile(card, best_profile->name, FALSE) != 0) {
+        pa_log_debug("Could not set profile %s", best_profile->name);
+        return FALSE;
+    }
+    return TRUE;
+}
+
+static void find_sink_and_source(pa_card *card, pa_device_port *port, pa_sink **si, pa_source **so)
+{
+    pa_sink *sink = NULL;
+    pa_source *source = NULL;
+    uint32_t state;
+
+    if (port->is_output)
+        PA_IDXSET_FOREACH(sink, card->sinks, state)
+            if (sink->ports && port == pa_hashmap_get(sink->ports, port->name))
+                break;
+
+    if (port->is_input)
+        PA_IDXSET_FOREACH(source, card->sources, state)
+            if (source->ports && port == pa_hashmap_get(source->ports, port->name))
+                break;
+
+    *si = sink;
+    *so = source;
+}
+
+static pa_hook_result_t port_available_hook_callback(pa_core *c, pa_device_port *port, void* userdata) {
+    uint32_t state;
+    pa_card* card;
+    pa_sink *sink;
+    pa_source *source;
+    pa_bool_t is_active_profile, is_active_port;
+
+    pa_log_debug("finding port %s", port->name);
+    PA_IDXSET_FOREACH(card, c->cards, state)
+        if (card->ports && port == pa_hashmap_get(card->ports, port->name))
+            break;
+
+    if (!card) {
+        pa_log_warn("Did not find port %s in array of cards", port->name);
+        return PA_HOOK_OK;
+    }
+
+    find_sink_and_source(card, port, &sink, &source);
+
+    is_active_profile = port->profiles && card->active_profile &&
+        card->active_profile == pa_hashmap_get(port->profiles, card->active_profile->name);
+    is_active_port = (sink && sink->active_port == port) || (source && source->active_port == port);
+
+    if (port->available == PA_PORT_AVAILABLE_NO && !is_active_port)
+        return PA_HOOK_OK;
+
+    if (port->available == PA_PORT_AVAILABLE_YES) {
+        if (is_active_port)
+            return PA_HOOK_OK;
+        if (!is_active_profile) {
+            if (!try_to_switch_profile(card, port))
+                return PA_HOOK_OK;
+            pa_assert(card->active_profile == pa_hashmap_get(port->profiles, card->active_profile->name));
+            /* Now that profile has changed, our sink and source pointers must be updated */
+            find_sink_and_source(card, port, &sink, &source);
+        }
+
+        if (source)
+            pa_source_set_port(source, port->name, FALSE);
+        if (sink)
+            pa_sink_set_port(sink, port->name, FALSE);
+    }
+
+    if (port->available == PA_PORT_AVAILABLE_NO) {
+        if (sink) {
+            pa_device_port *p2 = NULL;
+            p2 = find_best_port(sink->ports);
+            if (p2 && p2->available != PA_PORT_AVAILABLE_NO)
+                pa_sink_set_port(sink, p2->name, FALSE);
+            else {
+                /* Maybe try to switch to another profile? */
+            }
+        }
+
+        if (source) {
+            pa_device_port *p2 = NULL;
+            p2 = find_best_port(source->ports);
+            if (p2 && p2->available != PA_PORT_AVAILABLE_NO)
+                pa_source_set_port(source, p2->name, FALSE);
+            else {
+                /* Maybe try to switch to another profile? */
+            }
+        }
+    }
+
+    return PA_HOOK_OK;
+}
+
+static void handle_all_unavailable(pa_core *core) {
+    pa_card *card;
+    uint32_t state;
+
+    PA_IDXSET_FOREACH(card, core->cards, state) {
+        pa_device_port *port;
+        void *state2;
+        if (!card->ports)
+            continue;
+        PA_HASHMAP_FOREACH(port, card->ports, state2) {
+            if (port->available == PA_PORT_AVAILABLE_NO)
+                port_available_hook_callback(core, port, NULL);
+        }
+    }
+}
+
+int pa__init(pa_module*m) {
+    struct userdata *u;
+
+    pa_assert(m);
+
+    m->userdata = u = pa_xnew(struct userdata, 1);
+
+    u->callback_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_PORT_AVAILABLE_CHANGED], PA_HOOK_LATE, (pa_hook_cb_t) port_available_hook_callback, u);
+
+    handle_all_unavailable(m->core);
+
+    return 0;
+}
+
+void pa__done(pa_module*m) {
+    struct userdata *u;
+
+    pa_assert(m);
+
+    if (!(u = m->userdata))
+        return;
+
+    if (u->callback_slot)
+        pa_hook_slot_free(u->callback_slot);
+
+    pa_xfree(u);
+}
-- 
1.7.9