most of the 2.X->3.0 commit (up to rev 4299) except for gtk2_ardour/editor_canvas...
[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 "i18n.h"
13
14 #include <unistd.h>
15 #include <fcntl.h>
16 #include <poll.h>
17 #include <errno.h>
18
19 #include <iostream>
20 #include <string>
21 #include <vector>
22
23 using namespace std;
24 using namespace Mackie;
25 using namespace PBD;
26
27 const char * MackieControlProtocol::default_port_name = "mcu";
28
29 bool MackieControlProtocol::probe()
30 {
31         return MIDI::Manager::instance()->port( default_port_name ) != 0;
32 }
33
34 void * MackieControlProtocol::monitor_work()
35 {
36         PBD::notify_gui_about_thread_creation (pthread_self(), X_("Mackie"));
37
38         pthread_setcancelstate (PTHREAD_CANCEL_ENABLE, 0);
39         pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS, 0);
40
41         // read from midi ports
42         while ( _polling )
43         {
44                 try
45                 {
46                         if ( poll_ports() )
47                         {
48                                 try { read_ports(); }
49                                 catch ( exception & e ) {
50                                         cout << "MackieControlProtocol::poll_ports caught exception: " << e.what() << endl;
51                                         _ports_changed = true;
52                                         update_ports();
53                                 }
54                         }
55                         // poll for automation data from the routes
56                         poll_automation();
57                 }
58                 catch ( exception & e )
59                 {
60                         cout << "caught exception in MackieControlProtocol::monitor_work " << e.what() << endl;
61                 }
62         }
63
64         // TODO ports and pfd and nfds should be in a separate class
65         delete[] pfd;
66         pfd = 0;
67         nfds = 0;
68
69         return (void*) 0;
70 }
71
72 void MackieControlProtocol::update_ports()
73 {
74         if ( _ports_changed )
75         {
76                 Glib::Mutex::Lock ul( update_mutex );
77                 // yes, this is a double-test locking paradigm, or whatever it's called
78                 // because we don't *always* need to acquire the lock for the first test
79                 if ( _ports_changed )
80                 {
81                         // create new pollfd structures
82                         if ( pfd != 0 ) delete[] pfd;
83                         // TODO This might be a memory leak. How does thread cancellation cleanup work?
84                         pfd = new pollfd[_ports.size()];
85                         nfds = 0;
86
87                         for( MackiePorts::iterator it = _ports.begin(); it != _ports.end(); ++it )
88                         {
89                                 //cout << "adding port " << (*it)->port().name() << " to pollfd" << endl;
90                                 pfd[nfds].fd = (*it)->port().selectable();
91                                 pfd[nfds].events = POLLIN|POLLHUP|POLLERR;
92                                 ++nfds;
93                         }
94                         _ports_changed = false;
95                 }
96                 update_cond.signal();
97         }
98 }
99
100 void MackieControlProtocol::read_ports()
101 {
102         /* now read any data on the ports */
103         Glib::Mutex::Lock lock( update_mutex );
104         for ( int p = 0; p < nfds; ++p )
105         {
106                 // this will cause handle_midi_any in the MackiePort to be triggered
107                 // for alsa/raw ports
108                 // alsa/sequencer ports trigger the midi parser off poll
109                 if ( (pfd[p].revents & POLLIN) > 0 )
110                 {
111                         // avoid deadlocking?
112                         // doesn't seem to make a difference
113                         //lock.release();
114                         _ports[p]->read();
115                         //lock.acquire();
116                 }
117         }
118 }
119
120 bool MackieControlProtocol::poll_ports()
121 {
122         int timeout = 10; // milliseconds
123         int no_ports_sleep = 1000; // milliseconds
124
125         Glib::Mutex::Lock lock( update_mutex );
126         // if there are no ports
127         if ( nfds < 1 )
128         {
129                 lock.release();
130                 //cout << "poll_ports no ports" << endl;
131                 usleep( no_ports_sleep * 1000 );
132                 return false;
133         }
134
135         int retval = poll( pfd, nfds, timeout );
136         if ( retval < 0 )
137         {
138                 // gdb at work, perhaps
139                 if ( errno != EINTR )
140                 {
141                         error << string_compose(_("Mackie MIDI thread poll failed (%1)"), strerror( errno ) ) << endmsg;
142                 }
143                 return false;
144         }
145         
146         return retval > 0;
147 }
148
149 void MackieControlProtocol::handle_port_inactive( SurfacePort * port )
150 {
151         // port gone away. So stop polling it ASAP
152         {
153                 // delete the port instance
154                 Glib::Mutex::Lock lock( update_mutex );
155                 MackiePorts::iterator it = find( _ports.begin(), _ports.end(), port );
156                 if ( it != _ports.end() )
157                 {
158                         delete *it;
159                         _ports.erase( it );
160                 }
161         }
162         _ports_changed = true;
163         update_ports();
164         
165         // TODO all the rebuilding of surfaces and so on
166 }
167
168 void MackieControlProtocol::handle_port_active( SurfacePort * port )
169 {
170         // no need to re-add port because it was already added
171         // during the init phase. So just update the local surface
172         // representation and send the representation to 
173         // all existing ports
174         
175         // TODO update bank size
176         
177         // TODO rebuild surface, to have new units
178         
179         // finally update session state to the surface
180         // TODO but this is also done in set_active, and
181         // in fact update_surface won't execute unless
182         // _active == true
183         //cout << "update_surface in handle_port_active" << endl;
184         update_surface();
185 }
186
187 void MackieControlProtocol::handle_port_init( Mackie::SurfacePort * sport )
188 {
189         //cout << "MackieControlProtocol::handle_port_init" << endl;
190         _ports_changed = true;
191         update_ports();
192 }