f4fcec95a0a77a66a516ac5735adac6c0bd8667a
[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 #include "ardour/tempo.h"
27
28 #include "control_protocol/basic_ui.h"
29
30 #include "i18n.h"
31
32 using namespace ARDOUR;
33
34 PBD::Signal2<void,std::string,std::string> BasicUI::AccessAction;
35
36 BasicUI::BasicUI (Session& s)
37         : session (&s)
38 {
39 }
40
41 BasicUI::BasicUI ()
42         : session (0)
43 {
44 }
45
46 BasicUI::~BasicUI ()
47 {
48
49 }
50
51 void
52 BasicUI::register_thread (std::string name)
53 {
54         std::string pool_name = name;
55         pool_name += " events";
56
57         SessionEvent::create_per_thread_pool (pool_name, 64);
58 }
59
60 void
61 BasicUI::access_action ( std::string action_path )
62 {
63         int split_at = action_path.find( "/" );
64         std::string group = action_path.substr( 0, split_at );
65         std::string item = action_path.substr( split_at + 1 );
66
67         AccessAction( group, item );
68 }
69
70 void
71 BasicUI::loop_toggle ()
72 {
73         if (session->get_play_loop()) {
74                 session->request_play_loop (false);
75         } else {
76                 session->request_play_loop (true);
77                 if (!session->transport_rolling()) {
78                         session->request_transport_speed (1.0);
79                 }
80         }
81 }
82
83 void
84 BasicUI::loop_location (framepos_t start, framepos_t end)
85 {
86         Location* tll;
87         if ((tll = session->locations()->auto_loop_location()) == 0) {
88                 Location* loc = new Location (*session, start, end, _("Loop"),  Location::IsAutoLoop);
89                 session->locations()->add (loc, true);
90                 session->set_auto_loop_location (loc);
91         } else {
92                 tll->set_hidden (false, this);
93                 tll->set (start, end);
94         }
95 }
96
97 void
98 BasicUI::goto_start ()
99 {
100         session->goto_start ();
101 }
102
103 void
104 BasicUI::goto_zero ()
105 {
106         session->request_locate (0);
107 }
108
109 void
110 BasicUI::goto_end ()
111 {
112         session->goto_end ();
113 }
114
115 void
116 BasicUI::add_marker (const std::string& markername)
117 {
118         framepos_t where = session->audible_frame();
119         Location *location = new Location (*session, where, where, markername, Location::IsMark);
120         session->begin_reversible_command (_("add marker"));
121         XMLNode &before = session->locations()->get_state();
122         session->locations()->add (location, true);
123         XMLNode &after = session->locations()->get_state();
124         session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
125         session->commit_reversible_command ();
126 }
127
128 void
129 BasicUI::remove_marker_at_playhead ()
130 {
131         if (session) {
132                 //set up for undo
133                 XMLNode &before = session->locations()->get_state();
134                 bool removed = false;
135
136                 //find location(s) at this time
137                 Locations::LocationList locs;
138                 session->locations()->find_all_between (session->audible_frame(), session->audible_frame()+1, locs, Location::Flags(0));
139                 for (Locations::LocationList::iterator i = locs.begin(); i != locs.end(); ++i) {
140                         if ((*i)->is_mark()) {
141                                 session->locations()->remove (*i);
142                                 removed = true;
143                         }
144                 }
145
146                 //store undo
147                 if (removed) {
148                         session->begin_reversible_command (_("remove marker"));
149                         XMLNode &after = session->locations()->get_state();
150                         session->add_command(new MementoCommand<Locations>(*(session->locations()), &before, &after));
151                         session->commit_reversible_command ();
152                 }
153         }
154 }
155
156 void
157 BasicUI::rewind ()
158 {
159         session->request_transport_speed (session->transport_speed() - 1.5);
160 }
161
162 void
163 BasicUI::ffwd ()
164 {
165         session->request_transport_speed (session->transport_speed() + 1.5);
166 }
167
168 void
169 BasicUI::transport_stop ()
170 {
171         session->request_transport_speed (0.0);
172 }
173
174 void
175 BasicUI::transport_play (bool from_last_start)
176 {
177         bool rolling = session->transport_rolling ();
178
179         if (session->get_play_loop()) {
180                 session->request_play_loop (false);
181         }
182
183         if (session->get_play_range ()) {
184                 session->request_play_range (0);
185         }
186
187         if (from_last_start && rolling) {
188                 session->request_locate (session->last_transport_start(), true);
189
190         }
191
192         session->request_transport_speed (1.0f);
193 }
194
195 void
196 BasicUI::rec_enable_toggle ()
197 {
198         switch (session->record_status()) {
199         case Session::Disabled:
200                 if (session->ntracks() == 0) {
201                         // string txt = _("Please create 1 or more track\nbefore trying to record.\nCheck the Session menu.");
202                         // MessageDialog msg (*editor, txt);
203                         // msg.run ();
204                         return;
205                 }
206                 session->maybe_enable_record ();
207                 break;
208         case Session::Recording:
209         case Session::Enabled:
210                 session->disable_record (true);
211         }
212 }
213
214 void
215 BasicUI::all_tracks_rec_in ()
216 {
217         session->set_all_tracks_record_enabled (true);
218 }
219
220 void
221 BasicUI::all_tracks_rec_out ()
222 {
223         session->set_all_tracks_record_enabled (false);
224 }
225
226 void
227 BasicUI::save_state ()
228 {
229         session->save_state ("");
230 }
231
232 void
233 BasicUI::prev_marker ()
234 {
235         framepos_t pos = session->locations()->first_mark_before (session->transport_frame());
236
237         if (pos >= 0) {
238                 session->request_locate (pos, session->transport_rolling());
239         } else {
240                 session->goto_start ();
241         }
242 }
243
244 void
245 BasicUI::next_marker ()
246 {
247         framepos_t pos = session->locations()->first_mark_after (session->transport_frame());
248
249         if (pos >= 0) {
250                 session->request_locate (pos, session->transport_rolling());
251         } else {
252                 session->goto_end();
253         }
254 }
255
256 void
257 BasicUI::set_transport_speed (double speed)
258 {
259         session->request_transport_speed (speed);
260 }
261
262 double
263 BasicUI::get_transport_speed ()
264 {
265         return session->transport_speed ();
266 }
267
268 void
269 BasicUI::undo ()
270 {
271         session->undo (1);
272 }
273
274 void
275 BasicUI::redo ()
276 {
277         session->redo (1);
278 }
279
280 void
281 BasicUI::toggle_all_rec_enables ()
282 {
283         if (session->get_record_enabled()) {
284                 // session->record_disenable_all ();
285         } else {
286                 // session->record_enable_all ();
287         }
288 }
289
290 void
291 BasicUI::toggle_punch_in ()
292 {
293         session->config.set_punch_in (!session->config.get_punch_in());
294 }
295
296 void
297 BasicUI::toggle_punch_out ()
298 {
299         session->config.set_punch_out (!session->config.get_punch_out());
300 }
301
302 bool
303 BasicUI::get_record_enabled ()
304 {
305         return session->get_record_enabled();
306 }
307
308 void
309 BasicUI::set_record_enable (bool yn)
310 {
311         if (yn) {
312                 session->maybe_enable_record ();
313         } else {
314                 session->disable_record (false, true);
315         }
316 }
317
318 framepos_t
319 BasicUI::transport_frame ()
320 {
321         return session->transport_frame();
322 }
323
324 void
325 BasicUI::locate (framepos_t where, bool roll_after_locate)
326 {
327         session->request_locate (where, roll_after_locate);
328 }
329
330 void
331 BasicUI::jump_by_seconds (double secs)
332 {
333         framepos_t current = session->transport_frame();
334         double s = (double) current / (double) session->nominal_frame_rate();
335         
336         s+= secs;
337         if (s < 0) current = 0;
338         s = s * session->nominal_frame_rate();
339         
340         session->request_locate ( floor(s) );
341 }
342
343 void
344 BasicUI::jump_by_bars (double bars)
345 {
346         Timecode::BBT_Time bbt;
347         TempoMap& tmap (session->tempo_map());
348         tmap.bbt_time (session->transport_frame(), bbt);
349
350         bars += bbt.bars;
351         if (bars < 0) bars = 0;
352         
353         AnyTime any;
354         any.type = AnyTime::BBT;
355         any.bbt.bars = bars;
356         
357         session->request_locate ( session->convert_to_frames (any) );
358 }
359
360 void BasicUI::mark_in () { access_action("Editor/start-range-from-playhead"); }
361 void BasicUI::mark_out () { access_action("Editor/finish-range-from-playhead"); }
362
363 void BasicUI::toggle_click () { access_action("Transport/ToggleClick"); }
364 void BasicUI::midi_panic () { access_action("MIDI/panic"); }
365 void BasicUI::toggle_roll () { access_action("Transport/ToggleRoll"); }
366 void BasicUI::stop_forget () { access_action("Transport/ToggleRollForgetCapture"); }
367
368 void BasicUI::set_punch_range () { access_action("Editor/set-punch-from-edit-range"); }
369 void BasicUI::set_loop_range () { access_action("Editor/set-loop-from-edit-range"); }
370 void BasicUI::set_session_range () { access_action("Editor/set-session-from-edit-range"); }
371
372 void BasicUI::toggle_monitor_mute () { /*access_action("Editor/toggle_monitor_mute");  */ }
373 void BasicUI::toggle_monitor_dim () {  /*access_action("Editor/toggle_monitor_dim");  */ }
374 void BasicUI::toggle_monitor_mono () { /*access_action("Editor/toggle_monitor_mono");  */ }
375
376 void BasicUI::quick_snapshot_stay () { access_action("Main/QuickSnapshotStay"); }
377 void BasicUI::quick_snapshot_switch () { access_action("Main/QuickSnapshotSwitch"); }
378
379 void BasicUI::fit_1_track() { access_action("Editor/fit_1_track"); }
380 void BasicUI::fit_2_tracks() { access_action("Editor/fit_2_tracks"); }
381 void BasicUI::fit_4_tracks() { access_action("Editor/fit_4_tracks"); }
382 void BasicUI::fit_8_tracks() { access_action("Editor/fit_8_tracks"); }
383 void BasicUI::fit_16_tracks() { access_action("Editor/fit_16_tracks"); }
384 void BasicUI::fit_32_tracks() { access_action("Editor/fit_32_tracks"); }
385 void BasicUI::fit_all_tracks() { access_action("Editor/fit_all_tracks"); }
386
387 void BasicUI::zoom_10_ms() { access_action("Editor/zoom_10_ms"); }
388 void BasicUI::zoom_100_ms() { access_action("Editor/zoom_100_ms"); }
389 void BasicUI::zoom_1_sec() { access_action("Editor/zoom_1_sec"); }
390 void BasicUI::zoom_10_sec() { access_action("Editor/zoom_10_sec"); }
391 void BasicUI::zoom_1_min() { access_action("Editor/zoom_1_min"); }
392 void BasicUI::zoom_5_min() { access_action("Editor/zoom_5_min"); }
393 void BasicUI::zoom_10_min() { access_action("Editor/zoom_10_min"); }
394 void BasicUI::zoom_to_session() { access_action("Editor/zoom-to-session"); }
395 void BasicUI::temporal_zoom_in() { access_action("Editor/temporal-zoom-in"); }
396 void BasicUI::temporal_zoom_out() { access_action("Editor/temporal-zoom-out"); }
397
398 void BasicUI::scroll_up_1_track() { access_action("Editor/step-tracks-up"); }
399 void BasicUI::scroll_dn_1_track() { access_action("Editor/step-tracks-down"); }
400 void BasicUI::scroll_up_1_page() { access_action("Editor/scroll-tracks-up"); }
401 void BasicUI::scroll_dn_1_page() { access_action("Editor/scroll-tracks-down"); }
402
403
404 bool
405 BasicUI::locating ()
406 {
407         return session->locate_pending();
408 }
409
410 bool
411 BasicUI::locked ()
412 {
413         return session->transport_locked ();
414 }
415
416 ARDOUR::framecnt_t
417 BasicUI::timecode_frames_per_hour ()
418 {
419         return session->timecode_frames_per_hour ();
420 }
421
422 void
423 BasicUI::timecode_time (framepos_t where, Timecode::Time& timecode)
424 {
425         session->timecode_time (where, *((Timecode::Time *) &timecode));
426 }
427
428 void
429 BasicUI::timecode_to_sample (Timecode::Time& timecode, framepos_t & sample, bool use_offset, bool use_subframes) const
430 {
431         session->timecode_to_sample (*((Timecode::Time*)&timecode), sample, use_offset, use_subframes);
432 }
433
434 void
435 BasicUI::sample_to_timecode (framepos_t sample, Timecode::Time& timecode, bool use_offset, bool use_subframes) const
436 {
437         session->sample_to_timecode (sample, *((Timecode::Time*)&timecode), use_offset, use_subframes);
438 }
439
440 #if 0
441 this stuff is waiting to go in so that all UIs can offer complex solo/mute functionality
442
443 void
444 BasicUI::solo_release (boost::shared_ptr<Route> r)
445 {
446 }
447
448 void
449 BasicUI::solo_press (boost::shared_ptr<Route> r, bool momentary, bool global, bool exclusive, bool isolate, bool solo_group)
450 {
451         if (momentary) {
452                 _solo_release = new SoloMuteRelease (_route->soloed());
453         }
454
455         if (global) {
456
457                 if (_solo_release) {
458                         _solo_release->routes = _session->get_routes ();
459                 }
460
461                 if (Config->get_solo_control_is_listen_control()) {
462                         _session->set_listen (_session->get_routes(), !_route->listening(),  Session::rt_cleanup, true);
463                 } else {
464                         _session->set_solo (_session->get_routes(), !_route->soloed(),  Session::rt_cleanup, true);
465                 }
466
467         } else if (exclusive) {
468
469                 if (_solo_release) {
470                         _solo_release->exclusive = true;
471
472                         boost::shared_ptr<RouteList> routes = _session->get_routes();
473
474                         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
475                                 if ((*i)->soloed ()) {
476                                         _solo_release->routes_on->push_back (*i);
477                                 } else {
478                                         _solo_release->routes_off->push_back (*i);
479                                 }
480                         }
481                 }
482
483                 if (Config->get_solo_control_is_listen_control()) {
484                         /* ??? we need a just_one_listen() method */
485                 } else {
486                         _session->set_just_one_solo (_route, true);
487                 }
488
489         } else if (isolate) {
490
491                 // shift-click: toggle solo isolated status
492
493                 _route->set_solo_isolated (!_route->solo_isolated(), this);
494                 delete _solo_release;
495                 _solo_release = 0;
496
497         } else if (solo_group) {
498
499                 /* Primary-button1: solo mix group.
500                    NOTE: Primary-button2 is MIDI learn.
501                 */
502
503                 if (_route->route_group()) {
504
505                         if (_solo_release) {
506                                 _solo_release->routes = _route->route_group()->route_list();
507                         }
508
509                         if (Config->get_solo_control_is_listen_control()) {
510                                 _session->set_listen (_route->route_group()->route_list(), !_route->listening(),  Session::rt_cleanup, true);
511                         } else {
512                                 _session->set_solo (_route->route_group()->route_list(), !_route->soloed(),  Session::rt_cleanup, true);
513                         }
514                 }
515
516         } else {
517
518                 /* click: solo this route */
519
520                 boost::shared_ptr<RouteList> rl (new RouteList);
521                 rl->push_back (route());
522
523                 if (_solo_release) {
524                         _solo_release->routes = rl;
525                 }
526
527                 if (Config->get_solo_control_is_listen_control()) {
528                         _session->set_listen (rl, !_route->listening());
529                 } else {
530                         _session->set_solo (rl, !_route->soloed());
531                 }
532         }
533 }
534 #endif