Move video-tool paths to libardour (1/2)
[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/miscutils.h>
33 #include <glibmm/fileutils.h>
34
35 #include "ardour/filesystem_paths.h"
36 #include "ardour/rc_configuration.h"
37
38 #include "pbd/i18n.h"
39
40 #ifdef PLATFORM_WINDOWS
41 #include <windows.h>
42 #include "shlobj.h"
43 #include "pbd/windows_special_dirs.h"
44 #endif
45
46 using namespace PBD;
47
48 namespace ARDOUR {
49
50 #ifdef PLATFORM_WINDOWS
51 static bool
52 windows_install_dir (const char *regkey, std::string &rv) {
53         HKEY key;
54         DWORD size = PATH_MAX;
55         char tmp[PATH_MAX+1];
56
57         if (   (ERROR_SUCCESS == RegOpenKeyExA (HKEY_LOCAL_MACHINE, regkey, 0, KEY_READ, &key))
58             && (ERROR_SUCCESS == RegQueryValueExA (key, "Install_Dir", 0, NULL, reinterpret_cast<LPBYTE>(tmp), &size))
59                  )
60         {
61                 rv = Glib::locale_to_utf8(tmp);
62                 return true;
63         }
64
65         if (   (ERROR_SUCCESS == RegOpenKeyExA (HKEY_LOCAL_MACHINE, regkey, 0, KEY_READ | KEY_WOW64_32KEY, &key))
66             && (ERROR_SUCCESS == RegQueryValueExA (key, "Install_Dir", 0, NULL, reinterpret_cast<LPBYTE>(tmp), &size))
67                         )
68         {
69                 rv = Glib::locale_to_utf8(tmp);
70                 return true;
71         }
72
73         return false;
74 }
75 #endif
76
77 bool
78 ArdourVideoToolPaths::harvid_exe (std::string &harvid_exe)
79 {
80
81 #ifdef PLATFORM_WINDOWS
82         std::string reg;
83         std::string program_files = PBD::get_win_special_folder_path (CSIDL_PROGRAM_FILES);
84 #endif
85
86         harvid_exe = "";
87
88         std::string icsd_file_path;
89         if (find_file (PBD::Searchpath(Glib::getenv("PATH")), X_("harvid"), icsd_file_path)) {
90                 harvid_exe = icsd_file_path;
91         }
92 #ifdef PLATFORM_WINDOWS
93         else if ( windows_install_dir("Software\\" PROGRAM_NAME "\\v" PROGRAM_VERSION "\\video", reg))
94         {
95                 harvid_exe = g_build_filename(reg.c_str(), "harvid", "harvid.exe", NULL);
96         }
97         else if ( windows_install_dir("Software\\RSS\\harvid", reg))
98         {
99                 harvid_exe = g_build_filename(reg.c_str(), "harvid.exe", NULL);
100         }
101         else if (!program_files.empty() && Glib::file_test(g_build_filename(program_files.c_str(), "harvid", "harvid.exe", NULL), Glib::FILE_TEST_EXISTS))
102         {
103                 harvid_exe = g_build_filename(program_files.c_str(), "harvid", "harvid.exe", NULL);
104         }
105         else if (Glib::file_test(X_("C:\\Program Files\\harvid\\harvid.exe"), Glib::FILE_TEST_EXISTS)) {
106                 harvid_exe = X_("C:\\Program Files\\harvid\\harvid.exe");
107         }
108 #endif
109         else
110         {
111                 return false;
112         }
113         return true;
114 }
115
116 bool
117 ArdourVideoToolPaths::xjadeo_exe (std::string &xjadeo_exe)
118 {
119         std::string xjadeo_file_path;
120 #ifdef PLATFORM_WINDOWS
121         std::string reg;
122         std::string program_files = PBD::get_win_special_folder_path (CSIDL_PROGRAM_FILES);
123 #endif
124         xjadeo_exe = X_("");
125
126         if (getenv("XJREMOTE")) {
127                 xjadeo_exe = getenv("XJREMOTE");
128 #ifdef __APPLE__
129         } else if (!Config->get_xjadeo_binary().empty()
130                         && Glib::file_test (Config->get_xjadeo_binary() + "/Contents/MacOS/xjadeo", Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_EXECUTABLE)) {
131                 xjadeo_exe = Config->get_xjadeo_binary() + "/Contents/MacOS/xjadeo";
132 #endif
133         } else if (!Config->get_xjadeo_binary().empty()
134                         && Glib::file_test (Config->get_xjadeo_binary(), Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_EXECUTABLE)) {
135                 xjadeo_exe = Config->get_xjadeo_binary();
136         } else if (find_file (Searchpath(Glib::getenv("PATH")), X_("xjremote"), xjadeo_file_path)) {
137                 xjadeo_exe = xjadeo_file_path;
138         } else if (find_file (Searchpath(Glib::getenv("PATH")), X_("xjadeo"), xjadeo_file_path)) {
139                 xjadeo_exe = xjadeo_file_path;
140         }
141 #ifdef __APPLE__
142         else if (Glib::file_test(X_("/Applications/Xjadeo.app/Contents/MacOS/xjadeo"), Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_EXECUTABLE)) {
143                 xjadeo_exe = X_("/Applications/Xjadeo.app/Contents/MacOS/xjadeo");
144         }
145         else if (Glib::file_test(X_("/Applications/Jadeo.app/Contents/MacOS/Jadeo-bin"), Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_EXECUTABLE)) {
146                 xjadeo_exe = X_("/Applications/Jadeo.app/Contents/MacOS/Jadeo-bin");
147         }
148 #endif
149 #ifdef PLATFORM_WINDOWS
150         else if ( windows_install_dir("Software\\" PROGRAM_NAME "\\v" PROGRAM_VERSION "\\video", reg))
151         {
152                 xjadeo_exe = std::string(g_build_filename(reg.c_str(), "xjadeo", "xjadeo.exe", NULL));
153         }
154         else if ( windows_install_dir("Software\\RSS\\xjadeo", reg))
155         {
156                 xjadeo_exe = std::string(g_build_filename(reg.c_str(), "xjadeo.exe", NULL));
157         }
158         else if (!program_files.empty() && Glib::file_test(g_build_filename(program_files.c_str(), "xjadeo", "xjadeo.exe", NULL), Glib::FILE_TEST_EXISTS))
159         {
160                 xjadeo_exe = std::string(g_build_filename(program_files.c_str(), "xjadeo", "xjadeo.exe", NULL));
161         }
162         else if (Glib::file_test(X_("C:\\Program Files\\xjadeo\\xjadeo.exe"), Glib::FILE_TEST_EXISTS)) {
163                 xjadeo_exe = X_("C:\\Program Files\\xjadeo\\xjadeo.exe");
164         }
165 #endif
166
167         return (!xjadeo_exe.empty() && Glib::file_test(xjadeo_exe, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_EXECUTABLE));
168 }
169
170 bool
171 ArdourVideoToolPaths::transcoder_exe (std::string &ffmpeg_exe, std::string &ffprobe_exe)
172 {
173 #ifdef PLATFORM_WINDOWS
174         std::string reg;
175         std::string program_files = PBD::get_win_special_folder_path (CSIDL_PROGRAM_FILES);
176 #endif
177
178         ffmpeg_exe = X_("");
179         ffprobe_exe = X_("");
180
181         std::string ff_file_path;
182         if (find_file (Searchpath(Glib::getenv("PATH")), X_("ffmpeg_harvid"), ff_file_path)) {
183                 ffmpeg_exe = ff_file_path;
184         }
185 #ifdef PLATFORM_WINDOWS
186         else if ( windows_install_dir("Software\\" PROGRAM_NAME "\\v" PROGRAM_VERSION "\\video", reg))
187         {
188                 ffmpeg_exe = g_build_filename(reg.c_str(), X_("harvid"), X_("ffmpeg.exe"), NULL);
189                 ffprobe_exe = g_build_filename(reg.c_str(), X_("harvid"), X_("ffprobe.exe"), NULL);
190         }
191         else if ( windows_install_dir("Software\\RSS\\harvid", reg))
192         {
193                 ffmpeg_exe = g_build_filename(reg.c_str(), X_("ffmpeg.exe"), NULL);
194                 ffprobe_exe = g_build_filename(reg.c_str(), X_("ffprobe.exe"), NULL);
195         }
196
197         if (Glib::file_test(ffmpeg_exe, Glib::FILE_TEST_EXISTS)) {
198                 ;
199         }
200         else if (!program_files.empty() && Glib::file_test(g_build_filename(program_files.c_str(), "harvid", "ffmpeg.exe", NULL), Glib::FILE_TEST_EXISTS)) {
201                 ffmpeg_exe = g_build_filename(program_files.c_str(), "harvid", "ffmpeg.exe", NULL);
202         }
203         else if (Glib::file_test(X_("C:\\Program Files\\ffmpeg\\ffmpeg.exe"), Glib::FILE_TEST_EXISTS)) {
204                 ffmpeg_exe = X_("C:\\Program Files\\ffmpeg\\ffmpeg.exe");
205         } else {
206                 ffmpeg_exe = X_("");
207         }
208 #endif
209
210         if (find_file (Searchpath(Glib::getenv("PATH")), X_("ffprobe_harvid"), ff_file_path)) {
211                 ffprobe_exe = ff_file_path;
212         }
213 #ifdef PLATFORM_WINDOWS
214         if (Glib::file_test(ffprobe_exe, Glib::FILE_TEST_EXISTS)) {
215                 ;
216         }
217         else if (!program_files.empty() && Glib::file_test(g_build_filename(program_files.c_str(), "harvid", "ffprobe.exe", NULL), Glib::FILE_TEST_EXISTS)) {
218                 ffprobe_exe = g_build_filename(program_files.c_str(), "harvid", "ffprobe.exe", NULL);
219         }
220         else if (Glib::file_test(X_("C:\\Program Files\\ffmpeg\\ffprobe.exe"), Glib::FILE_TEST_EXISTS)) {
221                 ffprobe_exe = X_("C:\\Program Files\\ffmpeg\\ffprobe.exe");
222         } else {
223                 ffprobe_exe = X_("");
224         }
225 #endif
226
227         if (ffmpeg_exe.empty() || ffprobe_exe.empty()) {
228                 return false;
229         }
230         return true;
231 }
232
233 } // namespace ARDOUR