Fix for crash when loading DrumGizmo VST editor on Windows
[ardour.git] / libs / fst / scanner.cc
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <vector>
5
6 #include <glib.h>
7
8 #include "pbd/pbd.h"
9 #include "pbd/transmitter.h"
10 #include "pbd/receiver.h"
11
12 #ifdef __MINGW64__
13 #define NO_OLDNAMES // no backwards compat _pid_t, conflict with w64 pthread/sched
14 #endif
15
16 #include "ardour/filesystem_paths.h"
17 #ifdef LXVST_SUPPORT
18 #include "ardour/linux_vst_support.h"
19 #endif
20 #include "ardour/vst_info_file.h"
21
22 /* make stupid waf happy.
23  * waf cannot build multiple variants of .o object files from the same
24  * source using different wscripts.. it mingles e.g.
25  * build/libs/ardour/vst_info_file.cc.1.o for
26  * both lib/ardour/wscript and lib/fst/wscript
27  *
28  * ...but waf does track include dependencies.
29  */
30 #include "../ardour/vst_info_file.cc"
31 #ifdef LXVST_SUPPORT
32 #include "../ardour/linux_vst_support.cc"
33 #endif
34 #include "../ardour/filesystem_paths.cc"
35 #include "../ardour/directory_names.cc"
36
37
38 #ifdef LXVST_SUPPORT
39 void
40 vstfx_destroy_editor (VSTState* /*vstfx*/) { }
41 #endif
42
43 using namespace PBD;
44
45 class DummyReceiver : public Receiver {
46         protected:
47                 void receive (Transmitter::Channel chn, const char * str) {
48                         const char *prefix = "";
49                         switch (chn) {
50                                 case Transmitter::Error:
51                                         prefix = "[ERROR]: ";
52                                         break;
53                                 case Transmitter::Info:
54                                         /* ignore */
55                                         return;
56                                 case Transmitter::Warning:
57                                         prefix = "[WARNING]: ";
58                                         break;
59                                 case Transmitter::Fatal:
60                                         prefix = "[FATAL]: ";
61                                         break;
62                                 case Transmitter::Throw:
63                                         abort ();
64                         }
65
66                         std::cerr << prefix << str << std::endl;
67
68                         if (chn == Transmitter::Fatal) {
69                                 ::exit (1);
70                         }
71                 }
72 };
73
74 DummyReceiver dummy_receiver;
75
76 int main (int argc, char **argv) {
77         char *dllpath = NULL;
78         if (argc == 3 && !strcmp("-f", argv[1])) {
79                 dllpath = argv[2];
80                 const size_t slen = strlen (dllpath);
81                 if (
82                                 (slen > 3 && 0 == g_ascii_strcasecmp (&dllpath[slen-3], ".so"))
83                                 ||
84                                 (slen > 4 && 0 == g_ascii_strcasecmp (&dllpath[slen-4], ".dll"))
85                    ) {
86                         vstfx_remove_infofile(dllpath);
87                         vstfx_un_blacklist(dllpath);
88                 }
89
90         }
91         else if (argc != 2) {
92                 fprintf(stderr, "usage: %s [-f] <vst>\n", argv[0]);
93                 return EXIT_FAILURE;
94         } else {
95                 dllpath = argv[1];
96         }
97
98         PBD::init();
99
100         dummy_receiver.listen_to (error);
101         dummy_receiver.listen_to (info);
102         dummy_receiver.listen_to (fatal);
103         dummy_receiver.listen_to (warning);
104
105         std::vector<VSTInfo *> *infos = 0;
106
107         const size_t slen = strlen (dllpath);
108         if (0) { }
109 #ifdef LXVST_SUPPORT
110         else if (slen > 3 && 0 == g_ascii_strcasecmp (&dllpath[slen-3], ".so")) {
111                 infos = vstfx_get_info_lx(dllpath);
112         }
113 #endif
114
115 #ifdef WINDOWS_VST_SUPPORT
116         else if (slen > 4 && 0 == g_ascii_strcasecmp (&dllpath[slen-4], ".dll")) {
117                 infos = vstfx_get_info_fst(dllpath);
118         }
119 #endif
120         else {
121                 fprintf(stderr, "'%s' is not a supported VST plugin.\n", dllpath);
122         }
123
124         PBD::cleanup();
125
126         if (!infos || infos->empty()) {
127                 return EXIT_FAILURE;
128         } else {
129                 return EXIT_SUCCESS;
130         }
131 }