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