MCP: more tracing for rewind; change play LED illumination rule
[ardour.git] / libs / surfaces / control_protocol / basic_ui.cc
1 /*
2     Copyright (C) 2006 Paul Davis 
3
4     This program is free software; you can redistribute it
5     and/or modify it under the terms of the GNU Lesser
6     General Public License as published by the Free Software
7     Foundation; either version 2 of the License, or (at your
8     option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #include "pbd/pthread_utils.h"
22 #include "pbd/memento_command.h"
23
24 #include "ardour/session.h"
25 #include "ardour/location.h"
26
27 #include "control_protocol/basic_ui.h"
28
29 #include "i18n.h"
30
31 using namespace ARDOUR;
32
33 PBD::Signal2<void,std::string,std::string> BasicUI::AccessAction;
34
35 BasicUI::BasicUI (Session& s)
36         : session (&s)
37 {
38 }
39
40 BasicUI::BasicUI ()
41         : session (0)
42 {
43 }
44
45 BasicUI::~BasicUI ()
46 {
47         
48 }
49
50 void
51 BasicUI::register_thread (std::string name)
52 {
53         std::string pool_name = name;
54         pool_name += " events";
55
56         SessionEvent::create_per_thread_pool (pool_name, 64);
57 }
58
59 void
60 BasicUI::access_action ( std::string action_path ) 
61 {
62         int split_at = action_path.find( "/" );
63         std::string group = action_path.substr( 0, split_at );
64         std::string item = action_path.substr( split_at + 1 );
65
66         AccessAction( group, item );
67 }
68
69 void
70 BasicUI::loop_toggle () 
71 {
72         if (session->get_play_loop()) {
73                 session->request_play_loop (false);
74         } else {
75                 session->request_play_loop (true);
76                 if (!session->transport_rolling()) {
77                         session->request_transport_speed (1.0);
78                 }
79         }
80 }
81
82 void
83 BasicUI::goto_start ()
84 {
85         session->goto_start ();
86 }
87
88 void
89 BasicUI::goto_end ()
90 {
91         session->goto_end ();
92 }
93
94 void       
95 BasicUI::add_marker (const std::string& markername)
96 {
97         framepos_t where = session->audible_frame();
98         Location *location = new Location (*session, where, where, markername, Location::IsMark);
99         session->begin_reversible_command (_("add marker"));
100         XMLNode &before = session->locations()->get_state();
101         session->locations()->add (location, true);
102         XMLNode &after = session->locations()->get_state();
103         session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
104         session->commit_reversible_command ();
105 }
106
107 void
108 BasicUI::rewind ()
109 {
110         std::cerr << "request transport speed of " << session->transport_speed() - 1.5 << std::endl;
111         session->request_transport_speed (session->transport_speed() - 1.5);
112 }
113
114 void
115 BasicUI::ffwd ()
116 {
117         session->request_transport_speed (session->transport_speed() + 1.5);
118 }
119
120 void
121 BasicUI::transport_stop ()
122 {
123         session->request_transport_speed (0.0);
124 }
125
126 void
127 BasicUI::transport_play (bool from_last_start)
128 {
129         bool rolling = session->transport_rolling ();
130
131         if (session->get_play_loop()) {
132                 session->request_play_loop (false);
133         } 
134
135         if (session->get_play_range ()) {
136                 session->request_play_range (0);
137         }
138         
139         if (from_last_start && rolling) {
140                 session->request_locate (session->last_transport_start(), true);
141
142         }
143
144         session->request_transport_speed (1.0f);
145 }
146
147 void
148 BasicUI::rec_enable_toggle ()
149 {
150         switch (session->record_status()) {
151         case Session::Disabled:
152                 if (session->ntracks() == 0) {
153                         // string txt = _("Please create 1 or more track\nbefore trying to record.\nCheck the Session menu.");
154                         // MessageDialog msg (*editor, txt);
155                         // msg.run ();
156                         return;
157                 }
158                 session->maybe_enable_record ();
159                 break;
160         case Session::Recording:
161         case Session::Enabled:
162                 session->disable_record (true);
163         }
164 }
165
166 void
167 BasicUI::save_state ()
168 {
169         session->save_state ("");
170 }
171
172 void
173 BasicUI::prev_marker ()
174 {
175         Location *location = session->locations()->first_location_before (session->transport_frame());
176         
177         if (location) {
178                 session->request_locate (location->start(), session->transport_rolling());
179         } else {
180                 session->goto_start ();
181         }
182 }
183
184 void
185 BasicUI::next_marker ()
186 {
187         Location *location = session->locations()->first_location_after (session->transport_frame());
188
189         if (location) {
190                 session->request_locate (location->start(), session->transport_rolling());
191         } else {
192                 session->request_locate (session->current_end_frame());
193         }
194 }
195
196 void
197 BasicUI::set_transport_speed (double speed)
198 {
199         session->request_transport_speed (speed);
200 }
201
202 double
203 BasicUI::get_transport_speed ()
204 {
205         return session->transport_speed ();
206 }
207
208 void
209 BasicUI::undo ()
210 {
211         session->undo (1);
212 }
213
214 void
215 BasicUI::redo ()
216 {
217         session->redo (1);
218 }
219
220 void
221 BasicUI::toggle_all_rec_enables ()
222 {
223         if (session->get_record_enabled()) {
224                 // session->record_disenable_all ();
225         } else {
226                 // session->record_enable_all ();
227         }
228 }
229
230 void
231 BasicUI::toggle_punch_in ()
232 {
233         session->config.set_punch_in (!session->config.get_punch_in());
234 }
235
236 void
237 BasicUI::toggle_punch_out ()
238 {
239         session->config.set_punch_out (!session->config.get_punch_out());
240 }
241
242 bool
243 BasicUI::get_record_enabled ()
244 {
245         return session->get_record_enabled();
246 }
247
248 void
249 BasicUI::set_record_enable (bool yn)
250 {
251         if (yn) {
252                 session->maybe_enable_record ();
253         } else {
254                 session->disable_record (false, true);
255         }
256 }
257
258 framepos_t
259 BasicUI::transport_frame ()
260 {
261         return session->transport_frame();
262 }
263
264 void
265 BasicUI::locate (framepos_t where, bool roll_after_locate)
266 {
267         session->request_locate (where, roll_after_locate);
268 }
269
270 bool
271 BasicUI::locating ()
272 {
273         return session->locate_pending();
274 }
275
276 bool
277 BasicUI::locked ()
278 {
279         return session->transport_locked ();
280 }
281
282 ARDOUR::framecnt_t
283 BasicUI::timecode_frames_per_hour ()
284 {
285         return session->timecode_frames_per_hour ();
286 }
287
288 void
289 BasicUI::timecode_time (framepos_t where, Timecode::Time& timecode)
290 {
291         session->timecode_time (where, *((Timecode::Time *) &timecode));
292 }
293
294 void 
295 BasicUI::timecode_to_sample (Timecode::Time& timecode, framepos_t & sample, bool use_offset, bool use_subframes) const
296 {
297         session->timecode_to_sample (*((Timecode::Time*)&timecode), sample, use_offset, use_subframes);
298 }
299
300 void 
301 BasicUI::sample_to_timecode (framepos_t sample, Timecode::Time& timecode, bool use_offset, bool use_subframes) const
302 {
303         session->sample_to_timecode (sample, *((Timecode::Time*)&timecode), use_offset, use_subframes);
304 }
305
306 #if 0
307 this stuff is waiting to go in so that all UIs can offer complex solo/mute functionality
308
309 void
310 BasicUI::solo_release (boost::shared_ptr<Route> r)
311 {
312 }
313
314 void
315 BasicUI::solo_press (boost::shared_ptr<Route> r, bool momentary, bool global, bool exclusive, bool isolate, bool solo_group)
316 {
317         if (momentary) {
318                 _solo_release = new SoloMuteRelease (_route->soloed());
319         }
320         
321         if (global) {
322                 
323                 if (_solo_release) {
324                         _solo_release->routes = _session->get_routes ();
325                 }
326                 
327                 if (Config->get_solo_control_is_listen_control()) {
328                         _session->set_listen (_session->get_routes(), !_route->listening(),  Session::rt_cleanup, true);
329                 } else {
330                         _session->set_solo (_session->get_routes(), !_route->soloed(),  Session::rt_cleanup, true);
331                 }
332                 
333         } else if (exclusive) {
334                 
335                 if (_solo_release) {
336                         _solo_release->exclusive = true;
337                         
338                         boost::shared_ptr<RouteList> routes = _session->get_routes();
339                         
340                         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
341                                 if ((*i)->soloed ()) {
342                                         _solo_release->routes_on->push_back (*i);
343                                 } else {
344                                         _solo_release->routes_off->push_back (*i);
345                                 }
346                         }
347                 }
348                 
349                 if (Config->get_solo_control_is_listen_control()) {
350                         /* ??? we need a just_one_listen() method */
351                 } else {
352                         _session->set_just_one_solo (_route, true);
353                 }
354                 
355         } else if (isolate) {
356                 
357                 // shift-click: toggle solo isolated status
358                 
359                 _route->set_solo_isolated (!_route->solo_isolated(), this);
360                 delete _solo_release;
361                 _solo_release = 0;
362                 
363         } else if (solo_group) {
364                 
365                 /* Primary-button1: solo mix group.
366                    NOTE: Primary-button2 is MIDI learn.
367                 */
368                 
369                 if (_route->route_group()) {
370                         
371                         if (_solo_release) {
372                                 _solo_release->routes = _route->route_group()->route_list();
373                         }
374                         
375                         if (Config->get_solo_control_is_listen_control()) {
376                                 _session->set_listen (_route->route_group()->route_list(), !_route->listening(),  Session::rt_cleanup, true);
377                         } else {
378                                 _session->set_solo (_route->route_group()->route_list(), !_route->soloed(),  Session::rt_cleanup, true);
379                         }
380                 }
381                 
382         } else {
383                 
384                 /* click: solo this route */
385                 
386                 boost::shared_ptr<RouteList> rl (new RouteList);
387                 rl->push_back (route());
388                 
389                 if (_solo_release) {
390                         _solo_release->routes = rl;
391                 }
392                 
393                 if (Config->get_solo_control_is_listen_control()) {
394                         _session->set_listen (rl, !_route->listening());
395                 } else {
396                         _session->set_solo (rl, !_route->soloed());
397                 }
398         }
399 }
400 #endif