update surface when remote ids change
[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                                 _ports_changed = true;
69                                 update_ports();
70                         }
71                 }
72                 // provide a cancellation point
73                 pthread_testcancel();
74         }
75
76         // these never get called
77         cout << "MackieControlProtocol::poll_ports exiting" << endl;
78         
79         delete[] pfd;
80
81         return (void*) 0;
82 }
83
84 void MackieControlProtocol::update_ports()
85 {
86         // create pollfd structures if necessary
87         if ( _ports_changed )
88         {
89                 Glib::Mutex::Lock ul( update_mutex );
90                 // yes, this is a double-test locking paradigm, or whatever it's called
91                 // because we don't *always* need to acquire the lock for the first test
92                 if ( _ports_changed )
93                 {
94                         cout << "MackieControlProtocol::update_ports updating" << endl;
95                         if ( pfd != 0 ) delete[] pfd;
96                         // TODO This might be a memory leak. How does thread cancellation cleanup work?
97                         pfd = new pollfd[_ports.size()];
98                         nfds = 0;
99
100                         for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
101                         {
102                                 cout << "adding port " << (*it)->port().name() << " to pollfd" << endl;
103                                 pfd[nfds].fd = (*it)->port().selectable();
104                                 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
105                                 ++nfds;
106                         }
107                         _ports_changed = false;
108                 }
109                 cout << "MackieControlProtocol::update_ports signalling" << endl;
110                 update_cond.signal();
111                 cout << "MackieControlProtocol::update_ports finished" << endl;
112         }
113 }
114
115 void MackieControlProtocol::read_ports()
116 {
117         /* now read any data on the ports */
118         Glib::Mutex::Lock lock( update_mutex );
119         for ( int p = 0; p < nfds; ++p )
120         {
121                 // this will cause handle_midi_any in the MackiePort to be triggered
122                 if ( pfd[p].revents & POLLIN > 0 )
123                 {
124                         lock.release();
125                         _ports[p]->read();
126                         lock.acquire();
127                 }
128         }
129 }
130
131 bool MackieControlProtocol::poll_ports()
132 {
133         int timeout = 10; // milliseconds
134         int no_ports_sleep = 1000; // milliseconds
135
136         Glib::Mutex::Lock lock( update_mutex );
137         // if there are no ports
138         if ( nfds < 1 )
139         {
140                 lock.release();
141                 cout << "poll_ports no ports" << endl;
142                 usleep( no_ports_sleep * 1000 );
143                 return false;
144         }
145
146         int retval = poll( pfd, nfds, timeout );
147         if ( retval < 0 )
148         {
149                 // gdb at work, perhaps
150                 if ( errno != EINTR )
151                 {
152                         error << string_compose(_("Mackie MIDI thread poll failed (%1)"), strerror( errno ) ) << endmsg;
153                 }
154                 return false;
155         }
156         
157         return retval > 0;
158 }
159
160 void MackieControlProtocol::handle_port_changed( SurfacePort * port, bool active )
161 {
162         cout << "MackieControlProtocol::handle_port_changed port: " << *port << " active: " << active << endl;
163         if ( active == false )
164         {
165                 // port gone away. So stop polling it ASAP
166                 {
167                         // delete the port instance
168                         Glib::Mutex::Lock lock( update_mutex );
169                         MackiePorts::iterator it = find( _ports.begin(), _ports.end(), port );
170                         if ( it != _ports.end() )
171                         {
172                                 delete *it;
173                                 _ports.erase( it );
174                         }
175                 }
176                 _ports_changed = true;
177                 update_ports();
178         }
179         else
180         {
181                 _ports_changed = true;
182                 // port added
183                 update_ports();
184                 update_surface();
185                 
186                 // TODO update bank size
187                 
188                 // rebuild surface
189         }
190 }
191
192 void MackieControlProtocol::handle_port_init( Mackie::SurfacePort * sport )
193 {
194         cout << "MackieControlProtocol::handle_port_init" << endl;
195         _ports_changed = true;
196         update_ports();
197         cout << "MackieControlProtocol::handle_port_init finished" << endl;
198 }