void VolumeBarLogic::openConnection (bool init)
{
    /*
     * Check the connection first, maybe this function
     * only called because of lost connection
     */
    if ((dbus_conn != NULL) && (dbus_connection_get_is_connected (dbus_conn)))
        return;

    DBusError dbus_err;
    char *pa_bus_address = getenv ("PULSE_DBUS_SERVER");

    if (pa_bus_address == NULL)
        pa_bus_address = (char *) DEFAULT_ADDRESS;

    dbus_error_init (&dbus_err);

    dbus_conn = dbus_connection_open (pa_bus_address, &dbus_err);

    DBUS_ERR_CHECK (dbus_err);

    if (dbus_conn != NULL) {
        dbus_connection_setup_with_g_main (dbus_conn, NULL);

        dbus_connection_add_filter (
            dbus_conn,
            (DBusHandleMessageFunction) VolumeBarLogic::stepsUpdatedSignal,
            (void *) this, NULL);

        if (init == true)
            initValues ();
    }
}
Exemplo n.º 2
0
DBusHandlerResult PulseAudioControl::stepsUpdatedSignalHandler(DBusConnection *, DBusMessage *message, void *control)
{
    if (message && dbus_message_has_member(message, "StepsUpdated")) {
        DBusError error;
        quint32 currentStep = 0;
        quint32 stepCount = 0;

        dbus_error_init(&error);

        if (dbus_message_get_args(message, &error, DBUS_TYPE_UINT32, &stepCount, DBUS_TYPE_UINT32, &currentStep, DBUS_TYPE_INVALID)) {
            static_cast<PulseAudioControl*>(control)->setSteps(currentStep, stepCount);
        }

        DBUS_ERR_CHECK (error);
    }

    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
}
void VolumeBarLogic::stepsUpdatedSignal (
    DBusConnection *conn, DBusMessage *message, VolumeBarLogic *logic)
{
    Q_UNUSED (conn);

    if (message && dbus_message_has_member (message, "StepsUpdated")) {
        DBusError error;
        quint32   value = 0;
        quint32   maxvalue = 0;

        dbus_error_init (&error);

        if (dbus_message_get_args (message, &error,
                                   DBUS_TYPE_UINT32, &maxvalue,
                                   DBUS_TYPE_UINT32, &value,
                                   DBUS_TYPE_INVALID)) {
            logic->stepsUpdated (value, maxvalue);
        }

        DBUS_ERR_CHECK (error);
    }
}
Exemplo n.º 4
0
void PulseAudioControl::openConnection()
{
    //! If the connection already exists, do nothing
    if ((dbusConnection != NULL) && (dbus_connection_get_is_connected(dbusConnection))) {
        return;
    }

    // Establish a connection to the server
    char *pa_bus_address = getenv("PULSE_DBUS_SERVER");
    QByteArray addressArray;
    if (pa_bus_address == NULL) {
        QDBusMessage message = QDBusMessage::createMethodCall("org.pulseaudio.Server", "/org/pulseaudio/server_lookup1", "org.freedesktop.DBus.Properties", "Get");
        message.setArguments(QVariantList() << "org.PulseAudio.ServerLookup1" << "Address");
        QDBusMessage reply = QDBusConnection::sessionBus().call(message);
        if (reply.type() == QDBusMessage::ReplyMessage && reply.arguments().count() > 0) {
            addressArray = reply.arguments().first().value<QDBusVariant>().variant().toString().toAscii();
            pa_bus_address = addressArray.data();
        }
    }

    if (pa_bus_address != NULL) {
        DBusError dbus_err;
        dbus_error_init(&dbus_err);

        dbusConnection = dbus_connection_open(pa_bus_address, &dbus_err);

        DBUS_ERR_CHECK(dbus_err);
    }

    if (dbusConnection != NULL) {
        dbus_connection_setup_with_g_main(dbusConnection, NULL);
        dbus_connection_add_filter(dbusConnection, PulseAudioControl::stepsUpdatedSignalHandler, (void *)this, NULL);

        addSignalMatch();
    }
}
void VolumeBarLogic::initValues ()
{
    DBusMessage *msg;
    DBusMessage *reply;
    DBusError    error;

    dbus_error_init (&error);

    msg = dbus_message_new_method_call (VOLUME_SV,
                                        VOLUME_PATH,
                                        "org.freedesktop.DBus.Properties",
                                        "GetAll");
    const char *volume_if = VOLUME_IF;
    dbus_message_append_args (msg,
                              DBUS_TYPE_STRING, &volume_if,
                              DBUS_TYPE_INVALID);

    reply =
        dbus_connection_send_with_reply_and_block (
                        dbus_conn, msg, -1, &error);

    DBUS_ERR_CHECK (error);

    dbus_message_unref (msg);

    if (reply && (dbus_message_get_type (reply) ==
                  DBUS_MESSAGE_TYPE_METHOD_RETURN)) {
        DBusMessageIter iter;
        dbus_message_iter_init (reply, &iter);
        // Recurse into the array [array of dicts]
        while (dbus_message_iter_get_arg_type (&iter) != DBUS_TYPE_INVALID) {
            DBusMessageIter dict_entry;
            dbus_message_iter_recurse (&iter, &dict_entry);

            // Recurse into the dict [ dict_entry (string, variant(int)) ]
            while (dbus_message_iter_get_arg_type (&dict_entry) != DBUS_TYPE_INVALID) {
                DBusMessageIter in_dict;
                // Recurse into the dict_entry [ string, variant(int) ]
                dbus_message_iter_recurse (&dict_entry, &in_dict);
                {
                  char *prop_name = NULL;
                  // Get the string value, "property name"
                  dbus_message_iter_get_basic (&in_dict, &prop_name);

                  dbus_message_iter_next (&in_dict);

                  DBusMessageIter variant;
                  // Recurese into the variant [ variant(int) ]
                  dbus_message_iter_recurse (&in_dict, &variant);

                  quint32 value;
                  // Get the variant value which is uint32
                  dbus_message_iter_get_basic (&variant, &value);

                  if (prop_name &&
                      strcmp (prop_name, "StepCount") == 0)
                    currentmax = value;
                  else if (prop_name &&
                           strcmp (prop_name, "CurrentStep") == 0)
                    currentvolume = value;
                }

                dbus_message_iter_next (&dict_entry);
            }
            dbus_message_iter_next (&iter);
        }
    }

    if (reply)
        dbus_message_unref (reply);

    addSignalMatch ();
}
Exemplo n.º 6
0
void PulseAudioControl::update()
{
    openConnection();

    if (dbusConnection == NULL) {
        return;
    }

    DBusError error;
    dbus_error_init(&error);

    DBusMessage *reply = NULL;
    DBusMessage *msg = dbus_message_new_method_call(VOLUME_SERVICE, VOLUME_PATH, "org.freedesktop.DBus.Properties", "GetAll");
    if (msg != NULL) {
        dbus_message_append_args(msg, DBUS_TYPE_STRING, &VOLUME_INTERFACE, DBUS_TYPE_INVALID);

        reply = dbus_connection_send_with_reply_and_block(dbusConnection, msg, -1, &error);

        DBUS_ERR_CHECK (error);

        dbus_message_unref(msg);
    }

    int currentStep = -1, stepCount = -1;
    if (reply != NULL) {
        if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_METHOD_RETURN) {
            DBusMessageIter iter;
            dbus_message_iter_init(reply, &iter);
            // Recurse into the array [array of dicts]
            while (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_INVALID) {
                DBusMessageIter dict_entry;
                dbus_message_iter_recurse(&iter, &dict_entry);

                // Recurse into the dict [ dict_entry (string, variant(int)) ]
                while (dbus_message_iter_get_arg_type(&dict_entry) != DBUS_TYPE_INVALID) {
                    DBusMessageIter in_dict;
                    // Recurse into the dict_entry [ string, variant(int) ]
                    dbus_message_iter_recurse(&dict_entry, &in_dict);
                    {
                        char *prop_name = NULL;
                        // Get the string value, "property name"
                        dbus_message_iter_get_basic(&in_dict, &prop_name);

                        dbus_message_iter_next(&in_dict);

                        DBusMessageIter variant;
                        // Recurse into the variant [ variant(int) ]
                        dbus_message_iter_recurse(&in_dict, &variant);

                        quint32 value;
                        // Get the variant value which is uint32
                        dbus_message_iter_get_basic(&variant, &value);

                        if (prop_name && strcmp(prop_name, "StepCount") == 0) {
                            stepCount = value;
                        } else if (prop_name && strcmp(prop_name, "CurrentStep") == 0) {
                            currentStep = value;
                        }
                    }

                    dbus_message_iter_next(&dict_entry);
                }
                dbus_message_iter_next(&iter);
            }
        }
        dbus_message_unref(reply);
    }

    if (currentStep != -1 && stepCount != -1) {
        setSteps(currentStep, stepCount);
    }
}