diff options
| -rw-r--r-- | src/inkscape-main.cpp | 10 | ||||
| -rw-r--r-- | src/path-prefix.cpp | 65 | ||||
| -rw-r--r-- | src/path-prefix.h | 3 |
3 files changed, 78 insertions, 0 deletions
diff --git a/src/inkscape-main.cpp b/src/inkscape-main.cpp index a8499412c..e25c6b71c 100644 --- a/src/inkscape-main.cpp +++ b/src/inkscape-main.cpp @@ -19,6 +19,16 @@ static void set_extensions_env() { + // add inkscape to PATH, so the correct version is always available to extensions by simply calling "inkscape" + gchar *program_dir = get_program_dir(); + if (program_dir) { + gchar const *path = g_getenv("PATH"); + gchar *new_path = g_strdup_printf("%s" G_SEARCHPATH_SEPARATOR_S "%s", program_dir, path); + g_setenv("PATH", new_path, true); + g_free(new_path); + } + g_free(program_dir); + // add share/inkscape/extensions to PYTHONPATH so the inkex module is found by extensions in user folder gchar *pythonpath = get_extensions_path(); g_setenv("PYTHONPATH", pythonpath, true); diff --git a/src/path-prefix.cpp b/src/path-prefix.cpp index eefe7ff39..a2b7c9fbc 100644 --- a/src/path-prefix.cpp +++ b/src/path-prefix.cpp @@ -12,6 +12,14 @@ # include "config.h" // only include where actually required! #endif +#ifdef _WIN32 +#include <windows.h> // for GetModuleFileNameW +#endif + +#ifdef __APPLE__ +#include <mach-o/dyld.h> // for _NSGetExecutablePath +#endif + #include "io/resource.h" #include "path-prefix.h" #include <glib.h> @@ -115,6 +123,63 @@ gchar *get_datadir_path() return datadir; } + +/** + * Gets the the currently running program's executable name (including full path) + * + * @return executable name (including full path) encoded as UTF-8 + * or NULL if it can't be determined + */ +gchar *get_program_name() +{ + static gchar *program_name = NULL; + + if (program_name == NULL) { + // There is no portable way to get an executable's name including path, so we need to do it manually. + // TODO: Re-evaluate boost::dll::program_location() once we require Boost >= 1.61 + // + // The following platform-specific code is partially based on GdxPixbuf's get_toplevel() + // See also https://stackoverflow.com/a/1024937 +#if defined(_WIN32) + wchar_t module_file_name[MAX_PATH]; + if (GetModuleFileNameW(NULL, module_file_name, MAX_PATH)) { + program_name = g_utf16_to_utf8((gunichar2 *)module_file_name, -1, NULL, NULL, NULL); + } else { + g_warning("get_program_name() - GetModuleFileNameW failed"); + } +#elif defined(__APPLE__) + char pathbuf[PATH_MAX + 1]; + uint32_t bufsize = sizeof(pathbuf); + if (_NSGetExecutablePath(pathbuf, &bufsize) == 0) { + program_name = g_strdup(pathbuf); + } else { + g_warning("get_program_name() - _NSGetExecutablePath failed"); + } +#elif defined(__linux__) + program_name = g_file_read_link("/proc/self/exe", NULL); + if (!program_name) { + g_warning("get_program_name() - g_file_read_link failed"); + } +#else +#warning get_program_name() - no known way to obtain executable name on this platform + g_info("get_program_name() - no known way to obtain executable name on this platform"); +#endif + } + + return program_name; +} + +/** + * Gets the the full path to the directory containing the currently running program's executable + * + * @return full path to directory encoded as UTF-8 + * or NULL if it can't be determined + */ +gchar *get_program_dir() +{ + return g_path_get_dirname(get_program_name()); +} + /* Local Variables: mode:c++ diff --git a/src/path-prefix.h b/src/path-prefix.h index 37cb83ef4..e76eb5dc4 100644 --- a/src/path-prefix.h +++ b/src/path-prefix.h @@ -32,6 +32,9 @@ char *append_inkscape_datadir(const char *relative_path); char *get_datadir_path(); char *get_extensions_path(); +char *get_program_name(); +char *get_program_dir(); + #ifdef _WIN32 #undef INKSCAPE_DATADIR #define INKSCAPE_DATADIR append_inkscape_datadir(NULL) |
