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