"merge" (i.e. wholesale import) 2.0-ongoing Mackie code and then fix to compile in...
[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 , _in_use_timeout( 250 )
115 {
116 }
117
118 /**
119  generated with
120
121 controls[1].each do |x|
122   puts <<EOF
123 #{x.class.name} & Strip::#{x.name}()
124 {
125         if ( _#{x.name} == 0 )
126                 throw MackieControlException( "#{x.name} is null" );
127         return *_#{x.name};
128 }
129 EOF
130 end
131 */
132 Fader & Strip::gain()
133 {
134         if ( _gain == 0 )
135                 throw MackieControlException( "gain is null" );
136         return *_gain;
137 }
138 Pot & Strip::vpot()
139 {
140         if ( _vpot == 0 )
141                 throw MackieControlException( "vpot is null" );
142         return *_vpot;
143 }
144 Button & Strip::recenable()
145 {
146         if ( _recenable == 0 )
147                 throw MackieControlException( "recenable is null" );
148         return *_recenable;
149 }
150 Button & Strip::solo()
151 {
152         if ( _solo == 0 )
153                 throw MackieControlException( "solo is null" );
154         return *_solo;
155 }
156 Button & Strip::mute()
157 {
158         if ( _mute == 0 )
159                 throw MackieControlException( "mute is null" );
160         return *_mute;
161 }
162 Button & Strip::select()
163 {
164         if ( _select == 0 )
165                 throw MackieControlException( "select is null" );
166         return *_select;
167 }
168 Button & Strip::vselect()
169 {
170         if ( _vselect == 0 )
171                 throw MackieControlException( "vselect is null" );
172         return *_vselect;
173 }
174 Button & Strip::fader_touch()
175 {
176         if ( _fader_touch == 0 )
177                 throw MackieControlException( "fader_touch is null" );
178         return *_fader_touch;
179 }
180
181 bool Control::in_use() const
182 {
183         return _in_use;
184 }
185
186 Control & Control::in_use( bool rhs )
187 {
188         _in_use = rhs;
189         return *this;
190 }
191
192 ostream & Mackie::operator << ( ostream & os, const Mackie::Control & control )
193 {
194         os << typeid( control ).name();
195         os << " { ";
196         os << "name: " << control.name();
197         os << ", ";
198         os << "id: " << "0x" << setw(4) << setfill('0') << hex << control.id() << setfill(' ');
199         os << ", ";
200         os << "type: " << "0x" << setw(2) << setfill('0') << hex << control.type() << setfill(' ');
201         os << ", ";
202         os << "raw_id: " << "0x" << setw(2) << setfill('0') << hex << control.raw_id() << setfill(' ');
203         os << ", ";
204         os << "ordinal: " << dec << control.ordinal();
205         os << ", ";
206         os << "group: " << control.group().name();
207         os << " }";
208         
209         return os;
210 }
211
212 std::ostream & Mackie::operator << ( std::ostream & os, const Strip & strip )
213 {
214         os << typeid( strip ).name();
215         os << " { ";
216         os << "has_solo: " << boolalpha << strip.has_solo();
217         os << ", ";
218         os << "has_recenable: " << boolalpha << strip.has_recenable();
219         os << ", ";
220         os << "has_mute: " << boolalpha << strip.has_mute();
221         os << ", ";
222         os << "has_select: " << boolalpha << strip.has_select();
223         os << ", ";
224         os << "has_vselect: " << boolalpha << strip.has_vselect();
225         os << ", ";
226         os << "has_fader_touch: " << boolalpha << strip.has_fader_touch();
227         os << ", ";
228         os << "has_vpot: " << boolalpha << strip.has_vpot();
229         os << ", ";
230         os << "has_gain: " << boolalpha << strip.has_gain();
231         os << " }";
232         
233         return os;
234 }