1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
|
/*
* KMix -- KDE's full featured mini mixer
*
* Copyright 2006-2007 Christian Esken <esken@kde.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include "core/kmixdevicemanager.h"
#include <QRegExp>
#include <QString>
#include <QTimer>
#include "kmix_debug.h"
#include <solid/device.h>
#include <solid/devicenotifier.h>
static const int HOTPLUG_DELAY = 500; // settling delay, milliseconds
KMixDeviceManager* KMixDeviceManager::s_KMixDeviceManager = 0;
KMixDeviceManager* KMixDeviceManager::instance()
{
if ( s_KMixDeviceManager == 0 ) {
s_KMixDeviceManager = new KMixDeviceManager();
}
return s_KMixDeviceManager;
}
void KMixDeviceManager::initHotplug()
{
qCDebug(KMIX_LOG);
connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceAdded,
this, &KMixDeviceManager::pluggedSlot);
connect(Solid::DeviceNotifier::instance(), &Solid::DeviceNotifier::deviceRemoved,
this, &KMixDeviceManager::unpluggedSlot);
}
QString KMixDeviceManager::getUDI_ALSA(int num)
{
return (QString("hw%1").arg(num));
}
QString KMixDeviceManager::getUDI_OSS(const QString &devname)
{
return (devname);
}
static bool isSoundDevice(const QString &udi)
{
QRegExp rx("/sound/"); // any UDI mentioning sound
return (udi.contains(rx));
}
static int matchDevice(const QString &udi)
{
QRegExp rx("/sound/card(\\d+)$"); // match sound card and ID number
if (!udi.contains(rx)) return (-1); // UDI not recognised
return (rx.cap(1).toInt()); // assume conversion succeeds
}
void KMixDeviceManager::pluggedSlot(const QString &udi)
{
if (!isSoundDevice(udi)) return; // ignore non-sound devices
Solid::Device device(udi);
if (!device.isValid())
{
qCWarning(KMIX_LOG) << "Invalid device for UDI" << udi;
return;
}
// Solid device UDIs for a plugged sound card are of the form:
//
// /org/kde/solid/udev/sys/devices/pci0000:00/0000:00:13.2/usb2/2-4/2-4.1/2-4.1:1.0/sound/card3
// /org/kde/solid/udev/sys/devices/pci0000:00/0000:00:13.2/usb2/2-4/2-4.1/2-4.1:1.0/sound/card3/pcmC3D0p
// /org/kde/solid/udev/sys/devices/pci0000:00/0000:00:13.2/usb2/2-4/2-4.1/2-4.1:1.0/sound/card3/...
//
// The first of these is considered to be the canonical form and triggers
// device hotplug, after a settling delay.
//
// Note that the Solid device UDI is not used anwhere else within KMix,
// what is referred to as a "UDI" elsewhere is as described in the
// comment for Mixer::udi() in core/mixer.h and generated by getUDI_ALSA()
// above. Solid only ever supported ALSA anyway, even in KDE4, so we can
// assume the driver is ALSA here without having to ask. See
// https://community.kde.org/Frameworks/Porting_Notes#Solid_Changes
const int dev = matchDevice(udi);
if (dev!=-1)
{
qCDebug(KMIX_LOG) << "udi" << udi;
const QString alsaUDI = getUDI_ALSA(dev);
qCDebug(KMIX_LOG) << "ALSA udi" << alsaUDI << "device" << dev;
QTimer::singleShot(HOTPLUG_DELAY, [=](){ emit plugged("ALSA", alsaUDI, dev); });
} // allow hotplug to settle
else qCDebug(KMIX_LOG) << "Ignored unrecognised UDI" << udi;
}
void KMixDeviceManager::unpluggedSlot(const QString &udi)
{
if (!isSoundDevice(udi)) return; // ignore non-sound devices
// At this point the device has already been unplugged by the user.
// Solid doesn't know anything about the device except the UDI, but
// that can be matched using the same logic as for plugging.
const int dev = matchDevice(udi);
if (dev!=-1)
{
qCDebug(KMIX_LOG) << "udi" << udi;
const QString alsaUDI = getUDI_ALSA(dev);
qCDebug(KMIX_LOG) << "ALSA udi" << alsaUDI << "device" << dev;
QTimer::singleShot(HOTPLUG_DELAY, [=](){ emit unplugged(alsaUDI); });
} // allow hotplug to settle
else qCDebug(KMIX_LOG) << "Ignored unrecognised UDI" << udi;
}
void KMixDeviceManager::setHotpluggingBackends(const QString& backendName)
{
qCDebug(KMIX_LOG) << "using" << backendName;
_hotpluggingBackend = backendName;
}
|