1 /*
2  * Copyright (C) 2008 Hans Baier <hansfbaier@googlemail.com>
3  * Copyright (C) 2009-2014 David Robillard <d@drobilla.net>
4  * Copyright (C) 2009-2016 Paul Davis <paul@linuxaudiosystems.com>
5  * Copyright (C) 2012-2015 Tim Mayberry <mojofunk@gmail.com>
6  * Copyright (C) 2014-2016 Robin Gareus <robin@gareus.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22 
23 #include <boost/shared_ptr.hpp>
24 
25 #include <glibmm/fileutils.h>
26 
27 #include "pbd/error.h"
28 #include "pbd/file_utils.h"
29 #include "pbd/pthread_utils.h"
30 #include "pbd/unwind.h"
31 
32 #include "ardour/midi_patch_manager.h"
33 
34 #include "ardour/search_paths.h"
35 
36 #include "pbd/i18n.h"
37 
38 using namespace std;
39 using namespace ARDOUR;
40 using namespace MIDI;
41 using namespace MIDI::Name;
42 using namespace PBD;
43 
44 MidiPatchManager* MidiPatchManager::_manager = 0;
45 
MidiPatchManager()46 MidiPatchManager::MidiPatchManager ()
47 	: no_patch_changed_messages (false)
48 	, stop_thread (false)
49 {
50 	add_search_path (midi_patch_search_path ());
51 }
52 
~MidiPatchManager()53 MidiPatchManager::~MidiPatchManager ()
54 {
55 	_manager = 0;
56 
57 	stop_thread = true;
58 	_midnam_load_thread->join ();
59 }
60 
61 void
add_search_path(const Searchpath & search_path)62 MidiPatchManager::add_search_path (const Searchpath& search_path)
63 {
64 	for (Searchpath::const_iterator i = search_path.begin(); i != search_path.end(); ++i) {
65 
66 		if (_search_path.contains(*i)) {
67 			// already processed files from this path
68 			continue;
69 		}
70 
71 		if (!Glib::file_test (*i, Glib::FILE_TEST_EXISTS)) {
72 			continue;
73 		}
74 
75 		if (!Glib::file_test (*i, Glib::FILE_TEST_IS_DIR)) {
76 			continue;
77 		}
78 
79 		_search_path.add_directory (*i);
80 	}
81 }
82 
83 bool
add_custom_midnam(const std::string & id,char const * midnam)84 MidiPatchManager::add_custom_midnam (const std::string& id, char const* midnam)
85 {
86 	boost::shared_ptr<MIDINameDocument> document;
87 	document = boost::shared_ptr<MIDINameDocument>(new MIDINameDocument());
88 	XMLTree mxml;
89 	if (mxml.read_buffer (midnam, true)) {
90 		if (0 == document->set_state (mxml, *mxml.root())) {
91 			document->set_file_path ("custom:" + id);
92 			add_midi_name_document (document);
93 			return true;
94 		}
95 	}
96 	return false;
97 }
98 
99 bool
remove_custom_midnam(const std::string & id)100 MidiPatchManager::remove_custom_midnam (const std::string& id)
101 {
102 	return remove_midi_name_document ("custom:" + id);
103 }
104 
105 bool
update_custom_midnam(const std::string & id,char const * midnam)106 MidiPatchManager::update_custom_midnam (const std::string& id, char const* midnam)
107 {
108 	Glib::Threads::Mutex::Lock lm (_lock);
109 	remove_midi_name_document ("custom:" + id, false);
110 	return add_custom_midnam (id, midnam);
111 }
112 
113 bool
is_custom_model(const std::string & model) const114 MidiPatchManager::is_custom_model (const std::string& model) const
115 {
116 	boost::shared_ptr<MIDINameDocument> midnam = document_by_model (model);
117 	return (midnam && midnam->file_path().substr(0, 7) == "custom:");
118 }
119 
120 void
add_midnam_files_from_directory(const std::string & directory_path)121 MidiPatchManager::add_midnam_files_from_directory(const std::string& directory_path)
122 {
123 	vector<std::string> result;
124 	find_files_matching_pattern (result, directory_path, "*.midnam");
125 
126 	info << string_compose (P_("Loading %1 MIDI patch from %2", "Loading %1 MIDI patches from %2", result.size()), result.size(), directory_path) << endmsg;
127 
128 	for (vector<std::string>::const_iterator i = result.begin(); i != result.end(); ++i) {
129 		if (stop_thread) {
130 			break;
131 		}
132 		load_midi_name_document (*i);
133 	}
134 }
135 
136 void
remove_search_path(const Searchpath & search_path)137 MidiPatchManager::remove_search_path (const Searchpath& search_path)
138 {
139 	for (Searchpath::const_iterator i = search_path.begin(); i != search_path.end(); ++i) {
140 
141 		if (!_search_path.contains(*i)) {
142 			continue;
143 		}
144 
145 		remove_midnam_files_from_directory(*i);
146 
147 		_search_path.remove_directory (*i);
148 	}
149 }
150 
151 void
remove_midnam_files_from_directory(const std::string & directory_path)152 MidiPatchManager::remove_midnam_files_from_directory(const std::string& directory_path)
153 {
154 	vector<std::string> result;
155 	find_files_matching_pattern (result, directory_path, "*.midnam");
156 
157 	info << string_compose(
158 		P_("Unloading %1 MIDI patch from %2", "Unloading %1 MIDI patches from %2", result.size()),
159 		result.size(), directory_path)
160 	     << endmsg;
161 
162 	for (vector<std::string>::const_iterator i = result.begin(); i != result.end(); ++i) {
163 		remove_midi_name_document (*i);
164 	}
165 }
166 
167 bool
load_midi_name_document(const std::string & file_path)168 MidiPatchManager::load_midi_name_document (const std::string& file_path)
169 {
170 	boost::shared_ptr<MIDINameDocument> document;
171 	try {
172 		document = boost::shared_ptr<MIDINameDocument>(new MIDINameDocument(file_path));
173 	}
174 	catch (...) {
175 		error << string_compose(_("Error parsing MIDI patch file %1"), file_path)
176 		      << endmsg;
177 		return false;
178 	}
179 	return add_midi_name_document (document);
180 }
181 
182 boost::shared_ptr<MIDINameDocument>
document_by_model(std::string model_name) const183 MidiPatchManager::document_by_model(std::string model_name) const
184 {
185 	MidiNameDocuments::const_iterator i = _documents.find (model_name);
186 	if (i != _documents.end ()) {
187 		return i->second;
188 	}
189 	return boost::shared_ptr<MIDINameDocument> ();
190 }
191 
192 bool
add_midi_name_document(boost::shared_ptr<MIDINameDocument> document)193 MidiPatchManager::add_midi_name_document (boost::shared_ptr<MIDINameDocument> document)
194 {
195 	bool added = false;
196 	for (MIDINameDocument::MasterDeviceNamesList::const_iterator device =
197 		     document->master_device_names_by_model().begin();
198 	     device != document->master_device_names_by_model().end();
199 	     ++device) {
200 		if (_documents.find(device->first) != _documents.end()) {
201 			warning << string_compose(_("Duplicate MIDI device `%1' in `%2' ignored"),
202 			                          device->first,
203 			                          document->file_path()) << endmsg;
204 			continue;
205 		}
206 
207 		_documents[device->first] = document;
208 		_master_devices_by_model[device->first] = device->second;
209 
210 		_all_models.insert(device->first);
211 		const std::string& manufacturer = device->second->manufacturer();
212 		if (_devices_by_manufacturer.find(manufacturer) ==
213 		    _devices_by_manufacturer.end()) {
214 			MIDINameDocument::MasterDeviceNamesList empty;
215 			_devices_by_manufacturer.insert(std::make_pair(manufacturer, empty));
216 		}
217 		_devices_by_manufacturer[manufacturer].insert(
218 			std::make_pair(device->first, device->second));
219 
220 		added = true;
221 		// TODO: handle this gracefully.
222 		assert(_documents.count(device->first) == 1);
223 		assert(_master_devices_by_model.count(device->first) == 1);
224 	}
225 
226 	if (added && !no_patch_changed_messages) {
227 		PatchesChanged(); /* EMIT SIGNAL */
228 	}
229 
230 	return added;
231 }
232 
233 bool
remove_midi_name_document(const std::string & file_path,bool emit_signal)234 MidiPatchManager::remove_midi_name_document (const std::string& file_path, bool emit_signal)
235 {
236 	bool removed = false;
237 	for (MidiNameDocuments::iterator i = _documents.begin(); i != _documents.end();) {
238 		if (i->second->file_path() == file_path) {
239 
240 			boost::shared_ptr<MIDINameDocument> document = i->second;
241 
242 			info << string_compose(_("Removing MIDI patch file %1"), file_path) << endmsg;
243 
244 			_documents.erase(i++);
245 
246 			for (MIDINameDocument::MasterDeviceNamesList::const_iterator device =
247 				     document->master_device_names_by_model().begin();
248 			     device != document->master_device_names_by_model().end();
249 			     ++device) {
250 
251 				_master_devices_by_model.erase(device->first);
252 
253 				_all_models.erase(device->first);
254 
255 				const std::string& manufacturer = device->second->manufacturer();
256 
257 				_devices_by_manufacturer[manufacturer].erase(device->first);
258 			}
259 			removed = true;
260 		} else {
261 			++i;
262 		}
263 	}
264 	if (removed && emit_signal) {
265 		PatchesChanged(); /* EMIT SIGNAL */
266 	}
267 	return removed;
268 }
269 
270 void
load_midnams()271 MidiPatchManager::load_midnams ()
272 {
273 	/* really there's only going to be one x-thread request/signal before
274 	   this thread exits but we'll say 8 just to be sure.
275 	*/
276 
277 	PBD::notify_event_loops_about_thread_creation (pthread_self(), "midi-patch-manager", 8);
278 	pthread_set_name ("MIDNAMLoader");
279 
280 	{
281 		PBD::Unwinder<bool> npc (no_patch_changed_messages, true);
282 		for (Searchpath::const_iterator i = _search_path.begin(); i != _search_path.end(); ++i) {
283 			Glib::Threads::Mutex::Lock lm (_lock);
284 			add_midnam_files_from_directory (*i);
285 		}
286 	}
287 
288 	PatchesChanged (); /* EMIT SIGNAL */
289 }
290 
291 void
load_midnams_in_thread()292 MidiPatchManager::load_midnams_in_thread ()
293 {
294 	_midnam_load_thread = Glib::Threads::Thread::create (sigc::mem_fun (*this, &MidiPatchManager::load_midnams));
295 }
296 
297 void
maybe_use(PBD::ScopedConnectionList & cl,PBD::EventLoop::InvalidationRecord * ir,const boost::function<void ()> & midnam_info_method,PBD::EventLoop * event_loop)298 MidiPatchManager::maybe_use (PBD::ScopedConnectionList& cl,
299                              PBD::EventLoop::InvalidationRecord* ir,
300                              const boost::function<void()> & midnam_info_method,
301                              PBD::EventLoop* event_loop)
302 {
303 	{
304 		Glib::Threads::Mutex::Lock lm (_lock);
305 
306 		if (!_documents.empty()) {
307 			/* already have documents loaded, so call closure to use them */
308 			midnam_info_method ();
309 		}
310 
311 		/* if/when they ever change, call the closure (maybe multiple times) */
312 
313 		PatchesChanged.connect (cl, ir, midnam_info_method, event_loop);
314 	}
315 }
316