diff --git a/applet/contents/ui/ListItemBase.qml b/applet/contents/ui/ListItemBase.qml index 4047a41..d962eb5 100644 --- a/applet/contents/ui/ListItemBase.qml +++ b/applet/contents/ui/ListItemBase.qml @@ -1,374 +1,393 @@ /* Copyright 2014-2015 Harald Sitter This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 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 14 of version 3 of the license. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . */ import QtQuick 2.4 import QtQuick.Controls 1.0 import QtQuick.Layouts 1.0 import org.kde.kquickcontrolsaddons 2.0 import org.kde.plasma.components 2.0 as PlasmaComponents import org.kde.plasma.core 2.0 as PlasmaCore import org.kde.plasma.extras 2.0 as PlasmaExtras import org.kde.draganddrop 2.0 as DragAndDrop import org.kde.plasma.private.volume 0.1 import "../code/icon.js" as Icon PlasmaComponents.ListItem { id: item property alias label: textLabel.text property alias labelOpacity: textLabel.opacity property alias draggable: dragArea.enabled property alias icon: clientIcon.source property alias iconUsesPlasmaTheme: clientIcon.usesPlasmaTheme property string type checked: dropArea.containsDrag opacity: (draggedStream && draggedStream.deviceIndex == Index) ? 0.3 : 1.0 ListView.delayRemove: dragArea.dragActive Item { width: parent.width height: rowLayout.height RowLayout { id: rowLayout width: parent.width spacing: units.smallSpacing PlasmaCore.IconItem { id: clientIcon Layout.alignment: Qt.AlignHCenter Layout.preferredHeight: column.height * 0.75 Layout.preferredWidth: Layout.preferredHeight source: "unknown" onSourceChanged: { if (!valid && source != "unknown") { source = "unknown"; } } DragAndDrop.DragArea { id: dragArea anchors.fill: parent delegate: parent mimeData { source: item } onDragStarted: { draggedStream = PulseObject; beginMoveStream(type == "sink-input" ? "sink" : "source"); } onDrop: { draggedStream = null; endMoveStream(); } MouseArea { anchors.fill: parent cursorShape: dragArea.enabled ? (pressed && pressedButtons === Qt.LeftButton ? Qt.ClosedHandCursor : Qt.OpenHandCursor) : undefined acceptedButtons: Qt.LeftButton | Qt.MiddleButton onClicked: { if (mouse.button === Qt.MiddleButton) { Muted = !Muted; } } } } } ColumnLayout { id: column spacing: 1 RowLayout { Layout.fillWidth: true PlasmaExtras.Heading { id: textLabel Layout.fillWidth: true height: undefined level: 5 opacity: 0.6 wrapMode: Text.NoWrap elide: Text.ElideRight } SmallToolButton { id: contextMenuButton icon: "application-menu" checkable: true onClicked: contextMenu.show() tooltip: i18n("Show additional options for %1", textLabel.text) } } RowLayout { SmallToolButton { readonly property bool isPlayback: type.substring(0, 4) == "sink" icon: Icon.name(Volume, Muted, isPlayback ? "audio-volume" : "microphone-sensitivity") onClicked: Muted = !Muted tooltip: i18n("Mute %1", textLabel.text) } PlasmaComponents.Slider { id: slider // Helper properties to allow async slider updates. // While we are sliding we must not react to value updates // as otherwise we can easily end up in a loop where value // changes trigger volume changes trigger value changes. property int volume: Volume property bool ignoreValueChange: true property bool forceRaiseMaxVolume: false readonly property bool raiseMaxVolume: forceRaiseMaxVolume || volume >= PulseAudio.NormalVolume * 1.01 Layout.fillWidth: true minimumValue: PulseAudio.MinimalVolume maximumValue: raiseMaxVolume ? PulseAudio.MaximalVolume : maxVolumeValue stepSize: maximumValue / (maximumValue / PulseAudio.NormalVolume * 100.0) visible: HasVolume enabled: VolumeWritable opacity: Muted ? 0.5 : 1 Accessible.name: i18nc("Accessibility data on volume slider", "Adjust volume for %1", textLabel.text) Component.onCompleted: { ignoreValueChange = false; } onVolumeChanged: { var oldIgnoreValueChange = ignoreValueChange; ignoreValueChange = true; value = Volume; ignoreValueChange = oldIgnoreValueChange; } onValueChanged: { if (!ignoreValueChange) { Volume = value; Muted = value == 0; if (type == "sink") { playFeedback(Index); } if (!pressed) { updateTimer.restart(); } } } onPressedChanged: { if (!pressed) { // Make sure to sync the volume once the button was // released. // Otherwise it might be that the slider is at v10 // whereas PA rejected the volume change and is // still at v15 (e.g.). updateTimer.restart(); } } Timer { id: updateTimer interval: 200 onTriggered: slider.value = Volume } } PlasmaComponents.Label { id: percentText readonly property real value: PulseObject.volume > slider.maximumValue ? PulseObject.volume : slider.value Layout.alignment: Qt.AlignHCenter Layout.minimumWidth: percentMetrics.advanceWidth horizontalAlignment: Qt.AlignRight text: i18nc("volume percentage", "%1%", Math.round(value / PulseAudio.NormalVolume * 100.0)) } TextMetrics { id: percentMetrics font: percentText.font text: i18nc("only used for sizing, should be widest possible string", "100%") } } } } DragAndDrop.DropArea { id: dropArea anchors.fill: parent enabled: draggedStream onDragEnter: { if (draggedStream.deviceIndex == Index) { event.ignore(); } } onDrop: { draggedStream.deviceIndex = Index; } } MouseArea { anchors { fill: parent leftMargin: clientIcon.width } acceptedButtons: Qt.MiddleButton onClicked: Muted = !Muted } } PlasmaComponents.ContextMenu { id: contextMenu visualParent: contextMenuButton placement: PlasmaCore.Types.BottomPosedLeftAlignedPopup onStatusChanged: { if (status == PlasmaComponents.DialogStatus.Closed) { contextMenuButton.checked = false; } } function newSeperator() { return Qt.createQmlObject("import org.kde.plasma.components 2.0 as PlasmaComponents; PlasmaComponents.MenuItem { separator: true }", contextMenu); } function newMenuItem() { return Qt.createQmlObject("import org.kde.plasma.components 2.0 as PlasmaComponents; PlasmaComponents.MenuItem {}", contextMenu); } function loadDynamicActions() { contextMenu.clearMenuItems(); // Mute var menuItem = newMenuItem(); menuItem.text = i18nc("Checkable switch for (un-)muting sound output.", "Mute"); menuItem.checkable = true; menuItem.checked = Muted; menuItem.clicked.connect(function() { Muted = !Muted }); contextMenu.addMenuItem(menuItem); // Default if (typeof PulseObject.default === "boolean") { var menuItem = newMenuItem(); menuItem.text = i18nc("Checkable switch to change the current default output.", "Default"); menuItem.checkable = true; menuItem.checked = PulseObject.default menuItem.clicked.connect(function() { PulseObject.default = true }); contextMenu.addMenuItem(menuItem); } // Raise max volume menuItem = newMenuItem(); menuItem.text = i18n("Raise maximum volume"); menuItem.checkable = true; menuItem.checked = slider.forceRaiseMaxVolume; menuItem.clicked.connect(function() { slider.forceRaiseMaxVolume = !slider.forceRaiseMaxVolume; if (!slider.forceRaiseMaxVolume && Volume > PulseAudio.NormalVolume) { Volume = PulseAudio.NormalVolume; } }); contextMenu.addMenuItem(menuItem); + // Switch all streams of the relevant kind to this device + if (type == "source") { + menuItem = newMenuItem(); + menuItem.text = i18n("Record all audio via this device"); + menuItem.icon = "mic-on" // or "mic-ready" // or "audio-input-microphone-symbolic" + menuItem.clicked.connect(function() { + PulseObject.switchStreams(); + }); + contextMenu.addMenuItem(menuItem); + } else if (type == "sink") { + menuItem = newMenuItem(); + menuItem.text = i18n("Play all audio via this device"); + menuItem.icon = "audio-on" // or "audio-ready" // or "audio-speakers-symbolic" + menuItem.clicked.connect(function() { + PulseObject.switchStreams(); + }); + contextMenu.addMenuItem(menuItem); + } + // Ports if (PulseObject.ports && PulseObject.ports.length > 0) { contextMenu.addMenuItem(newSeperator()); var isMultiplePorts = (1 < PulseObject.ports.length); var menuItem = newMenuItem(); menuItem.text = i18nc("Heading for a list of ports of a device (for example built-in laptop speakers or a plug for headphones)", "Ports"); menuItem.section = true; contextMenu.addMenuItem(menuItem); for (var i = 0; i < PulseObject.ports.length; i++) { var port = PulseObject.ports[i]; var menuItem = newMenuItem(); menuItem.text = port.description; if (port.availability == Port.Unavailable) { if (port.name == "analog-output-speaker" || port.name == "analog-input-microphone-internal") { menuItem.text += i18nc("Port is unavailable", " (unavailable)"); } else { menuItem.text += i18nc("Port is unplugged", " (unplugged)"); } } menuItem.enabled = isMultiplePorts; menuItem.checkable = true; menuItem.checked = i === PulseObject.activePortIndex; var setActivePort = function(portIndex) { return function() { PulseObject.activePortIndex = portIndex; }; }; menuItem.clicked.connect(setActivePort(i)); contextMenu.addMenuItem(menuItem); } } // Choose output / input device // By choice only shown when there are at least two options if ((type == "sink-input" && sinkView.model.count > 1) || (type == "source-input" && sourceView.model.count > 1)) { contextMenu.addMenuItem(newSeperator()); var menuItem = newMenuItem(); if (type == "sink-input") { menuItem.text = i18nc("Heading for a list of possible output devices (speakers, headphones, ...) to choose", "Play audio using"); } else { menuItem.text = i18nc("Heading for a list of possible input devices (built-in microphone, headset, ...) to choose", "Record audio using"); } menuItem.section = true; contextMenu.addMenuItem(menuItem); var sModel = type == "sink-input" ? sinkView.model : sourceView.model; for (var i = 0; i < sModel.count; ++i) { var data = sModel.get(i); var menuItem = newMenuItem(); menuItem.text = data.Description; menuItem.enabled = true; menuItem.checkable = true; menuItem.checked = data.Index === PulseObject.deviceIndex; var setActiveSink = function(sinkIndex) { return function() { PulseObject.deviceIndex = sinkIndex; }; }; menuItem.clicked.connect(setActiveSink(data.Index)); contextMenu.addMenuItem(menuItem); } } } function show() { loadDynamicActions(); openRelative(); } } } diff --git a/src/device.h b/src/device.h index 2bc5921..a9f8844 100644 --- a/src/device.h +++ b/src/device.h @@ -1,155 +1,157 @@ /* 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 DEVICE_H #define DEVICE_H #include #include #include "volumeobject.h" #include "port.h" #include "pulseobject.h" namespace QPulseAudio { class Device : public VolumeObject { Q_OBJECT Q_PROPERTY(State state READ state NOTIFY stateChanged) Q_PROPERTY(QString name READ name NOTIFY nameChanged) Q_PROPERTY(QString description READ description NOTIFY descriptionChanged) Q_PROPERTY(QString formFactor READ formFactor NOTIFY formFactorChanged) Q_PROPERTY(quint32 cardIndex READ cardIndex NOTIFY cardIndexChanged) Q_PROPERTY(QList ports READ ports NOTIFY portsChanged) Q_PROPERTY(quint32 activePortIndex READ activePortIndex WRITE setActivePortIndex NOTIFY activePortIndexChanged) Q_PROPERTY(bool default READ isDefault WRITE setDefault NOTIFY defaultChanged) Q_PROPERTY(bool virtualDevice READ isVirtualDevice NOTIFY virtualDeviceChanged) public: enum State { InvalidState = 0, RunningState, IdleState, SuspendedState, UnknownState }; Q_ENUMS(State); ~Device() override {} template void updateDevice(const PAInfo *info) { updateVolumeObject(info); if (m_name != info->name) { m_name = info->name; Q_EMIT nameChanged(); } if (m_description != info->description) { m_description = info->description; Q_EMIT 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 formFactorChanged(); } } m_cardIndex = info->card; Q_EMIT 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(this); port->setInfo(*ports); m_ports.append(port); if (info->active_port == *ports) { m_activePortIndex = m_ports.length() - 1; } } Q_EMIT portsChanged(); Q_EMIT activePortIndexChanged(); State infoState = stateFromPaState(info->state); if (infoState != m_state) { m_state = infoState; Q_EMIT stateChanged(); } const bool isVirtual = !(info->flags & 4); // PA_X_HARDWARE if (m_virtualDevice != isVirtual) { m_virtualDevice = isVirtual; Q_EMIT virtualDeviceChanged(); } } State state() const; QString name() const; QString description() const; QString formFactor() const; quint32 cardIndex() const; QList ports() const; quint32 activePortIndex() const; virtual void setActivePortIndex(quint32 port_index) = 0; virtual bool isDefault() const = 0; virtual void setDefault(bool enable) = 0; bool isVirtualDevice() const; + virtual Q_INVOKABLE void switchStreams() = 0; + Q_SIGNALS: void stateChanged(); void nameChanged(); void descriptionChanged(); void formFactorChanged(); void cardIndexChanged(); void portsChanged(); void activePortIndexChanged(); void defaultChanged(); void virtualDeviceChanged(); protected: explicit Device(QObject *parent); private: State stateFromPaState(int value) const; QString m_name; QString m_description; QString m_formFactor; quint32 m_cardIndex = -1; QList m_ports; quint32 m_activePortIndex = -1; State m_state = UnknownState; bool m_virtualDevice = false; }; } // QPulseAudio #endif // DEVICE_H diff --git a/src/sink.cpp b/src/sink.cpp index 78eaafd..1cd0bd8 100644 --- a/src/sink.cpp +++ b/src/sink.cpp @@ -1,119 +1,128 @@ /* 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 "sink.h" #include "context.h" #include "server.h" +#include "sinkinput.h" #include "canberracontext.h" #include namespace QPulseAudio { Sink::Sink(QObject *parent) : Device(parent) { connect(context()->server(), &Server::defaultSinkChanged, this, &Sink::defaultChanged); CanberraContext::instance()->ref(); } Sink::~Sink() { CanberraContext::instance()->unref(); } void Sink::update(const pa_sink_info *info) { updateDevice(info); } void Sink::setVolume(qint64 volume) { context()->setGenericVolume(index(), -1, volume, cvolume(), &pa_context_set_sink_volume_by_index); } void Sink::setMuted(bool muted) { context()->setGenericMute(m_index, muted, &pa_context_set_sink_mute_by_index); } void Sink::setActivePortIndex(quint32 port_index) { Port *port = qobject_cast(ports().at(port_index)); if (!port) { qCWarning(PLASMAPA) << "invalid port set request" << port_index; return; } context()->setGenericPort(index(), port->name(), &pa_context_set_sink_port_by_index); } void Sink::setChannelVolume(int channel, qint64 volume) { context()->setGenericVolume(index(), channel, volume, cvolume(), &pa_context_set_sink_volume_by_index); } bool Sink::isDefault() const { return context()->server()->defaultSink() == this; } void Sink::setDefault(bool enable) { if (!isDefault() && enable) { context()->server()->setDefaultSink(this); } } void Sink::testChannel(const QString &name) { auto context = CanberraContext::instance()->canberra(); if (!context) return; char dev[64]; snprintf(dev, sizeof(dev), "%lu", (unsigned long) m_index); ca_context_change_device(context, dev); QString sound_name = QStringLiteral("audio-channel-") + name; ca_proplist *proplist; ca_proplist_create(&proplist); ca_proplist_sets(proplist, CA_PROP_MEDIA_ROLE, "test"); ca_proplist_sets(proplist, CA_PROP_MEDIA_NAME, name.toLatin1().constData()); ca_proplist_sets(proplist, CA_PROP_CANBERRA_FORCE_CHANNEL, name.toLatin1().data()); ca_proplist_sets(proplist, CA_PROP_CANBERRA_ENABLE, "1"); ca_proplist_sets(proplist, CA_PROP_EVENT_ID, sound_name.toLatin1().data()); if (ca_context_play_full(context, 0, proplist, nullptr, NULL) < 0) { // Try a different sound name. ca_proplist_sets(proplist, CA_PROP_EVENT_ID, "audio-test-signal"); if (ca_context_play_full(context, 0, proplist, nullptr, NULL) < 0) { // Finaly try this... if this doesn't work, then stuff it. ca_proplist_sets(proplist, CA_PROP_EVENT_ID, "bell-window-system"); ca_context_play_full(context, 0, proplist, nullptr, NULL); } } ca_context_change_device(context, nullptr); ca_proplist_destroy(proplist); } +void Sink::switchStreams() +{ + auto data = context()->sinkInputs().data(); + std::for_each(data.begin(), data.end(), [this](SinkInput* paObj) { + paObj->setDeviceIndex(m_index); + }); +} + } // QPulseAudio diff --git a/src/sink.h b/src/sink.h index 9cb5114..da08e42 100644 --- a/src/sink.h +++ b/src/sink.h @@ -1,53 +1,55 @@ /* 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 SINK_H #define SINK_H #include "device.h" #include #include namespace QPulseAudio { class Sink : public Device { Q_OBJECT public: explicit Sink(QObject *parent); virtual ~Sink(); void update(const pa_sink_info *info); void setVolume(qint64 volume) override; void setMuted(bool muted) override; void setActivePortIndex(quint32 port_index) override; void setChannelVolume(int channel, qint64 volume) override; bool isDefault() const override; void setDefault(bool enable) override; + void switchStreams() override; + public Q_SLOTS: void testChannel(const QString &name); }; } // QPulseAudio #endif // SINK_H diff --git a/src/source.cpp b/src/source.cpp index c010b5d..b63718f 100644 --- a/src/source.cpp +++ b/src/source.cpp @@ -1,77 +1,86 @@ /* 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 "source.h" #include "context.h" #include "server.h" +#include "sourceoutput.h" namespace QPulseAudio { Source::Source(QObject *parent) : Device(parent) { connect(context()->server(), &Server::defaultSourceChanged, this, &Source::defaultChanged); } void Source::update(const pa_source_info *info) { updateDevice(info); } void Source::setVolume(qint64 volume) { context()->setGenericVolume(index(), -1, volume, cvolume(), &pa_context_set_source_volume_by_index); } void Source::setMuted(bool muted) { context()->setGenericMute(index(), muted, &pa_context_set_source_mute_by_index); } void Source::setActivePortIndex(quint32 port_index) { Port *port = qobject_cast(ports().at(port_index)); if (!port) { qCWarning(PLASMAPA) << "invalid port set request" << port_index; return; } context()->setGenericPort(index(), port->name(), &pa_context_set_source_port_by_index); } void Source::setChannelVolume(int channel, qint64 volume) { context()->setGenericVolume(index(), channel, volume, cvolume(), &pa_context_set_source_volume_by_index); } bool Source::isDefault() const { return context()->server()->defaultSource() == this; } void Source::setDefault(bool enable) { if (!isDefault() && enable) { context()->server()->setDefaultSource(this); } } +void Source::switchStreams() +{ + auto data = context()->sourceOutputs().data(); + std::for_each(data.begin(), data.end(), [this](SourceOutput* paObj) { + paObj->setDeviceIndex(m_index); + }); +} + } // QPulseAudio diff --git a/src/source.h b/src/source.h index 26d5cd5..7be1c9f 100644 --- a/src/source.h +++ b/src/source.h @@ -1,47 +1,49 @@ /* 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 SOURCE_H #define SOURCE_H #include "device.h" namespace QPulseAudio { class Source : public Device { Q_OBJECT public: explicit Source(QObject *parent); void update(const pa_source_info *info); void setVolume(qint64 volume) override; void setMuted(bool muted) override; void setActivePortIndex(quint32 port_index) override; void setChannelVolume(int channel, qint64 volume) override; bool isDefault() const override; void setDefault(bool enable) override; + + void switchStreams() override; }; } // QPulseAudio #endif // SOURCE_H