diff --git a/CMakeLists.txt b/CMakeLists.txt index 23bf5d6..bce85fb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,91 +1,91 @@ cmake_minimum_required(VERSION 3.0) project(PulseAudioQt VERSION 1.0.1) include(FeatureSummary) find_package(ECM 5.44.0 NO_MODULE) set_package_properties(ECM PROPERTIES TYPE REQUIRED DESCRIPTION "Extra CMake Modules." URL "https://kde.org/products/frameworks/") feature_summary(WHAT REQUIRED_PACKAGES_NOT_FOUND FATAL_ON_MISSING_REQUIRED_PACKAGES) set(CMAKE_MODULE_PATH ${ECM_MODULE_PATH} ${ECM_KDE_MODULE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/cmake) include(GenerateExportHeader) include(CMakePackageConfigHelpers) include(ECMSetupVersion) include(ECMGenerateHeaders) include(ECMQtDeclareLoggingCategory) include(ECMAddQch) include(KDEInstallDirs) include(KDECompilerSettings NO_POLICY_SCOPE) # include(KDEFrameworkCompilerSettings NO_POLICY_SCOPE) include(KDECMakeSettings) option(BUILD_QCH "Build API documentation in QCH format (for e.g. Qt Assistant, Qt Creator & KDevelop)" OFF) add_feature_info(QCH ${BUILD_QCH} "API documentation in QCH format (for e.g. Qt Assistant, Qt Creator & KDevelop)") ecm_setup_version(PROJECT VARIABLE_PREFIX PULSEAUDIOQT VERSION_HEADER "${CMAKE_CURRENT_BINARY_DIR}/pulseaudioqt_version.h" PACKAGE_VERSION_FILE "${CMAKE_CURRENT_BINARY_DIR}/KF5PulseAudioQtConfigVersion.cmake" - SOVERSION 1) + SOVERSION 2) set(REQUIRED_QT_VERSION 5.10.0) find_package(Qt5 ${REQUIRED_QT_VERSION} CONFIG REQUIRED Core Gui DBus) find_package(Qt5Test ${REQUIRED_QT_VERSION} CONFIG) set_package_properties(Qt5Test PROPERTIES DESCRIPTION "Autotests for PulseAudioQt" TYPE OPTIONAL) find_package(Qt5Qml ${REQUIRED_QT_VERSION} CONFIG) set_package_properties(Qt5Qml PROPERTIES DESCRIPTION "Tests for PulseAudioQt" TYPE OPTIONAL) find_package(Qt5Quick ${REQUIRED_QT_VERSION} CONFIG) set_package_properties(Qt5Quick PROPERTIES DESCRIPTION "Tests for PulseAudioQt" TYPE OPTIONAL) # create a Config.cmake and a ConfigVersion.cmake file and install them set(CMAKECONFIG_INSTALL_DIR "${KDE_INSTALL_CMAKEPACKAGEDIR}/KF5PulseAudioQt") if (BUILD_QCH) ecm_install_qch_export( TARGETS KF5PulseAudioQt_QCH FILE KF5PulseAudioQtQchTargets.cmake DESTINATION "${CMAKECONFIG_INSTALL_DIR}" COMPONENT Devel ) set(PACKAGE_INCLUDE_QCHTARGETS "include(\"\${CMAKE_CURRENT_LIST_DIR}/KF5PulseAudioQtQchTargets.cmake\")") endif() install(FILES "${CMAKE_CURRENT_BINARY_DIR}/KF5PulseAudioQtConfig.cmake" "${CMAKE_CURRENT_BINARY_DIR}/KF5PulseAudioQtConfigVersion.cmake" DESTINATION "${CMAKECONFIG_INSTALL_DIR}" COMPONENT Devel ) configure_package_config_file( "${CMAKE_CURRENT_SOURCE_DIR}/KF5PulseAudioQtConfig.cmake.in" "${CMAKE_CURRENT_BINARY_DIR}/KF5PulseAudioQtConfig.cmake" INSTALL_DESTINATION ${CMAKECONFIG_INSTALL_DIR} ) find_package(PulseAudio 5.0.0 REQUIRED) find_package(GLIB2 REQUIRED) if (IS_DIRECTORY "${CMAKE_CURRENT_SOURCE_DIR}/po") ecm_install_po_files_as_qm(po) endif() add_subdirectory(src) if (BUILD_TESTING) add_subdirectory(tests) if(Qt5Test_FOUND) add_subdirectory(autotests) endif() endif() install(EXPORT KF5PulseAudioQtTargets DESTINATION "${CMAKECONFIG_INSTALL_DIR}" FILE KF5PulseAudioQtTargets.cmake NAMESPACE KF5:: ) install(FILES ${CMAKE_CURRENT_BINARY_DIR}/pulseaudioqt_version.h DESTINATION ${KDE_INSTALL_INCLUDEDIR_KF5} COMPONENT Devel ) feature_summary(WHAT ALL FATAL_ON_MISSING_REQUIRED_PACKAGES) diff --git a/src/card.cpp b/src/card.cpp index 5312525..7cff602 100644 --- a/src/card.cpp +++ b/src/card.cpp @@ -1,112 +1,134 @@ /* Copyright 2014-2015 Harald Sitter This library 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) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #include "card.h" #include "card_p.h" #include "debug.h" #include "context.h" #include "pulseobject.h" #include "pulseobject_p.h" #include "cardport.h" #include "profile_p.h" namespace PulseAudioQt { Card::Card(QObject *parent) : PulseObject(parent) , d(new CardPrivate(this)) { } Card::~Card(){ delete d; } CardPrivate::CardPrivate(Card* q) : q(q) { } CardPrivate::~CardPrivate() { } void CardPrivate::update(const pa_card_info *info) { q->PulseObject::d->updatePulseObject(info); QString infoName = QString::fromUtf8(info->name); if (m_name != infoName) { m_name = infoName; Q_EMIT q->nameChanged(); } - qDeleteAll(m_profiles); - m_profiles.clear(); + QStringList newProfiles; for (auto **it = info->profiles2; it && *it != nullptr; ++it) { - Profile *profile = new Profile(q); + const QString name = QString::fromUtf8((*it)->name); + newProfiles << name; + if (!m_profiles.contains(name)) { + m_profiles[name] = new Profile(q); + } + Profile *profile = m_profiles[name]; profile->d->setInfo(*it); - m_profiles.append(profile); if (info->active_profile2 == *it) { - m_activeProfileIndex = m_profiles.length() - 1; + m_activeProfileIndex = m_profiles.size() - 1; } } + + const QList profileKeys = m_profiles.keys(); + for (const QString &profileKey : profileKeys) { + if (!newProfiles.contains(profileKey)) { + delete m_profiles.take(profileKey); + } + } + Q_EMIT q->profilesChanged(); Q_EMIT q->activeProfileIndexChanged(); - qDeleteAll(m_ports); - m_ports.clear(); + QStringList newPorts; for (auto **it = info->ports; it && *it != nullptr; ++it) { - CardPort *port = new CardPort(q); + const QString name = QString::fromUtf8((*it)->name); + newPorts << name; + if (!m_ports.contains(name)) { + m_ports[name] = new CardPort(q); + } + CardPort *port = m_ports[name]; port->update(*it); - m_ports.append(port); } + + const QList portKeys = m_ports.keys(); + for (const QString &portKey : profileKeys) { + if (!newPorts.contains(portKey)) { + delete m_ports.take(portKey); + } + } + Q_EMIT q->portsChanged(); } QString Card::name() const { return d->m_name; } QVector Card::profiles() const { - return d->m_profiles; + return QVector::fromList(d->m_profiles.values()); } quint32 Card::activeProfileIndex() const { return d->m_activeProfileIndex; } void Card::setActiveProfileIndex(quint32 profileIndex) { const Profile *profile = qobject_cast(profiles().at(profileIndex)); context()->setCardProfile(index(), profile->name()); } -QVector Card::ports() const +QVector Card::ports() const { - return d->m_ports; + return QVector::fromList(d->m_ports.values()); } } // PulseAudioQt diff --git a/src/card.h b/src/card.h index 89e1d4f..6869462 100644 --- a/src/card.h +++ b/src/card.h @@ -1,66 +1,66 @@ /* Copyright 2014-2015 Harald Sitter This library 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) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #ifndef CARD_H #define CARD_H #include "pulseobject.h" struct pa_card_info; namespace PulseAudioQt { -class Port; +class CardPort; class Profile; class Card : public PulseObject { Q_OBJECT Q_PROPERTY(QString name READ name NOTIFY nameChanged) Q_PROPERTY(QVector profiles READ profiles NOTIFY profilesChanged) Q_PROPERTY(quint32 activeProfileIndex READ activeProfileIndex WRITE setActiveProfileIndex NOTIFY activeProfileIndexChanged) - Q_PROPERTY(QVector ports READ ports NOTIFY portsChanged) + Q_PROPERTY(QVector ports READ ports NOTIFY portsChanged) public: ~Card(); QString name() const; QVector profiles() const; quint32 activeProfileIndex() const; void setActiveProfileIndex(quint32 profileIndex); - QVector ports() const; + QVector ports() const; Q_SIGNALS: void nameChanged(); void profilesChanged(); void activeProfileIndexChanged(); void portsChanged(); private: explicit Card(QObject *parent); class CardPrivate *const d; friend class MapBase; }; } // PulseAudioQt #endif // CARD_H diff --git a/src/card_p.h b/src/card_p.h index 715c62c..239e5a7 100644 --- a/src/card_p.h +++ b/src/card_p.h @@ -1,49 +1,51 @@ /* Copyright 2018 Nicolas Fella This library 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) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #ifndef CARD_P_H #define CARD_P_H #include "card.h" #include "profile.h" +#include "cardport.h" #include +#include #include namespace PulseAudioQt { class CardPrivate { public: explicit CardPrivate(Card *q); virtual ~CardPrivate(); Card *q; void update(const pa_card_info *info); QString m_name; - QVector m_profiles; + QHash m_profiles; quint32 m_activeProfileIndex; - QVector m_ports; + QHash m_ports; }; } #endif diff --git a/src/device.cpp b/src/device.cpp index bc65d2b..5a7ef17 100644 --- a/src/device.cpp +++ b/src/device.cpp @@ -1,92 +1,92 @@ /* Copyright 2014-2015 Harald Sitter This library 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) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #include "device.h" #include "device_p.h" namespace PulseAudioQt { Device::State Device::state() const { return d->m_state; } QString Device::name() const { return d->m_name; } QString Device::description() const { return d->m_description; } QString Device::formFactor() const { return d->m_formFactor; } quint32 Device::cardIndex() const { return d->m_cardIndex; } QVector Device::ports() const { - return d->m_ports; + return QVector::fromList(d->m_ports.values()); } quint32 Device::activePortIndex() const { return d->m_activePortIndex; } Device::Device(QObject *parent) : VolumeObject(parent) , d(new DevicePrivate(this)) { } DevicePrivate::DevicePrivate(Device *q) : q(q) { } Device::State DevicePrivate::stateFromPaState(int value) const { switch (value) { case -1: // PA_X_INVALID_STATE return Device::InvalidState; case 0: // PA_X_RUNNING return Device::RunningState; case 1: // PA_X_IDLE return Device::IdleState; case 2: // PA_X_SUSPENDED return Device::SuspendedState; default: return Device::UnknownState; } } Device::~Device(){ delete d; } } // namespace PulseAudioQt diff --git a/src/device_p.h b/src/device_p.h index 6801d4e..711f399 100644 --- a/src/device_p.h +++ b/src/device_p.h @@ -1,106 +1,114 @@ /* Copyright 2018 Nicolas Fella This library 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) version 3, or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 6 of version 3 of the license. This library 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library. If not, see . */ #ifndef DEVICE_P_H #define DEVICE_P_H #include #include +#include #include "device.h" #include "port.h" #include "port_p.h" #include "volumeobject_p.h" namespace PulseAudioQt { class DevicePrivate { public: explicit DevicePrivate(Device *q); Device *q; QString m_name; QString m_description; QString m_formFactor; quint32 m_cardIndex = -1; - QVector m_ports; + QHash m_ports; quint32 m_activePortIndex = -1; Device::State m_state = Device::UnknownState; Device::State stateFromPaState(int value) const; template void updateDevice(const PAInfo *info) { q->VolumeObject::d->updateVolumeObject(info); if (m_name != info->name) { m_name = info->name; Q_EMIT q->nameChanged(); } if (m_description != info->description) { m_description = info->description; Q_EMIT q->descriptionChanged(); } const char *form_factor = pa_proplist_gets(info->proplist, PA_PROP_DEVICE_FORM_FACTOR); if (form_factor) { QString formFactor = QString::fromUtf8(form_factor); if (m_formFactor != formFactor) { m_formFactor = formFactor; Q_EMIT q->formFactorChanged(); } } m_cardIndex = info->card; Q_EMIT q->cardIndexChanged(); - // TODO: this rebuilds the entire port list on every update. would be - // nicer if it actually removed what needs removing updates what needs - // updating and adds what needs adding. Alas, this is a tad more - // involved. - qDeleteAll(m_ports); - m_ports.clear(); - for (auto **ports = info->ports; ports && *ports != nullptr; ++ports) { - Port *port = new Port(q); - port->d->setInfo(*ports); - m_ports.append(port); - if (info->active_port == *ports) { - m_activePortIndex = m_ports.length() - 1; + QStringList newPorts; + for (auto **it = info->ports; it && *it != nullptr; ++it) { + const QString name = QString::fromUtf8((*it)->name); + newPorts << name; + if (!m_ports.contains(name)) { + m_ports[name] = new Port(q); + } + Port *port = m_ports[name]; + port->d->setInfo(*it); + if (info->active_port == *it) { + m_activePortIndex = m_ports.size() - 1; } } + + const QList keys = m_ports.keys(); + for (const QString &portKey : keys) { + if (!newPorts.contains(portKey)) { + delete m_ports.take(portKey); + } + } + Q_EMIT q->portsChanged(); Q_EMIT q->activePortIndexChanged(); Device::State infoState = stateFromPaState(info->state); if (infoState != m_state) { m_state = infoState; Q_EMIT q->stateChanged(); } } }; } // namespace PulseAudioQt #endif