Inkscape
Vector Graphics Editor
Loading...
Searching...
No Matches
path-prefix.cpp
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
/*
4 * Authors:
5 * Patrick Storz <eduard.braun2@gmx.de>
6 *
7 * Copyright (C) 2018 Authors
8 * Released under GNU GPL v2+, read the file 'COPYING' for more information.
9 */
10
11#ifdef HAVE_CONFIG_H
12# include "config.h" // only include where actually required!
13#endif
14
15#ifdef _WIN32
16#include <libloaderapi.h> // for GetModuleFileNameW
17#endif
18
19#ifdef __APPLE__
20#include <mach-o/dyld.h> // for _NSGetExecutablePath
21#endif
22
23#ifdef __NetBSD__
24#include <sys/types.h>
25#include <sys/sysctl.h>
26#endif
27
28#ifdef __FreeBSD__
29#include <sys/param.h>
30#include <sys/types.h>
31#include <sys/sysctl.h>
32#endif
33
34#include <cassert>
35#include <glib.h>
36#include <glibmm.h>
37
38#include "path-prefix.h"
39
47static std::string _get_bundle_prefix_dir()
48{
49 char const *program_dir = get_program_dir();
50 auto prefix = Glib::path_get_dirname(program_dir);
51
52#if defined(__APPLE__)
53 if (g_str_has_suffix(program_dir, "Contents/MacOS")) {
54 // macOS
55 prefix += "/Resources";
56 }
57#elif defined(__linux__)
58 if (g_str_has_suffix(program_dir, "/lib64")) {
59 // AppImage
60 // program_dir=appdir/lib64
61 // prefix_dir=appdir/usr
62 prefix = Glib::build_filename(prefix, "usr");
63 }
64#endif
65
66 return prefix;
67}
68
80{
81 static char const *inkscape_datadir = nullptr;
82 if (!inkscape_datadir) {
83 static std::string datadir = Glib::getenv("INKSCAPE_DATADIR");
84
85 if (datadir.empty()) {
86 datadir = Glib::build_filename(_get_bundle_prefix_dir(), "share");
87
88 if (!Glib::file_test(Glib::build_filename(datadir, "inkscape"), Glib::FileTest::IS_DIR)) {
89 datadir = INKSCAPE_DATADIR;
90 }
91 }
92
93 inkscape_datadir = datadir.c_str();
94
95#if GLIB_CHECK_VERSION(2,58,0)
96 inkscape_datadir = g_canonicalize_filename(inkscape_datadir, nullptr);
97#endif
98 }
99
100 return inkscape_datadir;
101}
102
113{
114 static bool ready = false;
115 if (ready)
116 return;
117 ready = true;
118#ifdef __APPLE__
119 // use bundle identifier
120 // https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/MacOSXDirectories/MacOSXDirectories.html
121 auto app_support_dir = Glib::getenv("HOME") + "/Library/Application Support/org.inkscape.Inkscape";
122
123 auto bundle_resources_dir = Glib::path_get_dirname(get_inkscape_datadir());
124 auto bundle_resources_etc_dir = bundle_resources_dir + "/etc";
125 auto bundle_resources_bin_dir = bundle_resources_dir + "/bin";
126 auto bundle_resources_lib_dir = bundle_resources_dir + "/lib";
127 auto bundle_resources_share_dir = bundle_resources_dir + "/share";
128
129 // failsafe: Check if the expected content is really there, using GIO modules
130 // as an indicator.
131 // This is also helpful to developers as it enables the possibility to
132 // 1. cmake -DCMAKE_INSTALL_PREFIX=Inkscape.app/Contents/Resources
133 // 2. move binary to Inkscape.app/Contents/MacOS and set rpath
134 // 3. copy Info.plist
135 // to ease up on testing and get correct application behavior (like dock icon).
136 if (!Glib::file_test(bundle_resources_lib_dir + "/gio/modules", Glib::FileTest::EXISTS)) {
137 // doesn't look like a standalone bundle
138 return;
139 }
140
141 // XDG
142 // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
143 Glib::setenv("XDG_DATA_HOME", app_support_dir + "/share");
144 Glib::setenv("XDG_DATA_DIRS", bundle_resources_share_dir);
145 Glib::setenv("XDG_CONFIG_HOME", app_support_dir + "/config");
146 Glib::setenv("XDG_CONFIG_DIRS", bundle_resources_etc_dir + "/xdg");
147 Glib::setenv("XDG_CACHE_HOME", app_support_dir + "/cache");
148
149 // GdkPixbuf
150 // https://gitlab.gnome.org/GNOME/gdk-pixbuf
151 Glib::setenv("GDK_PIXBUF_MODULE_FILE", bundle_resources_lib_dir + "/gdk-pixbuf-2.0/2.10.0/loaders.cache");
152
153 // fontconfig
154 Glib::setenv("FONTCONFIG_PATH", bundle_resources_etc_dir + "/fonts");
155
156 // GIO
157 Glib::setenv("GIO_MODULE_DIR", bundle_resources_lib_dir + "/gio/modules");
158
159 // GObject Introspection
160 Glib::setenv("GI_TYPELIB_PATH", bundle_resources_lib_dir + "/girepository-1.0");
161
162 // libenchant (patched)
163 // https://gitlab.com/inkscape/devel/mibap/-/blob/f71d24a/modulesets/gtk-osx-random.modules#L138
164 Glib::setenv("ENCHANT_PREFIX", bundle_resources_dir);
165
166 // PATH
167 Glib::setenv("PATH", bundle_resources_bin_dir + ":" + Glib::getenv("PATH"));
168
169 // DYLD_LIBRARY_PATH
170 // This is required to make Python GTK bindings work as they use dlopen()
171 // to load libraries.
172 Glib::setenv("DYLD_LIBRARY_PATH", bundle_resources_lib_dir + ":"
173 + bundle_resources_lib_dir + "/gdk-pixbuf-2.0/2.10.0/loaders");
174#endif
175}
176
177
182{
183 set_xdg_env();
184 return g_get_user_config_dir();
185}
186
193char const *get_program_name()
194{
195 static gchar *program_name = NULL;
196
197 if (program_name == NULL) {
198 // There is no portable way to get an executable's name including path, so we need to do it manually.
199 // TODO: Re-evaluate boost::dll::program_location() once we require Boost >= 1.61
200 //
201 // The following platform-specific code is partially based on GdxPixbuf's get_toplevel()
202 // See also https://stackoverflow.com/a/1024937
203#if defined(_WIN32)
204 wchar_t module_file_name[MAX_PATH];
205 if (GetModuleFileNameW(NULL, module_file_name, MAX_PATH)) {
206 program_name = g_utf16_to_utf8((gunichar2 *)module_file_name, -1, NULL, NULL, NULL);
207 } else {
208 g_warning("get_program_name() - GetModuleFileNameW failed");
209 }
210#elif defined(__APPLE__)
211 char pathbuf[PATH_MAX + 1];
212 uint32_t bufsize = sizeof(pathbuf);
213 if (_NSGetExecutablePath(pathbuf, &bufsize) == 0) {
214 program_name = realpath(pathbuf, nullptr);
215 } else {
216 g_warning("get_program_name() - _NSGetExecutablePath failed");
217 }
218#elif defined(__linux__) || defined(__CYGWIN__)
219 program_name = g_file_read_link("/proc/self/exe", NULL);
220 if (!program_name) {
221 g_warning("get_program_name() - g_file_read_link failed");
222 }
223#elif defined(__NetBSD__)
224 static const int name[] = {CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME};
225 char path[MAXPATHLEN];
226 size_t len = sizeof(path);
227 if (sysctl(name, __arraycount(name), path, &len, NULL, 0) == 0) {
228 program_name = realpath(path, nullptr);
229 } else {
230 g_warning("get_program_name() - sysctl failed");
231 }
232#elif defined(__FreeBSD__)
233 int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
234 char buf[MAXPATHLEN];
235 size_t cb = sizeof(buf);
236 if (sysctl(mib, 4, buf, &cb, NULL, 0) == 0) {
237 program_name = realpath(buf, nullptr);
238 } else {
239 g_warning("get_program_name() - sysctl failed");
240 }
241#else
242#warning get_program_name() - no known way to obtain executable name on this platform
243 g_info("get_program_name() - no known way to obtain executable name on this platform");
244#endif
245 }
246
247 return program_name;
248}
249
256char const *get_program_dir()
257{
258 static char *program_dir = g_path_get_dirname(get_program_name());
259 return program_dir;
260}
261
262/*
263 Local Variables:
264 mode:c++
265 c-file-style:"stroustrup"
266 c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
267 indent-tabs-mode:nil
268 fill-column:99
269 End:
270*/
271// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :
char const * get_inkscape_datadir()
Determine the location of the Inkscape data directory (typically the share/ folder from where Inkscap...
char const * get_program_name()
Gets the the currently running program's executable name (including full path)
void set_xdg_env()
Sets environment variables for a relocatable application bundle.
char const * get_program_dir()
Gets the the full path to the directory containing the currently running program's executable.
const char * get_user_config_dir()
Get the user configuration directory.
static std::string _get_bundle_prefix_dir()
Guess the absolute path of the application bundle prefix directory.
TODO: insert short description here.
int buf
bool ready
static int constexpr bufsize
const char * program_name
Definition rtree-toy.cpp:59
auto len
Definition safe-printf.h:21
Glib::ustring name
Definition toolbars.cpp:55