summaryrefslogtreecommitdiff
path: root/soundset.c
blob: 640d0ae450947a1594e20eeb4cb72a2f8173feb1 (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
/*
 * sndctl  -  a plugin for the Video Disk Recorder
 *        file: soundset.c
 * description: representation of a soundset
 *
 *      author: Thomas Hildebrandt <toxym@web.de>
 *
 * inspired by and reengineered from 'avolctl'
 * thanks to Martin Prochnow <nordlichtl@martins-kabuff.de>
 */

#include "defaults.h"
#include "soundset.h"

/*********************************************************
 * member functions for class cSoundSet
 *********************************************************/

/*
 * constructors
 */
cSoundSet::cSoundSet( cMixer *Mixer, string Id ){
  int                          i = 0;
  vector<cControlId>           ctrls;
  vector<cControlId>::iterator it;

  dsyslog( "sndctl (cSoundSet::cSoundSet): New sound set created" );

  // initial value for volume
  volume = 0;

  // save mixer class
  mixer = Mixer;

  // save my ID
  id = Id;

  // set default name
  name = tr( SNDCTL_DEFAULT_SOUNDSET_NAME );

  // initialize control array
  if(( nrOfControls = mixer->CountControls()) > 0 )
    if( controls = new struct control[nrOfControls] ){
      // fill controls array with values
      ctrls = GetControls();
      for( it = ctrls.begin(); it != ctrls.end(); it++ ){
        (controls + i)->control = (*it);
        strncpy((controls + i)->value, "~", SNDCTL_MAX_LEN_CONTROL_VAL );
        i++;
      }
    }else
        esyslog( "sndctl (cSoundSet::cSoundSet): ERROR! Can't allocate memory for '%d' controls.", nrOfControls );
  else
    esyslog( "sndctl (cSoundSet::cSoundSet): WARNING! No controls found. Sound set is not initialized!" );
}

/*
 * destructor
 */
cSoundSet::~cSoundSet(){
  // remove mixers
  //delete &mixers;
  delete [] controls;
}

/*
 * returns the value for a control
 */
string cSoundSet::Get( cControlId Control){
  int    i;
  string result;

  result = string( "~" );

  // search for this control
  for( i = 0; i < nrOfControls; i++ ) {
    if( (controls + i)->control == Control){
      result = string((controls + i)->value );
      break;
    }
  }

  dsyslog( "sndctl (cSoundSet::Get): Value for '%s' requested, returning '%s'", Control.getDisplayName().c_str(), result.c_str());

  // nothing found, return default value
  return result;
}

/*
 * returns a vector with all controls
 */
vector<cControlId> cSoundSet::GetControls( void ){
  return mixer->GetControls();
}

/*
 * returns the ID of this sound set
 */
string cSoundSet::GetID( void ){
  return id;
}

/*
 * returns the name of this sound set
 */
string cSoundSet::GetName( void ){
  return name;
}

/*
 * set a parameter for this soundset
 */
bool cSoundSet::Set(string Name, string Value ){
  int i;

  dsyslog( "sndctl (cSoundSet::Set): Trying to set '%s' to '%s'.", Name.c_str(), Value.c_str());

  // name?
  if( Name == "name" ){
    name = Value;
    dsyslog( "sndctl (cSoundSet::Set): Sound set was renamed to '%s'.", name.c_str());
    return true;
  }

  // a known control?
  int pos = 0;
  if ((pos = Name.find_first_of("@")) != string::npos) {
    string soundCardId = Name.substr(0, pos);
    string name = Name.substr(pos+1);
    int mixerNr = atoi(name.c_str());
    cControlId id(soundCardId, string(""), mixerNr);
    for( i = 0; i < nrOfControls; i++ ){
      if( (controls + i)->control == id){
        // save value
        strncpy((controls + i)->value, Value.c_str(), SNDCTL_MAX_LEN_CONTROL_VAL );
        dsyslog( "sndctl (cSoundSet::Set): Control '%s@%s' in sound set '%s' is now '%s'.",
            (controls + i)->control.getSoundCardId().c_str(),
            (controls + i)->control.getDisplayName().c_str(),
            name.c_str(),
            Get( id ).c_str());

        mixer->Update( this );
        return true;
      }
    };
  }

  // nothing of all
  return false;
}

bool cSoundSet::Set(cControlId ControlId, string Value ){
  int i;

  dsyslog( "sndctl (cSoundSet::Set): Trying to set '%s'@'%s' to '%s'.", ControlId.getSoundCardId().c_str(), ControlId.getDisplayName().c_str(), Value.c_str());
  for( i = 0; i < nrOfControls; i++ ){
    if( (controls + i)->control == ControlId){
      // save value
      strncpy((controls + i)->value, Value.c_str(), SNDCTL_MAX_LEN_CONTROL_VAL );
      dsyslog( "sndctl (cSoundSet::Set): Control '%s@%s' in sound set '%s' is now '%s'.",
          (controls + i)->control.getSoundCardId().c_str(),
          (controls + i)->control.getDisplayName().c_str(),
          name.c_str(),
          Get( ControlId ).c_str());

      mixer->Update( this );
      return true;
    }
  }

  // nothing of all
  return false;
}

/*
 * store this sound sets settings
 */
bool cSoundSet::Store( string Id, cPluginSndctl *Plugin ){
  int i;
  char buf[64];

  // save my name
  Plugin->Store( Id + "_name", name );

  // save control settings
  for( i = 0; i < nrOfControls; i++ ) {
    if( strcmp((controls + i)->value, "~" )) {
      sprintf(buf, "%d", (controls + i)->control.getControlNr());
      Plugin->Store( Id + "_" + (controls + i)->control.getSoundCardId()+"@"+buf, (controls + i)->value );
    }
  }

  return true;
}

/*
 * sets the volume of this sound set
 * Vol range is here 0..100
 */
void cSoundSet::Volume( int Vol ){
  // save last set volume
  volume = Vol < 0 ? volume : Vol;

  isyslog( "sndctl (cSoundSet::Volume): Volume for soundset '%s' goes to %d%%.", name.c_str(), volume );
  mixer->Volume( volume );
}