Fix gmsynth detection
[ardour.git] / gtk2_ardour / instrument_selector.cc
1 /*
2   Copyright (C) 2003-2014 Paul Davis
3
4   This program is free software; you can redistribute it and/or modify
5   it under the terms of the GNU General Public License as published by
6   the Free Software Foundation; either version 2 of the License, or
7   (at your option) any later version.
8
9   This program is distributed in the hope that it will be useful,
10   but WITHOUT ANY WARRANTY; without even the implied warranty of
11   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   GNU General Public License for more details.
13
14   You should have received a copy of the GNU General Public License
15   along with this program; if not, write to the Free Software
16   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19 #include "pbd/convert.h"
20 #include "pbd/enumwriter.h"
21 #include "ardour/plugin_manager.h"
22 #include "gtkmm2ext/gui_thread.h"
23 #include "instrument_selector.h"
24
25 #include "pbd/i18n.h"
26
27 using namespace Gtk;
28 using namespace ARDOUR;
29
30 InstrumentSelector::InstrumentSelector()
31         : _reasonable_synth_id(0)
32         , _gmsynth_id(UINT32_MAX)
33 {
34         refill ();
35
36         PluginManager::instance ().PluginListChanged.connect (_update_connection, invalidator (*this), boost::bind (&InstrumentSelector::refill, this), gui_context());
37 }
38
39 void
40 InstrumentSelector::refill()
41 {
42         TreeModel::iterator iter = get_active();
43         std::string selected;
44         if (iter) {
45                 const TreeModel::Row& row = (*iter);
46                 selected = row[_instrument_list_columns.name];
47         }
48
49         unset_model ();
50         clear ();
51         build_instrument_list();
52         set_model(_instrument_list);
53         pack_start(_instrument_list_columns.name);
54         if (selected.empty ()) {
55                 if (_gmsynth_id != UINT32_MAX) {
56                         set_active(_gmsynth_id);
57                 } else {
58                         set_active(_reasonable_synth_id);
59                 }
60         } else {
61                 TreeModel::Children rows = _instrument_list->children();
62                 TreeModel::Children::iterator i;
63                 for (i = rows.begin(); i != rows.end(); ++i) {
64                         std::string cn = (*i)[_instrument_list_columns.name];
65                         if (cn == selected) {
66                                 set_active(*i);
67                                 break;
68                         }
69                 }
70         }
71         set_button_sensitivity(Gtk::SENSITIVITY_AUTO);
72 }
73
74 static bool
75 pluginsort (const PluginInfoPtr& a, const PluginInfoPtr& b)
76 {
77         return PBD::downcase(a->name) < PBD::downcase(b->name);
78 }
79
80 static bool
81 invalid_instrument (PluginInfoPtr p) {
82         const PluginManager& manager = PluginManager::instance();
83         if (manager.get_status(p) == PluginManager::Hidden) {
84                 return true;
85         }
86         return !p->is_instrument();
87 }
88
89 void
90 InstrumentSelector::build_instrument_list()
91 {
92         PluginManager& manager = PluginManager::instance();
93
94         PluginInfoList all_plugs;
95         all_plugs.insert(all_plugs.end(), manager.ladspa_plugin_info().begin(), manager.ladspa_plugin_info().end());
96         all_plugs.insert(all_plugs.end(), manager.lua_plugin_info().begin(), manager.lua_plugin_info().end());
97 #ifdef WINDOWS_VST_SUPPORT
98         all_plugs.insert(all_plugs.end(), manager.windows_vst_plugin_info().begin(), manager.windows_vst_plugin_info().end());
99 #endif
100 #ifdef LXVST_SUPPORT
101         all_plugs.insert(all_plugs.end(), manager.lxvst_plugin_info().begin(), manager.lxvst_plugin_info().end());
102 #endif
103 #ifdef MACVST_SUPPORT
104         all_plugs.insert(all_plugs.end(), manager.mac_vst_plugin_info().begin(), manager.mac_vst_plugin_info().end());
105 #endif
106 #ifdef AUDIOUNIT_SUPPORT
107         all_plugs.insert(all_plugs.end(), manager.au_plugin_info().begin(), manager.au_plugin_info().end());
108 #endif
109 #ifdef LV2_SUPPORT
110         all_plugs.insert(all_plugs.end(), manager.lv2_plugin_info().begin(), manager.lv2_plugin_info().end());
111 #endif
112
113         all_plugs.remove_if (invalid_instrument);
114         all_plugs.sort (pluginsort);
115
116         _instrument_list = ListStore::create(_instrument_list_columns);
117
118         TreeModel::Row row = *(_instrument_list->append());
119         row[_instrument_list_columns.info_ptr] = PluginInfoPtr();
120         row[_instrument_list_columns.name]     = _("-none-");
121
122         uint32_t n = 1;
123         std::string prev;
124         for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end();) {
125                 PluginInfoPtr p = *i;
126                 ++i;
127                 bool suffix_type = prev == p->name;
128                 if (!suffix_type && i != all_plugs.end() && (*i)->name == p->name) {
129                         suffix_type = true;
130                 }
131
132                 row = *(_instrument_list->append());
133                 if (suffix_type) {
134                         std::string pt;
135                         switch (p->type) {
136                                 case AudioUnit:
137                                         pt = "AU";
138                                         break;
139                                 case Windows_VST:
140                                 case LXVST:
141                                 case MacVST:
142                                         pt = "VST";
143                                         break;
144                                 default:
145                                         pt = enum_2_string (p->type);
146                         }
147                         row[_instrument_list_columns.name]   = p->name + " (" + pt + ")";
148                 } else {
149                         row[_instrument_list_columns.name]   = p->name;
150                 }
151                 row[_instrument_list_columns.info_ptr] = p;
152                 if (p->unique_id == "https://community.ardour.org/node/7596") {
153                         _reasonable_synth_id = n;
154                 }
155                 if (p->unique_id == "http://gareus.org/oss/lv2/gmsynth") {
156                         _gmsynth_id = n;
157                 }
158                 prev = p->name;
159                 n++;
160         }
161 }
162
163 PluginInfoPtr
164 InstrumentSelector::selected_instrument()
165 {
166         TreeModel::iterator iter = get_active();
167         if (!iter) {
168                 return PluginInfoPtr();
169         }
170
171         const TreeModel::Row& row = (*iter);
172         return row[_instrument_list_columns.info_ptr];
173 }