Fix crash when X11 is not available for VST UIs
[ardour.git] / gtk2_ardour / instrument_selector.cc
1 /*
2  * Copyright (C) 2016-2018 Robin Gareus <robin@gareus.org>
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 along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include "pbd/convert.h"
20 #include "pbd/enumwriter.h"
21
22 #include "ardour/plugin_manager.h"
23 #include "gtkmm2ext/gui_thread.h"
24
25 #include "instrument_selector.h"
26
27 #include "pbd/i18n.h"
28
29 using namespace Gtk;
30 using namespace ARDOUR;
31
32 InstrumentSelector::InstrumentSelector()
33         : _reasonable_synth_id(0)
34         , _gmsynth_id(UINT32_MAX)
35 {
36         refill ();
37
38         PluginManager::instance ().PluginListChanged.connect (_update_connection, invalidator (*this), boost::bind (&InstrumentSelector::refill, this), gui_context());
39 }
40
41 void
42 InstrumentSelector::refill()
43 {
44         TreeModel::iterator iter = get_active();
45         std::string selected;
46         if (iter) {
47                 const TreeModel::Row& row = (*iter);
48                 selected = row[_instrument_list_columns.name];
49         }
50
51         unset_model ();
52         clear ();
53         build_instrument_list();
54         set_model(_instrument_list);
55         pack_start(_instrument_list_columns.name);
56         if (selected.empty ()) {
57                 if (_gmsynth_id != UINT32_MAX) {
58                         set_active(_gmsynth_id);
59                 } else {
60                         set_active(_reasonable_synth_id);
61                 }
62         } else {
63                 TreeModel::Children rows = _instrument_list->children();
64                 TreeModel::Children::iterator i;
65                 for (i = rows.begin(); i != rows.end(); ++i) {
66                         std::string cn = (*i)[_instrument_list_columns.name];
67                         if (cn == selected) {
68                                 set_active(*i);
69                                 break;
70                         }
71                 }
72         }
73         set_button_sensitivity(Gtk::SENSITIVITY_AUTO);
74 }
75
76 static bool
77 pluginsort (const PluginInfoPtr& a, const PluginInfoPtr& b)
78 {
79         return PBD::downcase(a->name) < PBD::downcase(b->name);
80 }
81
82 static bool
83 invalid_instrument (PluginInfoPtr p) {
84         const PluginManager& manager = PluginManager::instance();
85         if (manager.get_status(p) == PluginManager::Hidden) {
86                 return true;
87         }
88         return !p->is_instrument();
89 }
90
91 void
92 InstrumentSelector::build_instrument_list()
93 {
94         PluginManager& manager = PluginManager::instance();
95
96         PluginInfoList all_plugs;
97         all_plugs.insert(all_plugs.end(), manager.ladspa_plugin_info().begin(), manager.ladspa_plugin_info().end());
98         all_plugs.insert(all_plugs.end(), manager.lua_plugin_info().begin(), manager.lua_plugin_info().end());
99 #ifdef WINDOWS_VST_SUPPORT
100         all_plugs.insert(all_plugs.end(), manager.windows_vst_plugin_info().begin(), manager.windows_vst_plugin_info().end());
101 #endif
102 #ifdef LXVST_SUPPORT
103         all_plugs.insert(all_plugs.end(), manager.lxvst_plugin_info().begin(), manager.lxvst_plugin_info().end());
104 #endif
105 #ifdef MACVST_SUPPORT
106         all_plugs.insert(all_plugs.end(), manager.mac_vst_plugin_info().begin(), manager.mac_vst_plugin_info().end());
107 #endif
108 #ifdef AUDIOUNIT_SUPPORT
109         all_plugs.insert(all_plugs.end(), manager.au_plugin_info().begin(), manager.au_plugin_info().end());
110 #endif
111 #ifdef LV2_SUPPORT
112         all_plugs.insert(all_plugs.end(), manager.lv2_plugin_info().begin(), manager.lv2_plugin_info().end());
113 #endif
114
115         all_plugs.remove_if (invalid_instrument);
116         all_plugs.sort (pluginsort);
117
118         _instrument_list = ListStore::create(_instrument_list_columns);
119
120         TreeModel::Row row = *(_instrument_list->append());
121         row[_instrument_list_columns.info_ptr] = PluginInfoPtr();
122         row[_instrument_list_columns.name]     = _("-none-");
123
124         uint32_t n = 1;
125         std::string prev;
126         for (PluginInfoList::const_iterator i = all_plugs.begin(); i != all_plugs.end(); ++i, ++n) {
127                 PluginInfoPtr p = *i;
128                 
129                 std::string suffix;
130
131 #ifdef MIXBUS
132                 uint32_t n_outs = p->max_configurable_ouputs ();
133                 if (n_outs > 2) {
134                         if (p->reconfigurable_io ()) {
135                                 suffix = string_compose(_("\u2264 %1 outs"), n_outs);
136                         } else {
137                                 suffix = string_compose(_("%1 outs"), n_outs);
138                         }
139                 }
140 #else
141                 if (p->multichannel_name_ambiguity) {
142                         uint32_t n_outs = p->max_configurable_ouputs ();
143                         if (n_outs > 2) {
144                                 if (p->reconfigurable_io ()) {
145                                         suffix = string_compose(_("\u2264 %1 outs"), n_outs);
146                                 } else {
147                                         suffix = string_compose(_("%1 outs"), n_outs);
148                                 }
149                         } else if (n_outs == 2) {
150                                 suffix = _("stereo");
151                         }
152                 }
153 #endif
154
155                 if (p->plugintype_name_ambiguity) {
156                         std::string pt;
157                         switch (p->type) {
158                                 case AudioUnit:
159                                         pt = "AU";
160                                         break;
161                                 case Windows_VST:
162                                 case LXVST:
163                                 case MacVST:
164                                         pt = "VST";
165                                         break;
166                                 default:
167                                         pt = enum_2_string (p->type);
168                         }
169                         if (!suffix.empty ()) {
170                                 suffix += ", ";
171                         }
172                         suffix += pt;
173                 }
174
175                 std::string name = p->name;
176                 if (!suffix.empty ()) {
177                         name += " (" + suffix + ")";
178                 }
179
180                 row = *(_instrument_list->append());
181                 row[_instrument_list_columns.name] = name;
182
183                 row[_instrument_list_columns.info_ptr] = p;
184                 if (p->unique_id == "https://community.ardour.org/node/7596") {
185                         _reasonable_synth_id = n;
186                 }
187                 if (p->unique_id == "http://gareus.org/oss/lv2/gmsynth") {
188                         _gmsynth_id = n;
189                 }
190                 prev = p->name;
191         }
192 }
193
194 PluginInfoPtr
195 InstrumentSelector::selected_instrument()
196 {
197         TreeModel::iterator iter = get_active();
198         if (!iter) {
199                 return PluginInfoPtr();
200         }
201
202         const TreeModel::Row& row = (*iter);
203         return row[_instrument_list_columns.info_ptr];
204 }