From bd95b5060ec8963e6bb59a22e51434472a83cc52 Mon Sep 17 00:00:00 2001
From: Jannis Pohlmann <jannis@xfce.org>
Date: Tue, 11 Sep 2012 17:33:37 +0100
Subject: [PATCH 4/6] wiimote: update LEDs on transport/recording state
 changes again

---
 libs/surfaces/wiimote/wiimote.cc | 39 ++++++++++++++++++++++++++++++++++++++-
 libs/surfaces/wiimote/wiimote.h  |  3 +++
 2 files changed, 41 insertions(+), 1 deletion(-)

diff --git a/libs/surfaces/wiimote/wiimote.cc b/libs/surfaces/wiimote/wiimote.cc
index 445ac50..97c7d03 100644
--- a/libs/surfaces/wiimote/wiimote.cc
+++ b/libs/surfaces/wiimote/wiimote.cc
@@ -29,7 +29,7 @@ WiimoteControlProtocol::~WiimoteControlProtocol ()
 	stop ();
 }
 
-bool 
+bool
 WiimoteControlProtocol::probe ()
 {
 	return true;
@@ -97,6 +97,10 @@ WiimoteControlProtocol::start ()
 {
 	DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::start init\n");
 
+	// update LEDs whenever the transport or recording state changes
+	session->TransportStateChange.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&WiimoteControlProtocol::update_led_state, this), this);
+	session->RecordStateChanged.connect (session_connections, MISSING_INVALIDATOR, boost::bind (&WiimoteControlProtocol::update_led_state, this), this);
+
 	// start the Wiimote control UI; it will run in its own thread context
 	BaseUI::run ();
 
@@ -118,6 +122,9 @@ WiimoteControlProtocol::stop ()
 	// stop the Wiimote control UI
 	BaseUI::quit ();
 
+	// no longer update the LEDs
+	session_connections.drop_connections ();
+
 	DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::stop done\n");
 
 	return 0;
@@ -344,3 +351,33 @@ WiimoteControlProtocol::connect_wiimote ()
 	return success;
 }
 
+void
+WiimoteControlProtocol::update_led_state ()
+{
+	DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::update_led_state init\n");
+
+	uint8_t state = 0;
+
+	// do nothing if we do not have a Wiimote
+	if (!wiimote) {
+		DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::update_led_state no wiimote connected\n");
+		return;
+	}
+
+	// enable LED1 if Ardour is playing
+	if (session->transport_rolling ()) {
+		DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::update_led_state playing, activate LED1\n");
+		state |= CWIID_LED1_ON;
+	}
+
+	// enable LED4 if Ardour is recording
+	if (session->actively_recording ()) {
+		DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::update_led_state recording, activate LED4\n");
+		state |= CWIID_LED4_ON;
+	}
+
+	// apply the LED state
+	cwiid_set_led (wiimote, state);
+
+	DEBUG_TRACE (DEBUG::WiimoteControl, "WiimoteControlProtocol::update_led_state done\n");
+}
diff --git a/libs/surfaces/wiimote/wiimote.h b/libs/surfaces/wiimote/wiimote.h
index e0dec14..2a2db7a 100644
--- a/libs/surfaces/wiimote/wiimote.h
+++ b/libs/surfaces/wiimote/wiimote.h
@@ -39,7 +39,10 @@ protected:
 	bool idle ();
 	bool connect_wiimote ();
 
+	void update_led_state ();
+
 protected:
+	PBD::ScopedConnectionList session_connections;
 	cwiid_wiimote_t* wiimote;
 	GSource *idle_source;
 	uint16_t button_state;
-- 
1.7.11.4

