2 * Copyright (C) 2007-2016 Tim Mayberry <mojofunk@gmail.com>
3 * Copyright (C) 2008-2011 David Robillard <d@drobilla.net>
4 * Copyright (C) 2008-2016 Paul Davis <paul@linuxaudiosystems.com>
5 * Copyright (C) 2009-2012 Carl Hetherington <carl@carlh.net>
6 * Copyright (C) 2013-2014 John Emmas <john@creativepost.co.uk>
7 * Copyright (C) 2014-2019 Robin Gareus <robin@gareus.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include "pbd/compose.h"
27 #include "pbd/error.h"
28 #include "pbd/string_convert.h"
30 #include <glibmm/miscutils.h>
31 #include <glibmm/fileutils.h>
33 #include "ardour/directory_names.h"
34 #include "ardour/filesystem_paths.h"
38 #ifdef PLATFORM_WINDOWS
40 #include "pbd/windows_special_dirs.h"
50 user_config_directory_name (int version = -1)
53 version = string_to<int32_t>(X_(PROGRAM_VERSION));
56 /* ARDOUR::Profile may not be available when this is
57 called, so rely on build-time detection of the
61 const string config_dir_name = string_compose ("%1%2", X_(PROGRAM_NAME), version);
63 #if defined (__APPLE__) || defined (PLATFORM_WINDOWS)
64 /* Use mixed-case folder name on OS X and Windows */
65 return config_dir_name;
67 /* use lower case folder name on Linux */
68 return downcase (config_dir_name);
73 user_config_directory (int version)
79 p = Glib::build_filename (Glib::get_home_dir(), "Library/Preferences");
84 /* adopt freedesktop standards, and put .ardour3 into $XDG_CONFIG_HOME or ~/.config */
85 if ((c = getenv ("XDG_CONFIG_HOME")) != 0) {
89 #ifdef PLATFORM_WINDOWS
90 // Not technically the home dir (since it needs to be a writable folder)
91 const string home_dir = Glib::get_user_config_dir();
93 const string home_dir = Glib::get_home_dir();
95 if (home_dir.empty ()) {
96 error << "Unable to determine home directory" << endmsg;
101 #ifndef PLATFORM_WINDOWS
102 p = Glib::build_filename (p, ".config");
106 #endif // end not __APPLE__
108 p = Glib::build_filename (p, user_config_directory_name (version));
111 /* Only create the user config dir if the version was negative,
112 meaning "for the current version.
114 if (!Glib::file_test (p, Glib::FILE_TEST_EXISTS)) {
115 if (g_mkdir_with_parents (p.c_str(), 0755)) {
116 error << string_compose (_("Cannot create Configuration directory %1 - cannot run"),
120 } else if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) {
121 fatal << string_compose (_("Configuration directory %1 already exists and is not a directory/folder - cannot run"),
123 abort(); /*NOTREACHED*/
131 user_cache_directory (std::string cachename)
136 p = Glib::build_filename (Glib::get_home_dir(), "Library/Caches");
140 /* adopt freedesktop standards, and put .ardour3 into $XDG_CACHE_HOME
141 * defaulting to or ~/.config
143 if ((c = getenv ("XDG_CACHE_HOME")) != 0) {
147 #ifdef PLATFORM_WINDOWS
148 // Not technically the home dir (since it needs to be a writable folder)
149 const string home_dir = Glib::get_user_data_dir();
151 const string home_dir = Glib::get_home_dir();
153 if (home_dir.empty ()) {
154 error << "Unable to determine home directory" << endmsg;
159 #ifndef PLATFORM_WINDOWS
160 p = Glib::build_filename (p, ".cache");
164 #endif // end not __APPLE__
166 if (cachename.empty ()) {
167 p = Glib::build_filename (p, user_config_directory_name ());
169 p = Glib::build_filename (p, cachename);
172 #ifdef PLATFORM_WINDOWS
173 /* On Windows Glib::get_user_data_dir is the folder to use for local
174 * (as opposed to roaming) application data.
175 * See documentation for CSIDL_LOCAL_APPDATA.
176 * Glib::get_user_data_dir() == GLib::get_user_config_dir()
177 * so we add an extra subdir *below* the config dir.
179 p = Glib::build_filename (p, "cache");
182 if (!Glib::file_test (p, Glib::FILE_TEST_EXISTS)) {
183 if (g_mkdir_with_parents (p.c_str(), 0755)) {
184 error << string_compose (_("Cannot create cache directory %1 - cannot run"),
188 } else if (!Glib::file_test (p, Glib::FILE_TEST_IS_DIR)) {
189 fatal << string_compose (_("Cache directory %1 already exists and is not a directory/folder - cannot run"),
191 abort(); /*NOTREACHED*/
198 ardour_dll_directory ()
200 #ifdef PLATFORM_WINDOWS
201 std::string dll_dir_path(windows_package_directory_path());
202 dll_dir_path = Glib::build_filename (dll_dir_path, "lib");
203 return Glib::build_filename (dll_dir_path, LIBARDOUR);
205 std::string s = Glib::getenv("ARDOUR_DLL_PATH");
207 std::cerr << _("ARDOUR_DLL_PATH not set in environment - exiting\n");
208 ::exit (EXIT_FAILURE);
214 #ifdef PLATFORM_WINDOWS
216 windows_search_path ()
218 std::string dll_dir_path(windows_package_directory_path());
219 dll_dir_path = Glib::build_filename (dll_dir_path, "share");
220 return Glib::build_filename (dll_dir_path, LIBARDOUR);
224 windows_package_directory_path ()
227 g_win32_get_package_installation_directory_of_module (NULL);
229 if (package_dir == NULL) {
230 fatal << string_compose (_("Cannot determine %1 package directory"),
231 PROGRAM_NAME) << endmsg;
232 abort(); /*NOTREACHED*/
235 std::string package_dir_path(package_dir);
237 return package_dir_path;
242 ardour_config_search_path ()
244 static Searchpath search_path;
246 if (search_path.empty()) {
247 // Start by adding the user's personal config folder
248 search_path += user_config_directory();
249 #ifdef PLATFORM_WINDOWS
250 // On Windows, add am intermediate configuration folder
251 // (one that's guaranteed to be writable by all users).
252 const gchar* const *all_users_folder = g_get_system_config_dirs();
253 // Despite its slightly odd name, the above returns a single entry which
254 // corresponds to 'All Users' on Windows (according to the documentation)
256 if (all_users_folder) {
257 std::string writable_all_users_path = all_users_folder[0];
258 writable_all_users_path += "\\";
259 writable_all_users_path += PROGRAM_NAME;
260 writable_all_users_path += "\\.config";
262 writable_all_users_path += "\\win64";
264 writable_all_users_path += "\\win32";
266 search_path += writable_all_users_path;
269 // now add a suitable config path from the bundle
270 search_path += windows_search_path ();
272 // finally, add any paths from ARDOUR_CONFIG_PATH if it exists
273 std::string s = Glib::getenv("ARDOUR_CONFIG_PATH");
275 std::cerr << _("ARDOUR_CONFIG_PATH not set in environment\n");
277 search_path += Searchpath (s);
285 ardour_data_search_path ()
287 static Searchpath search_path;
289 if (search_path.empty()) {
290 search_path += user_config_directory();
291 #ifdef PLATFORM_WINDOWS
292 search_path += windows_search_path ();
294 std::string s = Glib::getenv("ARDOUR_DATA_PATH");
296 std::cerr << _("ARDOUR_DATA_PATH not set in environment\n");
298 search_path += Searchpath (s);
306 been_here_before_path (int version)
309 version = atoi (PROGRAM_VERSION);
312 return Glib::build_filename (user_config_directory (version), string (".a") + to_string (version));
316 } // namespace ARDOUR