summaryrefslogtreecommitdiffstats
path: root/kmix/mixdevice.cpp
blob: 243ae3d16d1e718c35977b1018411da87402230a (plain)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
 * KMix -- KDE's full featured mini mixer
 *
 *
 * Copyright (C) 1996-2004 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 <kdebug.h>
#include <tdelocale.h>

#include "mixdevice.h"


/**
 * Constructs a MixDevice. A MixDevice represents one channel or control of
 * the mixer hardware. A MixDevice has a type (e.g. PCM), a descriptive name
 * (for example "Master" or "Headphone" or "IEC 958 Output"),
 * can have a volume level (2 when stereo), can be recordable and muted.
 * The category tells which kind of control the MixDevice is.
 *
 * Hints: Meaning of "category" has changed. In future the MixDevice might contain two
 * Volume objects, one for Output (Playback volume) and one for Input (Record volume).
 */
MixDevice::MixDevice( int num, Volume &vol, bool recordable, bool mute,
		      TQString name, ChannelType type, DeviceCategory category ) :
    _volume( vol ), _type( type ), _num( num ), _recordable( recordable ),
    _mute( mute ), _category( category )
{
    // Hint: "_volume" gets COPIED from "vol" due to the fact that the copy-constructor actually copies the volume levels.
    _switch = false;
    _recSource = false;
    if( name.isEmpty() )
	_name = i18n("unknown");
    else
	_name = name;

    _pk.setNum(num);


    if( category == MixDevice::SWITCH )
	_switch = true;
}

MixDevice::MixDevice(const MixDevice &md) : TQObject()
{
   _name = md._name;
   _volume = md._volume;
   _type = md._type;
   _num = md._num;
   _pk = md._pk;
   _recordable = md._recordable;
   _recSource  = md._recSource;
   _category = md._category;
   _switch = md._switch;
   _mute = md._mute;
   _enumValues = md._enumValues;
}

MixDevice::~MixDevice() {
  // Clear MixDevices enum Strings (switch on auto-delete, so the TQString's inside will be cleared)
  _enumValues.setAutoDelete(true);
  _enumValues.clear();
}

Volume& MixDevice::getVolume()
{
   return _volume;
}

long MixDevice::getVolume(Volume::ChannelID chid) {
    return _volume.getVolume(chid);
}

long MixDevice::maxVolume() {
    return _volume.maxVolume();
}

long MixDevice::minVolume() {
    return _volume.minVolume();
}

void MixDevice::setEnumId(int enumId)
{
  if ( enumId < _enumValues.count() ) {
    _enumCurrentId = enumId;
  }
}

unsigned int MixDevice::enumId()
{
  return _enumCurrentId;
}

TQPtrList<TQString>& MixDevice::enumValues() {
  return _enumValues;
}


// @todo Used only at mixdevicewidget.cpp:625 . Replace that ASAP  !!!
void MixDevice::setVolume( int channel, int volume )
{
  _volume.setVolume( (Volume::ChannelID)channel /* ARGH! */, volume );
}

TQString& MixDevice::getPK() {
    return _pk;
}

void MixDevice::setPK(TQString &PK) {
    _pk = PK;
    // The key is used in the config file. It should not contain spaces
    _pk.replace(' ', '_');
}

/**
 * This mehtod is currently only called on "kmixctrl --restore"
 *
 * Normally we have a working _volume object already, which is very important,
 * because we need to read the minimum and maximum volume levels.
 * (Another solutien would be to "equip" volFromConfig with maxInt and minInt values).
 */
void MixDevice::read( TDEConfig *config, const TQString& grp )
{
   TQString devgrp;
   devgrp.sprintf( "%s.Dev%i", grp.ascii(), _num );
   config->setGroup( devgrp );
   //kdDebug(67100) << "MixDevice::read() of group devgrp=" << devgrp << endl;

   char *nameLeftVolume, *nameRightVolume;
   if ( _volume.isCapture() ) {
		nameLeftVolume = "volumeLCapture";
		nameRightVolume = "volumeRCapture";
   } else {
		nameLeftVolume = "volumeL";
		nameRightVolume = "volumeR";
   }
   Volume::ChannelMask chMask = Volume::MNONE;
   int vl = config->readNumEntry(nameLeftVolume, -1);
   if (vl!=-1) {
        chMask = (Volume::ChannelMask)(chMask | Volume::MLEFT);
   }
   int vr = config->readNumEntry(nameRightVolume, -1);
   if (vr!=-1) {
       chMask = (Volume::ChannelMask)(chMask | Volume::MRIGHT);
   }

   /*
    * Now start construction a temporary Volume object.
    * We take the maxvol and minvol values from _volume, which is already constructed.
    * Otherwise we would have to wildly guess those values
    */
   Volume *volFromConfig = new Volume(chMask, _volume._maxVolume, _volume._minVolume);
   if (vl!=-1) {
       volFromConfig->setVolume(Volume::LEFT , vl);
   }
   if (vr!=-1) {
       volFromConfig->setVolume(Volume::RIGHT, vr);
   }
   // commit the read config
   _volume.setVolume(*volFromConfig);
   delete volFromConfig;

   int mute = config->readNumEntry("is_muted", -1);
   if ( mute!=-1 ) {
        _volume.setMuted( mute!=0 );
   }

   int recsrc = config->readNumEntry("is_recsrc", -1);
   if ( recsrc!=-1 ) {
        setRecSource( recsrc!=0 );
   }

   int enumId = config->readNumEntry("enum_id", -1);
   if ( enumId != -1 ) {
      setEnumId( enumId );
   }
}

/**
 *  called on "kmixctrl --save" and from the GUI's (currently only on exit)
 */
void MixDevice::write( TDEConfig *config, const TQString& grp )
{
   TQString devgrp;
   devgrp.sprintf( "%s.Dev%i", grp.ascii(), _num );
   config->setGroup(devgrp);
   // kdDebug(67100) << "MixDevice::write() of group devgrp=" << devgrp << endl;
   char *nameLeftVolume, *nameRightVolume;
   if ( _volume.isCapture() ) {
		nameLeftVolume = "volumeLCapture";
		nameRightVolume = "volumeRCapture";
   } else {
		nameLeftVolume = "volumeL";
		nameRightVolume = "volumeR";
   }
   config->writeEntry(nameLeftVolume, getVolume( Volume::LEFT ) );
   config->writeEntry(nameRightVolume, getVolume( Volume::RIGHT ) );
   config->writeEntry("is_muted", (int)_volume.isMuted() );
   config->writeEntry("is_recsrc", (int)isRecSource() );
   config->writeEntry("name", _name);
   if ( isEnum() ) {
      config->writeEntry("enum_id", enumId() );
   }
}

#include "mixdevice.moc"