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

« back to all changes in this revision

Viewing changes to kmix/backends/mixer_backend.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 <klocale.h>
 
22
 
 
23
#include "mixer_backend.h"
 
24
// for the "ERR_" declartions, #include mixer.h
 
25
#include "core/mixer.h"
 
26
#include <QTimer>
 
27
 
 
28
#include "mixer_backend_i18n.cpp"
 
29
 
 
30
Mixer_Backend::Mixer_Backend(Mixer *mixer, int device) :
 
31
        m_devnum (device) , m_isOpen(false), m_recommendedMaster(0), _mixer(mixer), _pollingTimer(0)
 
32
 
 
33
{
 
34
   // In all cases create a QTimer. We will use it once as a singleShot(), even if something smart
 
35
   // like ::select() is possible (as in ALSA).
 
36
   _pollingTimer = new QTimer(); // will be started on open() and stopped on close()
 
37
   connect( _pollingTimer, SIGNAL(timeout()), this, SLOT(readSetFromHW()));
 
38
}
 
39
 
 
40
Mixer_Backend::~Mixer_Backend()
 
41
{
 
42
    delete _pollingTimer;
 
43
    qDeleteAll(m_mixDevices);
 
44
    m_mixDevices.clear();
 
45
}
 
46
 
 
47
 
 
48
bool Mixer_Backend::openIfValid() {
 
49
    bool valid = false;
 
50
    int ret = open();
 
51
    if ( ret == 0  && (m_mixDevices.count() > 0 || _mixer->isDynamic())) {
 
52
        valid = true;
 
53
        // A better ID is now calculated in mixertoolbox.cpp, and set via setID(),
 
54
        // but we want a somehow usable fallback just in case.
 
55
    
 
56
        if ( needsPolling() ) {
 
57
            _pollingTimer->start(50);
 
58
        }
 
59
        else {
 
60
            // The initial state must be read manually
 
61
            QTimer::singleShot( 50, this, SLOT( readSetFromHW() ) );
 
62
        }
 
63
    } // cold be opened
 
64
    else {
 
65
        close();
 
66
    }
 
67
    return valid;
 
68
}
 
69
 
 
70
bool Mixer_Backend::isOpen() {
 
71
  return m_isOpen;
 
72
}
 
73
 
 
74
/**
 
75
 * Queries the backend driver whether there are new changes in any of the controls.
 
76
 * If you cannot find out for a backend, return "true" - this is also the default implementation.
 
77
 * @return true, if there are changes. Otherwise false is returned.
 
78
 */
 
79
bool Mixer_Backend::prepareUpdateFromHW() {
 
80
  return true;
 
81
}
 
82
 
 
83
 
 
84
/**
 
85
 * After calling this, readSetFromHW() will do a complete update. This will
 
86
 * trigger emitting the appropriate signals like controlChanged().
 
87
 *
 
88
 * This method is useful, if you need to get a "refresh signal" - used at:
 
89
 * 1) Start of KMix - so that we can be sure an initial signal is emitted
 
90
 * 2) When reconstructing any MixerWidget (e.g. DockIcon after applying preferences)
 
91
 */
 
92
void Mixer_Backend::readSetFromHWforceUpdate() const {
 
93
   _readSetFromHWforceUpdate = true;
 
94
}
 
95
 
 
96
 
 
97
/**
 
98
   You can call this to retrieve the freshest information from the mixer HW.
 
99
   This method is also called regulary by the mixer timer.
 
100
*/
 
101
void Mixer_Backend::readSetFromHW()
 
102
{
 
103
    bool updated = prepareUpdateFromHW();
 
104
    if ( (! updated) && (! _readSetFromHWforceUpdate) ) {
 
105
        // Some drivers (ALSA) are smart. We don't need to run the following
 
106
        // time-consuming update loop if there was no change
 
107
        kDebug(67100) << "Mixer::readSetFromHW(): smart-update-tick";
 
108
        return;
 
109
    }
 
110
    _readSetFromHWforceUpdate = false;
 
111
 
 
112
    int mdCount = m_mixDevices.count();
 
113
    for(int i=0; i<mdCount  ; ++i )
 
114
    {
 
115
        MixDevice *md = m_mixDevices[i];
 
116
        readVolumeFromHW( md->id(), md );
 
117
        if (md->isEnum() ) {
 
118
            /*
 
119
             * This could be reworked:
 
120
             * Plan: Read everything (incuding enum's) in readVolumeFromHW().
 
121
             * readVolumeFromHW() should then be renamed to readHW().
 
122
             */
 
123
            md->setEnumId( enumIdHW(md->id()) ); 
 
124
        }
 
125
    }
 
126
    emit controlChanged();
 
127
}
 
128
 
 
129
/**
 
130
 * Return the MixDevice, that would qualify best as MasterDevice. The default is to return the
 
131
 * first device in the device list. Backends can override this (i.e. the ALSA Backend does so).
 
132
 * The users preference is NOT returned by this method - see the Mixer class for that.
 
133
 */
 
134
MixDevice* Mixer_Backend::recommendedMaster() {
 
135
   if ( m_recommendedMaster != 0 ) {
 
136
      return m_recommendedMaster;   // Backend has set a recommended master. Thats fine.
 
137
   } // recommendation from Backend
 
138
   else if ( m_mixDevices.count() > 0 ) {
 
139
      return m_mixDevices.at(0);  // Backend has NOT set a recommended master. Evil backend => lets help out.
 
140
   } //first device (if exists)
 
141
   else {
 
142
      if ( !_mixer->isDynamic()) {
 
143
         // This should never ever happen, as KMix doe NOT accept soundcards without controls
 
144
         kError(67100) << "Mixer_Backend::recommendedMaster(): returning invalid master. This is a bug in KMix. Please file a bug report stating how you produced this." << endl;
 
145
      }
 
146
      return (MixDevice*)0;
 
147
   }
 
148
}
 
149
 
 
150
/**
 
151
 * Sets the ID of the currently selected Enum entry.
 
152
 * This is a dummy implementation - if the Mixer backend
 
153
 * wants to support it, it must implement the driver specific 
 
154
 * code in its subclass (see Mixer_ALSA.cpp for an example).
 
155
 */
 
156
void Mixer_Backend::setEnumIdHW(const QString& , unsigned int) {
 
157
  return;
 
158
}
 
159
 
 
160
/**
 
161
 * Return the ID of the currently selected Enum entry.
 
162
 * This is a dummy implementation - if the Mixer backend
 
163
 * wants to support it, it must implement the driver specific
 
164
 * code in its subclass (see Mixer_ALSA.cpp for an example).
 
165
 */
 
166
unsigned int Mixer_Backend::enumIdHW(const QString& ) {
 
167
  return 0;
 
168
}
 
169
 
 
170
/**
 
171
 * Move the stream to a new destination
 
172
 */
 
173
bool Mixer_Backend::moveStream( const QString& id, const QString& destId ) {
 
174
  Q_UNUSED(id);
 
175
  Q_UNUSED(destId);
 
176
  return false;
 
177
}
 
178
 
 
179
void Mixer_Backend::errormsg(int mixer_error)
 
180
{
 
181
  QString l_s_errText;
 
182
  l_s_errText = errorText(mixer_error);
 
183
  kError() << l_s_errText << "\n";
 
184
}
 
185
 
 
186
int Mixer_Backend::id2num(const QString& id)
 
187
{
 
188
   return id.toInt();
 
189
}
 
190
 
 
191
QString Mixer_Backend::errorText(int mixer_error)
 
192
{
 
193
  QString l_s_errmsg;
 
194
  switch (mixer_error)
 
195
  {
 
196
    case Mixer::ERR_PERM:
 
197
      l_s_errmsg = i18n("kmix:You do not have permission to access the mixer device.\n" \
 
198
          "Please check your operating systems manual to allow the access.");
 
199
      break;
 
200
    case Mixer::ERR_WRITE:
 
201
      l_s_errmsg = i18n("kmix: Could not write to mixer.");
 
202
      break;
 
203
    case Mixer::ERR_READ:
 
204
      l_s_errmsg = i18n("kmix: Could not read from mixer.");
 
205
      break;
 
206
    case Mixer::ERR_OPEN:
 
207
      l_s_errmsg = i18n("kmix: Mixer cannot be found.\n" \
 
208
          "Please check that the soundcard is installed and that\n" \
 
209
          "the soundcard driver is loaded.\n");
 
210
      break;
 
211
    default:
 
212
      l_s_errmsg = i18n("kmix: Unknown error. Please report how you produced this error.");
 
213
      break;
 
214
  }
 
215
  return l_s_errmsg;
 
216
}
 
217
 
 
218
 
 
219
 
 
220
 
 
221
#include "mixer_backend.moc"