add goto_zero() function for BasicUI
[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::loop_location (framepos_t start, framepos_t end)
84 {
85         Location* tll;
86         if ((tll = session->locations()->auto_loop_location()) == 0) {
87                 Location* loc = new Location (*session, start, end, _("Loop"),  Location::IsAutoLoop);
88                 session->locations()->add (loc, true);
89                 session->set_auto_loop_location (loc);
90         } else {
91                 tll->set_hidden (false, this);
92                 tll->set (start, end);
93         }
94 }
95
96 void
97 BasicUI::goto_start ()
98 {
99         session->goto_start ();
100 }
101
102 void
103 BasicUI::goto_zero ()
104 {
105         session->request_locate (0);
106 }
107
108 void
109 BasicUI::goto_end ()
110 {
111         session->goto_end ();
112 }
113
114 void
115 BasicUI::add_marker (const std::string& markername)
116 {
117         framepos_t where = session->audible_frame();
118         Location *location = new Location (*session, where, where, markername, Location::IsMark);
119         session->begin_reversible_command (_("add marker"));
120         XMLNode &before = session->locations()->get_state();
121         session->locations()->add (location, true);
122         XMLNode &after = session->locations()->get_state();
123         session->add_command (new MementoCommand<Locations>(*(session->locations()), &before, &after));
124         session->commit_reversible_command ();
125 }
126
127 void
128 BasicUI::rewind ()
129 {
130         session->request_transport_speed (session->transport_speed() - 1.5);
131 }
132
133 void
134 BasicUI::ffwd ()
135 {
136         session->request_transport_speed (session->transport_speed() + 1.5);
137 }
138
139 void
140 BasicUI::transport_stop ()
141 {
142         session->request_transport_speed (0.0);
143 }
144
145 void
146 BasicUI::transport_play (bool from_last_start)
147 {
148         bool rolling = session->transport_rolling ();
149
150         if (session->get_play_loop()) {
151                 session->request_play_loop (false);
152         }
153
154         if (session->get_play_range ()) {
155                 session->request_play_range (0);
156         }
157
158         if (from_last_start && rolling) {
159                 session->request_locate (session->last_transport_start(), true);
160
161         }
162
163         session->request_transport_speed (1.0f);
164 }
165
166 void
167 BasicUI::rec_enable_toggle ()
168 {
169         switch (session->record_status()) {
170         case Session::Disabled:
171                 if (session->ntracks() == 0) {
172                         // string txt = _("Please create 1 or more track\nbefore trying to record.\nCheck the Session menu.");
173                         // MessageDialog msg (*editor, txt);
174                         // msg.run ();
175                         return;
176                 }
177                 session->maybe_enable_record ();
178                 break;
179         case Session::Recording:
180         case Session::Enabled:
181                 session->disable_record (true);
182         }
183 }
184
185 void
186 BasicUI::save_state ()
187 {
188         session->save_state ("");
189 }
190
191 void
192 BasicUI::prev_marker ()
193 {
194         framepos_t pos = session->locations()->first_mark_before (session->transport_frame());
195
196         if (pos >= 0) {
197                 session->request_locate (pos, session->transport_rolling());
198         } else {
199                 session->goto_start ();
200         }
201 }
202
203 void
204 BasicUI::next_marker ()
205 {
206         framepos_t pos = session->locations()->first_mark_after (session->transport_frame());
207
208         if (pos >= 0) {
209                 session->request_locate (pos, session->transport_rolling());
210         } else {
211                 session->goto_end();
212         }
213 }
214
215 void
216 BasicUI::set_transport_speed (double speed)
217 {
218         session->request_transport_speed (speed);
219 }
220
221 double
222 BasicUI::get_transport_speed ()
223 {
224         return session->transport_speed ();
225 }
226
227 void
228 BasicUI::undo ()
229 {
230         session->undo (1);
231 }
232
233 void
234 BasicUI::redo ()
235 {
236         session->redo (1);
237 }
238
239 void
240 BasicUI::toggle_all_rec_enables ()
241 {
242         if (session->get_record_enabled()) {
243                 // session->record_disenable_all ();
244         } else {
245                 // session->record_enable_all ();
246         }
247 }
248
249 void
250 BasicUI::toggle_punch_in ()
251 {
252         session->config.set_punch_in (!session->config.get_punch_in());
253 }
254
255 void
256 BasicUI::toggle_punch_out ()
257 {
258         session->config.set_punch_out (!session->config.get_punch_out());
259 }
260
261 bool
262 BasicUI::get_record_enabled ()
263 {
264         return session->get_record_enabled();
265 }
266
267 void
268 BasicUI::set_record_enable (bool yn)
269 {
270         if (yn) {
271                 session->maybe_enable_record ();
272         } else {
273                 session->disable_record (false, true);
274         }
275 }
276
277 framepos_t
278 BasicUI::transport_frame ()
279 {
280         return session->transport_frame();
281 }
282
283 void
284 BasicUI::locate (framepos_t where, bool roll_after_locate)
285 {
286         session->request_locate (where, roll_after_locate);
287 }
288
289 bool
290 BasicUI::locating ()
291 {
292         return session->locate_pending();
293 }
294
295 bool
296 BasicUI::locked ()
297 {
298         return session->transport_locked ();
299 }
300
301 ARDOUR::framecnt_t
302 BasicUI::timecode_frames_per_hour ()
303 {
304         return session->timecode_frames_per_hour ();
305 }
306
307 void
308 BasicUI::timecode_time (framepos_t where, Timecode::Time& timecode)
309 {
310         session->timecode_time (where, *((Timecode::Time *) &timecode));
311 }
312
313 void
314 BasicUI::timecode_to_sample (Timecode::Time& timecode, framepos_t & sample, bool use_offset, bool use_subframes) const
315 {
316         session->timecode_to_sample (*((Timecode::Time*)&timecode), sample, use_offset, use_subframes);
317 }
318
319 void
320 BasicUI::sample_to_timecode (framepos_t sample, Timecode::Time& timecode, bool use_offset, bool use_subframes) const
321 {
322         session->sample_to_timecode (sample, *((Timecode::Time*)&timecode), use_offset, use_subframes);
323 }
324
325 #if 0
326 this stuff is waiting to go in so that all UIs can offer complex solo/mute functionality
327
328 void
329 BasicUI::solo_release (boost::shared_ptr<Route> r)
330 {
331 }
332
333 void
334 BasicUI::solo_press (boost::shared_ptr<Route> r, bool momentary, bool global, bool exclusive, bool isolate, bool solo_group)
335 {
336         if (momentary) {
337                 _solo_release = new SoloMuteRelease (_route->soloed());
338         }
339
340         if (global) {
341
342                 if (_solo_release) {
343                         _solo_release->routes = _session->get_routes ();
344                 }
345
346                 if (Config->get_solo_control_is_listen_control()) {
347                         _session->set_listen (_session->get_routes(), !_route->listening(),  Session::rt_cleanup, true);
348                 } else {
349                         _session->set_solo (_session->get_routes(), !_route->soloed(),  Session::rt_cleanup, true);
350                 }
351
352         } else if (exclusive) {
353
354                 if (_solo_release) {
355                         _solo_release->exclusive = true;
356
357                         boost::shared_ptr<RouteList> routes = _session->get_routes();
358
359                         for (RouteList::iterator i = routes->begin(); i != routes->end(); ++i) {
360                                 if ((*i)->soloed ()) {
361                                         _solo_release->routes_on->push_back (*i);
362                                 } else {
363                                         _solo_release->routes_off->push_back (*i);
364                                 }
365                         }
366                 }
367
368                 if (Config->get_solo_control_is_listen_control()) {
369                         /* ??? we need a just_one_listen() method */
370                 } else {
371                         _session->set_just_one_solo (_route, true);
372                 }
373
374         } else if (isolate) {
375
376                 // shift-click: toggle solo isolated status
377
378                 _route->set_solo_isolated (!_route->solo_isolated(), this);
379                 delete _solo_release;
380                 _solo_release = 0;
381
382         } else if (solo_group) {
383
384                 /* Primary-button1: solo mix group.
385                    NOTE: Primary-button2 is MIDI learn.
386                 */
387
388                 if (_route->route_group()) {
389
390                         if (_solo_release) {
391                                 _solo_release->routes = _route->route_group()->route_list();
392                         }
393
394                         if (Config->get_solo_control_is_listen_control()) {
395                                 _session->set_listen (_route->route_group()->route_list(), !_route->listening(),  Session::rt_cleanup, true);
396                         } else {
397                                 _session->set_solo (_route->route_group()->route_list(), !_route->soloed(),  Session::rt_cleanup, true);
398                         }
399                 }
400
401         } else {
402
403                 /* click: solo this route */
404
405                 boost::shared_ptr<RouteList> rl (new RouteList);
406                 rl->push_back (route());
407
408                 if (_solo_release) {
409                         _solo_release->routes = rl;
410                 }
411
412                 if (Config->get_solo_control_is_listen_control()) {
413                         _session->set_listen (rl, !_route->listening());
414                 } else {
415                         _session->set_solo (rl, !_route->soloed());
416                 }
417         }
418 }
419 #endif