Add valgrind option for tests.
[ardour.git] / libs / surfaces / mackie / controls.cc
1 /*
2         Copyright (C) 2006,2007 John Anderson
3
4         This program is free software; you can redistribute it and/or modify
5         it under the terms of the GNU General Public License as published by
6         the Free Software Foundation; either version 2 of the License, or
7         (at your option) any later version.
8
9         This program is distributed in the hope that it will be useful,
10         but WITHOUT ANY WARRANTY; without even the implied warranty of
11         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12         GNU General Public License for more details.
13
14         You should have received a copy of the GNU General Public License
15         along with this program; if not, write to the Free Software
16         Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18 #include "controls.h"
19 #include "types.h"
20 #include "mackie_midi_builder.h"
21
22 #include <iostream>
23 #include <iomanip>
24 #include <sstream>
25
26 using namespace Mackie;
27 using namespace std;
28
29 void Group::add( Control & control )
30 {
31         _controls.push_back( &control );
32 }
33
34 Strip::Strip( const std::string & name, int index )
35         : Group( name )
36         , _solo( 0 )
37         , _recenable( 0 )
38         , _mute( 0 )
39         , _select( 0 )
40         , _vselect( 0 )
41         , _fader_touch( 0 )
42         , _vpot( 0 )
43         , _gain( 0 )
44         , _index( index )
45 {
46 }
47
48 /**
49         TODO could optimise this to use enum, but it's only
50         called during the protocol class instantiation.
51
52         generated using
53
54         irb -r controls.rb
55         sf=Surface.new
56         sf.parse
57         controls = sf.groups.find{|x| x[0] =~ /strip/}.each{|x| puts x[1]}
58         controls[1].each {|x| puts "\telse if ( control.name() == \"#{x.name}\" )\n\t{\n\t\t_#{x.name} = reinterpret_cast<#{x.class.name}*>(&control);\n\t}\n"}
59 */
60 void Strip::add( Control & control )
61 {
62         Group::add( control );
63         if ( control.name() == "gain" )
64         {
65                 _gain = reinterpret_cast<Fader*>(&control);
66         }
67         else if ( control.name() == "vpot" )
68         {
69                 _vpot = reinterpret_cast<Pot*>(&control);
70         }
71         else if ( control.name() == "recenable" )
72         {
73                 _recenable = reinterpret_cast<Button*>(&control);
74         }
75         else if ( control.name() == "solo" )
76         {
77                 _solo = reinterpret_cast<Button*>(&control);
78         }
79         else if ( control.name() == "mute" )
80         {
81                 _mute = reinterpret_cast<Button*>(&control);
82         }
83         else if ( control.name() == "select" )
84         {
85                 _select = reinterpret_cast<Button*>(&control);
86         }
87         else if ( control.name() == "vselect" )
88         {
89                 _vselect = reinterpret_cast<Button*>(&control);
90         }
91         else if ( control.name() == "fader_touch" )
92         {
93                 _fader_touch = reinterpret_cast<Button*>(&control);
94         }
95         else if ( control.type() == Control::type_led || control.type() == Control::type_led_ring )
96         {
97                 // do nothing
98                 cout << "Strip::add not adding " << control << endl;
99         }
100         else
101         {
102                 ostringstream os;
103                 os << "Strip::add: unknown control type " << control;
104                 throw MackieControlException( os.str() );
105         }
106 }
107
108 Control::Control( int id, int ordinal, std::string name, Group & group )
109 : _id( id )
110 , _ordinal( ordinal )
111 , _name( name )
112 , _group( group )
113 , _in_use( false )
114 {
115 }
116
117 /**
118  generated with
119
120 controls[1].each do |x|
121   puts <<EOF
122 #{x.class.name} & Strip::#{x.name}()
123 {
124         if ( _#{x.name} == 0 )
125                 throw MackieControlException( "#{x.name} is null" );
126         return *_#{x.name};
127 }
128 EOF
129 end
130 */
131 Fader & Strip::gain()
132 {
133         if ( _gain == 0 )
134                 throw MackieControlException( "gain is null" );
135         return *_gain;
136 }
137 Pot & Strip::vpot()
138 {
139         if ( _vpot == 0 )
140                 throw MackieControlException( "vpot is null" );
141         return *_vpot;
142 }
143 Button & Strip::recenable()
144 {
145         if ( _recenable == 0 )
146                 throw MackieControlException( "recenable is null" );
147         return *_recenable;
148 }
149 Button & Strip::solo()
150 {
151         if ( _solo == 0 )
152                 throw MackieControlException( "solo is null" );
153         return *_solo;
154 }
155 Button & Strip::mute()
156 {
157         if ( _mute == 0 )
158                 throw MackieControlException( "mute is null" );
159         return *_mute;
160 }
161 Button & Strip::select()
162 {
163         if ( _select == 0 )
164                 throw MackieControlException( "select is null" );
165         return *_select;
166 }
167 Button & Strip::vselect()
168 {
169         if ( _vselect == 0 )
170                 throw MackieControlException( "vselect is null" );
171         return *_vselect;
172 }
173 Button & Strip::fader_touch()
174 {
175         if ( _fader_touch == 0 )
176                 throw MackieControlException( "fader_touch is null" );
177         return *_fader_touch;
178 }
179
180 /** @return true if the control is in use, or false otherwise.
181     Buttons are `in use' when they are held down.
182     Faders with touch support are `in use' when they are being touched.
183     Pots, or faders without touch support, are `in use' from the first move
184     event until a timeout after the last move event.
185 */
186 bool
187 Control::in_use () const
188 {
189         return _in_use;
190 }
191
192 void
193 Control::set_in_use (bool in_use)
194 {
195         _in_use = in_use;
196 }
197
198 ostream & Mackie::operator << ( ostream & os, const Mackie::Control & control )
199 {
200         os << typeid( control ).name();
201         os << " { ";
202         os << "name: " << control.name();
203         os << ", ";
204         os << "id: " << "0x" << setw(4) << setfill('0') << hex << control.id() << setfill(' ');
205         os << ", ";
206         os << "type: " << "0x" << setw(2) << setfill('0') << hex << control.type() << setfill(' ');
207         os << ", ";
208         os << "raw_id: " << "0x" << setw(2) << setfill('0') << hex << control.raw_id() << setfill(' ');
209         os << ", ";
210         os << "ordinal: " << dec << control.ordinal();
211         os << ", ";
212         os << "group: " << control.group().name();
213         os << " }";
214         
215         return os;
216 }
217
218 std::ostream & Mackie::operator << ( std::ostream & os, const Strip & strip )
219 {
220         os << typeid( strip ).name();
221         os << " { ";
222         os << "has_solo: " << boolalpha << strip.has_solo();
223         os << ", ";
224         os << "has_recenable: " << boolalpha << strip.has_recenable();
225         os << ", ";
226         os << "has_mute: " << boolalpha << strip.has_mute();
227         os << ", ";
228         os << "has_select: " << boolalpha << strip.has_select();
229         os << ", ";
230         os << "has_vselect: " << boolalpha << strip.has_vselect();
231         os << ", ";
232         os << "has_fader_touch: " << boolalpha << strip.has_fader_touch();
233         os << ", ";
234         os << "has_vpot: " << boolalpha << strip.has_vpot();
235         os << ", ";
236         os << "has_gain: " << boolalpha << strip.has_gain();
237         os << " }";
238         
239         return os;
240 }