Don't add duplicate remote_control_id to active banks. Notes and tweaks.
[ardour.git] / libs / surfaces / mackie / mackie_control_protocol_poll.cc
1 #include "mackie_control_protocol.h"
2
3 #include "midi_byte_array.h"
4 #include "surface_port.h"
5
6 #include <pbd/pthread_utils.h>
7 #include <pbd/error.h>
8
9 #include <midi++/types.h>
10 #include <midi++/port.h>
11 #include <midi++/manager.h>
12 #include <midi++/port_request.h>
13 #include "i18n.h"
14
15 #include <unistd.h>
16 #include <fcntl.h>
17 #include <poll.h>
18 #include <errno.h>
19
20 #include <iostream>
21 #include <string>
22 #include <vector>
23
24 using namespace std;
25 using namespace Mackie;
26 using namespace PBD;
27
28 const char * MackieControlProtocol::default_port_name = "mcu";
29
30 bool MackieControlProtocol::probe()
31 {
32         return MIDI::Manager::instance()->port( default_port_name ) != 0;
33 }
34
35 void * MackieControlProtocol::monitor_work()
36 {
37         cout << "MackieControlProtocol::monitor_work" << endl;
38         // What does ThreadCreatedWithRequestSize do?
39         PBD::ThreadCreated (pthread_self(), X_("Mackie"));
40
41 #if 0
42         // it seems to do the "block" on poll less often
43         // with this code disabled
44         struct sched_param rtparam;
45         memset (&rtparam, 0, sizeof (rtparam));
46         rtparam.sched_priority = 9; /* XXX should be relative to audio (JACK) thread */
47         
48         int err;
49         if ((err = pthread_setschedparam (pthread_self(), SCHED_FIFO, &rtparam)) != 0) {
50                 // do we care? not particularly.
51                 PBD::info << string_compose (_("%1: thread not running with realtime scheduling (%2)"), name(), strerror( errno )) << endmsg;
52         } 
53 #endif
54         
55         pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, 0);
56         pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
57
58         // read from midi ports
59         cout << "start poll cycle" << endl;
60         while ( true )
61         {
62                 update_ports();
63                 if ( poll_ports() )
64                 {
65                         try { read_ports(); }
66                         catch ( exception & e ) {
67                                 cout << "MackieControlProtocol::poll_ports caught exception: " << e.what() << endl;
68                         }
69                 }
70                 // provide a cancellation point
71                 pthread_testcancel();
72         }
73
74         // these never get called
75         cout << "MackieControlProtocol::poll_ports exiting" << endl;
76         
77         delete[] pfd;
78
79         return (void*) 0;
80 }
81
82 void MackieControlProtocol::update_ports()
83 {
84         // create pollfd structures if necessary
85         if ( _ports_changed )
86         {
87                 Glib::Mutex::Lock ul( update_mutex );
88                 // yes, this is a double-test locking paradigm, or whatever it's called
89                 // because we don't *always* need to acquire the lock for the first test
90                 if ( _ports_changed )
91                 {
92                         cout << "MackieControlProtocol::update_ports updating" << endl;
93                         if ( pfd != 0 ) delete[] pfd;
94                         // TODO This might be a memory leak. How does thread cancellation cleanup work?
95                         pfd = new pollfd[_ports.size()];
96                         nfds = 0;
97
98                         for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
99                         {
100                                 cout << "adding port " << (*it)->port().name() << " to pollfd" << endl;
101                                 pfd[nfds].fd = (*it)->port().selectable();
102                                 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
103                                 ++nfds;
104                         }
105                         _ports_changed = false;
106                 }
107                 cout << "MackieControlProtocol::update_ports signalling" << endl;
108                 update_cond.signal();
109                 cout << "MackieControlProtocol::update_ports finished" << endl;
110         }
111 }
112
113 void MackieControlProtocol::read_ports()
114 {
115         /* now read any data on the ports */
116         Glib::Mutex::Lock lock( update_mutex );
117         for ( int p = 0; p < nfds; ++p )
118         {
119                 // this will cause handle_midi_any in the MackiePort to be triggered
120                 if ( pfd[p].revents & POLLIN > 0 )
121                 {
122                         lock.release();
123                         _ports[p]->read();
124                         lock.acquire();
125                 }
126         }
127 }
128
129 bool MackieControlProtocol::poll_ports()
130 {
131         int timeout = 10; // milliseconds
132         int no_ports_sleep = 10; // milliseconds
133
134         Glib::Mutex::Lock lock( update_mutex );
135         // if there are no ports
136         if ( nfds < 1 )
137         {
138                 lock.release();
139                 cout << "poll_ports no ports" << endl;
140                 usleep( no_ports_sleep * 1000 );
141                 return false;
142         }
143
144         int retval = poll( pfd, nfds, timeout );
145         if ( retval < 0 )
146         {
147                 // gdb at work, perhaps
148                 if ( errno != EINTR )
149                 {
150                         error << string_compose(_("Mackie MIDI thread poll failed (%1)"), strerror( errno ) ) << endmsg;
151                 }
152                 return false;
153         }
154         
155         return retval > 0;
156 }
157
158 void MackieControlProtocol::handle_port_changed( SurfacePort * port, bool active )
159 {
160         cout << "MackieControlProtocol::handle_port_changed port: " << *port << " active: " << active << endl;
161         if ( active == false )
162         {
163                 // port gone away. So stop polling it ASAP
164                 {
165                         // delete the port instance
166                         Glib::Mutex::Lock lock( update_mutex );
167                         MackiePorts::iterator it = find( _ports.begin(), _ports.end(), port );
168                         if ( it != _ports.end() )
169                         {
170                                 delete *it;
171                                 _ports.erase( it );
172                         }
173                 }
174                 _ports_changed = true;
175                 update_ports();
176         }
177         else
178         {
179                 _ports_changed = true;
180                 // port added
181                 update_ports();
182                 update_surface();
183                 
184                 // TODO update bank size
185                 
186                 // rebuild surface
187         }
188 }
189
190 void MackieControlProtocol::handle_port_init( Mackie::SurfacePort * sport )
191 {
192         cout << "MackieControlProtocol::handle_port_init" << endl;
193         _ports_changed = true;
194         update_ports();
195         cout << "MackieControlProtocol::handle_port_init finished" << endl;
196 }