2 Copyright (C) 2009-2013 Paul Davis
3 Authors: Sampo Savolainen, Jannis Pohlmann
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
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.
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.
23 #include "pbd/compose.h"
24 #include "pbd/error.h"
25 #include "ardour/debug.h"
26 #include "ardour/session.h"
31 using namespace ARDOUR;
35 #include "pbd/abstract_ui.cc" // instantiate template
37 void wiimote_control_protocol_mesg_callback (cwiid_wiimote_t *wiimote, int mesg_count, union cwiid_mesg mesg[], timespec *t);
39 WiimoteControlProtocol::WiimoteControlProtocol (Session& session)
40 : ControlProtocol (session, X_("Wiimote"))
41 , AbstractUI<WiimoteControlUIRequest> ("wiimote")
45 , callback_thread_registered (false)
49 WiimoteControlProtocol::~WiimoteControlProtocol ()
55 WiimoteControlProtocol::probe ()
61 WiimoteControlProtocol::set_active (bool yn)
65 DEBUG_TRACE (DEBUG::WiimoteControl, string_compose ("WiimoteControlProtocol::set_active init with yn: '%1'\n", yn));
67 /* do nothing if the active state is not changing */
74 /* activate Wiimote control surface */
77 /* deactivate Wiimote control surface */
81 ControlProtocol::set_active (yn);
83 DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::set_active done\n");
89 WiimoteControlProtocol::get_state ()
91 XMLNode& node (ControlProtocol::get_state());
92 node.add_property (X_("feedback"), "0");
97 WiimoteControlProtocol::set_state (const XMLNode&, int)
103 WiimoteControlProtocol::do_request (WiimoteControlUIRequest* req)
105 DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::do_request init\n");
107 if (req->type == CallSlot) {
108 call_slot (MISSING_INVALIDATOR, req->the_slot);
109 } else if (req->type == Quit) {
113 DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::do_request done\n");
117 WiimoteControlProtocol::start ()
119 DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::start init\n");
121 // update LEDs whenever the transport or recording state changes
122 session->TransportStateChange.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&WiimoteControlProtocol::update_led_state, this), this);
123 session->RecordStateChanged.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&WiimoteControlProtocol::update_led_state, this), this);
125 // start the Wiimote control UI; it will run in its own thread context
128 DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::start done\n");
134 WiimoteControlProtocol::stop ()
136 DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::stop init\n");
138 // stop wiimote discovery, just in case
139 stop_wiimote_discovery ();
141 // close and reset the wiimote handle
143 cwiid_close (wiimote);
145 callback_thread_registered = false;
148 // stop the Wiimote control UI
151 // no longer update the LEDs
152 session_connections.drop_connections ();
154 DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::stop done\n");
160 WiimoteControlProtocol::thread_init ()
162 DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::thread_init init\n");
164 pthread_set_name (X_("wiimote"));
166 // allow to make requests to the GUI and RT thread(s)
167 PBD::notify_gui_about_thread_creation (X_("gui"), pthread_self (), X_("wiimote"), 2048);
168 BasicUI::register_thread ("wiimote");
171 start_wiimote_discovery ();
173 DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::thread_init done\n");
177 WiimoteControlProtocol::start_wiimote_discovery ()
179 DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::start_wiimote_discovery init\n");
181 // connect to the Wiimote using an idle source
182 Glib::RefPtr<Glib::IdleSource> source = Glib::IdleSource::create ();
183 source->connect (sigc::mem_fun (*this, &WiimoteControlProtocol::connect_idle));
184 source->attach (_main_loop->get_context ());
186 // grab a reference on the underlying idle source to keep it around
187 idle_source = source->gobj ();
188 g_source_ref (idle_source);
190 DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::start_wiimote_discovery done\n");
194 WiimoteControlProtocol::stop_wiimote_discovery ()
196 DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::stop_wiimote_discovery init\n");
199 g_source_unref (idle_source);
203 DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::stop_wiimote_discovery done\n");
207 WiimoteControlProtocol::connect_idle ()
209 DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::connect_idle init\n");
213 if (connect_wiimote ()) {
214 stop_wiimote_discovery ();
218 DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::connect_idle done\n");
224 WiimoteControlProtocol::connect_wiimote ()
226 // abort the discovery and do nothing else if we already have a Wiimote
233 // if we don't have a Wiimote yet, try to discover it; if that
234 // fails, wait for a short period of time and try again
236 cerr << "Wiimote: Not discovered yet, press 1+2 to connect" << endl;
238 bdaddr_t bdaddr = {{ 0, 0, 0, 0, 0, 0 }};
239 wiimote = cwiid_open (&bdaddr, 0);
240 callback_thread_registered = false;
244 // a Wiimote was discovered
245 cerr << "Wiimote: Connected successfully" << endl;
247 // attach the WiimoteControlProtocol object to the Wiimote handle
248 if (cwiid_set_data (wiimote, this)) {
249 cerr << "Wiimote: Failed to attach control protocol" << endl;
253 // clear the last button state to start processing events cleanly
258 // enable message based communication with the Wiimote
259 if (success && cwiid_enable (wiimote, CWIID_FLAG_MESG_IFC)) {
260 cerr << "Wiimote: Failed to enable message based communication" << endl;
264 // enable button events to be received from the Wiimote
265 if (success && cwiid_command (wiimote, CWIID_CMD_RPT_MODE, CWIID_RPT_BTN)) {
266 cerr << "Wiimote: Failed to enable button events" << endl;
270 // receive an event for every single button pressed, not just when
271 // a different button was pressed than before
272 if (success && cwiid_enable (wiimote, CWIID_FLAG_REPEAT_BTN)) {
273 cerr << "Wiimote: Failed to enable repeated button events" << endl;
277 // be notified of new input events
278 if (success && cwiid_set_mesg_callback (wiimote, wiimote_control_protocol_mesg_callback)) {
281 // reset Wiimote handle if the configuration failed
282 if (!success && wiimote) {
283 cwiid_close (wiimote);
285 callback_thread_registered = false;
292 WiimoteControlProtocol::update_led_state ()
294 DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::update_led_state init\n");
298 // do nothing if we do not have a Wiimote
300 DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::update_led_state no wiimote connected\n");
304 // enable LED1 if Ardour is playing
305 if (session->transport_rolling ()) {
306 DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::update_led_state playing, activate LED1\n");
307 state |= CWIID_LED1_ON;
310 // enable LED4 if Ardour is recording
311 if (session->actively_recording ()) {
312 DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::update_led_state recording, activate LED4\n");
313 state |= CWIID_LED4_ON;
316 // apply the LED state
317 cwiid_set_led (wiimote, state);
319 DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::update_led_state done\n");
323 WiimoteControlProtocol::wiimote_callback (int mesg_count, union cwiid_mesg mesg[])
325 // register the cwiid callback thread if that hasn't happened yet
326 if (!callback_thread_registered) {
327 BasicUI::register_thread ("wiimote callback");
328 callback_thread_registered = true;
331 for (int i = 0; i < mesg_count; i++) {
332 // restart Wiimote discovery when receiving errors
333 if (mesg[i].type == CWIID_MESG_ERROR) {
334 cerr << "Wiimote: disconnected" << endl;
335 cwiid_close (wiimote);
337 callback_thread_registered = false;
338 start_wiimote_discovery ();
342 // skip non-button events
343 if (mesg[i].type != CWIID_MESG_BTN) {
347 // drop buttons from the event that were already pressed before
348 uint16_t b = mesg[i].btn_mesg.buttons & ~button_state;
350 // remember new button state
351 button_state = mesg[i].btn_mesg.buttons;
353 if (button_state & CWIID_BTN_B) {
354 // B + A = abort recording and jump back
355 if (b & CWIID_BTN_A) {
356 access_action ("Transport/ToggleRollForgetCapture");
359 // B + left = move playhead to previous region boundary
360 if (b & CWIID_BTN_LEFT) {
361 access_action ("Editor/playhead-to-previous-region-boundary");
364 // B + right = move playhead to next region boundary
365 if (b & CWIID_BTN_RIGHT) {
366 access_action ("Editor/playhead-to-next-region-boundary");
369 // B + up = move playhead to next marker
370 if (b & CWIID_BTN_UP) {
374 // B + down = move playhead to prev marker
375 if (b & CWIID_BTN_DOWN) {
379 // B + Home = add marker at playhead
380 if (b & CWIID_BTN_HOME) {
381 access_action ("Editor/add-location-from-playhead");
384 // B + minus = move playhead to the start
385 if (b & CWIID_BTN_MINUS) {
386 access_action ("Transport/GotoStart");
389 // B + plus = move playhead to the end
390 if (b & CWIID_BTN_PLUS) {
391 access_action ("Transport/GotoEnd");
394 // A = toggle playback
395 if (b & CWIID_BTN_A) {
396 access_action ("Transport/ToggleRoll");
399 // 1 = toggle recording on the current track
400 if (b & CWIID_BTN_1) {
401 access_action ("Editor/track-record-enable-toggle");
404 // 2 = enable recording in general
405 if (b & CWIID_BTN_2) {
406 rec_enable_toggle ();
409 // left = move playhead back a bit
410 if (b & CWIID_BTN_LEFT) {
411 access_action ("Editor/nudge-playhead-backward");
414 // right = move playhead forward a bit
415 if (b & CWIID_BTN_RIGHT) {
416 access_action ("Editor/nudge-playhead-forward");
419 // up = select previous track
420 if (b & CWIID_BTN_UP) {
421 access_action ("Editor/select-prev-route");
424 // down = select next track
425 if (b & CWIID_BTN_DOWN) {
426 access_action ("Editor/select-next-route");
430 if (b & CWIID_BTN_PLUS) {
431 access_action ("Editor/temporal-zoom-in");
435 if (b & CWIID_BTN_MINUS) {
436 access_action ("Editor/temporal-zoom-out");
440 if (b & CWIID_BTN_HOME) {
441 access_action ("Editor/playhead-to-edit");
448 wiimote_control_protocol_mesg_callback (cwiid_wiimote_t *wiimote, int mesg_count, union cwiid_mesg mesg[], timespec *)
450 DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::mesg_callback init\n");
452 WiimoteControlProtocol *protocol = (WiimoteControlProtocol *)cwiid_get_data (wiimote);
455 protocol->wiimote_callback (mesg_count, mesg);
458 DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::mesg_callback done\n");