add some horz and vert zoom actions; remove some whitespace from the code of the...
[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::save_state ()
216 {
217         session->save_state ("");
218 }
219
220 void
221 BasicUI::prev_marker ()
222 {
223         framepos_t pos = session->locations()->first_mark_before (session->transport_frame());
224
225         if (pos >= 0) {
226                 session->request_locate (pos, session->transport_rolling());
227         } else {
228                 session->goto_start ();
229         }
230 }
231
232 void
233 BasicUI::next_marker ()
234 {
235         framepos_t pos = session->locations()->first_mark_after (session->transport_frame());
236
237         if (pos >= 0) {
238                 session->request_locate (pos, session->transport_rolling());
239         } else {
240                 session->goto_end();
241         }
242 }
243
244 void
245 BasicUI::set_transport_speed (double speed)
246 {
247         session->request_transport_speed (speed);
248 }
249
250 double
251 BasicUI::get_transport_speed ()
252 {
253         return session->transport_speed ();
254 }
255
256 void
257 BasicUI::undo ()
258 {
259         session->undo (1);
260 }
261
262 void
263 BasicUI::redo ()
264 {
265         session->redo (1);
266 }
267
268 void
269 BasicUI::toggle_all_rec_enables ()
270 {
271         if (session->get_record_enabled()) {
272                 // session->record_disenable_all ();
273         } else {
274                 // session->record_enable_all ();
275         }
276 }
277
278 void
279 BasicUI::toggle_punch_in ()
280 {
281         session->config.set_punch_in (!session->config.get_punch_in());
282 }
283
284 void
285 BasicUI::toggle_punch_out ()
286 {
287         session->config.set_punch_out (!session->config.get_punch_out());
288 }
289
290 bool
291 BasicUI::get_record_enabled ()
292 {
293         return session->get_record_enabled();
294 }
295
296 void
297 BasicUI::set_record_enable (bool yn)
298 {
299         if (yn) {
300                 session->maybe_enable_record ();
301         } else {
302                 session->disable_record (false, true);
303         }
304 }
305
306 framepos_t
307 BasicUI::transport_frame ()
308 {
309         return session->transport_frame();
310 }
311
312 void
313 BasicUI::locate (framepos_t where, bool roll_after_locate)
314 {
315         session->request_locate (where, roll_after_locate);
316 }
317
318 void
319 BasicUI::jump_by_seconds (double secs)
320 {
321         framepos_t current = session->transport_frame();
322         double s = (double) current / (double) session->nominal_frame_rate();
323         
324         s+= secs;
325         if (s < 0) current = 0;
326         s = s * session->nominal_frame_rate();
327         
328         session->request_locate ( floor(s) );
329 }
330
331 void
332 BasicUI::jump_by_bars (double bars)
333 {
334         Timecode::BBT_Time bbt;
335         TempoMap& tmap (session->tempo_map());
336         tmap.bbt_time (session->transport_frame(), bbt);
337
338         bars += bbt.bars;
339         if (bars < 0) bars = 0;
340         
341         AnyTime any;
342         any.type = AnyTime::BBT;
343         any.bbt.bars = bars;
344         
345         session->request_locate ( session->convert_to_frames (any) );
346 }
347
348 void BasicUI::mark_in () { access_action("Editor/start-range"); }
349 void BasicUI::mark_out () { access_action("Editor/finish-range"); }
350
351 void BasicUI::toggle_click () { access_action("Transport/ToggleClick"); }
352 void BasicUI::midi_panic () { access_action("MIDI/panic"); }
353 void BasicUI::toggle_roll () { access_action("Transport/ToggleRoll"); }
354 void BasicUI::stop_forget () { access_action("Transport/ToggleRollForgetCapture"); }
355
356 void BasicUI::set_punch_range () { access_action("Editor/set-punch-from-edit-range"); }
357 void BasicUI::set_loop_range () { access_action("Editor/set-loop-from-edit-range"); }
358 void BasicUI::set_session_range () { access_action("Editor/set-session-from-edit-range"); }
359
360 void BasicUI::toggle_monitor_mute () { /*access_action("Editor/toggle_monitor_mute");  */ }
361 void BasicUI::toggle_monitor_dim () {  /*access_action("Editor/toggle_monitor_dim");  */ }
362 void BasicUI::toggle_monitor_mono () { /*access_action("Editor/toggle_monitor_mono");  */ }
363
364 void BasicUI::quick_snapshot_stay () { access_action("Main/QuickSnapshotStay"); }
365 void BasicUI::quick_snapshot_switch () { access_action("Main/QuickSnapshotSwitch"); }
366
367 void BasicUI::fit_1_track() { access_action("Editor/fit_1_track"); }
368 void BasicUI::fit_2_tracks() { access_action("Editor/fit_2_tracks"); }
369 void BasicUI::fit_4_tracks() { access_action("Editor/fit_4_tracks"); }
370 void BasicUI::fit_8_tracks() { access_action("Editor/fit_8_tracks"); }
371 void BasicUI::fit_16_tracks() { access_action("Editor/fit_16_tracks"); }
372 void BasicUI::fit_32_tracks() { access_action("Editor/fit_32_tracks"); }
373 void BasicUI::fit_all_tracks() { access_action("Editor/fit_all_tracks"); }
374
375 void BasicUI::zoom_10_ms() { access_action("Editor/zoom_10_ms"); }
376 void BasicUI::zoom_100_ms() { access_action("Editor/zoom_100_ms"); }
377 void BasicUI::zoom_1_sec() { access_action("Editor/zoom_1_sec"); }
378 void BasicUI::zoom_10_sec() { access_action("Editor/zoom_10_sec"); }
379 void BasicUI::zoom_1_min() { access_action("Editor/zoom_1_min"); }
380 void BasicUI::zoom_5_min() { access_action("Editor/zoom_5_min"); }
381 void BasicUI::zoom_10_min() { access_action("Editor/zoom_10_min"); }
382 void BasicUI::zoom_to_session() { access_action("Editor/zooom-to-session"); }
383
384 void BasicUI::scroll_up_1_track() { access_action("Editor/scroll_up_1_track"); }
385 void BasicUI::scroll_dn_1_track() { access_action("Editor/scroll_dn_1_track"); }
386
387
388 bool
389 BasicUI::locating ()
390 {
391         return session->locate_pending();
392 }
393
394 bool
395 BasicUI::locked ()
396 {
397         return session->transport_locked ();
398 }
399
400 ARDOUR::framecnt_t
401 BasicUI::timecode_frames_per_hour ()
402 {
403         return session->timecode_frames_per_hour ();
404 }
405
406 void
407 BasicUI::timecode_time (framepos_t where, Timecode::Time& timecode)
408 {
409         session->timecode_time (where, *((Timecode::Time *) &timecode));
410 }
411
412 void
413 BasicUI::timecode_to_sample (Timecode::Time& timecode, framepos_t & sample, bool use_offset, bool use_subframes) const
414 {
415         session->timecode_to_sample (*((Timecode::Time*)&timecode), sample, use_offset, use_subframes);
416 }
417
418 void
419 BasicUI::sample_to_timecode (framepos_t sample, Timecode::Time& timecode, bool use_offset, bool use_subframes) const
420 {
421         session->sample_to_timecode (sample, *((Timecode::Time*)&timecode), use_offset, use_subframes);
422 }
423
424 #if 0
425 this stuff is waiting to go in so that all UIs can offer complex solo/mute functionality
426
427 void
428 BasicUI::solo_release (boost::shared_ptr<Route> r)
429 {
430 }
431
432 void
433 BasicUI::solo_press (boost::shared_ptr<Route> r, bool momentary, bool global, bool exclusive, bool isolate, bool solo_group)
434 {
435         if (momentary) {
436                 _solo_release = new SoloMuteRelease (_route->soloed());
437         }
438
439         if (global) {
440
441                 if (_solo_release) {
442                         _solo_release->routes = _session->get_routes ();
443                 }
444
445                 if (Config->get_solo_control_is_listen_control()) {
446                         _session->set_listen (_session->get_routes(), !_route->listening(),  Session::rt_cleanup, true);
447                 } else {
448                         _session->set_solo (_session->get_routes(), !_route->soloed(),  Session::rt_cleanup, true);
449                 }
450
451         } else if (exclusive) {
452
453                 if (_solo_release) {
454                         _solo_release->exclusive = true;
455
456                         boost::shared_ptr<RouteList> routes = _session->get_routes();
457
458                         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
459                                 if ((*i)->soloed ()) {
460                                         _solo_release->routes_on->push_back (*i);
461                                 } else {
462                                         _solo_release->routes_off->push_back (*i);
463                                 }
464                         }
465                 }
466
467                 if (Config->get_solo_control_is_listen_control()) {
468                         /* ??? we need a just_one_listen() method */
469                 } else {
470                         _session->set_just_one_solo (_route, true);
471                 }
472
473         } else if (isolate) {
474
475                 // shift-click: toggle solo isolated status
476
477                 _route->set_solo_isolated (!_route->solo_isolated(), this);
478                 delete _solo_release;
479                 _solo_release = 0;
480
481         } else if (solo_group) {
482
483                 /* Primary-button1: solo mix group.
484                    NOTE: Primary-button2 is MIDI learn.
485                 */
486
487                 if (_route->route_group()) {
488
489                         if (_solo_release) {
490                                 _solo_release->routes = _route->route_group()->route_list();
491                         }
492
493                         if (Config->get_solo_control_is_listen_control()) {
494                                 _session->set_listen (_route->route_group()->route_list(), !_route->listening(),  Session::rt_cleanup, true);
495                         } else {
496                                 _session->set_solo (_route->route_group()->route_list(), !_route->soloed(),  Session::rt_cleanup, true);
497                         }
498                 }
499
500         } else {
501
502                 /* click: solo this route */
503
504                 boost::shared_ptr<RouteList> rl (new RouteList);
505                 rl->push_back (route());
506
507                 if (_solo_release) {
508                         _solo_release->routes = rl;
509                 }
510
511                 if (Config->get_solo_control_is_listen_control()) {
512                         _session->set_listen (rl, !_route->listening());
513                 } else {
514                         _session->set_solo (rl, !_route->soloed());
515                 }
516         }
517 }
518 #endif