From df1a7bcf0fccb075a30d8a74a9014469618204ed Mon Sep 17 00:00:00 2001
From: Jannis Pohlmann <jannis@xfce.org>
Date: Sun, 21 Oct 2012 15:41:50 +0100
Subject: [PATCH 7/7] wiimote: properly register the cwiid callback thread
 with Ardour

This is needed in order to make calls from the cwiid thread to
Ardour via methods like next_marker(), prev_marker(), which would
otherwise result in "programming error: no per-thread pool" errors.
---
 libs/surfaces/wiimote/wiimote.cc | 13 ++++++++++++-
 libs/surfaces/wiimote/wiimote.h  |  1 +
 tools/resample_session.pl        | 16 +++++++++-------
 3 files changed, 22 insertions(+), 8 deletions(-)

diff --git a/libs/surfaces/wiimote/wiimote.cc b/libs/surfaces/wiimote/wiimote.cc
index 879ef67..fc44d2a 100644
--- a/libs/surfaces/wiimote/wiimote.cc
+++ b/libs/surfaces/wiimote/wiimote.cc
@@ -22,6 +22,7 @@ WiimoteControlProtocol::WiimoteControlProtocol (Session& session)
 	, wiimote (0)
 	, idle_source (0)
 	, button_state (0)
+	, callback_thread_registered (false)
 {
 }
 
@@ -123,6 +124,7 @@ WiimoteControlProtocol::stop ()
 	if (wiimote) {
 		cwiid_close (wiimote);
 		wiimote = 0;
+		callback_thread_registered = false;
 	}
 
 	// stop the Wiimote control UI
@@ -145,7 +147,7 @@ WiimoteControlProtocol::thread_init ()
 
 	// allow to make requests to the GUI and RT thread(s)
 	PBD::notify_gui_about_thread_creation (X_("gui"), pthread_self (), X_("wiimote"), 2048);
-	SessionEvent::create_per_thread_pool (X_("wiimote"), 128);
+	BasicUI::register_thread ("wiimote");
 
 	// connect a Wiimote
 	start_wiimote_discovery ();
@@ -217,6 +219,7 @@ WiimoteControlProtocol::connect_wiimote ()
 
 		bdaddr_t bdaddr = {{ 0, 0, 0, 0, 0, 0 }};
 		wiimote = cwiid_open (&bdaddr, 0);
+		callback_thread_registered = false;
 		if (!wiimote) {
 			success = false;
 		} else {
@@ -261,6 +264,7 @@ WiimoteControlProtocol::connect_wiimote ()
 	if (!success && wiimote) {
 		cwiid_close (wiimote);
 		wiimote = 0;
+		callback_thread_registered = false;
 	}
 
 	return success;
@@ -300,12 +304,19 @@ WiimoteControlProtocol::update_led_state ()
 void
 WiimoteControlProtocol::wiimote_callback (int mesg_count, union cwiid_mesg mesg[])
 {
+	// register the cwiid callback thread if that hasn't happened yet
+	if (!callback_thread_registered) {
+		BasicUI::register_thread ("wiimote callback");
+		callback_thread_registered = true;
+	}
+
 	for (int i = 0; i < mesg_count; i++) {
 		// restart Wiimote discovery when receiving errors
 		if (mesg[i].type == CWIID_MESG_ERROR) {
 			cerr << "Wiimote: disconnected" << endl;
 			cwiid_close (wiimote);
 			wiimote = 0;
+			callback_thread_registered = false;
 			start_wiimote_discovery ();
 			return;
 		}
diff --git a/libs/surfaces/wiimote/wiimote.h b/libs/surfaces/wiimote/wiimote.h
index ec4a683..fff168d 100644
--- a/libs/surfaces/wiimote/wiimote.h
+++ b/libs/surfaces/wiimote/wiimote.h
@@ -49,6 +49,7 @@ protected:
 	cwiid_wiimote_t* wiimote;
 	GSource *idle_source;
 	uint16_t button_state;
+	bool callback_thread_registered;
 };
 
 #endif  /* ardour_wiimote_control_protocol_h */
diff --git a/tools/resample_session.pl b/tools/resample_session.pl
index 60bb212..c1f6a79 100755
--- a/tools/resample_session.pl
+++ b/tools/resample_session.pl
@@ -6,7 +6,7 @@
 #
 # Copies the session to another directory and changes it's sampling rate in all respects.
 # The frames in .ardour and .automation files are converted according to the conversion ration.
-# The peakfiles and dead_sounds aren't copied. Only "identified" files are copied, instant.xml's
+# The peakfiles and dead aren't copied. Only "identified" files are copied, instant.xml's
 # or .bak's aren't copied either.
 
 use FindBin '$Bin';
@@ -50,7 +50,7 @@ if ( -d $destDirectory) {
 print "Checking source and destination directories\n";
 
 my @sounds;
-my @dead_sounds;
+my @dead;
 my @dot_ardour;
 my @automation;
 
@@ -80,11 +80,13 @@ if ( -d $sourceDirectory."/sounds/") {
 }
 close(SOUNDS);
 
-# Read the names of all audio files in /dead_sounds/
-opendir(DEAD_SOUNDS,$sourceDirectory."/dead_sounds/") || die ($sourceDirectory.": not a valid session, no dead_sounds/ directory");
+# Read the names of all audio files in /dead/
+opendir(DEAD_SOUNDS,$sourceDirectory."/dead/")
+  || opendir(DEAD_SOUNDS,$sourceDirectory."/dead_sounds/")
+  || die ($sourceDirectory.": not a valid session, no dead/ directory");
 while ( my $file=readdir(DEAD_SOUNDS) ) {
-	if ( -f $sourceDirectory."/dead_sounds/".$file ) {
-		push(@dead_sounds,$file);
+	if ( -f $sourceDirectory."/dead/".$file || -f $sourceDirectory."/dead_sounds/".$file ) {
+		push(@dead,$file);
 	}
 }
 close(DEAD_SOUNDS);
@@ -199,7 +201,7 @@ if ($version_099x eq 1) {
 	}
 }
 
-mkdir $destDirectory."/dead_sounds";
+mkdir $destDirectory."/dead";
 mkdir $destDirectory."/peaks";
 
 
-- 
1.7.11.4

