Fix a tiny memory-leak when calling vfork
[ardour.git] / libs / ardour / video_tools_paths.cc
1 /*
2  * Copyright (C) 2018 Robin Gareus <robin@gareus.org>
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17  */
18
19 /* This extends ardour/filesystem_paths.cc but requires additional
20  * includes, in particular 'rc_configuration.h' which in turn
21  * pulls in types.h, which include temporal/bbt_time and evoral
22  *
23  * filesystem_paths.cc is used by various standalone utils,
24  * e.g. the VST scanner and pulling in most of libardour's dependencies
25  * there is not reasonable.
26  */
27
28 #include <string>
29
30 #include "pbd/file_utils.h"
31
32 #include <glibmm/convert.h>
33 #include <glibmm/miscutils.h>
34 #include <glibmm/fileutils.h>
35
36 #include "ardour/filesystem_paths.h"
37 #include "ardour/rc_configuration.h"
38
39 #include "pbd/i18n.h"
40
41 #ifdef PLATFORM_WINDOWS
42 #include <windows.h>
43 #include "shlobj.h"
44 #include "pbd/windows_special_dirs.h"
45 #endif
46
47 using namespace PBD;
48
49 namespace ARDOUR {
50
51 #ifdef PLATFORM_WINDOWS
52 static bool
53 windows_install_dir (const char *regkey, std::string &rv) {
54         HKEY key;
55         DWORD size = PATH_MAX;
56         char tmp[PATH_MAX+1];
57
58         if (   (ERROR_SUCCESS == RegOpenKeyExA (HKEY_LOCAL_MACHINE, regkey, 0, KEY_READ, &key))
59             && (ERROR_SUCCESS == RegQueryValueExA (key, "Install_Dir", 0, NULL, reinterpret_cast<LPBYTE>(tmp), &size))
60                  )
61         {
62                 rv = Glib::locale_to_utf8(tmp);
63                 return true;
64         }
65
66         if (   (ERROR_SUCCESS == RegOpenKeyExA (HKEY_LOCAL_MACHINE, regkey, 0, KEY_READ | KEY_WOW64_32KEY, &key))
67             && (ERROR_SUCCESS == RegQueryValueExA (key, "Install_Dir", 0, NULL, reinterpret_cast<LPBYTE>(tmp), &size))
68                         )
69         {
70                 rv = Glib::locale_to_utf8(tmp);
71                 return true;
72         }
73
74         return false;
75 }
76 #endif
77
78 bool
79 ArdourVideoToolPaths::harvid_exe (std::string &harvid_exe)
80 {
81
82 #ifdef PLATFORM_WINDOWS
83         std::string reg;
84         std::string program_files = PBD::get_win_special_folder_path (CSIDL_PROGRAM_FILES);
85 #endif
86
87         harvid_exe = "";
88
89         std::string icsd_file_path;
90         if (find_file (PBD::Searchpath(Glib::getenv("PATH")), X_("harvid"), icsd_file_path)) {
91                 harvid_exe = icsd_file_path;
92         }
93 #ifdef PLATFORM_WINDOWS
94         else if ( windows_install_dir("Software\\" PROGRAM_NAME "\\v" PROGRAM_VERSION "\\video", reg))
95         {
96                 harvid_exe = g_build_filename(reg.c_str(), "harvid", "harvid.exe", NULL);
97         }
98         else if ( windows_install_dir("Software\\RSS\\harvid", reg))
99         {
100                 harvid_exe = g_build_filename(reg.c_str(), "harvid.exe", NULL);
101         }
102         else if (!program_files.empty() && Glib::file_test(g_build_filename(program_files.c_str(), "harvid", "harvid.exe", NULL), Glib::FILE_TEST_EXISTS))
103         {
104                 harvid_exe = g_build_filename(program_files.c_str(), "harvid", "harvid.exe", NULL);
105         }
106         else if (Glib::file_test(X_("C:\\Program Files\\harvid\\harvid.exe"), Glib::FILE_TEST_EXISTS)) {
107                 harvid_exe = X_("C:\\Program Files\\harvid\\harvid.exe");
108         }
109 #endif
110         else
111         {
112                 return false;
113         }
114         return true;
115 }
116
117 bool
118 ArdourVideoToolPaths::xjadeo_exe (std::string &xjadeo_exe)
119 {
120         std::string xjadeo_file_path;
121 #ifdef PLATFORM_WINDOWS
122         std::string reg;
123         std::string program_files = PBD::get_win_special_folder_path (CSIDL_PROGRAM_FILES);
124 #endif
125         xjadeo_exe = X_("");
126
127         if (getenv("XJREMOTE")) {
128                 xjadeo_exe = getenv("XJREMOTE");
129 #ifdef __APPLE__
130         } else if (!Config->get_xjadeo_binary().empty()
131                         && Glib::file_test (Config->get_xjadeo_binary() + "/Contents/MacOS/xjadeo", Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_EXECUTABLE)) {
132                 xjadeo_exe = Config->get_xjadeo_binary() + "/Contents/MacOS/xjadeo";
133 #endif
134         } else if (!Config->get_xjadeo_binary().empty()
135                         && Glib::file_test (Config->get_xjadeo_binary(), Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_EXECUTABLE)) {
136                 xjadeo_exe = Config->get_xjadeo_binary();
137         } else if (find_file (Searchpath(Glib::getenv("PATH")), X_("xjremote"), xjadeo_file_path)) {
138                 xjadeo_exe = xjadeo_file_path;
139         } else if (find_file (Searchpath(Glib::getenv("PATH")), X_("xjadeo"), xjadeo_file_path)) {
140                 xjadeo_exe = xjadeo_file_path;
141         }
142 #ifdef __APPLE__
143         else if (Glib::file_test(X_("/Applications/Xjadeo.app/Contents/MacOS/xjadeo"), Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_EXECUTABLE)) {
144                 xjadeo_exe = X_("/Applications/Xjadeo.app/Contents/MacOS/xjadeo");
145         }
146         else if (Glib::file_test(X_("/Applications/Jadeo.app/Contents/MacOS/Jadeo-bin"), Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_EXECUTABLE)) {
147                 xjadeo_exe = X_("/Applications/Jadeo.app/Contents/MacOS/Jadeo-bin");
148         }
149 #endif
150 #ifdef PLATFORM_WINDOWS
151         else if ( windows_install_dir("Software\\" PROGRAM_NAME "\\v" PROGRAM_VERSION "\\video", reg))
152         {
153                 xjadeo_exe = std::string(g_build_filename(reg.c_str(), "xjadeo", "xjadeo.exe", NULL));
154         }
155         else if ( windows_install_dir("Software\\RSS\\xjadeo", reg))
156         {
157                 xjadeo_exe = std::string(g_build_filename(reg.c_str(), "xjadeo.exe", NULL));
158         }
159         else if (!program_files.empty() && Glib::file_test(g_build_filename(program_files.c_str(), "xjadeo", "xjadeo.exe", NULL), Glib::FILE_TEST_EXISTS))
160         {
161                 xjadeo_exe = std::string(g_build_filename(program_files.c_str(), "xjadeo", "xjadeo.exe", NULL));
162         }
163         else if (Glib::file_test(X_("C:\\Program Files\\xjadeo\\xjadeo.exe"), Glib::FILE_TEST_EXISTS)) {
164                 xjadeo_exe = X_("C:\\Program Files\\xjadeo\\xjadeo.exe");
165         }
166 #endif
167
168         return (!xjadeo_exe.empty() && Glib::file_test(xjadeo_exe, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_EXECUTABLE));
169 }
170
171 bool
172 ArdourVideoToolPaths::transcoder_exe (std::string &ffmpeg_exe, std::string &ffprobe_exe)
173 {
174 #ifdef PLATFORM_WINDOWS
175         std::string reg;
176         std::string program_files = PBD::get_win_special_folder_path (CSIDL_PROGRAM_FILES);
177 #endif
178
179         ffmpeg_exe = X_("");
180         ffprobe_exe = X_("");
181
182         std::string ff_file_path;
183         if (find_file (Searchpath(Glib::getenv("PATH")), X_("ffmpeg_harvid"), ff_file_path)) {
184                 ffmpeg_exe = ff_file_path;
185         }
186 #ifdef PLATFORM_WINDOWS
187         else if ( windows_install_dir("Software\\" PROGRAM_NAME "\\v" PROGRAM_VERSION "\\video", reg))
188         {
189                 ffmpeg_exe = g_build_filename(reg.c_str(), X_("harvid"), X_("ffmpeg.exe"), NULL);
190                 ffprobe_exe = g_build_filename(reg.c_str(), X_("harvid"), X_("ffprobe.exe"), NULL);
191         }
192         else if ( windows_install_dir("Software\\RSS\\harvid", reg))
193         {
194                 ffmpeg_exe = g_build_filename(reg.c_str(), X_("ffmpeg.exe"), NULL);
195                 ffprobe_exe = g_build_filename(reg.c_str(), X_("ffprobe.exe"), NULL);
196         }
197
198         if (Glib::file_test(ffmpeg_exe, Glib::FILE_TEST_EXISTS)) {
199                 ;
200         }
201         else if (!program_files.empty() && Glib::file_test(g_build_filename(program_files.c_str(), "harvid", "ffmpeg.exe", NULL), Glib::FILE_TEST_EXISTS)) {
202                 ffmpeg_exe = g_build_filename(program_files.c_str(), "harvid", "ffmpeg.exe", NULL);
203         }
204         else if (Glib::file_test(X_("C:\\Program Files\\ffmpeg\\ffmpeg.exe"), Glib::FILE_TEST_EXISTS)) {
205                 ffmpeg_exe = X_("C:\\Program Files\\ffmpeg\\ffmpeg.exe");
206         } else {
207                 ffmpeg_exe = X_("");
208         }
209 #endif
210
211         if (find_file (Searchpath(Glib::getenv("PATH")), X_("ffprobe_harvid"), ff_file_path)) {
212                 ffprobe_exe = ff_file_path;
213         }
214 #ifdef PLATFORM_WINDOWS
215         if (Glib::file_test(ffprobe_exe, Glib::FILE_TEST_EXISTS)) {
216                 ;
217         }
218         else if (!program_files.empty() && Glib::file_test(g_build_filename(program_files.c_str(), "harvid", "ffprobe.exe", NULL), Glib::FILE_TEST_EXISTS)) {
219                 ffprobe_exe = g_build_filename(program_files.c_str(), "harvid", "ffprobe.exe", NULL);
220         }
221         else if (Glib::file_test(X_("C:\\Program Files\\ffmpeg\\ffprobe.exe"), Glib::FILE_TEST_EXISTS)) {
222                 ffprobe_exe = X_("C:\\Program Files\\ffmpeg\\ffprobe.exe");
223         } else {
224                 ffprobe_exe = X_("");
225         }
226 #endif
227
228         if (ffmpeg_exe.empty() || ffprobe_exe.empty()) {
229                 return false;
230         }
231         return true;
232 }
233
234 } // namespace ARDOUR