~ubuntu-branches/ubuntu/natty/kdemultimedia/natty-proposed

« back to all changes in this revision

Viewing changes to kmix/core/kmixdevicemanager.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Debian Qt/KDE Maintainers
  • Date: 2011-05-26 02:41:36 UTC
  • mfrom: (0.2.3 upstream)
  • mto: This revision was merged to the branch mainline in revision 108.
  • Revision ID: james.westby@ubuntu.com-20110526024136-jjwsigfy402jhupm
Tags: upstream-4.6.3
ImportĀ upstreamĀ versionĀ 4.6.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * KMix -- KDE's full featured mini mixer
 
3
 *
 
4
 * Copyright 2006-2007 Christian Esken <esken@kde.org>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or
 
7
 * modify it under the terms of the GNU Library General Public
 
8
 * License as published by the Free Software Foundation; either
 
9
 * version 2 of the License, or (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 * Library General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU Library General Public
 
17
 * License along with this program; if not, write to the Free
 
18
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
19
 */
 
20
 
 
21
#include "core/kmixdevicemanager.h"
 
22
#include <kdebug.h>
 
23
 
 
24
#include <iostream>
 
25
 
 
26
#include <QRegExp>
 
27
#include <QString>
 
28
 
 
29
 
 
30
#include <solid/device.h>
 
31
#include <solid/devicenotifier.h>
 
32
#include <solid/audiointerface.h>
 
33
 
 
34
KMixDeviceManager* KMixDeviceManager::s_KMixDeviceManager = 0;
 
35
 
 
36
KMixDeviceManager::KMixDeviceManager()
 
37
{
 
38
}
 
39
 
 
40
KMixDeviceManager::~KMixDeviceManager()
 
41
{
 
42
}
 
43
 
 
44
KMixDeviceManager* KMixDeviceManager::instance()
 
45
{
 
46
    if ( s_KMixDeviceManager == 0 ) {
 
47
        s_KMixDeviceManager = new KMixDeviceManager();
 
48
    }
 
49
    return s_KMixDeviceManager;
 
50
}
 
51
 
 
52
void KMixDeviceManager::initHotplug()
 
53
{
 
54
    connect (Solid::DeviceNotifier::instance(), SIGNAL(deviceAdded(const QString&)), SLOT(pluggedSlot(const QString&)) );
 
55
    connect (Solid::DeviceNotifier::instance(), SIGNAL(deviceRemoved(const QString&)), SLOT(unpluggedSlot(const QString&)) );
 
56
}
 
57
 
 
58
QString KMixDeviceManager::getUDI_ALSA(int num)
 
59
{
 
60
    QList<Solid::Device> dl = Solid::Device::listFromType(Solid::DeviceInterface::AudioInterface);
 
61
 
 
62
    QString numString;
 
63
    numString.setNum(num);
 
64
    bool found = false;
 
65
    QString udi;
 
66
    QString devHandle;
 
67
    foreach ( const Solid::Device &device, dl )
 
68
    {
 
69
//        std::cout << "Coldplug udi = '" << device.udi().toUtf8().data() << "'\n";
 
70
        const Solid::AudioInterface *audiohw = device.as<Solid::AudioInterface>();
 
71
        if (audiohw && (audiohw->deviceType() & ( Solid::AudioInterface::AudioControl))) {
 
72
            switch (audiohw->driver()) {
 
73
                case Solid::AudioInterface::Alsa:
 
74
                    devHandle = audiohw->driverHandle().toList().first().toString();
 
75
//                    std::cout << ">>> Coldplugged ALSA ='" <<  devHandle.toUtf8().data() << "'\n";
 
76
                    if ( numString == devHandle ) {
 
77
                        found = true;
 
78
//                        std::cout << ">>> Match!!! Coldplugged ALSA ='" <<  devHandle.toUtf8().data() << "'\n";
 
79
                        udi = device.udi();
 
80
                    }
 
81
                    break;
 
82
                default:
 
83
                    break;
 
84
            } // driver type
 
85
        } // is an audio control
 
86
        if ( found) break;
 
87
    } // foreach
 
88
    return udi;
 
89
}
 
90
 
 
91
QString KMixDeviceManager::getUDI_OSS(const QString& devname)
 
92
{
 
93
    QList<Solid::Device> dl = Solid::Device::listFromType(Solid::DeviceInterface::AudioInterface);
 
94
 
 
95
    bool found = false;
 
96
    QString udi;
 
97
    QString devHandle;
 
98
    foreach ( const Solid::Device &device, dl )
 
99
    {
 
100
//        std::cout << "Coldplug udi = '" << device.udi().toUtf8().data() << "'\n";
 
101
        const Solid::AudioInterface *audiohw = device.as<Solid::AudioInterface>();
 
102
        if (audiohw && (audiohw->deviceType() & ( Solid::AudioInterface::AudioControl))) {
 
103
            switch (audiohw->driver()) {
 
104
                case Solid::AudioInterface::OpenSoundSystem:
 
105
                    devHandle = audiohw->driverHandle().toString();
 
106
//                    std::cout << ">>> Coldplugged OSS ='" <<  devHandle.toUtf8().data() << "'\n";
 
107
                    if ( devname == devHandle ) {
 
108
                        found = true;
 
109
//                        std::cout << ">>> Match!!! Coldplugged OSS ='" <<  devHandle.toUtf8().data() << "'\n";
 
110
                        udi = device.udi();
 
111
                    }
 
112
                     break;
 
113
                default:
 
114
                    break;
 
115
            } // driver type
 
116
        } // is an audio control
 
117
        if ( found) break;
 
118
    } // foreach
 
119
    return udi;
 
120
}
 
121
 
 
122
 
 
123
void KMixDeviceManager::pluggedSlot(const QString& udi) {
 
124
//   std::cout << "Plugged udi='" <<  udi.toUtf8().data() << "'\n";
 
125
   Solid::Device device(udi);
 
126
   Solid::AudioInterface *audiohw = device.as<Solid::AudioInterface>();
 
127
   if (audiohw && (audiohw->deviceType() & ( Solid::AudioInterface::AudioControl))) {
 
128
       QString dev;
 
129
       QRegExp devExpr( QLatin1String( "^\\D+(\\d+)$" ));
 
130
        switch (audiohw->driver()) {
 
131
           case Solid::AudioInterface::Alsa:
 
132
               if ( _hotpluggingBackend == "ALSA" || _hotpluggingBackend == "*" ) {
 
133
                    dev = audiohw->driverHandle().toList().first().toString();
 
134
                    emit plugged("ALSA", udi, dev);
 
135
               }
 
136
               break;
 
137
           case Solid::AudioInterface::OpenSoundSystem:
 
138
                if ( _hotpluggingBackend == "OSS" || _hotpluggingBackend == "*" ) {
 
139
                    dev = audiohw->driverHandle().toString();
 
140
                    if ( devExpr.indexIn(dev) > -1 ) {
 
141
                        dev = devExpr.cap(1); // Get device number from device name (e.g "/dev/mixer1" or "/dev/sound/mixer2")
 
142
                    }
 
143
                    else {
 
144
                        dev = "0"; // "/dev/mixer" or "/dev/sound/mixer"
 
145
                    }
 
146
                    emit plugged("OSS", udi, dev);
 
147
                }
 
148
                break;
 
149
           default:
 
150
               kError(67100) <<  "Plugged UNKNOWN Audio device (ignored)";
 
151
               break;
 
152
       }
 
153
    }
 
154
}
 
155
 
 
156
 
 
157
void KMixDeviceManager::unpluggedSlot(const QString& udi) {
 
158
//    std::cout << "Unplugged udi='" <<  udi.toUtf8().data() << "'\n";
 
159
    Solid::Device device(udi);
 
160
    // At this point the device has already been unplugged by the user. Solid doesn't know anything about the
 
161
    // device except the UDI (not even device.as<Solid::AudioInterface>() is possible). Thus I'll forward any
 
162
    // unplugging action (could e.g. also be HID or mass storage). The receiver of the signal as to deal with it,
 
163
    // but a simple UDI matching is enough.
 
164
    emit unplugged(udi);
 
165
 
 
166
}
 
167
 
 
168
 
 
169
#include "kmixdevicemanager.moc"
 
170