MCP: alternative method for clearing route table when switching banks
[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 uint32_t Control::button_cnt = 0;
30 uint32_t Control::pot_cnt = 0;
31 uint32_t Control::fader_cnt = 0;
32 uint32_t Control::led_cnt = 0;
33 uint32_t Control::jog_cnt = 0;
34
35 void Group::add( Control & control )
36 {
37         _controls.push_back( &control );
38 }
39
40 Strip::Strip( const std::string & name, int index )
41         : Group( name )
42         , _solo( 0 )
43         , _recenable( 0 )
44         , _mute( 0 )
45         , _select( 0 )
46         , _vselect( 0 )
47         , _fader_touch( 0 )
48         , _vpot( 0 )
49         , _gain( 0 )
50         , _index( index )
51 {
52 }
53
54 /**
55         TODO could optimise this to use enum, but it's only
56         called during the protocol class instantiation.
57
58         generated using
59
60         irb -r controls.rb
61         sf=Surface.new
62         sf.parse
63         controls = sf.groups.find{|x| x[0] =~ /strip/}.each{|x| puts x[1]}
64         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"}
65 */
66 void Strip::add( Control & control )
67 {
68         Group::add( control );
69         if ( control.name() == "gain" )
70         {
71                 _gain = reinterpret_cast<Fader*>(&control);
72         }
73         else if ( control.name() == "vpot" )
74         {
75                 _vpot = reinterpret_cast<Pot*>(&control);
76         }
77         else if ( control.name() == "recenable" )
78         {
79                 _recenable = reinterpret_cast<Button*>(&control);
80         }
81         else if ( control.name() == "solo" )
82         {
83                 _solo = reinterpret_cast<Button*>(&control);
84         }
85         else if ( control.name() == "mute" )
86         {
87                 _mute = reinterpret_cast<Button*>(&control);
88         }
89         else if ( control.name() == "select" )
90         {
91                 _select = reinterpret_cast<Button*>(&control);
92         }
93         else if ( control.name() == "vselect" )
94         {
95                 _vselect = reinterpret_cast<Button*>(&control);
96         }
97         else if ( control.name() == "fader_touch" )
98         {
99                 _fader_touch = reinterpret_cast<Button*>(&control);
100         }
101         else if ( control.type() == Control::type_led || control.type() == Control::type_led_ring )
102         {
103                 // do nothing
104                 cout << "Strip::add not adding " << control << endl;
105         }
106         else
107         {
108                 ostringstream os;
109                 os << "Strip::add: unknown control type " << control;
110                 throw MackieControlException( os.str() );
111         }
112 }
113
114 Control::Control( int id, int ordinal, std::string name, Group & group )
115 : _id( id )
116 , _ordinal( ordinal )
117 , _name( name )
118 , _group( group )
119 , _in_use( false )
120 {
121 }
122
123 /**
124  generated with
125
126 controls[1].each do |x|
127   puts <<EOF
128 #{x.class.name} & Strip::#{x.name}()
129 {
130         if ( _#{x.name} == 0 )
131                 throw MackieControlException( "#{x.name} is null" );
132         return *_#{x.name};
133 }
134 EOF
135 end
136 */
137 Fader & Strip::gain()
138 {
139         if ( _gain == 0 )
140                 throw MackieControlException( "gain is null" );
141         return *_gain;
142 }
143 Pot & Strip::vpot()
144 {
145         if ( _vpot == 0 )
146                 throw MackieControlException( "vpot is null" );
147         return *_vpot;
148 }
149 Button & Strip::recenable()
150 {
151         if ( _recenable == 0 )
152                 throw MackieControlException( "recenable is null" );
153         return *_recenable;
154 }
155 Button & Strip::solo()
156 {
157         if ( _solo == 0 )
158                 throw MackieControlException( "solo is null" );
159         return *_solo;
160 }
161 Button & Strip::mute()
162 {
163         if ( _mute == 0 )
164                 throw MackieControlException( "mute is null" );
165         return *_mute;
166 }
167 Button & Strip::select()
168 {
169         if ( _select == 0 )
170                 throw MackieControlException( "select is null" );
171         return *_select;
172 }
173 Button & Strip::vselect()
174 {
175         if ( _vselect == 0 )
176                 throw MackieControlException( "vselect is null" );
177         return *_vselect;
178 }
179 Button & Strip::fader_touch()
180 {
181         if ( _fader_touch == 0 )
182                 throw MackieControlException( "fader_touch is null" );
183         return *_fader_touch;
184 }
185
186 /** @return true if the control is in use, or false otherwise.
187     Buttons are `in use' when they are held down.
188     Faders with touch support are `in use' when they are being touched.
189     Pots, or faders without touch support, are `in use' from the first move
190     event until a timeout after the last move event.
191 */
192 bool
193 Control::in_use () const
194 {
195         return _in_use;
196 }
197
198 void
199 Control::set_in_use (bool in_use)
200 {
201         _in_use = in_use;
202 }
203
204 ostream & Mackie::operator << ( ostream & os, const Mackie::Control & control )
205 {
206         os << typeid( control ).name();
207         os << " { ";
208         os << "name: " << control.name();
209         os << ", ";
210         os << "id: " << "0x" << setw(4) << setfill('0') << hex << control.id() << setfill(' ');
211         os << ", ";
212         os << "type: " << "0x" << setw(2) << setfill('0') << hex << control.type() << setfill(' ');
213         os << ", ";
214         os << "raw_id: " << "0x" << setw(2) << setfill('0') << hex << control.raw_id() << setfill(' ');
215         os << ", ";
216         os << "ordinal: " << dec << control.ordinal();
217         os << ", ";
218         os << "group: " << control.group().name();
219         os << " }";
220         
221         return os;
222 }
223
224 std::ostream & Mackie::operator << ( std::ostream & os, const Strip & strip )
225 {
226         os << typeid( strip ).name();
227         os << " { ";
228         os << "has_solo: " << boolalpha << strip.has_solo();
229         os << ", ";
230         os << "has_recenable: " << boolalpha << strip.has_recenable();
231         os << ", ";
232         os << "has_mute: " << boolalpha << strip.has_mute();
233         os << ", ";
234         os << "has_select: " << boolalpha << strip.has_select();
235         os << ", ";
236         os << "has_vselect: " << boolalpha << strip.has_vselect();
237         os << ", ";
238         os << "has_fader_touch: " << boolalpha << strip.has_fader_touch();
239         os << ", ";
240         os << "has_vpot: " << boolalpha << strip.has_vpot();
241         os << ", ";
242         os << "has_gain: " << boolalpha << strip.has_gain();
243         os << " }";
244         
245         return os;
246 }