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

« back to all changes in this revision

Viewing changes to kmix/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 "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) {
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
 
      // This should never ever happen, as KMix doe NOT accept soundcards without controls
143
 
      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;
144
 
      return (MixDevice*)0;
145
 
   }
146
 
}
147
 
 
148
 
/**
149
 
 * Sets the ID of the currently selected Enum entry.
150
 
 * This is a dummy implementation - if the Mixer backend
151
 
 * wants to support it, it must implement the driver specific 
152
 
 * code in its subclass (see Mixer_ALSA.cpp for an example).
153
 
 */
154
 
void Mixer_Backend::setEnumIdHW(const QString& , unsigned int) {
155
 
  return;
156
 
}
157
 
 
158
 
/**
159
 
 * Return the ID of the currently selected Enum entry.
160
 
 * This is a dummy implementation - if the Mixer backend
161
 
 * wants to support it, it must implement the driver specific
162
 
 * code in its subclass (see Mixer_ALSA.cpp for an example).
163
 
 */
164
 
unsigned int Mixer_Backend::enumIdHW(const QString& ) {
165
 
  return 0;
166
 
}
167
 
 
168
 
void Mixer_Backend::errormsg(int mixer_error)
169
 
{
170
 
  QString l_s_errText;
171
 
  l_s_errText = errorText(mixer_error);
172
 
  kError() << l_s_errText << "\n";
173
 
}
174
 
 
175
 
int Mixer_Backend::id2num(const QString& id)
176
 
{
177
 
   return id.toInt();
178
 
}
179
 
 
180
 
QString Mixer_Backend::errorText(int mixer_error)
181
 
{
182
 
  QString l_s_errmsg;
183
 
  switch (mixer_error)
184
 
  {
185
 
    case Mixer::ERR_PERM:
186
 
      l_s_errmsg = i18n("kmix:You do not have permission to access the mixer device.\n" \
187
 
          "Please check your operating systems manual to allow the access.");
188
 
      break;
189
 
    case Mixer::ERR_WRITE:
190
 
      l_s_errmsg = i18n("kmix: Could not write to mixer.");
191
 
      break;
192
 
    case Mixer::ERR_READ:
193
 
      l_s_errmsg = i18n("kmix: Could not read from mixer.");
194
 
      break;
195
 
    case Mixer::ERR_OPEN:
196
 
      l_s_errmsg = i18n("kmix: Mixer cannot be found.\n" \
197
 
          "Please check that the soundcard is installed and that\n" \
198
 
          "the soundcard driver is loaded.\n");
199
 
      break;
200
 
    default:
201
 
      l_s_errmsg = i18n("kmix: Unknown error. Please report how you produced this error.");
202
 
      break;
203
 
  }
204
 
  return l_s_errmsg;
205
 
}
206
 
 
207
 
 
208
 
 
209
 
 
210
 
#include "mixer_backend.moc"