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 if (u
->save_time_event
)
180 u
->save_time_event
= pa_core_rttime_new(u
->core
, pa_rtclock_now() + SAVE_INTERVAL
, save_time_callback
, u
);
183 static pa_bool_t
entries_equal(const struct entry
*a
, const struct entry
*b
) {
184 if (strncmp(a
->description
, b
->description
, sizeof(a
->description
)))
190 static void subscribe_callback(pa_core
*c
, pa_subscription_event_type_t t
, uint32_t idx
, void *userdata
) {
191 struct userdata
*u
= userdata
;
192 struct entry entry
, *old
= NULL
;
199 if (t
!= (PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_NEW
) &&
200 t
!= (PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_CHANGE
) &&
201 t
!= (PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_NEW
) &&
202 t
!= (PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_CHANGE
))
206 entry
.version
= ENTRY_VERSION
;
208 if ((t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
) == PA_SUBSCRIPTION_EVENT_SINK
) {
211 if (!(sink
= pa_idxset_get_by_index(c
->sinks
, idx
)))
214 name
= pa_sprintf_malloc("sink:%s", sink
->name
);
216 if ((old
= read_entry(u
, name
)))
219 pa_strlcpy(entry
.description
, pa_strnull(pa_proplist_gets(sink
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)), sizeof(entry
.description
));
224 pa_assert((t
& PA_SUBSCRIPTION_EVENT_FACILITY_MASK
) == PA_SUBSCRIPTION_EVENT_SOURCE
);
226 if (!(source
= pa_idxset_get_by_index(c
->sources
, idx
)))
229 if (source
->monitor_of
)
232 name
= pa_sprintf_malloc("source:%s", source
->name
);
234 if ((old
= read_entry(u
, name
)))
237 pa_strlcpy(entry
.description
, pa_strnull(pa_proplist_gets(source
->proplist
, PA_PROP_DEVICE_DESCRIPTION
)), sizeof(entry
.description
));
242 if (entries_equal(old
, &entry
)) {
252 key
.size
= strlen(name
);
255 data
.size
= sizeof(entry
);
257 pa_log_info("Storing device description for %s.", name
);
259 pa_database_set(u
->database
, &key
, &data
, TRUE
);
266 static pa_hook_result_t
sink_new_hook_callback(pa_core
*c
, pa_sink_new_data
*new_data
, struct userdata
*u
) {
274 name
= pa_sprintf_malloc("sink:%s", new_data
->name
);
276 if ((e
= read_entry(u
, name
))) {
277 if (strncmp(e
->description
, pa_proplist_gets(new_data
->proplist
, PA_PROP_DEVICE_DESCRIPTION
), sizeof(e
->description
)) != 0) {
278 pa_log_info("Restoring description for sink %s.", new_data
->name
);
279 pa_proplist_sets(new_data
->proplist
, PA_PROP_DEVICE_DESCRIPTION
, e
->description
);
290 static pa_hook_result_t
source_new_hook_callback(pa_core
*c
, pa_source_new_data
*new_data
, struct userdata
*u
) {
298 name
= pa_sprintf_malloc("source:%s", new_data
->name
);
300 if ((e
= read_entry(u
, name
))) {
302 if (strncmp(e
->description
, pa_proplist_gets(new_data
->proplist
, PA_PROP_DEVICE_DESCRIPTION
), sizeof(e
->description
)) != 0) {
303 /* NB, We cannot detect if we are a monitor here... this could mess things up a bit... */
304 pa_log_info("Restoring description for sink %s.", new_data
->name
);
305 pa_proplist_sets(new_data
->proplist
, PA_PROP_DEVICE_DESCRIPTION
, e
->description
);
316 static char *get_name(const char *key
, const char *prefix
) {
319 if (strncmp(key
, prefix
, strlen(prefix
)))
322 t
= pa_xstrdup(key
+ strlen(prefix
));
326 static void apply_entry(struct userdata
*u
, const char *name
, struct entry
*e
) {
336 if ((n
= get_name(name
, "sink:"))) {
337 for (sink
= pa_idxset_first(u
->core
->sinks
, &idx
); sink
; sink
= pa_idxset_next(u
->core
->sinks
, &idx
)) {
338 if (!pa_streq(sink
->name
, n
)) {
342 pa_log_info("Setting description for sink %s.", sink
->name
);
343 pa_sink_set_description(sink
, e
->description
);
347 else if ((n
= get_name(name
, "source:"))) {
348 for (source
= pa_idxset_first(u
->core
->sources
, &idx
); source
; source
= pa_idxset_next(u
->core
->sources
, &idx
)) {
349 if (!pa_streq(source
->name
, n
)) {
353 if (source
->monitor_of
) {
354 pa_log_warn("Cowardly refusing to set the description for monitor source %s.", source
->name
);
358 pa_log_info("Setting description for source %s.", source
->name
);
359 pa_source_set_description(source
, e
->description
);
366 static uint32_t get_role_index(const char* role
) {
369 if (strcmp(role
, "") == 0)
371 if (strcmp(role
, "video") == 0)
373 if (strcmp(role
, "music") == 0)
375 if (strcmp(role
, "game") == 0)
377 if (strcmp(role
, "event") == 0)
379 if (strcmp(role
, "phone") == 0)
381 if (strcmp(role
, "animation") == 0)
382 return ROLE_ANIMATION
;
383 if (strcmp(role
, "production") == 0)
384 return ROLE_PRODUCTION
;
385 if (strcmp(role
, "a11y") == 0)
387 return PA_INVALID_INDEX
;
390 #define EXT_VERSION 1
392 static int extension_cb(pa_native_protocol
*p
, pa_module
*m
, pa_native_connection
*c
, uint32_t tag
, pa_tagstruct
*t
) {
395 pa_tagstruct
*reply
= NULL
;
404 if (pa_tagstruct_getu32(t
, &command
) < 0)
407 reply
= pa_tagstruct_new(NULL
, 0);
408 pa_tagstruct_putu32(reply
, PA_COMMAND_REPLY
);
409 pa_tagstruct_putu32(reply
, tag
);
412 case SUBCOMMAND_TEST
: {
413 if (!pa_tagstruct_eof(t
))
416 pa_tagstruct_putu32(reply
, EXT_VERSION
);
420 case SUBCOMMAND_READ
: {
424 if (!pa_tagstruct_eof(t
))
427 done
= !pa_database_first(u
->database
, &key
, NULL
);
434 done
= !pa_database_next(u
->database
, &key
, &next_key
, NULL
);
436 name
= pa_xstrndup(key
.data
, key
.size
);
439 if ((e
= read_entry(u
, name
))) {
440 pa_tagstruct_puts(reply
, name
);
441 pa_tagstruct_puts(reply
, e
->description
);
454 case SUBCOMMAND_RENAME
: {
457 const char *device
, *description
;
459 if (pa_tagstruct_gets(t
, &device
) < 0 ||
460 pa_tagstruct_gets(t
, &description
) < 0)
463 if (!device
|| !*device
|| !description
|| !*description
)
466 if ((e
= read_entry(u
, device
)) && ENTRY_VERSION
== e
->version
) {
469 pa_strlcpy(e
->description
, description
, sizeof(e
->description
));
471 key
.data
= (char *) device
;
472 key
.size
= strlen(device
);
475 data
.size
= sizeof(*e
);
477 if (pa_database_set(u
->database
, &key
, &data
, FALSE
) == 0) {
478 apply_entry(u
, device
, e
);
483 pa_log_warn("Could not save device");
488 pa_log_warn("Could not rename device %s, no entry in database", device
);
493 case SUBCOMMAND_DELETE
:
495 while (!pa_tagstruct_eof(t
)) {
499 if (pa_tagstruct_gets(t
, &name
) < 0)
502 key
.data
= (char*) name
;
503 key
.size
= strlen(name
);
505 pa_database_unset(u
->database
, &key
);
512 case SUBCOMMAND_ROLE_DEVICE_PRIORITY_ROUTING
: {
515 uint32_t sridx
= PA_INVALID_INDEX
;
519 if (pa_tagstruct_get_boolean(t
, &enable
) < 0)
522 /* If this is the first run, check for stream restore module */
523 if (!u
->checked_stream_restore
) {
524 u
->checked_stream_restore
= TRUE
;
526 for (module
= pa_idxset_first(u
->core
->modules
, &idx
); module
; module
= pa_idxset_next(u
->core
->modules
, &idx
)) {
527 if (strcmp(module
->name
, "module-stream-restore") == 0) {
528 pa_log_debug("Detected module-stream-restore is currently in use");
529 u
->stream_restore_used
= TRUE
;
530 sridx
= module
->index
;
535 u
->role_device_priority_routing
= enable
;
537 if (u
->stream_restore_used
) {
538 if (PA_INVALID_INDEX
== sridx
) {
539 /* As a shortcut on first load, we have sridx filled in, but otherwise we search for it. */
540 for (module
= pa_idxset_first(u
->core
->modules
, &idx
); module
; module
= pa_idxset_next(u
->core
->modules
, &idx
)) {
541 if (strcmp(module
->name
, "module-stream-restore") == 0) {
542 sridx
= module
->index
;
546 if (PA_INVALID_INDEX
!= sridx
) {
547 pa_log_debug("Unloading module-stream-restore to enable role-based device-priority routing");
548 pa_module_unload_request_by_index(u
->core
, sridx
, TRUE
);
551 } else if (u
->stream_restore_used
) {
552 /* We want to reload module-stream-restore */
553 if (!pa_module_load(u
->core
, "module-stream-restore", ""))
554 pa_log_warn("Failed to load module-stream-restore while disabling role-based device-priority routing");
560 case SUBCOMMAND_PREFER_DEVICE
:
561 case SUBCOMMAND_DEFER_DEVICE
: {
563 const char *role
, *device
;
567 if (pa_tagstruct_gets(t
, &role
) < 0 ||
568 pa_tagstruct_gets(t
, &device
) < 0)
571 if (!role
|| !device
|| !*device
)
574 role_index
= get_role_index(role
);
575 if (PA_INVALID_INDEX
== role_index
)
578 if ((e
= read_entry(u
, device
)) && ENTRY_VERSION
== e
->version
) {
584 pa_bool_t haschanged
= FALSE
;
586 if (strncmp(device
, "sink:", 5) == 0)
587 prefix
= pa_xstrdup("sink:");
589 prefix
= pa_xstrdup("source:");
591 priority
= e
->priority
[role_index
];
593 /* Now we need to load up all the other entries of this type and shuffle the priroities around */
595 done
= !pa_database_first(u
->database
, &key
, NULL
);
597 while (!done
&& !haschanged
) {
600 done
= !pa_database_next(u
->database
, &key
, &next_key
, NULL
);
602 /* Only read devices with the right prefix */
603 if (key
.size
> strlen(prefix
) && strncmp(key
.data
, prefix
, strlen(prefix
)) == 0) {
607 name
= pa_xstrndup(key
.data
, key
.size
);
610 if ((e2
= read_entry(u
, name
))) {
611 if (SUBCOMMAND_PREFER_DEVICE
== command
) {
613 if (e2
->priority
[role_index
] == (priority
- 1)) {
614 e2
->priority
[role_index
]++;
619 if (e2
->priority
[role_index
] == (priority
+ 1)) {
620 e2
->priority
[role_index
]--;
627 data
.size
= sizeof(*e2
);
629 if (pa_database_set(u
->database
, &key
, &data
, FALSE
))
630 pa_log_warn("Could not save device");
641 /* Now write out our actual entry */
643 if (SUBCOMMAND_PREFER_DEVICE
== command
)
644 e
->priority
[role_index
]--;
646 e
->priority
[role_index
]++;
648 key
.data
= (char *) device
;
649 key
.size
= strlen(device
);
652 data
.size
= sizeof(*e
);
654 if (pa_database_set(u
->database
, &key
, &data
, FALSE
))
655 pa_log_warn("Could not save device");
665 pa_log_warn("Could not reorder device %s, no entry in database", device
);
670 case SUBCOMMAND_SUBSCRIBE
: {
674 if (pa_tagstruct_get_boolean(t
, &enabled
) < 0 ||
675 !pa_tagstruct_eof(t
))
679 pa_idxset_put(u
->subscribed
, c
, NULL
);
681 pa_idxset_remove_by_data(u
->subscribed
, c
, NULL
);
690 pa_pstream_send_tagstruct(pa_native_connection_get_pstream(c
), reply
);
696 pa_tagstruct_free(reply
);
701 static pa_hook_result_t
connection_unlink_hook_cb(pa_native_protocol
*p
, pa_native_connection
*c
, struct userdata
*u
) {
706 pa_idxset_remove_by_data(u
->subscribed
, c
, NULL
);
710 int pa__init(pa_module
*m
) {
711 pa_modargs
*ma
= NULL
;
720 if (!(ma
= pa_modargs_new(m
->argument
, valid_modargs
))) {
721 pa_log("Failed to parse module arguments");
725 m
->userdata
= u
= pa_xnew0(struct userdata
, 1);
728 u
->subscribed
= pa_idxset_new(pa_idxset_trivial_hash_func
, pa_idxset_trivial_compare_func
);
730 u
->protocol
= pa_native_protocol_get(m
->core
);
731 pa_native_protocol_install_ext(u
->protocol
, m
, extension_cb
);
733 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
);
735 u
->subscription
= pa_subscription_new(m
->core
, PA_SUBSCRIPTION_MASK_SINK
|PA_SUBSCRIPTION_MASK_SOURCE
, subscribe_callback
, u
);
737 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
);
738 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
);
740 if (!(fname
= pa_state_path("device-manager", TRUE
)))
743 if (!(u
->database
= pa_database_open(fname
, TRUE
))) {
744 pa_log("Failed to open volume database '%s': %s", fname
, pa_cstrerror(errno
));
749 pa_log_info("Sucessfully opened database file '%s'.", fname
);
752 for (sink
= pa_idxset_first(m
->core
->sinks
, &idx
); sink
; sink
= pa_idxset_next(m
->core
->sinks
, &idx
))
753 subscribe_callback(m
->core
, PA_SUBSCRIPTION_EVENT_SINK
|PA_SUBSCRIPTION_EVENT_NEW
, sink
->index
, u
);
755 for (source
= pa_idxset_first(m
->core
->sources
, &idx
); source
; source
= pa_idxset_next(m
->core
->sources
, &idx
))
756 subscribe_callback(m
->core
, PA_SUBSCRIPTION_EVENT_SOURCE
|PA_SUBSCRIPTION_EVENT_NEW
, source
->index
, u
);
770 void pa__done(pa_module
*m
) {
775 if (!(u
= m
->userdata
))
779 pa_subscription_free(u
->subscription
);
781 if (u
->sink_new_hook_slot
)
782 pa_hook_slot_free(u
->sink_new_hook_slot
);
783 if (u
->source_new_hook_slot
)
784 pa_hook_slot_free(u
->source_new_hook_slot
);
786 if (u
->save_time_event
)
787 u
->core
->mainloop
->time_free(u
->save_time_event
);
790 pa_database_close(u
->database
);
793 pa_native_protocol_remove_ext(u
->protocol
, m
);
794 pa_native_protocol_unref(u
->protocol
);
798 pa_idxset_free(u
->subscribed
, NULL
, NULL
);