2 This file is part of PulseAudio.
4 Copyright 2006-2008 Lennart Poettering
5 Copyright 2009 Colin Guthrie
7 PulseAudio is free software; you can redistribute it and/or modify
8 it under the terms of the GNU Lesser General Public License as published
9 by the Free Software Foundation; either version 2.1 of the License,
10 or (at your option) any later version.
12 PulseAudio is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with PulseAudio; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
30 #include <sys/types.h>
35 #include <pulse/xmalloc.h>
36 #include <pulse/volume.h>
37 #include <pulse/timeval.h>
38 #include <pulse/util.h>
39 #include <pulse/rtclock.h>
41 #include <pulsecore/core-error.h>
42 #include <pulsecore/module.h>
43 #include <pulsecore/core-util.h>
44 #include <pulsecore/modargs.h>
45 #include <pulsecore/log.h>
46 #include <pulsecore/core-subscribe.h>
47 #include <pulsecore/sink-input.h>
48 #include <pulsecore/source-output.h>
49 #include <pulsecore/namereg.h>
50 #include <pulsecore/protocol-native.h>
51 #include <pulsecore/pstream.h>
52 #include <pulsecore/pstream-util.h>
53 #include <pulsecore/database.h>
55 #include "module-device-manager-symdef.h"
57 PA_MODULE_AUTHOR("Colin Guthrie");
58 PA_MODULE_DESCRIPTION("Keep track of devices (and their descriptions) both past and present");
59 PA_MODULE_VERSION(PACKAGE_VERSION
);
60 PA_MODULE_LOAD_ONCE(TRUE
);
61 PA_MODULE_USAGE("This module does not take any arguments");
63 #define SAVE_INTERVAL (10 * PA_USEC_PER_SEC)
65 static const char* const valid_modargs
[] = {
72 pa_subscription
*subscription
;
75 *source_new_hook_slot
,
76 *connection_unlink_hook_slot
;
77 pa_time_event
*save_time_event
;
78 pa_database
*database
;
80 pa_native_protocol
*protocol
;
81 pa_idxset
*subscribed
;
83 pa_bool_t role_device_priority_routing
;
84 pa_bool_t stream_restore_used
;
85 pa_bool_t checked_stream_restore
;
88 #define ENTRY_VERSION 1
105 char description
[PA_NAME_MAX
];
106 uint32_t priority
[NUM_ROLES
];
114 SUBCOMMAND_ROLE_DEVICE_PRIORITY_ROUTING
,
115 SUBCOMMAND_PREFER_DEVICE
,
116 SUBCOMMAND_DEFER_DEVICE
,
117 SUBCOMMAND_SUBSCRIBE
,
121 static void save_time_callback(pa_mainloop_api
*a
, pa_time_event
* e
, const struct timeval
*t
, void *userdata
) {
122 struct userdata
*u
= userdata
;
128 pa_assert(e
== u
->save_time_event
);
129 u
->core
->mainloop
->time_free(u
->save_time_event
);
130 u
->save_time_event
= NULL
;
132 pa_database_sync(u
->database
);
133 pa_log_info("Synced.");
136 static struct entry
* read_entry(struct userdata
*u
, const char *name
) {
143 key
.data
= (char*) name
;
144 key
.size
= strlen(name
);
148 if (!pa_database_get(u
->database
, &key
, &data
))
151 if (data
.size
!= sizeof(struct entry
)) {
152 pa_log_debug("Database contains entry for device %s of wrong size %lu != %lu. Probably due to upgrade, ignoring.", name
, (unsigned long) data
.size
, (unsigned long) sizeof(struct entry
));
156 e
= (struct entry
*) data
.data
;
158 if (e
->version
!= ENTRY_VERSION
) {
159 pa_log_debug("Version of database entry for device %s doesn't match our version. Probably due to upgrade, ignoring.", name
);
163 if (!memchr(e
->description
, 0, sizeof(e
->description
))) {
164 pa_log_warn("Database contains entry for device %s with missing NUL byte in description", name
);
172 pa_datum_free(&data
);
176 static void trigger_save(struct userdata
*u
) {
177 pa_native_connection
*c
;
180 for (c
= pa_idxset_first(u
->subscribed
, &idx
); c
; c
= pa_idxset_next(u
->subscribed
, &idx
)) {
183 t
= pa_tagstruct_new(NULL
, 0);
184 pa_tagstruct_putu32(t
, PA_COMMAND_EXTENSION
);
185 pa_tagstruct_putu32(t
, 0);
186 pa_tagstruct_putu32(t
, u
->module
->index
);
187 pa_tagstruct_puts(t
, u
->module
->name
);
188 pa_tagstruct_putu32(t
, SUBCOMMAND_EVENT
);
190 pa_pstream_send_tagstruct(pa_native_connection_get_pstream(c
), t
);
193 if (u
->save_time_event
)
196 u
->save_time_event
= pa_core_rttime_new(u
->core
, pa_rtclock_now() + SAVE_INTERVAL
, save_time_callback
, u
);
199 static pa_bool_t
entries_equal(const struct entry
*a
, const struct entry
*b
) {
200 if (strncmp(a
->description
, b
->description
, sizeof(a
->description
)))
206 static void subscribe_callback(pa_core
*c
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
207 struct userdata
*u
= userdata
;
208 struct entry entry
, *old
= NULL
;
215 if (t
!= (PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_NEW
) &&
216 t
!= (PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
) &&
217 t
!= (PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_NEW
) &&
218 t
!= (PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
))
222 entry
.version
= ENTRY_VERSION
;
224 if ((t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
) == PA_SUBSCRIPTION_EVENT_SINK
) {
227 if (!(sink
= pa_idxset_get_by_index(c
->sinks
, idx
)))
230 name
= pa_sprintf_malloc("sink:%s", sink
->name
);
232 if ((old
= read_entry(u
, name
)))
235 pa_strlcpy(entry
.description
, pa_strnull(pa_proplist_gets(sink
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)), sizeof(entry
.description
));
240 pa_assert((t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
) == PA_SUBSCRIPTION_EVENT_SOURCE
);
242 if (!(source
= pa_idxset_get_by_index(c
->sources
, idx
)))
245 if (source
->monitor_of
)
248 name
= pa_sprintf_malloc("source:%s", source
->name
);
250 if ((old
= read_entry(u
, name
)))
253 pa_strlcpy(entry
.description
, pa_strnull(pa_proplist_gets(source
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)), sizeof(entry
.description
));
258 if (entries_equal(old
, &entry
)) {
268 key
.size
= strlen(name
);
271 data
.size
= sizeof(entry
);
273 pa_log_info("Storing device description for %s.", name
);
275 pa_database_set(u
->database
, &key
, &data
, TRUE
);
282 static pa_hook_result_t
sink_new_hook_callback(pa_core
*c
, pa_sink_new_data
*new_data
, struct userdata
*u
) {
290 name
= pa_sprintf_malloc("sink:%s", new_data
->name
);
292 if ((e
= read_entry(u
, name
))) {
293 if (strncmp(e
->description
, pa_proplist_gets(new_data
->proplist
, PA_PROP_DEVICE_DESCRIPTION
), sizeof(e
->description
)) != 0) {
294 pa_log_info("Restoring description for sink %s.", new_data
->name
);
295 pa_proplist_sets(new_data
->proplist
, PA_PROP_DEVICE_DESCRIPTION
, e
->description
);
306 static pa_hook_result_t
source_new_hook_callback(pa_core
*c
, pa_source_new_data
*new_data
, struct userdata
*u
) {
314 name
= pa_sprintf_malloc("source:%s", new_data
->name
);
316 if ((e
= read_entry(u
, name
))) {
318 if (strncmp(e
->description
, pa_proplist_gets(new_data
->proplist
, PA_PROP_DEVICE_DESCRIPTION
), sizeof(e
->description
)) != 0) {
319 /* NB, We cannot detect if we are a monitor here... this could mess things up a bit... */
320 pa_log_info("Restoring description for sink %s.", new_data
->name
);
321 pa_proplist_sets(new_data
->proplist
, PA_PROP_DEVICE_DESCRIPTION
, e
->description
);
332 static char *get_name(const char *key
, const char *prefix
) {
335 if (strncmp(key
, prefix
, strlen(prefix
)))
338 t
= pa_xstrdup(key
+ strlen(prefix
));
342 static void apply_entry(struct userdata
*u
, const char *name
, struct entry
*e
) {
352 if ((n
= get_name(name
, "sink:"))) {
353 for (sink
= pa_idxset_first(u
->core
->sinks
, &idx
); sink
; sink
= pa_idxset_next(u
->core
->sinks
, &idx
)) {
354 if (!pa_streq(sink
->name
, n
)) {
358 pa_log_info("Setting description for sink %s.", sink
->name
);
359 pa_sink_set_description(sink
, e
->description
);
363 else if ((n
= get_name(name
, "source:"))) {
364 for (source
= pa_idxset_first(u
->core
->sources
, &idx
); source
; source
= pa_idxset_next(u
->core
->sources
, &idx
)) {
365 if (!pa_streq(source
->name
, n
)) {
369 if (source
->monitor_of
) {
370 pa_log_warn("Cowardly refusing to set the description for monitor source %s.", source
->name
);
374 pa_log_info("Setting description for source %s.", source
->name
);
375 pa_source_set_description(source
, e
->description
);
382 static uint32_t get_role_index(const char* role
) {
385 if (strcmp(role
, "") == 0)
387 if (strcmp(role
, "video") == 0)
389 if (strcmp(role
, "music") == 0)
391 if (strcmp(role
, "game") == 0)
393 if (strcmp(role
, "event") == 0)
395 if (strcmp(role
, "phone") == 0)
397 if (strcmp(role
, "animation") == 0)
398 return ROLE_ANIMATION
;
399 if (strcmp(role
, "production") == 0)
400 return ROLE_PRODUCTION
;
401 if (strcmp(role
, "a11y") == 0)
403 return PA_INVALID_INDEX
;
406 #define EXT_VERSION 1
408 static int extension_cb(pa_native_protocol
*p
, pa_module
*m
, pa_native_connection
*c
, uint32_t tag
, pa_tagstruct
*t
) {
411 pa_tagstruct
*reply
= NULL
;
420 if (pa_tagstruct_getu32(t
, &command
) < 0)
423 reply
= pa_tagstruct_new(NULL
, 0);
424 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
425 pa_tagstruct_putu32(reply
, tag
);
428 case SUBCOMMAND_TEST
: {
429 if (!pa_tagstruct_eof(t
))
432 pa_tagstruct_putu32(reply
, EXT_VERSION
);
436 case SUBCOMMAND_READ
: {
440 if (!pa_tagstruct_eof(t
))
443 done
= !pa_database_first(u
->database
, &key
, NULL
);
450 done
= !pa_database_next(u
->database
, &key
, &next_key
, NULL
);
452 name
= pa_xstrndup(key
.data
, key
.size
);
455 if ((e
= read_entry(u
, name
))) {
456 pa_tagstruct_puts(reply
, name
);
457 pa_tagstruct_puts(reply
, e
->description
);
470 case SUBCOMMAND_RENAME
: {
473 const char *device
, *description
;
475 if (pa_tagstruct_gets(t
, &device
) < 0 ||
476 pa_tagstruct_gets(t
, &description
) < 0)
479 if (!device
|| !*device
|| !description
|| !*description
)
482 if ((e
= read_entry(u
, device
)) && ENTRY_VERSION
== e
->version
) {
485 pa_strlcpy(e
->description
, description
, sizeof(e
->description
));
487 key
.data
= (char *) device
;
488 key
.size
= strlen(device
);
491 data
.size
= sizeof(*e
);
493 if (pa_database_set(u
->database
, &key
, &data
, FALSE
) == 0) {
494 apply_entry(u
, device
, e
);
499 pa_log_warn("Could not save device");
504 pa_log_warn("Could not rename device %s, no entry in database", device
);
509 case SUBCOMMAND_DELETE
:
511 while (!pa_tagstruct_eof(t
)) {
515 if (pa_tagstruct_gets(t
, &name
) < 0)
518 key
.data
= (char*) name
;
519 key
.size
= strlen(name
);
521 pa_database_unset(u
->database
, &key
);
528 case SUBCOMMAND_ROLE_DEVICE_PRIORITY_ROUTING
: {
531 uint32_t sridx
= PA_INVALID_INDEX
;
535 if (pa_tagstruct_get_boolean(t
, &enable
) < 0)
538 /* If this is the first run, check for stream restore module */
539 if (!u
->checked_stream_restore
) {
540 u
->checked_stream_restore
= TRUE
;
542 for (module
= pa_idxset_first(u
->core
->modules
, &idx
); module
; module
= pa_idxset_next(u
->core
->modules
, &idx
)) {
543 if (strcmp(module
->name
, "module-stream-restore") == 0) {
544 pa_log_debug("Detected module-stream-restore is currently in use");
545 u
->stream_restore_used
= TRUE
;
546 sridx
= module
->index
;
551 u
->role_device_priority_routing
= enable
;
553 if (u
->stream_restore_used
) {
554 if (PA_INVALID_INDEX
== sridx
) {
555 /* As a shortcut on first load, we have sridx filled in, but otherwise we search for it. */
556 for (module
= pa_idxset_first(u
->core
->modules
, &idx
); module
; module
= pa_idxset_next(u
->core
->modules
, &idx
)) {
557 if (strcmp(module
->name
, "module-stream-restore") == 0) {
558 sridx
= module
->index
;
562 if (PA_INVALID_INDEX
!= sridx
) {
563 pa_log_debug("Unloading module-stream-restore to enable role-based device-priority routing");
564 pa_module_unload_request_by_index(u
->core
, sridx
, TRUE
);
567 } else if (u
->stream_restore_used
) {
568 /* We want to reload module-stream-restore */
569 if (!pa_module_load(u
->core
, "module-stream-restore", ""))
570 pa_log_warn("Failed to load module-stream-restore while disabling role-based device-priority routing");
576 case SUBCOMMAND_PREFER_DEVICE
:
577 case SUBCOMMAND_DEFER_DEVICE
: {
579 const char *role
, *device
;
583 if (pa_tagstruct_gets(t
, &role
) < 0 ||
584 pa_tagstruct_gets(t
, &device
) < 0)
587 if (!role
|| !device
|| !*device
)
590 role_index
= get_role_index(role
);
591 if (PA_INVALID_INDEX
== role_index
)
594 if ((e
= read_entry(u
, device
)) && ENTRY_VERSION
== e
->version
) {
600 pa_bool_t haschanged
= FALSE
;
602 if (strncmp(device
, "sink:", 5) == 0)
603 prefix
= pa_xstrdup("sink:");
605 prefix
= pa_xstrdup("source:");
607 priority
= e
->priority
[role_index
];
609 /* Now we need to load up all the other entries of this type and shuffle the priroities around */
611 done
= !pa_database_first(u
->database
, &key
, NULL
);
613 while (!done
&& !haschanged
) {
616 done
= !pa_database_next(u
->database
, &key
, &next_key
, NULL
);
618 /* Only read devices with the right prefix */
619 if (key
.size
> strlen(prefix
) && strncmp(key
.data
, prefix
, strlen(prefix
)) == 0) {
623 name
= pa_xstrndup(key
.data
, key
.size
);
626 if ((e2
= read_entry(u
, name
))) {
627 if (SUBCOMMAND_PREFER_DEVICE
== command
) {
629 if (e2
->priority
[role_index
] == (priority
- 1)) {
630 e2
->priority
[role_index
]++;
635 if (e2
->priority
[role_index
] == (priority
+ 1)) {
636 e2
->priority
[role_index
]--;
643 data
.size
= sizeof(*e2
);
645 if (pa_database_set(u
->database
, &key
, &data
, FALSE
))
646 pa_log_warn("Could not save device");
657 /* Now write out our actual entry */
659 if (SUBCOMMAND_PREFER_DEVICE
== command
)
660 e
->priority
[role_index
]--;
662 e
->priority
[role_index
]++;
664 key
.data
= (char *) device
;
665 key
.size
= strlen(device
);
668 data
.size
= sizeof(*e
);
670 if (pa_database_set(u
->database
, &key
, &data
, FALSE
))
671 pa_log_warn("Could not save device");
681 pa_log_warn("Could not reorder device %s, no entry in database", device
);
686 case SUBCOMMAND_SUBSCRIBE
: {
690 if (pa_tagstruct_get_boolean(t
, &enabled
) < 0 ||
691 !pa_tagstruct_eof(t
))
695 pa_idxset_put(u
->subscribed
, c
, NULL
);
697 pa_idxset_remove_by_data(u
->subscribed
, c
, NULL
);
706 pa_pstream_send_tagstruct(pa_native_connection_get_pstream(c
), reply
);
712 pa_tagstruct_free(reply
);
717 static pa_hook_result_t
connection_unlink_hook_cb(pa_native_protocol
*p
, pa_native_connection
*c
, struct userdata
*u
) {
722 pa_idxset_remove_by_data(u
->subscribed
, c
, NULL
);
726 int pa__init(pa_module
*m
) {
727 pa_modargs
*ma
= NULL
;
736 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
737 pa_log("Failed to parse module arguments");
741 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
744 u
->subscribed
= pa_idxset_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
746 u
->protocol
= pa_native_protocol_get(m
->core
);
747 pa_native_protocol_install_ext(u
->protocol
, m
, extension_cb
);
749 u
->connection_unlink_hook_slot
= pa_hook_connect(&pa_native_protocol_hooks(u
->protocol
)[PA_NATIVE_HOOK_CONNECTION_UNLINK
], PA_HOOK_NORMAL
, (pa_hook_cb_t
) connection_unlink_hook_cb
, u
);
751 u
->subscription
= pa_subscription_new(m
->core
, PA_SUBSCRIPTION_MASK_SINK
|PA_SUBSCRIPTION_MASK_SOURCE
, subscribe_callback
, u
);
753 u
->sink_new_hook_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SINK_NEW
], PA_HOOK_EARLY
, (pa_hook_cb_t
) sink_new_hook_callback
, u
);
754 u
->source_new_hook_slot
= pa_hook_connect(&m
->core
->hooks
[PA_CORE_HOOK_SOURCE_NEW
], PA_HOOK_EARLY
, (pa_hook_cb_t
) source_new_hook_callback
, u
);
756 if (!(fname
= pa_state_path("device-manager", TRUE
)))
759 if (!(u
->database
= pa_database_open(fname
, TRUE
))) {
760 pa_log("Failed to open volume database '%s': %s", fname
, pa_cstrerror(errno
));
765 pa_log_info("Sucessfully opened database file '%s'.", fname
);
768 for (sink
= pa_idxset_first(m
->core
->sinks
, &idx
); sink
; sink
= pa_idxset_next(m
->core
->sinks
, &idx
))
769 subscribe_callback(m
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_NEW
, sink
->index
, u
);
771 for (source
= pa_idxset_first(m
->core
->sources
, &idx
); source
; source
= pa_idxset_next(m
->core
->sources
, &idx
))
772 subscribe_callback(m
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_NEW
, source
->index
, u
);
786 void pa__done(pa_module
*m
) {
791 if (!(u
= m
->userdata
))
795 pa_subscription_free(u
->subscription
);
797 if (u
->sink_new_hook_slot
)
798 pa_hook_slot_free(u
->sink_new_hook_slot
);
799 if (u
->source_new_hook_slot
)
800 pa_hook_slot_free(u
->source_new_hook_slot
);
802 if (u
->save_time_event
)
803 u
->core
->mainloop
->time_free(u
->save_time_event
);
806 pa_database_close(u
->database
);
809 pa_native_protocol_remove_ext(u
->protocol
, m
);
810 pa_native_protocol_unref(u
->protocol
);
814 pa_idxset_free(u
->subscribed
, NULL
, NULL
);