[pulseaudio-discuss] [PATCH 01/19] dbus: Use hooks for card profile events

Juho Hämäläinen juho.hamalainen at tieto.com
Thu Mar 19 04:50:42 PDT 2015


---
 src/modules/dbus/iface-card.c | 69 +++++++++++++++++++++----------------------
 1 file changed, 34 insertions(+), 35 deletions(-)

diff --git a/src/modules/dbus/iface-card.c b/src/modules/dbus/iface-card.c
index da975c8..d0f13c9 100644
--- a/src/modules/dbus/iface-card.c
+++ b/src/modules/dbus/iface-card.c
@@ -59,9 +59,9 @@ struct pa_dbusiface_card {
     pa_proplist *proplist;
 
     pa_hook_slot *card_profile_added_slot;
+    pa_hook_slot *card_profile_changed_slot;
 
     pa_dbus_protocol *dbus_protocol;
-    pa_subscription *subscription;
 };
 
 enum property_handler_index {
@@ -432,37 +432,8 @@ static void handle_get_profile_by_name(DBusConnection *conn, DBusMessage *msg, v
     pa_dbus_send_basic_value_reply(conn, msg, DBUS_TYPE_OBJECT_PATH, &profile_path);
 }
 
-static void subscription_cb(pa_core *core, pa_subscription_event_type_t t, uint32_t idx, void *userdata) {
-    pa_dbusiface_card *c = userdata;
-    DBusMessage *signal_msg = NULL;
-
-    pa_assert(core);
-    pa_assert((t & PA_SUBSCRIPTION_EVENT_FACILITY_MASK) == PA_SUBSCRIPTION_EVENT_CARD);
-    pa_assert(c);
-
-    /* We can't use idx != c->card->index, because the c->card pointer may
-     * be stale at this point. */
-    if (pa_idxset_get_by_index(core->cards, idx) != c->card)
-        return;
-
-    if ((t & PA_SUBSCRIPTION_EVENT_TYPE_MASK) != PA_SUBSCRIPTION_EVENT_CHANGE)
-        return;
-
-    if (c->active_profile != c->card->active_profile) {
-        const char *object_path;
-
-        c->active_profile = c->card->active_profile;
-        object_path = pa_dbusiface_card_profile_get_path(pa_hashmap_get(c->profiles, c->active_profile->name));
-
-        pa_assert_se(signal_msg = dbus_message_new_signal(c->path,
-                                                          PA_DBUSIFACE_CARD_INTERFACE,
-                                                          signals[SIGNAL_ACTIVE_PROFILE_UPDATED].name));
-        pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
-
-        pa_dbus_protocol_send_signal(c->dbus_protocol, signal_msg);
-        dbus_message_unref(signal_msg);
-        signal_msg = NULL;
-    }
+static void check_card_proplist(pa_dbusiface_card *c) {
+    DBusMessage *signal_msg;
 
     if (!pa_proplist_equal(c->proplist, c->card->proplist)) {
         DBusMessageIter msg_iter;
@@ -477,10 +448,35 @@ static void subscription_cb(pa_core *core, pa_subscription_event_type_t t, uint3
 
         pa_dbus_protocol_send_signal(c->dbus_protocol, signal_msg);
         dbus_message_unref(signal_msg);
-        signal_msg = NULL;
     }
 }
 
+static pa_hook_result_t card_profile_changed_cb(void *hook_data, void *call_data, void *slot_data) {
+    pa_dbusiface_card *c = slot_data;
+    pa_card_profile *profile = call_data;
+    const char *object_path;
+    DBusMessage *signal_msg;
+
+    if (profile->card != c->card)
+        return PA_HOOK_OK;
+
+    c->active_profile = c->card->active_profile;
+
+    object_path = pa_dbusiface_card_profile_get_path(pa_hashmap_get(c->profiles, c->active_profile->name));
+
+    pa_assert_se(signal_msg = dbus_message_new_signal(c->path,
+                                                      PA_DBUSIFACE_CARD_INTERFACE,
+                                                      signals[SIGNAL_ACTIVE_PROFILE_UPDATED].name));
+    pa_assert_se(dbus_message_append_args(signal_msg, DBUS_TYPE_OBJECT_PATH, &object_path, DBUS_TYPE_INVALID));
+
+    pa_dbus_protocol_send_signal(c->dbus_protocol, signal_msg);
+    dbus_message_unref(signal_msg);
+
+    check_card_proplist(c);
+
+    return PA_HOOK_OK;
+}
+
 static pa_hook_result_t card_profile_added_cb(void *hook_data, void *call_data, void *slot_data) {
     pa_core *core = hook_data;
     pa_dbusiface_card *c = slot_data;
@@ -506,6 +502,8 @@ static pa_hook_result_t card_profile_added_cb(void *hook_data, void *call_data,
     pa_dbus_protocol_send_signal(c->dbus_protocol, signal_msg);
     dbus_message_unref(signal_msg);
 
+    check_card_proplist(c);
+
     return PA_HOOK_OK;
 }
 
@@ -527,7 +525,6 @@ pa_dbusiface_card *pa_dbusiface_card_new(pa_dbusiface_core *core, pa_card *card)
     c->active_profile = card->active_profile;
     c->proplist = pa_proplist_copy(card->proplist);
     c->dbus_protocol = pa_dbus_protocol_get(card->core);
-    c->subscription = pa_subscription_new(card->core, PA_SUBSCRIPTION_MASK_CARD, subscription_cb, c);
 
     PA_HASHMAP_FOREACH(profile, card->profiles, state) {
         pa_dbusiface_card_profile *p = pa_dbusiface_card_profile_new(c, card->core, profile, c->next_profile_index++);
@@ -536,6 +533,8 @@ pa_dbusiface_card *pa_dbusiface_card_new(pa_dbusiface_core *core, pa_card *card)
 
     pa_assert_se(pa_dbus_protocol_add_interface(c->dbus_protocol, c->path, &card_interface_info, c) >= 0);
 
+    c->card_profile_changed_slot = pa_hook_connect(&card->core->hooks[PA_CORE_HOOK_CARD_PROFILE_CHANGED], PA_HOOK_NORMAL,
+                                                   card_profile_changed_cb, c);
     c->card_profile_added_slot = pa_hook_connect(&card->core->hooks[PA_CORE_HOOK_CARD_PROFILE_ADDED], PA_HOOK_NORMAL,
                                                  card_profile_added_cb, c);
 
@@ -548,11 +547,11 @@ void pa_dbusiface_card_free(pa_dbusiface_card *c) {
     pa_assert_se(pa_dbus_protocol_remove_interface(c->dbus_protocol, c->path, card_interface_info.name) >= 0);
 
     pa_hook_slot_free(c->card_profile_added_slot);
+    pa_hook_slot_free(c->card_profile_changed_slot);
 
     pa_hashmap_free(c->profiles);
     pa_proplist_free(c->proplist);
     pa_dbus_protocol_unref(c->dbus_protocol);
-    pa_subscription_free(c->subscription);
 
     pa_xfree(c->path);
     pa_xfree(c);
-- 
1.9.1



More information about the pulseaudio-discuss mailing list